[R] Newb Prediction Question using stepAIC and predict(), is R wrong?

Greg Snow Greg.Snow at imail.org
Mon Feb 14 17:57:15 CET 2011


There is a section on environments on the help page ?formula, but that may not be completely clear to newer users.

Basically the idea is that when you specify a formula, the default place that R will look for the variables in the formula is the data or newdata argument.  So if you use those properly, then the data argument data frame will be used to fit the original model and the equivalent parts of the newdata data frame will be used when predicting.  But if you use a term like mydata$x in the formula, then R will look for something named `mydata$x` in the specified data frame, but not find it (it does not look for just 'x'), then continue in the search path until it finds the original data you used, bypassing the data you want it to use.

It is kind of like trying to teach a robot to tie a know in a rope, but rather than telling the robot to take the end of the rope that is currently in its left hand over the end currently in its right hand (so that it can tie a knot in any rope it has), you instead tell it to always use the rope that was in your hands at the time of giving it the instructions.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of BSanders
> Sent: Sunday, February 13, 2011 9:19 PM
> To: r-help at r-project.org
> Subject: Re: [R] Newb Prediction Question using stepAIC and predict(),
> is R wrong?
> 
> 
> "So do not use '$' or '[..]' terms in model formulae - this is going to
> cause
> problems when it comes to predict, because your formula will not
> associate
> with the names it has in its formula in the new data frame.  When you
> think
> about it, this is obvious. "
> 
> I really don't understand why this is obvious.  I'm new to R
> programming,
> and not all that experienced in programming in general.  I would be
> grateful
> for any further explanation.
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Newb-
> Prediction-Question-using-stepAIC-and-predict-is-R-wrong-
> tp3298569p3304421.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list