0
reg_ss <- predict(lm(stem_d~stand_id*yr,ss))
fitted.values(reg_ss)

#Error: $ operator is invalid for atomic vectors

I have tried this with fitted() and fitted.values() and receive the same error.

stand_id is a factor with 300+ levels and yr is an integer 1-19, but both are numbers.

I have data on tree stem density collected in stands every 2-3 years for 20 years. I want to run a linear regression and predict stem density for stands in the years between samplings, i.e. use data from year 1 and 3 to predict stem density in year 2.

Any suggestions on how I can get predicted values using fitted() or any other method would be greatly appreciated. I suspect it has something to do with dummy variables assigned to the categories but can't seem to find any information on a solution.

Thanks in advance!

MrFlick
  • 195,160
  • 17
  • 277
  • 295

1 Answers1

2

If you want fitted values, you should not be calling predict() first.

reg_ss <- lm(stem_d~stand_id*yr,ss)
predict(reg_ss)
fitted(reg_ss)

When you don't pass new data to predict, it's basically doing the same thing as fitted so you get essentially the same values back. Both fitted and predict will return a simple named vector. You cannot use fitted on a named vector (hence the error message).

If you want to predict unobserved values, you need to pass a newdata= parameter to predict(). You should pass in a data.frame with columns named "stand_id" and "yr" just like ss. Make sure to match up the factor levels as well.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks so much for your help! So both predicted() and fitted() just return the error message. "#Error: $ operator is invalid for atomic vectors" - So I do not get any results. I will try creating newdata = as described but how do I match up the factor levels? The combination of unique combinations of these factors is in the thousands. Should I use something like expand.grid to create a dataframe with all possible combos and then pass that in as newdata = ? – doing_my_best Jul 21 '14 at 16:06
  • Then please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that recreates the same error message. This runs just fine for me if I define `ss<-data.frame(stand_id=runif(10),stem_d=rnorm(10), yr=1:10)` – MrFlick Jul 21 '14 at 16:09
  • If I understand the ss you created, it has continuous variables for stand_id and yr where my data has stand_id as a factor and yr is an integer. Would this cause the error? – doing_my_best Jul 21 '14 at 16:42
  • It also works just fine with `ss<-data.frame(stand_id=factor(sample(1:3, 10, replace=T)),stem_d=rnorm(10), yr=1L:10L)`. I need *you* to come you with a reproducible example that doesn't work because everything I come up with works just fine. – MrFlick Jul 21 '14 at 16:50
  • So I figured out how to pass in the newdata and it works now. Thanks for all your help. – doing_my_best Jul 22 '14 at 16:14