Dear list members,
I've encountered a problem using expand.model.frame() with a model specified
without an explicit data argument. To illustrate (R 2.0.1 under Windows XP):
> x <- rnorm(10)
> y <- x + rnorm(10)
> mod <- lm(y ~ x)
> z <- 1:10
> expand.model.frame(mod, "z")
Error in eval(expr, envir, enclos) : Object "y" not found
> environment(formula(mod))
<environment: R_GlobalEnv>
>
But when data is specified in the call to lm(), I get the expected
behaviour:
> DF <- data.frame(x, y, z)
> remove(x, y, z)
> mod <- lm(y ~ x, data=DF)
> expand.model.frame(mod, "z")
y x z
1 1.0321783 -0.8145171 1
2 -1.7639244 -0.5965655 2
3 -0.8150426 -1.4833768 3
4 0.6047758 0.2030853 4
5 -1.9870830 -1.5559511 5
6 -0.4807146 -1.0751953 6
7 1.3683736 0.1610050 7
8 0.9323799 0.3537983 8
9 -1.0797075 -0.4218457 9
10 -3.0528776 -1.9750668 10
> environment(formula(mod))
<environment: R_GlobalEnv>
>From ?expand.model.frame:
"Usage
expand.model.frame(model, extras,
envir = environment(formula(model)),
na.expand = FALSE)
Arguments
model a fitted model
extras one-sided formula or vector of character strings describing new
variables to be added
envir an environment to evaluate things in
na.expand logical; see below"
So, if "things" are evaluated by default in
environment(formula(model)), why
does the first example fail and the second one work? Obviously, I'm not
understanding something properly here.
Any help would be appreciated -- either to get expand.model.frame() to work
or to suggest an alternative approach.
Happy new year to all.
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
John Fox <jfox <at> mcmaster.ca> writes: : : Dear list members, : : I've encountered a problem using expand.model.frame() with a model specified : without an explicit data argument. To illustrate (R 2.0.1 under Windows XP): : : > x <- rnorm(10) : > y <- x + rnorm(10) : > mod <- lm(y ~ x) : > z <- 1:10 : > expand.model.frame(mod, "z") : Error in eval(expr, envir, enclos) : Object "y" not found : > environment(formula(mod)) : <environment: R_GlobalEnv> : > : : But when data is specified in the call to lm(), I get the expected : behaviour: : : > DF <- data.frame(x, y, z) : > remove(x, y, z) : > mod <- lm(y ~ x, data=DF) : > expand.model.frame(mod, "z") : y x z : 1 1.0321783 -0.8145171 1 : 2 -1.7639244 -0.5965655 2 : 3 -0.8150426 -1.4833768 3 : 4 0.6047758 0.2030853 4 : 5 -1.9870830 -1.5559511 5 : 6 -0.4807146 -1.0751953 6 : 7 1.3683736 0.1610050 7 : 8 0.9323799 0.3537983 8 : 9 -1.0797075 -0.4218457 9 : 10 -3.0528776 -1.9750668 10 : > environment(formula(mod)) : <environment: R_GlobalEnv> : : >From ?expand.model.frame: : : "Usage : expand.model.frame(model, extras, : envir = environment(formula(model)), : na.expand = FALSE) : : Arguments : model a fitted model : extras one-sided formula or vector of character strings describing new : variables to be added : envir an environment to evaluate things in : na.expand logical; see below" : : So, if "things" are evaluated by default in environment(formula(model)), why : does the first example fail and the second one work? Obviously, I'm not : understanding something properly here. : : Any help would be appreciated -- either to get expand.model.frame() to work : or to suggest an alternative approach. : : Happy new year to all. : : John Look at the source of expand.model.frame. The second line of expand.model.frame uses the data component of mod$call and if its not there expand.model.frame chokes. As a workaround check if its missing and supply it yourself: if (is.null(mod$call$data)) mod$call$data <- environment(formula(mod)) expand.model.frame(mod, "z") expand.model.frame should probably be doing that itself.