[R] update?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 2 22:08:24 CET 2006


Here is another solution in addition to the ones already
provided.   This creates a proto object which is an
environment that contains v and whose parent is
the formula's environment.  update will look in the proto
object and find v.  It won't find x and y but will next
look to the parent of the proto object and will find them there.
The proto package makes it convenient to create an environment
with a specified parent and contents all in one line
but at the expensive of a few more lines you could also do
it without the proto package.

library(proto)
nonadd <- function(formula.) {
   f <- lm(formula.)
   g <- update(f, . ~ . + v, data = proto(environment(formula.), v =
f$fitted.values^2))
   anova(f, g)
}

x <- rnorm(20)
y <- rnorm(20)
nonadd(y ~ x)



On 1/2/06, roger koenker <rkoenker at uiuc.edu> wrote:
> I'm having problems with environments and update() that
> I expect have a simple explanation.  To illustrate, suppose
> I wanted to make a very primitive Tukey one-degree-of-
> freedom for nonadditivity test and naively wrote:
>
> nonadd <- function(formula){
>         f <- lm(formula)
>         v <- f$fitted.values^2
>         g <- update(f, . ~ . + v)
>         anova(f,g)
>         }
>
> x <- rnorm(20)
> y <- rnorm(20)
> nonadd(y ~ x)
>
> Evidently, update is looking in the environment producingf f and
> doesn't find v, so I get:
>
> Error in eval(expr, envir, enclos) : Object "v" not found
>
> This may (or may not) be related to the discussion at:
> http://bugs.r-project.org/cgi-bin/R/Models?id=1861;user=guest
>
> but in any case I hope that someone can suggest how such
> difficulties can be circumvented.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    rkoenker at uiuc.edu            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list