Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi can any one write help me write a for loop or apply function to run the below code for multiple models

Simulated Data

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)

Splitting Data to train and test sets

dt = sort(sample(nrow(df), nrow(df)*.5, replace = F))
trainset=df[dt,]; testset=df[-dt,]

Fitting logistic regression models

model1=glm( y~x1,data=trainset,family="binomial")
model2=glm( y~x1+x2,data=trainset,family="binomial")

Testing Model accuracy in test and train ets

I want to loop the below mentioned code for multiple models fitted above and print the AUC in train set and test set for each model

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just put your models in a list

models <- list(
  model1 = glm( y~x1,data=trainset,family="binomial"),
  model2 = glm( y~x1+x2,data=trainset,family="binomial")
)

Define a function for value extraction

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)
}

And sapply() that function to your list

sapply(models, getauc)
#          model1    model2
# train 0.5273818 0.5448066
# test  0.5025038 0.5146211

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...