Search code examples
rfunctionloopsregressionmodels

apply function for for list of glm models


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)

Solution

  • 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