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]]
On Aug 7, 2015, at 2:41 PM, kira taylor wrote:> 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")]-> > octoberdatadougfirThose are no longer lists or dataframe, which are the proper classes of object to pass to predict. -- David.> > 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA
David Winsemius <dwinsemius <at> comcast.net> writes:> > > On Aug 7, 2015, at 2:41 PM, kira taylor wrote: > > > 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 > >Please don't cross-post on StackOverflow and the r-help lists (it will usually lead to duplicated/wasted effort). If you must, at least post a link/indicate in each venue that you have cross-posted to the other: (line-broken link, reassemble to visit) http://stackoverflow.com/questions/31887043/ error-with-predict-and-newdata-dependent-on-number-of- predictor-variable-in-mod/31887398#31887398