7

working with a 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 am getting this erro:

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

what am I doing wrong?

here is the dput output:

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")
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 1
    Is the `z` in your first line supposed to be an `x`? otherwise we don't know what `z` is. You aren't getting an error, you're getting a warning. Also, nothing you've show us has 29 rows so we cannot repeat this. Use `dput` to show us your actual data. – Justin Aug 03 '12 at 20:47
  • sorry, yest it is x. I just updated it – user1471980 Aug 03 '12 at 20:50

4 Answers4

10

Your variable names, as stored in the x.lm model, refer to the x dataframe. There are no variables of the same names in a, so it will use those 29 from x again, which is probably not what you wanted, thus the 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)
MvG
  • 57,380
  • 22
  • 148
  • 276
  • 1
    or `x.lm <- lm(Val~Date,data=x); predict(x.lm, newdata=data.frame(Date=a))` – Ben Bolker Aug 03 '12 at 21:53
  • @BenBolker, you're right, a `with(x,…)` is probably overkill here. Changed my answer accordingly. Thanks. – MvG Aug 04 '12 at 05:54
  • Incidentally, is there any way to look at how R parameter passing works that does not hurt your brain? – Florian Mayer Aug 06 '14 at 21:33
  • @FlorianMayer: Have you read [section 4.3 of the language reference](http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Evaluation)? – MvG Aug 06 '14 at 22:40
1

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

when you make your data.frame a name that column Date and you're good to go:

a <- data.frame(Date=a)

Then it runs without the warning.

Per comment:

Edit your lm call to be:

lm(Val ~ Date, data=x)
Justin
  • 42,475
  • 9
  • 93
  • 111
  • Still gives the warning for me, as the variable is called `x$Date` here. Even actually calling it `"x$Date"` in the data frame dos not appear to be enough. Have you tested this just as the OP wrote it? If so, there might be a difference between your version of R and my 2.15.1. – MvG Aug 03 '12 at 21:27
  • @MvG yeah, you're right, I had changed the OP's code while fiddling and forgot. – Justin Aug 03 '12 at 22:02
0

If you can't make predict.lm() work, then you should try to write your own function using function():

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

use yourown_function to predict from any new dataframe:

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

using the new values, you can compute residuals, MSE, etc...

pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
0

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

Priya
  • 151
  • 1
  • 2