为 glm 模型列表应用函数
apply function for for list of glm models
嗨,谁能帮我写一个 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")
在测试和训练中测试模型准确性
我想为上面安装的多个模型循环下面提到的代码,并在每个模型的训练集和测试集中打印 AUC
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)
只需将您的模型放入列表中
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
嗨,谁能帮我写一个 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")
在测试和训练中测试模型准确性
我想为上面安装的多个模型循环下面提到的代码,并在每个模型的训练集和测试集中打印 AUC
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)
只需将您的模型放入列表中
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