如何解决在对逻辑套索回归进行重复交叉验证后,对 predict() 函数使用 type = "raw" 选项返回空向量
我使用 caret 和 glmnet pacakges 运行套索逻辑回归,使用重复交叉验证来选择优化的最小 lambda。
glmnet.obj <- train(outcome ~ .,data = df.train,method = "glmnet",metric = "ROC",family = "binomial",trControl = trainControl(
method = "repeatedcv",repeats = 10,number = 10,summaryFunction = twoClassSummary,classProbs = TRUE,savePredictions = "all",selectionFunction = "best"))
在那之后,我得到了最好的 lambda 和 alpha:
best_lambda<- get_best_result(glmnet.obj)$lambda
best_alpha<- get_best_result(glmnet.obj)$alpha
然后我获得测试集的预测概率:
pred_prob<- predict(glmnet.obj,s=best_lambda,alpha=best_alpha,type="prob",newx = x.test)
然后得到我打算在 ConfusionMatrix 中使用的预测类:
pred_class<-predict(glmnet.obj,type="raw",newx=x.test)
但是当我运行 pred_class
时它返回 NULL
。
我会在这里遗漏什么?
解决方法
您需要使用 newdata =
而不是 newx=
,因为当您执行 predict(glmnet.obj)
时,它会在插入符号对象上调用 predict.train
。
你没有提供一个函数,但我想它是来自这个source:
get_best_result = function(caret_fit) {
best = which(rownames(caret_fit$results) == rownames(caret_fit$bestTune))
best_result = caret_fit$results[best,]
rownames(best_result) = NULL
best_result
}
使用示例数据
set.seed(111)
df = data.frame(outcome = factor(sample(c("y","n"),100,replace=TRUE)),matrix(rnorm(1000),ncol=10))
colnames(df.train)[-1] = paste0("col",1:10)
df.train = df[1:70,]
x.test = df[71:100,]
我们运行您的模型,然后您可以使用该函数进行预测:
pred_class<-predict(glmnet.obj,type="raw",newdata=x.test)
confusionMatrix(table(pred_class,x.test$outcome))
Confusion Matrix and Statistics
pred_class n y
n 1 5
y 11 13
lambda =
和 newx=
的参数来自 glmnet,您可以在 glmnet.obj$finalModel 上使用它,但您需要将数据转换为矩阵,例如:
predict(glmnet.obj$finalModel,s=best_lambda,alpha=best_alpha,type="class",newx=as.matrix(x.test[,-1]))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。