update
This function solves two problems with update
: pipes
will break when the data is first piped to the lm
object, and data is re-evaluated against the full dataset, even if the model
being updated had na.action = "na.omit"
. See details below for more
information.
update(old, new, ...)
Changes to the formula; see update.formula
for
details.
Additional arguments to the call (see
update
), or arguments with changed values. Use
name = NULL
to remove the argument name.
If evaluate = TRUE the fitted object, otherwise the updated call.
Problem with pipes: update
does not support
linear models where the data was piped via %>%
to
the model function as in mtcars %>% lm(mpg ~ hp, data = .) %>%
stats::update()
. This is because the update
function
relies on getCall
, which returns a call that has
data = .
argument, which is completely uninformative. This function
creates a new model by extracting the formula from the old model via
formula
and the data used in the old model via the
model$call
and environment
. Then the new (old) model is updated
as it now has all the necessary components.
Problem with na.action
: When updating models with missing
data, the new model is evaluated against the full dataset even if the
original model had missing data for some of the values. For example, if there
are missing values on X for a model where Y ~ X, when updating to fit the
null model Y ~ NULL, the full set of Y is used instead of only the values of
Y where there are values of X. This is not inherently a problem, however it
yields incorrect degrees of freedom and sum of squares for the total row in
an ANOVA because different datasets are being used for the total row and the
model and error rows. The present solution creates NA
values listwise
for all variables in the old model so that the new model does not use those
data.