I'm trying to write some functions to ease refitting multiple models, but find it painful, as R is unable to locate proper data
, when it plunges deeper into evaluation tree.
Despite an effort was made to store the formula environment inside the model, I guess there's really no way to unambiguously point to the raw data object.
This becomes even harder for fitting survival curves using survfit
, where no terms
object is being stored inside.
Do I really need to retype the data/formula as a parameter each time?
Example:
# model-fitting wrapper function
fn <- function(fn_formula, fn_data) {
lm(formula = fn_formula, data = fn_data)
}
# specify exemplary data and formula
data <- data.frame(
y = rnorm(100),
x1 = rnorm(100),
x2 = rnorm(100))
formula <- y ~ x1
# try to create and update the fit with different parameters
fn_fit <- fn(formula, data)
update(fn_fit, ~ x2)
# Error in is.data.frame(data) : object 'fn_data' not found
terms(fn_fit) %>% attr('.Environment')
# <environment: R_GlobalEnv>
terms(fn_fit$model) %>% attr('.Environment')
# <environment: R_GlobalEnv>
getCall(fn_fit)
# lm(formula = fn_formula, data = fn_data)