Debian Codegraph @

man/augment.lm.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/stats-lm.R
3 \name{augment.lm}
4 \alias{augment.lm}
5 \title{Augment data with information from a(n) lm object}
6 \usage{
7 \method{augment}{lm}(
8 x,
9 data = model.frame(x),
10 newdata = NULL,
11 se_fit = FALSE,
12 interval = c("none", "confidence", "prediction"),
13 conf.level = 0.95,
14 ...
15 )
16 }
17 \arguments{
18 \item{x}{An \code{lm} object created by \code{\link[stats:lm]{stats::lm()}}.}
19
20 \item{data}{A \link[base:data.frame]{base::data.frame} or \code{\link[tibble:tibble]{tibble::tibble()}} containing the original
21 data that was used to produce the object \code{x}. Defaults to
22 \code{stats::model.frame(x)} so that \code{augment(my_fit)} returns the augmented
23 original data. \strong{Do not} pass new data to the \code{data} argument.
24 Augment will report information such as influence and cooks distance for
25 data passed to the \code{data} argument. These measures are only defined for
26 the original training data.}
27
28 \item{newdata}{A \code{\link[base:data.frame]{base::data.frame()}} or \code{\link[tibble:tibble]{tibble::tibble()}} containing all
29 the original predictors used to create \code{x}. Defaults to \code{NULL}, indicating
30 that nothing has been passed to \code{newdata}. If \code{newdata} is specified,
31 the \code{data} argument will be ignored.}
32
33 \item{se_fit}{Logical indicating whether or not a \code{.se.fit} column should be
34 added to the augmented output. For some models, this calculation can be
35 somewhat time-consuming. Defaults to \code{FALSE}.}
36
37 \item{interval}{Character indicating the type of confidence interval columns
38 to be added to the augmented output. Passed on to \code{predict()} and defaults
39 to "none".}
40
41 \item{conf.level}{The confidence level to use for the interval created if
42 \code{interval} is \code{"confidence"} or \code{"prediction"}. Must be strictly greater
43 than 0 and less than 1. Defaults to 0.95, which corresponds to a 95
44 percent confidence/prediction interval.}
45
46 \item{...}{Additional arguments. Not used. Needed to match generic
47 signature only. \strong{Cautionary note:} Misspelled arguments will be
48 absorbed in \code{...}, where they will be ignored. If the misspelled
49 argument has a default value, the default value will be used.
50 For example, if you pass \code{conf.lvel = 0.9}, all computation will
51 proceed using \code{conf.level = 0.95}. Two exceptions here are:
52 \itemize{
53 \item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if
54 it will be ignored.
55 \item \code{augment()} methods will warn when supplied a \code{newdata} argument if it
56 will be ignored.
57 }}
58 }
59 \description{
60 Augment accepts a model object and a dataset and adds
61 information about each observation in the dataset. Most commonly, this
62 includes predicted values in the \code{.fitted} column, residuals in the
63 \code{.resid} column, and standard errors for the fitted values in a \code{.se.fit}
64 column. New columns always begin with a \code{.} prefix to avoid overwriting
65 columns in the original dataset.
66
67 Users may pass data to augment via either the \code{data} argument or the
68 \code{newdata} argument. If the user passes data to the \code{data} argument,
69 it \strong{must} be exactly the data that was used to fit the model
70 object. Pass datasets to \code{newdata} to augment data that was not used
71 during model fitting. This still requires that at least all predictor
72 variable columns used to fit the model are present. If the original outcome
73 variable used to fit the model is not included in \code{newdata}, then no
74 \code{.resid} column will be included in the output.
75
76 Augment will often behave differently depending on whether \code{data} or
77 \code{newdata} is given. This is because there is often information
78 associated with training observations (such as influences or related)
79 measures that is not meaningfully defined for new observations.
80
81 For convenience, many augment methods provide default \code{data} arguments,
82 so that \code{augment(fit)} will return the augmented training data. In these
83 cases, augment tries to reconstruct the original data based on the model
84 object with varying degrees of success.
85
86 The augmented dataset is always returned as a \link[tibble:tibble]{tibble::tibble} with the
87 \strong{same number of rows} as the passed dataset. This means that the passed
88 data must be coercible to a tibble. If a predictor enters the model as part
89 of a matrix of covariates, such as when the model formula uses
90 \code{\link[splines:ns]{splines::ns()}}, \code{\link[stats:poly]{stats::poly()}}, or \code{\link[survival:Surv]{survival::Surv()}}, it is represented
91 as a matrix column.
92
93 We are in the process of defining behaviors for models fit with various
94 \code{na.action} arguments, but make no guarantees about behavior when data is
95 missing at this time.
96 }
97 \details{
98 When the modeling was performed with \code{na.action = "na.omit"}
99 (as is the typical default), rows with NA in the initial data are omitted
100 entirely from the augmented data frame. When the modeling was performed
101 with \code{na.action = "na.exclude"}, one should provide the original data
102 as a second argument, at which point the augmented data will contain those
103 rows (typically with NAs in place of the new columns). If the original data
104 is not provided to \code{\link[=augment]{augment()}} and \code{na.action = "na.exclude"}, a
105 warning is raised and the incomplete rows are dropped.
106
107 Some unusual \code{lm} objects, such as \code{rlm} from MASS, may omit
108 \code{.cooksd} and \code{.std.resid}. \code{gam} from mgcv omits \code{.sigma}.
109
110 When \code{newdata} is supplied, only returns \code{.fitted}, \code{.resid} and
111 \code{.se.fit} columns.
112 }
113 \examples{
114 \dontshow{if (rlang::is_installed("ggplot2")) withAutoprint(\{ # examplesIf}
115
116 library(ggplot2)
117 library(dplyr)
118
119 mod <- lm(mpg ~ wt + qsec, data = mtcars)
120
121 tidy(mod)
122 glance(mod)
123
124 # coefficient plot
125 d <- tidy(mod, conf.int = TRUE)
126
127 ggplot(d, aes(estimate, term, xmin = conf.low, xmax = conf.high, height = 0)) +
128 geom_point() +
129 geom_vline(xintercept = 0, lty = 4) +
130 geom_errorbarh()
131
132 # aside: There are tidy() and glance() methods for lm.summary objects too.
133 # this can be useful when you want to conserve memory by converting large lm
134 # objects into their leaner summary.lm equivalents.
135 s <- summary(mod)
136 tidy(s, conf.int = TRUE)
137 glance(s)
138
139 augment(mod)
140 augment(mod, mtcars, interval = "confidence")
141
142 # predict on new data
143 newdata <- mtcars |>
144 head(6) |>
145 mutate(wt = wt + 1)
146 augment(mod, newdata = newdata)
147
148 # ggplot2 example where we also construct 95\% prediction interval
149
150 # simpler bivariate model since we're plotting in 2D
151 mod2 <- lm(mpg ~ wt, data = mtcars)
152
153 au <- augment(mod2, newdata = newdata, interval = "prediction")
154
155 ggplot(au, aes(wt, mpg)) +
156 geom_point() +
157 geom_line(aes(y = .fitted)) +
158 geom_ribbon(aes(ymin = .lower, ymax = .upper), col = NA, alpha = 0.3)
159
160 # predict on new data without outcome variable. Output does not include .resid
161 newdata <- newdata |>
162 select(-mpg)
163
164 augment(mod, newdata = newdata)
165
166 au <- augment(mod, data = mtcars)
167
168 ggplot(au, aes(.hat, .std.resid)) +
169 geom_vline(size = 2, colour = "white", xintercept = 0) +
170 geom_hline(size = 2, colour = "white", yintercept = 0) +
171 geom_point() +
172 geom_smooth(se = FALSE)
173
174 plot(mod, which = 6)
175
176 ggplot(au, aes(.hat, .cooksd)) +
177 geom_vline(xintercept = 0, colour = NA) +
178 geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") +
179 geom_smooth(se = FALSE) +
180 geom_point()
181
182 # column-wise models
183 a <- matrix(rnorm(20), nrow = 10)
184 b <- a + rnorm(length(a))
185 result <- lm(b ~ a)
186
187 tidy(result)
188 \dontshow{\}) # examplesIf}
189 }
190 \seealso{
191 \link[stats:na.action]{stats::na.action}
192
193 \code{\link[=augment]{augment()}}, \code{\link[stats:predict.lm]{stats::predict.lm()}}
194
195 Other lm tidiers:
196 \code{\link{augment.glm}()},
197 \code{\link{glance.glm}()},
198 \code{\link{glance.lm}()},
199 \code{\link{glance.summary.lm}()},
200 \code{\link{glance.svyglm}()},
201 \code{\link{tidy.glm}()},
202 \code{\link{tidy.lm}()},
203 \code{\link{tidy.lm.beta}()},
204 \code{\link{tidy.mlm}()},
205 \code{\link{tidy.summary.lm}()}
206 }
207 \concept{lm tidiers}
208 \value{
209 A \code{\link[tibble:tibble]{tibble::tibble()}} with columns:
210 \item{.cooksd}{Cooks distance.}
211 \item{.fitted}{Fitted or predicted value.}
212 \item{.hat}{Diagonal of the hat matrix.}
213 \item{.lower}{Lower bound on interval for fitted values.}
214 \item{.resid}{The difference between observed and fitted values.}
215 \item{.se.fit}{Standard errors of fitted values.}
216 \item{.sigma}{Estimated residual standard deviation when corresponding observation is dropped from model.}
217 \item{.std.resid}{Standardised residuals.}
218 \item{.upper}{Upper bound on interval for fitted values.}
219
220 }