【问题标题】:Predicting with new data having greater length使用长度更大的新数据进行预测
【发布时间】:2021-04-16 19:23:53
【问题描述】:

我想对比我的训练集所在的数据框更长的数据集进行预测。

Df<-data.frame(MW=c(192700,117900,99300,54100,37800,29500,20200,740),
              Bands1<-c(0.0427334,0.2393070,0.3206159,0.5732002,0.7228141,0.8164857,0.8462922,0.9273532))

Df.pred<-data.frame(Band2=c(0.4470235,0.4884748,0.5345757,0.5898747,0.6405655,0.6774131,0.7557672,0.7972277,0.8940148,0.9493461,1.0138248,1.0414651))

mod<-lm(log10(Df$MW)~Df$Bands1, data=Df) ## Making the model

Df.pred$PredMW<-predict(lm(log10(Df$MW)~Df$Bands1, data=Df), newdata=Df.pred) ## Asking the model to predict values corresponding to Df.pred based on mod

我似乎得到以下输出:

Warning message:
'newdata' had 12 rows but variables found have 8 rows

我该如何解决这个问题?我已经阅读了 ?predict 以及 ?predict.lm。我无法弄清楚这一点。

【问题讨论】:

    标签: r predict variable-length


    【解决方案1】:

    Df.pred列名改为Bands1,与Df中的一样:

    Df.pred <- data.frame(Bands1 = c(0.4470235, 0.4884748 ,0.5345757 ,0.5898747 ,0.6405655,
                                0.6774131, 0.7557672, 0.7972277, 0.8940148, 0.9493461,
                                1.0138248, 1.0414651))
    
    mod <- lm(log10(MW) ~ Bands1, data = Df) ## Making the model
    
    Df.pred$PredMW <- predict(mod, newdata = Df.pred) ## Asking the model to predict values corresponding to Df.pred based on mod
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多