| 1 |
#' @templateVar class lm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `lm` object created by [stats::lm()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_exponentiate |
| 7 |
#' @template param_unused_dots |
| 8 |
#' |
| 9 |
#' @evalRd return_tidy(regression = TRUE) |
| 10 |
#' |
| 11 |
#' @details If the linear model is an `mlm` object (multiple linear model), |
| 12 |
#' there is an additional column `response`. See [tidy.mlm()]. |
| 13 |
#' |
| 14 |
#' @examplesIf rlang::is_installed("ggplot2") |
| 15 |
#' |
| 16 |
#' library(ggplot2) |
| 17 |
#' library(dplyr) |
| 18 |
#' |
| 19 |
#' mod <- lm(mpg ~ wt + qsec, data = mtcars) |
| 20 |
#' |
| 21 |
#' tidy(mod) |
| 22 |
#' glance(mod) |
| 23 |
#' |
| 24 |
#' # coefficient plot |
| 25 |
#' d <- tidy(mod, conf.int = TRUE) |
| 26 |
#' |
| 27 |
#' ggplot(d, aes(estimate, term, xmin = conf.low, xmax = conf.high, height = 0)) + |
| 28 |
#' geom_point() + |
| 29 |
#' geom_vline(xintercept = 0, lty = 4) + |
| 30 |
#' geom_errorbarh() |
| 31 |
#' |
| 32 |
#' # aside: There are tidy() and glance() methods for lm.summary objects too. |
| 33 |
#' # this can be useful when you want to conserve memory by converting large lm |
| 34 |
#' # objects into their leaner summary.lm equivalents. |
| 35 |
#' s <- summary(mod) |
| 36 |
#' tidy(s, conf.int = TRUE) |
| 37 |
#' glance(s) |
| 38 |
#' |
| 39 |
#' augment(mod) |
| 40 |
#' augment(mod, mtcars, interval = "confidence") |
| 41 |
#' |
| 42 |
#' # predict on new data |
| 43 |
#' newdata <- mtcars |> |
| 44 |
#' head(6) |> |
| 45 |
#' mutate(wt = wt + 1) |
| 46 |
#' augment(mod, newdata = newdata) |
| 47 |
#' |
| 48 |
#' # ggplot2 example where we also construct 95% prediction interval |
| 49 |
#' |
| 50 |
#' # simpler bivariate model since we're plotting in 2D |
| 51 |
#' mod2 <- lm(mpg ~ wt, data = mtcars) |
| 52 |
#' |
| 53 |
#' au <- augment(mod2, newdata = newdata, interval = "prediction") |
| 54 |
#' |
| 55 |
#' ggplot(au, aes(wt, mpg)) + |
| 56 |
#' geom_point() + |
| 57 |
#' geom_line(aes(y = .fitted)) + |
| 58 |
#' geom_ribbon(aes(ymin = .lower, ymax = .upper), col = NA, alpha = 0.3) |
| 59 |
#' |
| 60 |
#' # predict on new data without outcome variable. Output does not include .resid |
| 61 |
#' newdata <- newdata |> |
| 62 |
#' select(-mpg) |
| 63 |
#' |
| 64 |
#' augment(mod, newdata = newdata) |
| 65 |
#' |
| 66 |
#' au <- augment(mod, data = mtcars) |
| 67 |
#' |
| 68 |
#' ggplot(au, aes(.hat, .std.resid)) + |
| 69 |
#' geom_vline(size = 2, colour = "white", xintercept = 0) + |
| 70 |
#' geom_hline(size = 2, colour = "white", yintercept = 0) + |
| 71 |
#' geom_point() + |
| 72 |
#' geom_smooth(se = FALSE) |
| 73 |
#' |
| 74 |
#' plot(mod, which = 6) |
| 75 |
#' |
| 76 |
#' ggplot(au, aes(.hat, .cooksd)) + |
| 77 |
#' geom_vline(xintercept = 0, colour = NA) + |
| 78 |
#' geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") + |
| 79 |
#' geom_smooth(se = FALSE) + |
| 80 |
#' geom_point() |
| 81 |
#' |
| 82 |
#' # column-wise models |
| 83 |
#' a <- matrix(rnorm(20), nrow = 10) |
| 84 |
#' b <- a + rnorm(length(a)) |
| 85 |
#' result <- lm(b ~ a) |
| 86 |
#' |
| 87 |
#' tidy(result) |
| 88 |
#' |
| 89 |
#' @aliases lm_tidiers |
| 90 |
#' @export |
| 91 |
#' @seealso [tidy()], [stats::summary.lm()] |
| 92 |
#' @family lm tidiers |
| 93 |
tidy.lm <- function( |
| 94 |
x, |
| 95 |
conf.int = FALSE, |
| 96 |
conf.level = 0.95, |
| 97 |
exponentiate = FALSE, |
| 98 |
... |
| 99 |
) { |
| 100 |
warn_on_subclass(x, "tidy") |
| 101 |
|
| 102 |
ret <- as_tibble(summary(x)$coefficients, rownames = "term") |
| 103 |
colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 104 |
|
| 105 |
coefs <- stats::coef(x) |
| 106 |
|
| 107 |
if (length(coefs) != nrow(ret)) { |
| 108 |
# summary(x)$coefficients misses rank deficient rows (i.e. coefs that |
| 109 |
# summary.lm() sets to NA), catch them here and add them back. This join is |
| 110 |
# costly, so only do it when necessary. |
| 111 |
coefs <- tibble::enframe(coefs, name = "term", value = "estimate") |
| 112 |
ret <- left_join(coefs, ret, by = c("term", "estimate")) |
| 113 |
} |
| 114 |
|
| 115 |
if (conf.int) { |
| 116 |
ci <- broom_confint_terms(x, level = conf.level) |
| 117 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 118 |
} |
| 119 |
|
| 120 |
if (exponentiate) { |
| 121 |
ret <- exponentiate(ret) |
| 122 |
} |
| 123 |
|
| 124 |
ret |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
#' @templateVar class lm |
| 129 |
#' @template title_desc_augment |
| 130 |
#' |
| 131 |
#' @template augment_NAs |
| 132 |
#' |
| 133 |
#' @inherit tidy.lm params examples |
| 134 |
#' @template param_data |
| 135 |
#' @template param_newdata |
| 136 |
#' @template param_se_fit |
| 137 |
#' @template param_interval |
| 138 |
#' @param conf.level The confidence level to use for the interval created if |
| 139 |
#' `interval` is `"confidence"` or `"prediction"`. Must be strictly greater |
| 140 |
#' than 0 and less than 1. Defaults to 0.95, which corresponds to a 95 |
| 141 |
#' percent confidence/prediction interval. |
| 142 |
#' @template param_unused_dots |
| 143 |
#' |
| 144 |
#' @evalRd return_augment( |
| 145 |
#' ".hat", |
| 146 |
#' ".lower", |
| 147 |
#' ".upper", |
| 148 |
#' ".sigma", |
| 149 |
#' ".cooksd", |
| 150 |
#' ".se.fit", |
| 151 |
#' ".std.resid" |
| 152 |
#' ) |
| 153 |
#' |
| 154 |
#' @details Some unusual `lm` objects, such as `rlm` from MASS, may omit |
| 155 |
#' `.cooksd` and `.std.resid`. `gam` from mgcv omits `.sigma`. |
| 156 |
#' |
| 157 |
#' When `newdata` is supplied, only returns `.fitted`, `.resid` and |
| 158 |
#' `.se.fit` columns. |
| 159 |
#' |
| 160 |
#' @export |
| 161 |
#' @seealso [augment()], [stats::predict.lm()] |
| 162 |
#' @family lm tidiers |
| 163 |
augment.lm <- function( |
| 164 |
x, |
| 165 |
data = model.frame(x), |
| 166 |
newdata = NULL, |
| 167 |
se_fit = FALSE, |
| 168 |
interval = c("none", "confidence", "prediction"), |
| 169 |
conf.level = 0.95, |
| 170 |
... |
| 171 |
) { |
| 172 |
warn_on_subclass(x, "augment") |
| 173 |
check_ellipses("level", "augment", "lm", ...) |
| 174 |
|
| 175 |
interval <- match.arg(interval) |
| 176 |
df <- augment_newdata(x, data, newdata, se_fit, interval, level = conf.level) |
| 177 |
|
| 178 |
if (is.null(newdata)) { |
| 179 |
tryCatch( |
| 180 |
{ |
| 181 |
infl <- influence(x, do.coef = FALSE) |
| 182 |
df <- add_hat_sigma_cols(df, x, infl) |
| 183 |
}, |
| 184 |
error = data_error |
| 185 |
) |
| 186 |
} |
| 187 |
|
| 188 |
df |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
#' @templateVar class lm |
| 193 |
#' @template title_desc_glance |
| 194 |
#' |
| 195 |
#' @inherit tidy.lm params examples |
| 196 |
#' |
| 197 |
#' @evalRd return_glance( |
| 198 |
#' "r.squared", |
| 199 |
#' "adj.r.squared", |
| 200 |
#' "sigma", |
| 201 |
#' "statistic", |
| 202 |
#' "p.value", |
| 203 |
#' df = "The degrees for freedom from the numerator of the overall |
| 204 |
#' F-statistic. This is new in broom 0.7.0. Previously, this reported |
| 205 |
#' the rank of the design matrix, which is one more than the numerator |
| 206 |
#' degrees of freedom of the overall F-statistic.", |
| 207 |
#' "logLik", |
| 208 |
#' "AIC", |
| 209 |
#' "BIC", |
| 210 |
#' "deviance", |
| 211 |
#' "df.residual", |
| 212 |
#' "nobs" |
| 213 |
#' ) |
| 214 |
#' |
| 215 |
#' |
| 216 |
#' @export |
| 217 |
#' @seealso [glance()], [glance.summary.lm()] |
| 218 |
#' @family lm tidiers |
| 219 |
glance.lm <- function(x, ...) { |
| 220 |
warn_on_subclass(x, "glance") |
| 221 |
|
| 222 |
# check whether the model was fitted with only an intercept, in which |
| 223 |
# case drop the fstatistic related columns |
| 224 |
int_only <- nrow(summary(x)$coefficients) == 1 & |
| 225 |
"(Intercept)" %in% row.names(summary(x)$coefficients) |
| 226 |
|
| 227 |
with( |
| 228 |
summary(x), |
| 229 |
tibble( |
| 230 |
r.squared = r.squared, |
| 231 |
adj.r.squared = adj.r.squared, |
| 232 |
sigma = sigma, |
| 233 |
statistic = if (!int_only) { |
| 234 |
fstatistic["value"] |
| 235 |
} else { |
| 236 |
NA_real_ |
| 237 |
}, |
| 238 |
p.value = if (!int_only) { |
| 239 |
stats::pf( |
| 240 |
fstatistic["value"], |
| 241 |
fstatistic["numdf"], |
| 242 |
fstatistic["dendf"], |
| 243 |
lower.tail = FALSE |
| 244 |
) |
| 245 |
} else { |
| 246 |
NA_real_ |
| 247 |
}, |
| 248 |
df = if (!int_only) { |
| 249 |
fstatistic["numdf"] |
| 250 |
} else { |
| 251 |
NA_real_ |
| 252 |
}, |
| 253 |
logLik = as.numeric(stats::logLik(x)), |
| 254 |
AIC = stats::AIC(x), |
| 255 |
BIC = stats::BIC(x), |
| 256 |
deviance = stats::deviance(x), |
| 257 |
df.residual = df.residual(x), |
| 258 |
nobs = stats::nobs(x) |
| 259 |
) |
| 260 |
) |
| 261 |
} |