用更长的新数据进行预测

问题描述

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

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。我无法弄清楚这一点。

解决方法

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