[R] use of expand.model.frame
John Fox
jfox at mcmaster.ca
Fri Mar 29 02:48:19 CET 2002
Dear R-help list members,
I'm encountering problems using expand.model.frame. To illustrate, consider
the function
> test <- function(model){
+ expand.model.frame(model, "income")
+ }
>
The data frame Prestige (from the car library) has several variables,
including prestige, income and education. I've attached this data frame
> search()
[1] ".GlobalEnv" "Prestige" "package:lattice"
"package:grid" "package:car" "package:ctest"
[7] "Autoloads" "package:base"
>
and fit the following model
> mod.1 <- lm(prestige ~ (log(income) + education)*type)
>
From the documentation for expand.model.frame, the default environment in
which "things" are evaluated is environment(formula(model)), where model is
the first argument to expand.model frame; in this case
> environment(formula(mod.1))
<environment: R_GlobalEnv>
>
When I call the test function with mod.1, however, I get the following error:
> test(mod.1)
Error in eval(expr, envir, enclos) : Object "prestige" not found
>
But if I fit the same model specifying the data argument to lm as Prestige,
then test works,
> mod.2 <- lm(prestige ~ (log(income) + education)*type, data=Prestige)
> test(mod.2)
prestige log(income) education type income
GOV.ADMINISTRATORS 68.8 9.421492 13.11 prof 12351
GENERAL.MANAGERS 69.1 10.161187 12.26 prof 25879
ACCOUNTANTS 63.4 9.134647 12.77 prof 9271
. . .
TYPESETTERS 42.2 8.773694 10.00 bc 6462
BOOKBINDERS 35.2 8.193400 8.55 bc 3617
>
even though
> environment(formula(mod.2))
<environment: R_GlobalEnv>
>
that is, still the global environment.
Obviously, I'm not thinking correctly about the scoping issue.
What I need is a version of test that works in both cases -- whether or not
the data argument is specified to lm. (Of course, I need to use the
expanded model frame as part of a more complicated function.)
I'm running R version 1.4.1 under Windows 2000.
Any help would be greatly appreciated.
John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list