0

I would like to make a prediction on a dataset which is longer than the dataframe in which my training set is present.

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

I seem to get the following output:

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

How do I solve this? I have read the ?predict as well as ?predict.lm. I am unable to figure this out.

Ginko-Mitten
  • 304
  • 1
  • 11

1 Answers1

0

Change the Df.pred column name to Bands1, the same as in 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
Paul
  • 2,877
  • 1
  • 12
  • 28