因此,我试图使用LIME来理解R中logit模型的预测。我知道我不需要这样做,但我试图用一个人们可以简单地理解为演示起点的模型来说明它的作用。
但我很难让它正常工作。我确信这是由于model.predict方面的原因,但我的几个解决方案都不起作用。
基本上这是我想要做的:
model.logit <- glm(formula = formula, data = build.dat, family = binomial(link = "logit"))
train.x <- build.dat[ , all.vars(formula[[3]])]
test.x <- reject.dat[1:100, all.vars(formula[[3]])]
explainer <- lime(train.x, as_classifier(model.logit ), n_bins = 20, quantile_bins = TRUE)
explain.me <- lime::explain(test.x[2 , ], explainer, n_labels = 1, n_features = 8, n_permutations = 5000,
feature_select = "forward_selection", type = "response" )现在我得到了错误
Error in match.arg(type) :'arg' should be one of “link”, “response”, “terms”但是在'lime‘代码中移动我的'type =“response’并不能解决这个问题。
我已经尝试创建了一个'predict_model.glm‘函数,我认为这可能会纠正这个问题,因为我在使用randomForest时认为发生了什么,并使其正常工作:
predict_model.glm <- function(x, newdata, type = "response" ) {
res <- as.data.frame(c(predict(x, newdata = newdata, type = type), 1-predict(x, newdata = newdata, type = type)))
}但这似乎只是增加了错误。
我确信这是由于我遗漏了'lime‘方面到底在寻找什么(因此我没有用'predict_model.glm’函数纠正这个错误),但是我似乎找不到任何澄清。
任何帮助都是最好的,谢谢!
发布于 2020-12-06 00:13:40
您必须在predict_model.glm中转换predict的输出。作为第一步,我将重新注释打印type和调用predict的结果的第一行。根据传入的类型,对glm的调用和返回的结果会有所不同- ?predict_model提示:对于'raw',它是一个单一的res,对于'prob',它是概率(或者对于真正的线性模型:数值结果)。
总体而言,就我(希望)理解您的情况而言,类似于该函数的函数可能会让您前进一步:
predict_model.glm <- function(x, newdata, type, ...) {
print(type)
res <- predict(x, newdata);
print(res[1])
data.frame(Response = res)
}https://stackoverflow.com/questions/51843277
复制相似问题