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 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
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
>