[R] update.default evaluating in wrong environment?
Thomas Lumley
tlumley at u.washington.edu
Tue Oct 10 20:52:30 CEST 2006
On Tue, 10 Oct 2006, Martin C. Martin wrote:
> Thanks, or even just:
>
> e <- environment(formula(f.lm))
>
> But this was more of a bug report. Is update.default wrong? Should it
> be changed? I don't see how evaluating in update's parent environment
> would ever be better default behavior than the formula's environment.
>
This is deliberate (although that doesn't necessarily imply that it is
optimal). There already is a bug report (PR#1861) on this issue, so
another one certainly isn't appropriate.
The comments for this bug report give an example taken from the scripts
for MASS chapter 6, doing a bootstrap of a linear model
ph.fun <- function(data, i) {
d <- data
d$calls <- d$fitted + d$res[i]
coef(update(fit, data=d))
}
This is not uncommon for resampling and other perturbations of a model and
requires looking in the calling frame of update().
For other uses such as yours the right place would be the frame where the
model was created. Neither the environment of the formula nor the calling
frame of update() is guaranteed to be the right place and it is easy to
come up with examples where each works and the other doesn't.
There really isn't a trivial solution to this.
-thomas
More information about the R-help
mailing list