[R] Using update() within a function with a changed formula

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat Nov 13 21:07:08 CET 2021


On 13/11/2021 2:24 p.m., Viechtbauer, Wolfgang (SP) wrote:
> Hello all,
> 
> Say I would like to change the outcome in a formula to a variable not part of the original dataset. This works just fine:
> 
> res <- lm(mpg ~ wt + cyl, data=mtcars)
> res
> y <- rnorm(nobs(x))
> update(x, formula = y ~ .)
> 
> But not when doing so within a function:
> 
> rm(y)
> 
> f <- function(x) {
>     y <- rnorm(nobs(x))
>     update(x, formula = y ~ .)
> }
> 
> f(res)
> 
> Is there a way to make this work? Using y <<- ... inside the function works, but I would like to avoid such a heavy-handed approach.

Formulas have associated environments, and that's where functions using 
them look for variables.  update() leaves the formula environment 
unchanged, as documented in ?update.formula. Since yours started out as 
globalenv(), you would need to put y there, and that's what you want to 
avoid.

However, the default update() method (the one you call on res, that 
calls the formula method) allows you to change other things.

So you could do it this way:

res <- lm(mpg ~ wt + cyl, data=mtcars)

f <- function(x) {
   y <- rnorm(nobs(x))
   newdata <- cbind(model.frame(x), y)
   update(x, formula = y ~ ., data = newdata)
}
f(res)

Duncan Murdoch



More information about the R-help mailing list