Cross validation for glm() models

后端 未结 2 914
南旧
南旧 2021-01-31 11:33

I\'m trying to do a 10-fold cross validation for some glm models that I have built earlier in R. I\'m a little confused about the cv.glm() function in the boo

2条回答
  •  耶瑟儿~
    2021-01-31 12:23

    I am always a little cautious about using various packages 10-fold cross validation methods. I have my own simple script to create the test and training partitions manually for any machine learning package:

    #Randomly shuffle the data
    yourData<-yourData[sample(nrow(yourData)),]
    
    #Create 10 equally size folds
    folds <- cut(seq(1,nrow(yourData)),breaks=10,labels=FALSE)
    
    #Perform 10 fold cross validation
    for(i in 1:10){
        #Segement your data by fold using the which() function 
        testIndexes <- which(folds==i,arr.ind=TRUE)
        testData <- yourData[testIndexes, ]
        trainData <- yourData[-testIndexes, ]
        #Use test and train data partitions however you desire...
    }
    

提交回复
热议问题