Safely fit a linear model¶
Source: R/utils.R ยท Help: ?safe_lm
Wraps stats::lm() in a tryCatch block. If the fit fails, it logs the
error via ht_log() and, with .recover = TRUE, refits the model without
the rows whose variables of the formula hold missing or non-finite values,
if there are any: it returns that refit when it succeeds, and otherwise
stops with the message of the error and any suggestions for cleaning the
data. This helper is used internally by diagnostic tests so failures are
easier to debug.
Usage¶
Arguments¶
formula- Model formula.
data- Data frame to evaluate the formula in.
...- Additional arguments passed to
stats::lm(). .recover- Logical flag: whether a failed fit is attempted again without the problematic rows before the helper stops.
Value¶
A fitted model object: the fit of formula to data, or the refit
that recovered from its failure.