| 1 |
#' @templateVar class lmRob |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `lmRob` object returned from [robust::lmRob()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @details For tidiers for robust models from the \pkg{MASS} package see |
| 8 |
#' [tidy.rlm()]. |
| 9 |
#' |
| 10 |
#' @examplesIf rlang::is_installed("robust") |
| 11 |
#' |
| 12 |
#' # load modeling library |
| 13 |
#' library(robust) |
| 14 |
#' |
| 15 |
#' # fit model |
| 16 |
#' m <- lmRob(mpg ~ wt, data = mtcars) |
| 17 |
#' |
| 18 |
#' # summarize model fit with tidiers |
| 19 |
#' tidy(m) |
| 20 |
#' augment(m) |
| 21 |
#' glance(m) |
| 22 |
#' |
| 23 |
#' @aliases robust_tidiers |
| 24 |
#' @export |
| 25 |
#' @family robust tidiers |
| 26 |
#' @seealso [robust::lmRob()] |
| 27 |
tidy.lmRob <- function(x, ...) { |
| 28 |
co <- stats::coef(summary(x)) |
| 29 |
ret <- as_tibble(co, rownames = "term") |
| 30 |
names(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 31 |
ret |
| 32 |
} |
| 33 |
|
| 34 |
#' @templateVar class lmRob |
| 35 |
#' @template title_desc_augment |
| 36 |
#' |
| 37 |
#' @inherit tidy.lmRob params examples |
| 38 |
#' @template param_data |
| 39 |
#' @template param_newdata |
| 40 |
#' |
| 41 |
#' @details For tidiers for robust models from the \pkg{MASS} package see |
| 42 |
#' [tidy.rlm()]. |
| 43 |
#' |
| 44 |
#' @export |
| 45 |
#' @family robust tidiers |
| 46 |
#' @seealso [robust::lmRob()] |
| 47 |
augment.lmRob <- function(x, data = model.frame(x), newdata = NULL, ...) { |
| 48 |
# NOTES on predict.lmRob: |
| 49 |
# - there's no na.action = na.pass argument |
| 50 |
# - predict(x, newdata = NULL) gives an error (i.e. newdata either must |
| 51 |
# be a dataset or missing) |
| 52 |
# - predict(x, single_row_df_with_missing_data) returns numeric(0) |
| 53 |
|
| 54 |
passed_newdata <- !is.null(newdata) |
| 55 |
df <- if (passed_newdata) newdata else data |
| 56 |
df <- as_augment_tibble(df) |
| 57 |
|
| 58 |
# this is a really ugly way to recover NA predictions |
| 59 |
rows <- split(df, 1:nrow(df)) |
| 60 |
preds <- purrr::map(rows, \(.x) predict(x, newdata = .x)) |
| 61 |
no_pred <- purrr::map_lgl(preds, \(x) length(x) == 0) |
| 62 |
preds[no_pred] <- NA |
| 63 |
|
| 64 |
df$.fitted <- as.numeric(preds) |
| 65 |
|
| 66 |
resp <- safe_response(x, df) |
| 67 |
if (!is.null(resp)) { |
| 68 |
df$.resid <- df$.fitted - resp |
| 69 |
} |
| 70 |
df |
| 71 |
} |
| 72 |
|
| 73 |
#' @templateVar class lmRob |
| 74 |
#' @template title_desc_glance |
| 75 |
#' |
| 76 |
#' @inherit tidy.lmRob params examples |
| 77 |
#' @template param_unused_dots |
| 78 |
#' |
| 79 |
#' @evalRd return_glance( |
| 80 |
#' "r.squared", |
| 81 |
#' "deviance", |
| 82 |
#' "sigma", |
| 83 |
#' "df.residual", |
| 84 |
#' "nobs" |
| 85 |
#' ) |
| 86 |
#' |
| 87 |
#' @export |
| 88 |
#' @family robust tidiers |
| 89 |
#' @seealso [robust::lmRob()] |
| 90 |
#' |
| 91 |
glance.lmRob <- function(x, ...) { |
| 92 |
as_glance_tibble( |
| 93 |
r.squared = x$r.squared, |
| 94 |
deviance = x$dev, |
| 95 |
sigma = summary(x)$sigma, |
| 96 |
df.residual = x$df.residual, |
| 97 |
nobs = stats::nobs(x), |
| 98 |
na_types = "rrrii" |
| 99 |
) |
| 100 |
} |