[R] Error with predict and newdata
kira taylor
kiradanielletaylor at gmail.com
Fri Aug 7 23:41:20 CEST 2015
Hi!
I am trying to use predict to apply my model to data from one time period to
see what might be the values for another time period. I did this
successfully for one dataset, and then tried on another with identical code
and got the following error:
Error in eval(predvars, data, env) :
numeric 'envir' arg not of length one
The only difference between the two datasets was that my predictor model for
the first dataset had two predictor variables and my model for the second
dataset had only one. Why would this make a difference?
My dougfir.csv contains just two columns with thirty numbers in each,
labeled height and dryshoot.
my lm is: fitdougfir <- lm(dryshoot~height,data=dougfir)
It gets a little complicated (and messy, sorry! I am new to R) because I
then made a second .csv - the one I used to make my model contained values
from just June. My new .csv (called alldatadougfir.csv) includes values
from October as well, and also contains a "date" column that labels the
values either "june" or "october."
I did the following to separate the height data by date:
alldatadougfir[alldatadougfir$date=="june",c("height")]->junedatadougfir
alldatadougfir[alldatadougfir$date=="october",c("height")]->
octoberdatadougfir
I then want to use my June model to predict my October dryshoots using
height as my variable and I did the following:
predict(fitdougfir, newdata=junedatadougfir)
predict(fitdougfir, newdata=octoberdatadougfir)
Again, I did this with an identical dataset successfully - the only
difference was that my model in the successful dataset had two predictor
variables instead of the one variable (height) I have in this dataset.
Sorry again for my messy code!
Thank you very much,
Kira
[[alternative HTML version deleted]]
More information about the R-help
mailing list