[R] update?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 2 21:18:59 CET 2006


R was changed 1.2.0 to look for variables in the environment of formula 
(see ?model.frame and ?formula). So either use data() or put v there as in

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

or (I think preferable)

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


On Mon, 2 Jan 2006, roger koenker 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list