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
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.