如何使用for循环运行代码efor 1000次?

时间:2017-07-30 23:04:15

标签: r loops for-loop dataframe dataset

我有这个代码产生不同的" auc"每次我运行它。我想运行此代码1000次,以计算1000存储的AUC的平均值。我怎样才能申请循环来做我想要的东西?如果你们为我这样做并粘贴新代码,将不胜感激。正如我4天来一直试图解决的那样。

#iris is a built-in dataset
main_df<- iris
# extract data for "setosa"
setosa <-main_df[main_df$Species=="setosa" ,]
# extract data for "virginica"
virginica<-main_df[main_df$Species=="virginica" ,]
#merge "setosa" and "virginica" as new dataset
df<- rbind(setosa,virginica)

Cross.AUC<-rep(0,1000) # create a vector of zeros, here will be stored the auc values from each of 1000 runs
for (i in seq(1:1000)) {
#---------------devide data into two datasets 70:30 train:test ----
#---------------train dataset
#select randomly 70% of setosa, generates a 35-by-5 matrix
setosa_70<-setosa[sample(nrow(setosa),round(0.7*dim(setosa)[1])),]
#select randomly 70% of virginica, generates a 35-by-5 matrix
virginica_70<-virginica[sample(nrow(virginica),round(0.7*dim(virginica)[1])),]
#merge setosa and virginica
train<-rbind(setosa_70,virginica_70)
#convert "setosa" to "0" and "virginica" to 1""
train$Species<-ifelse(train$Species=="setosa",0,1)
#select 1st, 2nd and 5th columns
train <-subset(train,select = c(1,2,5))
#--------------test dataset
#select randomly 30% of setosa, generates a 15-by-5 matrix
setosa_30<-setosa[sample(nrow(setosa),round(0.3*dim(setosa)[1])),]
#select randomly 30% of virginica, generates a 15-by-5 matrix
virginica_30<-virginica[sample(nrow(virginica),round(0.3*dim(virginica)[1])),]
#merge setosa and virginica
test<-rbind(setosa_30,virginica_30)
#convert "setosa" to "0" and "virginica" to 1""
test$Species<-ifelse(test$Species=="setosa",0,1)
#select 1st, 2nd and 5th columns
test <-subset(test,select = c(1,2,5))
#merge "train" and "test"
train_test<-rbind(train,test)
#--Model_1--
model <-glm(Species~., family = binomial(link = "logit"),data = train_test)
# install.packages("ROCR")
library(ROCR)
p <- predict(model, newdata=test, type="response")
pr <- prediction(p, test$Species)
auc <- performance(pr, measure = "auc")
auc <- auc@y.values[[1]]
AUC[i]<-auc
}

1 个答案:

答案 0 :(得分:0)

AUC替换为Cross.AUC。您没有在范围中定义任何名为AUC的对象,您可以在for循环中访问该对象。

#iris is a built-in dataset
main_df<- iris
# extract data for "setosa"
setosa <-main_df[main_df$Species=="setosa" ,]
# extract data for "virginica"
virginica<-main_df[main_df$Species=="virginica" ,]
#merge "setosa" and "virginica" as new dataset
df<- rbind(setosa,virginica)

Cross.AUC<-rep(0,1000) # create a vector of zeros, here will be stored the auc values from each of 1000 runs
for (i in seq(1:1000)) {
  #---------------devide data into two datasets 70:30 train:test ----
  #---------------train dataset
  #select randomly 70% of setosa, generates a 35-by-5 matrix
  setosa_70<-setosa[sample(nrow(setosa),round(0.7*dim(setosa)[1])),]
  #select randomly 70% of virginica, generates a 35-by-5 matrix
  virginica_70<-virginica[sample(nrow(virginica),round(0.7*dim(virginica)[1])),]
  #merge setosa and virginica
  train<-rbind(setosa_70,virginica_70)
  #convert "setosa" to "0" and "virginica" to 1""
  train$Species<-ifelse(train$Species=="setosa",0,1)
  #select 1st, 2nd and 5th columns
  train <-subset(train,select = c(1,2,5))
  #--------------test dataset
  #select randomly 30% of setosa, generates a 15-by-5 matrix
  setosa_30<-setosa[sample(nrow(setosa),round(0.3*dim(setosa)[1])),]
  #select randomly 30% of virginica, generates a 15-by-5 matrix
  virginica_30<-virginica[sample(nrow(virginica),round(0.3*dim(virginica)[1])),]
  #merge setosa and virginica
  test<-rbind(setosa_30,virginica_30)
  #convert "setosa" to "0" and "virginica" to 1""
  test$Species<-ifelse(test$Species=="setosa",0,1)
  #select 1st, 2nd and 5th columns
  test <-subset(test,select = c(1,2,5))
  #merge "train" and "test"
  train_test<-rbind(train,test)
  #--Model_1--
  model <-glm(Species~., family = binomial(link = "logit"),data = train_test)
  # install.packages("ROCR")
  library(ROCR)
  p <- predict(model, newdata=test, type="response")
  pr <- prediction(p, test$Species)
  auc <- performance(pr, measure = "auc")
  auc <- auc@y.values[[1]]
  Cross.AUC[i]<-auc
}

cat('Mean AUC:', mean(Cross.AUC), '\n')