我使用插入符号和glmnet包运行套索逻辑回归,使用重复交叉验证来选择优化的最小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,s=best_lambda, alpha=best_alpha, type="raw",newx=x.test)
但是当我运行pred_class
时,它返回NULL
。
我还能错过什么呢?
您需要使用newdata =
而不是newx=
,因为当您执行predict(glmnet.obj)
时,它在插入符号对象上调用predict.train
。
您没有提供一个函数,但我想它来自以下来源:
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]))