Viechtbauer, Wolfgang (SP)
2021-Nov-13 19:24 UTC
[R] Using update() within a function with a changed formula
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. Best, Wolfgang
Duncan Murdoch
2021-Nov-13 20:07 UTC
[R] Using update() within a function with a changed formula
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