Warning when calculating predicted values

works with data frame

x Date Val 1/1/2012 7 2/1/2012 9 3/1/2012 20 4/1/2012 24 5/1/2012 50 a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5) a <- data.frame(a) x.lm <- lm(x$Val ~ x$Date) x.pre<-predict(x.lm, newdata=a) 

I get this erro:

 Warning message: 'newdata' had 5 rows but variable(s) found have 29 rows 

what am I doing wrong?

here is the output of dput:

 dput(x) structure(list(Date = structure(c(14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461), class = "Date"), Val = c(45, 51, 56, 56, 59, 60, 60, 60, 64, 65, 75, 73, 74, 80, 87, 91, 92, 96, 109, 108, 123, 129, 133, 143, 127, 127, 123, 121, 130)), .Names = c("Date", "Val"), row.names = c(NA, 29L), class = "data.frame") 
+7
source share
4 answers

Variable names stored in the x.lm model refer to x dataframe. There are no variables with the same names in a, so they will again use those 29 of x , which is probably not the way you wanted, so this is a warning. You can do the following to always use an unqualified variable named Date in the model:

 a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5) a <- data.frame(Date = a) x.lm <- lm(Val ~ Date, data=x) x.pre<-predict(x.lm, newdata=a) 
+10
source

Your data.frame a has a column named a . You created your model with columns named Val and Date , so this is what it is looking for.

when you enter the name data.frame a in the Date column and you go well:

 a <- data.frame(Date=a) 

Then it starts without warning.

For the comment:

Edit your lm call:

 lm(Val ~ Date, data=x) 
+1
source

If you cannot do predict.lm() , try writing your own function using function() :

 yourown_function<- function(predictor1, predictor2,...){intercept+b1*predictor1+b2*predictor2+...} 

use yourown_function to predict from any new data frame:

 newvalues<- yourown_function(predictor1=data.frame$predictor1, predictor2=data.frame$predictor2,....) 

using the new values, you can calculate the balances, MSE, etc.

0
source

Instead of x.lm <- lm (x $ Val ~ x $ Date, data = x) use x.lm <- lm (Val ~ Date, data = x). Removing dataset information before the variable name in the lm function should help.

0
source

All Articles