[R] Environment of a LM created in a function

Peter Ehlers ehlers at ucalgary.ca
Sun Jul 31 16:06:11 CEST 2011


I haven't seen an answer yet, so I'll give it a shot (below).

On 2011-07-29 01:37, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote:
> 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.

True. You can call update() with 'evaluate = FALSE' to see that
it can't find 'df'. The particular form of the error is because
'df' is a function (and hence possibly not a good choice for a
variable name). Replacing your 'df' with 'dta' I get

  update(models[[1]], . ~ .)
  #Error in inherits(x, "data.frame") : object 'dta' not found

But ?update tells us that we can provide additional or changed
arguments to the call. So an easy fix is:

  update(models[[1]], . ~ ., data = dat)

or

  update(models[[1]], . ~ ., data = model.frame(models[[1]]))

Peter Ehlers

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list