[R] Environment of a LM created in a function

Thaler, Thorn, LAUSANNE, Applied Mathematics Thorn.Thaler at rdls.nestle.com
Fri Jul 29 10:37:26 CEST 2011


Dear all,

Quite often I have the situation that I've multiple response variables
and I create Linear Models for them in a function. The following code
illustrates my usual approach:

---------------8<---------------
set.seed(123)
dat <- data.frame(x = rep(rep(1:3, each = 3), 4), y = rep(1:3, 12))
dat$z1 <- rnorm(36, dat$x + dat$y)
dat$z2 <- rnorm(36, dat$x + 2*dat$y)
dat$z3 <- rnorm(36, dat$x + 3*dat$y)

modelInFunction <- function(resp, expl, df) {
  fo <- as.formula(paste(resp, paste(expl, collapse = " + "), sep = " ~
"))
  lm(fo, data = df)
}

ex <- c("x", "y")
resp <- paste("z", 1:3, sep = "")

models <- lapply(resp, modelInFunction, expl = ex, df = dat)
---------------8<---------------

So far so good. But if I try to update any of the models afterwards, I
get an error:

---------------8<---------------
> update(models[[1]], . ~ . )
Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type
---------------8<---------------

In my opinion this happens, because the update function does not know
where to look for the data frame containing the original values.
However, if I try

---------------8<---------------
model.frame(models[[1]])
---------------8<---------------

I get the right answer. Thus, I guess it has something to do with
different environments and I was wondering what the recommended way
would be to create an LM object within a function, which could be
processed outside this particular function in the usual way? Or is it
simply a bug in update?

Any help highly appreciated.

Thanks,

-Thorn



More information about the R-help mailing list