61

I'm trying to predict a value in R using the predict() function, by passing along variables into the model.

I am getting the following error:

Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

Here is my data frame, name df:

df <- read.table(text = '
     Quarter Coupon      Total
1   "Dec 06"  25027.072  132450574
2   "Dec 07"  76386.820  194154767
3   "Dec 08"  79622.147  221571135
4   "Dec 09"  74114.416  205880072
5   "Dec 10"  70993.058  188666980
6   "Jun 06"  12048.162  139137919
7   "Jun 07"  46889.369  165276325
8   "Jun 08"  84732.537  207074374
9   "Jun 09"  83240.084  221945162
10  "Jun 10"  81970.143  236954249
11  "Mar 06"   3451.248  116811392
12  "Mar 07"  34201.197  155190418
13  "Mar 08"  73232.900  212492488
14  "Mar 09"  70644.948  203663201
15  "Mar 10"  72314.945  203427892
16  "Mar 11"  88708.663  214061240
17  "Sep 06"  15027.252  121285335
18  "Sep 07"  60228.793  195428991
19  "Sep 08"  85507.062  257651399
20  "Sep 09"  77763.365  215048147
21  "Sep 10"  62259.691  168862119', header=TRUE)


str(df)
'data.frame':   21 obs. of  3 variables:
 $ Quarter   : Factor w/ 24 levels "Dec 06","Dec 07",..: 1 2 3 4 5 7 8 9 10 11 ...
 $ Coupon: num  25027 76387 79622 74114 70993 ...
 $ Total: num  132450574 194154767 221571135 205880072 188666980 ...

Code:

model <- lm(df$Total ~ df$Coupon)

> model

Call:
lm(formula = df$Total ~ df$Coupon)

Coefficients:
(Intercept)    df$Coupon  
  107286259         1349 

Now, when I run predict, I get the error I showed above.

> predict(model, df$Total, interval="confidence")
Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

Any idea where I am going wrong?

Thanks

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
mikebmassey
  • 8,354
  • 26
  • 70
  • 95

1 Answers1

51

There are several problems here:

  1. The newdata argument of predict() needs a predictor variable. You should thus pass it values for Coupon, instead of Total, which is the response variable in your model.

  2. The predictor variable needs to be passed in as a named column in a data frame, so that predict() knows what the numbers its been handed represent. (The need for this becomes clear when you consider more complicated models, having more than one predictor variable).

  3. For this to work, your original call should pass df in through the data argument, rather than using it directly in your formula. (This way, the name of the column in newdata will be able to match the name on the RHS of the formula).

With those changes incorporated, this will work:

model <- lm(Total ~ Coupon, data=df)
new <- data.frame(Coupon = df$Coupon)
predict(model, newdata = new, interval="confidence")
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 6
    +1 They don't really need to pass the `Coupon` values from the original data frame. Maybe they really want to predict the response at the values for `Total`. But those values have to be in something _named_ `Coupon` so that it matches up with the model. – joran Jan 26 '12 at 22:41
  • 5
    @joran -- thanks for making that point. Usually you will want to pass in values for `Coupon` other than those used in fitting the model. And when you **do** want the predicted values for points in the model, you can just say `predict(model, interval="confidence")` – Josh O'Brien Jan 26 '12 at 22:49
  • 3
    josh: that may not be doing what you think it does! Consider what happens with `predict(model, Coupon=NA, interval="confidence")` ... – Hong Ooi Jan 27 '12 at 00:23
  • @HongOoi -- Thanks so much for pointing that out! It's been too long since I've used predict, and I answered too quickly, without checking out a few permutations. Should be fixed now. – Josh O'Brien Jan 27 '12 at 01:00
  • @Josh Let's say I can't convert the lm model structure to the one you propose (using "data= ") because my variables come from 2 separate data.frames. (i.e., `lm(mod1$var ~ mod2$var)` ). Is there a way to go about making predict work in this scenario?? – theforestecologist Nov 19 '15 at 21:12
  • @theforestecologist The simplest solution would be to "work with R" by putting both those variables in a single data.frame before running the `lm()`. Any reason you can't do something like `df <- data.frame(var1 = mod1$var, var2=mod2$var); model <- lm(var1~var2, data=df); predict(model, newdata=data.frame(var2=1:10))`? I suspect you'll do best to accommodate yourself to R's natural workflow rather than come up with some more exotic solution... – Josh O'Brien Nov 19 '15 at 21:34
  • @Josh. So I have a loop where I want to do an independent regression on each variable of two data.frames (a 1977 vs 2015 comparison). I originally used `for(i in 1:3) { lm(dat77[,i] ~ dat15[,i]) }` which won't work with predict. However, your fix makes exchanging variables names in the loop difficult. I tried `dat=cbind(dat77,dat15) ; var.y <- names(dat)[i] ; var.x <- names(dat)[i+(length(names(dat))/2)] ; lm(var.y ~ var.x, dat)` but it gives me the error "Error in contrasts<-(*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels..." – theforestecologist Nov 19 '15 at 23:15
  • @theforestecologist I'd suggest having a look at `reformulate()`, [as for example here](http://stackoverflow.com/a/18474639/980833), and if that isn't enough to get you over the hump, constructing a minimal reproducible example and posing this as a question. With an actual example in hand, there are any number of folks here who will be able to quickly answer your question. Cheers! – Josh O'Brien Nov 19 '15 at 23:28