|
| 1 | +#' Supplement the data fitted to a linear model with model fit statistics. |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' `r lifecycle::badge("deprecated")` |
| 5 | +#' |
| 6 | +#' This method is deprecated because using `broom::augment()` is a better |
| 7 | +#' solution to supplement data from a linear model. |
| 8 | +#' If you have missing values in your model data, you may need to refit |
| 9 | +#' the model with `na.action = na.exclude`. |
| 10 | +#' |
| 11 | +#' @return The original data with extra columns: |
| 12 | +#' \item{.hat}{Diagonal of the hat matrix} |
| 13 | +#' \item{.sigma}{Estimate of residual standard deviation when |
| 14 | +#' corresponding observation is dropped from model} |
| 15 | +#' \item{.cooksd}{Cooks distance, [cooks.distance()]} |
| 16 | +#' \item{.fitted}{Fitted values of model} |
| 17 | +#' \item{.resid}{Residuals} |
| 18 | +#' \item{.stdresid}{Standardised residuals} |
| 19 | +#' @param model linear model |
| 20 | +#' @param data data set, defaults to data used to fit model |
| 21 | +#' @param ... not used by this method |
| 22 | +#' @keywords internal |
| 23 | +#' @export |
| 24 | +#' @examplesIf require("broom") |
| 25 | +#' mod <- lm(mpg ~ wt, data = mtcars) |
| 26 | +#' |
| 27 | +#' # Show augmented model |
| 28 | +#' head(augment(mod)) |
| 29 | +#' head(fortify(mod)) |
| 30 | +#' |
| 31 | +#' # Using augment to convert model to ready-to-plot data |
| 32 | +#' ggplot(augment(mod), aes(.fitted, .resid)) + |
| 33 | +#' geom_point() + |
| 34 | +#' geom_hline(yintercept = 0) + |
| 35 | +#' geom_smooth(se = FALSE) |
| 36 | +#' |
| 37 | +#' # Colouring by original data not included in the model |
| 38 | +#' ggplot(augment(mod, mtcars), aes(.fitted, .std.resid, colour = factor(cyl))) + |
| 39 | +#' geom_point() |
| 40 | +fortify.lm <- function(model, data = model$model, ...) { |
| 41 | + lifecycle::deprecate_warn( |
| 42 | + "3.6.0", I("`fortify(<lm>)`"), I("`broom::augment(<lm>)`") |
| 43 | + ) |
| 44 | + infl <- stats::influence(model, do.coef = FALSE) |
| 45 | + data$.hat <- infl$hat |
| 46 | + data$.sigma <- infl$sigma |
| 47 | + data$.cooksd <- stats::cooks.distance(model, infl) |
| 48 | + |
| 49 | + data$.fitted <- stats::predict(model) |
| 50 | + data$.resid <- stats::resid(model) |
| 51 | + data$.stdresid <- stats::rstandard(model, infl) |
| 52 | + |
| 53 | + data |
| 54 | +} |
| 55 | + |
1 | 56 | #' Fortify methods for objects produced by \pkg{multcomp} |
2 | 57 | #' |
3 | 58 | #' @description |
|
0 commit comments