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)