你可以写任何一个帮我编写for循环或应用函数来运行以下代码的多个模型
模拟数据
set.seed(666)
x1 = rnorm(1000)
x2 = rnorm(1000)
y = rbinom(1000,1,0.8)
df = data.frame(y=as.factor(y),x1=x1,x2=x2)
将数据拆分为训练和测试集
dt = sort(sample(nrow(df), nrow(df)*.5, replace = F))
trainset=df[dt,]; testset=df[-dt,]
拟合逻辑回归模型
model1=glm( y~x1,data=trainset,family="binomial")
model2=glm( y~x1+x2,data=trainset,family="binomial")
测试测试和训练中的模型准确性
require(pROC)
trainpredictions <- predict(object=model1,newdata = trainset);
trainpredictions <- as.ordered(trainpredictions)
testpredictions <- predict(object=model1,newdata = testset);
testpredictions <- as.ordered(testpredictions)
trainauc <- roc(trainset$y, trainpredictions);
testauc <- roc(testset$y, testpredictions)
print(trainauc$auc); print(testauc$auc)
答案 0 :(得分:0)
只需将模型放入列表
即可models <- list(
model1 = glm( y~x1,data=trainset,family="binomial"),
model2 = glm( y~x1+x2,data=trainset,family="binomial")
)
定义值提取功能
getauc <- function(model) {
trainpredictions <- predict(object=model,newdata = trainset);
trainpredictions <- as.ordered(trainpredictions)
testpredictions <- predict(object=model,newdata = testset);
testpredictions <- as.ordered(testpredictions)
trainauc <- roc(trainset$y, trainpredictions);
testauc <- roc(testset$y, testpredictions)
c(train=trainauc$auc, test=testauc$auc)
}
sapply()
对您的列表起作用
sapply(models, getauc)
# model1 model2
# train 0.5273818 0.5448066
# test 0.5025038 0.5146211