手动进行交叉验证

时间:2019-04-13 21:26:26

标签: r

我正在尝试创建一个手动进行交叉验证的手册。

我的代码在这里,铁渣包含53个值。但是,我的拟合模型仅包含52,因此我想知道自己做错了什么。

MwSt

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些修改,认为对您有所帮助!

 #First initialize the output vector as an empty object outside the loop.
        fitted_value <- NULL
        for(i in 1:53){
        #you did this part right
          validation<-ironslag[i,]
          training<-ironslag[-i,]
          model1<-lm(magnetic ~ chemical, data = training)
        #when you fit the model, use the newdata argument to predict on a new row
        #also, fitted_value needs the index [i], so the each loop doesn't overwrite the previous
          fitted_value[i] <- predict(model1, newdata = validation)}