【问题标题】:Leave one out cross validation manually手动留下一个交叉验证
【发布时间】:2019-04-14 06:59:56
【问题描述】:

我正在尝试创建手动遗漏交叉验证。

我的代码在这里,ironslag 包含 53 个值。但是,我的拟合模型只包含 52,所以我想知道我做错了什么。


for(i in 1:53){

  validation<-ironslag[i,]
  training<-ironslag[-i,]
  model1<-lm(magnetic ~ chemical, data = training)

  fitted_value <- predict(model1, training)}

【问题讨论】:

    标签: r


    【解决方案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)}
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2014-09-13
      • 2013-03-04
      • 2019-10-27
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多