2017-07-30 8 views
0

私はそれを実行するたびに異なる "auc"を生成するこのコードを持っています。私は1000回保存されたAUCの平均を計算するためにこのコードを1000回実行したいと思います。私は何を探しているのですか?皆さんが私のためにそれを行い、新しいコードを貼り付けることができれば幸いです。私が今4日以来解決しようとしているように。forループを使ってコードを1000回実行するには?

#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 <- [email protected][[1]] 
AUC[i]<-auc 
} 

答えて

0

Cross.AUCAUCを交換してください。 forループでアクセスできる範囲に定義されたAUCというオブジェクトはありません。

#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 <- [email protected][[1]] 
    Cross.AUC[i]<-auc 
} 

cat('Mean AUC:', mean(Cross.AUC), '\n') 
関連する問題