Skip to content

problem with expand.model.frame

2 messages · John Fox

#
Dear R list members,

I'm encountering a problem with expand.model.frame(): Suppose that I define 
the following simple function (meant
just to illustrate the problem):

     > fun <- function(model){
     +     expand.model.frame(model, all.vars(formula(model)))
     +     }
     >

and I have the following model, created with an explicit data argument:

     > mod

     Call:
     lm(formula = prestige ~ income + I(education^2), data = Prestige)

     Coefficients:
     (Intercept)          income  I(education^2)
         15.129723        0.001262        0.188479

Here fun() works fine, adding education from the data frame Prestige to the 
model frame:

     > fun(mod)
                             prestige income I(education^2) education
     GOV.ADMINISTRATORS            68.8  12351       171.8721     13.11
     GENERAL.MANAGERS              69.1  25879       150.3076     12.26
     ACCOUNTANTS                   63.4   9271       163.0729     12.77
     . . .
     BOOKBINDERS                   35.2   3617        73.1025      8.55

Now suppose that I have the following model, fit with the data frame 
Prestige attached to the search path, but no
explicit data argument:

     > mod.2

     Call: lm(formula = prestige ~ income + I(education^2))

     Coefficients:
     (Intercept)          income  I(education^2)
         15.129723        0.001262        0.188479

Calling fun() with this model as an argument produces an error:

     > fun(mod.2)
     Error in eval(expr, envir, enclos) : Object "prestige" not found

     > traceback()
     8: eval(expr, envir, enclos)
     7: eval(predvars, data, env)
     6: model.frame.default(prestige ~ income + I(education^2) + (prestige +
         income + education), data = NULL, subset = NULL, na.action = NULL)
     5: model.frame(prestige ~ income + I(education^2) + (prestige +
         income + education), data = NULL, subset = NULL, na.action = NULL)
     4: eval(expr, envir, enclos)
     3: eval(call("model.frame", ff, data = data, subset = subset, 
na.action = naa),
         envir)
     2: expand.model.frame(model, all.vars(formula(model)))
     1: fun(mod.2)


Yet the environment of the model formula for mod.2 (which is the default 
environment for expand.model.formula) is
the global environment, and the variable prestige is accessible (via the 
attached data frame Prestige) from the
global environment):

     > environment(formula(mod.2))
     <environment: R_GlobalEnv>

     > get("prestige", env=.GlobalEnv)
     [1] 68.8 69.1 63.4 56.8 73.5 77.6 72.6 78.1 73.1 68.8 62.0 60.0 53.8 
62.2 74.9
     [16] 55.1 82.3 58.1 58.3 72.8 84.6 59.6 66.1 87.2 66.7 68.4 64.7 34.9 
72.1 69.3
     [31] 67.5 57.2 57.6 54.1 46.0 41.9 49.4 42.3 47.7 30.9 32.7 38.7 36.1 
37.2 38.1
     [46] 29.4 51.1 35.7 35.6 41.5 40.2 26.5 14.8 23.3 47.3 47.1 51.1 43.5 
51.6 29.7
     [61] 20.2 54.9 25.9 20.8 17.3 20.1 44.1 21.5 35.3 38.9 25.2 34.8 23.2 
33.3 28.8
     [76] 42.5 44.2 35.9 41.8 35.9 43.7 50.8 37.2 28.2 38.1 50.3 27.3 40.9 
50.2 51.1
     [91] 38.9 36.2 29.9 42.9 26.5 66.1 48.9 35.9 25.1 26.1 42.2 35.2

     > search()
     [1] ".GlobalEnv"     "Prestige"       "package:modreg" "package:car"
     [5] "package:ctest"  "Autoloads"      "package:base"
     >

Obviously, I'm not thinking about this properly. My aim is to have fun() 
work whether or not the model was created
with an explicit data argument.

I'm using R 1.6.0 under WIndows 2000.

Any help would be appreciated.

Thanks,
  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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Dear R list members,

To answer the question that I previously posted, the following slightly 
altered definition of expand.model.frame seems to do the trick:

     expand.model.frame <- function (model, extras, envir = 
environment(formula(model)),
         na.expand = FALSE){  # modified version of R base function
         f <- formula(model)
         data <- eval(model$call$data, envir)
         ff <- foo ~ bar + baz
         if (is.call(extras))
             gg <- extras
         else gg <- parse(text = paste("~", paste(extras, collapse = 
"+")))[[1]]
         ff[[2]] <- f[[2]]
         ff[[3]][[2]] <- f[[3]]
         ff[[3]][[3]] <- gg[[2]]
         if (!na.expand) {
             naa <- model$call$na.action
             subset <- model$call$subset
             rval <- if (is.null(data)) eval(call("model.frame", ff, # modified
                 subset = subset, na.action = naa), envir)           #  lines
             else eval(call("model.frame", ff, data = data,          #
                 subset = subset, na.action = naa), envir)           #
             }
         else {
             subset <- model$call$subset
             rval <- eval(call("model.frame", ff, data = data, subset = 
subset,
                 na.action = I), envir)
             oldmf <- model.frame(model)
             keep <- match(rownames(oldmf), rownames(rval))
             rval <- rval[keep, ]
             class(rval) <- "data.frame"
             }
         return(rval)
         }

I'm hesitant to screw with the definition of expand.model.frame (maybe the 
new name space mechanism helps here), so I've made the redefinition local 
to my application, and now everything seems to work.

I'm still interested in people's reactions to the problem.

Thanks,
  John
At 12:11 PM 11/2/2002 -0500, I wrote:
-----------------------------------------------------
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._