| 1 |
#' @templateVar class speedlm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `speedlm` object returned from [speedglm::speedlm()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_unused_dots |
| 7 |
#' |
| 8 |
#' @evalRd return_tidy(regression = TRUE) |
| 9 |
#' |
| 10 |
#' @examplesIf rlang::is_installed("speedglm") |
| 11 |
#' |
| 12 |
#' # load modeling library |
| 13 |
#' library(speedglm) |
| 14 |
#' |
| 15 |
#' # fit model |
| 16 |
#' mod <- speedlm(mpg ~ wt + qsec, data = mtcars, fitted = TRUE) |
| 17 |
#' |
| 18 |
#' # summarize model fit with tidiers |
| 19 |
#' tidy(mod) |
| 20 |
#' glance(mod) |
| 21 |
#' augment(mod) |
| 22 |
#' |
| 23 |
#' @aliases speedlm_tidiers |
| 24 |
#' @export |
| 25 |
#' @family speedlm tidiers |
| 26 |
#' @seealso [speedglm::speedlm()], [tidy.lm()] |
| 27 |
#' @include stats-lm.R |
| 28 |
tidy.speedlm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { |
| 29 |
check_ellipses("exponentiate", "tidy", "speedlm", ...) |
| 30 |
|
| 31 |
ret <- as_tibble(summary(x)$coefficients, rownames = "term") |
| 32 |
colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 33 |
|
| 34 |
# summary(x)$coefficients misses rank deficient rows (i.e. coefs that |
| 35 |
# summary.lm() sets to NA), catch them here and add them back |
| 36 |
coefs <- tibble::enframe(stats::coef(x), name = "term", value = "estimate") |
| 37 |
ret <- left_join(coefs, ret, by = c("term", "estimate")) |
| 38 |
|
| 39 |
if (conf.int) { |
| 40 |
ci <- broom_confint_terms(x, level = conf.level) |
| 41 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 42 |
} |
| 43 |
|
| 44 |
ret |
| 45 |
} |
| 46 |
|
| 47 |
#' @templateVar class speedlm |
| 48 |
#' @template title_desc_glance |
| 49 |
#' |
| 50 |
#' @inherit tidy.speedlm params examples |
| 51 |
#' @template param_unused_dots |
| 52 |
#' |
| 53 |
#' @evalRd return_glance( |
| 54 |
#' "r.squared", |
| 55 |
#' "adj.r.squared", |
| 56 |
#' statistic = "F-statistic.", |
| 57 |
#' "p.value", |
| 58 |
#' "df", |
| 59 |
#' "logLik", |
| 60 |
#' "AIC", |
| 61 |
#' "BIC", |
| 62 |
#' "deviance", |
| 63 |
#' "df.residual", |
| 64 |
#' "nobs" |
| 65 |
#' ) |
| 66 |
#' |
| 67 |
#' @export |
| 68 |
#' @family speedlm tidiers |
| 69 |
#' @seealso [speedglm::speedlm()] |
| 70 |
glance.speedlm <- function(x, ...) { |
| 71 |
s <- summary(x) |
| 72 |
as_glance_tibble( |
| 73 |
r.squared = s$r.squared, |
| 74 |
adj.r.squared = s$adj.r.squared, |
| 75 |
statistic = s$fstatistic[1], |
| 76 |
p.value = s$f.pvalue, |
| 77 |
df = x$nvar, |
| 78 |
logLik = as.numeric(stats::logLik(x)), |
| 79 |
AIC = stats::AIC(x), |
| 80 |
BIC = stats::BIC(x), |
| 81 |
deviance = x$RSS, |
| 82 |
df.residual = stats::df.residual(x), |
| 83 |
nobs = stats::nobs(x), |
| 84 |
na_types = "rrrrirrrrii" |
| 85 |
) |
| 86 |
} |
| 87 |
|
| 88 |
#' @templateVar class speedlm |
| 89 |
#' @template title_desc_augment |
| 90 |
#' |
| 91 |
#' @inherit tidy.speedlm params examples |
| 92 |
#' @template param_data |
| 93 |
#' @template param_newdata |
| 94 |
#' @template param_unused_dots |
| 95 |
#' |
| 96 |
#' @evalRd return_augment() |
| 97 |
#' |
| 98 |
#' @importFrom rlang expr enexpr |
| 99 |
#' @export |
| 100 |
#' @family speedlm tidiers |
| 101 |
#' @seealso [speedglm::speedlm()] |
| 102 |
augment.speedlm <- function(x, data = model.frame(x), newdata = NULL, ...) { |
| 103 |
# this is a hacky way to prevent the following bug: |
| 104 |
# speedglm::speedglm(hp ~ log(mpg), mtcars, fitted = TRUE) |
| 105 |
# this also protects against the fact that speedlm doesn't save fitted |
| 106 |
# values by default, in which case predict(x, newdata = NULL) returns NULL |
| 107 |
default_data_arg <- identical(enexpr(data), expr(model.frame(x))) |
| 108 |
|
| 109 |
# both speedlm and speedglm work the same for cts outcomes, except they save |
| 110 |
# the fitted values in different ways. |
| 111 |
no_fitted <- is.null(x$linear.predictors) && is.null(x$fitted.values) |
| 112 |
|
| 113 |
if (default_data_arg && no_fitted) { |
| 114 |
cli::cli_abort( |
| 115 |
"Must specify {.arg data} argument or refit with |
| 116 |
{.code fitted = TRUE}." |
| 117 |
) |
| 118 |
} |
| 119 |
|
| 120 |
# no influence measures for speedlm, can only get fitted values |
| 121 |
# standard errors also not available for fit |
| 122 |
augment_newdata(x, data, newdata, .se_fit = FALSE) |
| 123 |
} |