【发布时间】:2019-01-21 10:05:21
【问题描述】:
所以我正在尝试使用 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”
但是将我的 'type = "response"' 移动到 'lime' 代码中并不能解决它。
我已经尝试创建一个函数“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' 函数更正此问题),但我似乎无法在任何地方找到澄清。
任何帮助都会很棒,谢谢!
【问题讨论】:
-
所以更新了。如果我在函数'predict_model.glm 中直接调用'type = "response"',我发现此错误已解决
-
但是,现在内部使用的 glm.fit 函数似乎不像我的预测,我得到:
-
glm.fit(x = x_fit, y = y, weights = weights, family = gaussian()) 中的错误:V(mu) 中的 NA。我已尝试通过更改“predict_model.glm”中数据框中的输出格式来解决此问题,但似乎无法解决此问题。我确实有一个 'model_type.glm