View source: R/statslmtidiers.R
augment.lm  R Documentation 
Augment accepts a model object and a dataset and adds
information about each observation in the dataset. Most commonly, this
includes predicted values in the .fitted
column, residuals in the
.resid
column, and standard errors for the fitted values in a .se.fit
column. New columns always begin with a .
prefix to avoid overwriting
columns in the original dataset.
Users may pass data to augment via either the data
argument or the
newdata
argument. If the user passes data to the data
argument,
it must be exactly the data that was used to fit the model
object. Pass datasets to newdata
to augment data that was not used
during model fitting. This still requires that at least all predictor
variable columns used to fit the model are present. If the original outcome
variable used to fit the model is not included in newdata
, then no
.resid
column will be included in the output.
Augment will often behave differently depending on whether data
or
newdata
is given. This is because there is often information
associated with training observations (such as influences or related)
measures that is not meaningfully defined for new observations.
For convenience, many augment methods provide default data
arguments,
so that augment(fit)
will return the augmented training data. In these
cases, augment tries to reconstruct the original data based on the model
object with varying degrees of success.
The augmented dataset is always returned as a tibble::tibble with the
same number of rows as the passed dataset. This means that the
passed data must be coercible to a tibble. At this time, tibbles do not
support matrixcolumns. This means you should not specify a matrix
of covariates in a model formula during the original model fitting
process, and that splines::ns()
, stats::poly()
and
survival::Surv()
objects are not supported in input data. If you
encounter errors, try explicitly passing a tibble, or fitting the original
model on data in a tibble.
We are in the process of defining behaviors for models fit with various
na.action
arguments, but make no guarantees about behavior when data is
missing at this time.
## S3 method for class 'lm' augment( x, data = model.frame(x), newdata = NULL, se_fit = FALSE, interval = c("none", "confidence", "prediction"), ... )
x 
An 
data 
A base::data.frame or 
newdata 
A 
se_fit 
Logical indicating whether or not a 
interval 
Character indicating the type of confidence interval columns
to be added to the augmented output. Passed on to 
... 
Additional arguments. Not used. Needed to match generic
signature only. Cautionary note: Misspelled arguments will be
absorbed in

When the modeling was performed with na.action = "na.omit"
(as is the typical default), rows with NA in the initial data are omitted
entirely from the augmented data frame. When the modeling was performed
with na.action = "na.exclude"
, one should provide the original data
as a second argument, at which point the augmented data will contain those
rows (typically with NAs in place of the new columns). If the original data
is not provided to augment()
and na.action = "na.exclude"
, a
warning is raised and the incomplete rows are dropped.
Some unusual lm
objects, such as rlm
from MASS, may omit
.cooksd
and .std.resid
. gam
from mgcv omits .sigma
.
When newdata
is supplied, only returns .fitted
, .resid
and
.se.fit
columns.
A tibble::tibble()
with columns:
.cooksd 
Cooks distance. 
.fitted 
Fitted or predicted value. 
.hat 
Diagonal of the hat matrix. 
.lower 
Lower bound on interval for fitted values. 
.resid 
The difference between observed and fitted values. 
.se.fit 
Standard errors of fitted values. 
.sigma 
Estimated residual standard deviation when corresponding observation is dropped from model. 
.std.resid 
Standardised residuals. 
.upper 
Upper bound on interval for fitted values. 
stats::na.action
augment()
, stats::predict.lm()
Other lm tidiers:
augment.glm()
,
glance.glm()
,
glance.lm()
,
glance.summary.lm()
,
glance.svyglm()
,
tidy.glm()
,
tidy.lm.beta()
,
tidy.lm()
,
tidy.mlm()
,
tidy.summary.lm()
library(ggplot2) library(dplyr) mod < lm(mpg ~ wt + qsec, data = mtcars) tidy(mod) glance(mod) # coefficient plot d < tidy(mod, conf.int = TRUE) ggplot(d, aes(estimate, term, xmin = conf.low, xmax = conf.high, height = 0)) + geom_point() + geom_vline(xintercept = 0, lty = 4) + geom_errorbarh() # aside: There are tidy() and glance() methods for lm.summary objects too. # this can be useful when you want to conserve memory by converting large lm # objects into their leaner summary.lm equivalents. s < summary(mod) tidy(s, conf.int = TRUE) glance(s) augment(mod) augment(mod, mtcars, interval = "confidence") # predict on new data newdata < mtcars %>% head(6) %>% mutate(wt = wt + 1) augment(mod, newdata = newdata) # ggplot2 example where we also construct 95% prediction interval # simpler bivariate model since we're plotting in 2D mod2 < lm(mpg ~ wt, data = mtcars) au < augment(mod2, newdata = newdata, interval = "prediction") ggplot(au, aes(wt, mpg)) + geom_point() + geom_line(aes(y = .fitted)) + geom_ribbon(aes(ymin = .lower, ymax = .upper), col = NA, alpha = 0.3) # predict on new data without outcome variable. Output does not include .resid newdata < newdata %>% select(mpg) augment(mod, newdata = newdata) au < augment(mod, data = mtcars) ggplot(au, aes(.hat, .std.resid)) + geom_vline(size = 2, colour = "white", xintercept = 0) + geom_hline(size = 2, colour = "white", yintercept = 0) + geom_point() + geom_smooth(se = FALSE) plot(mod, which = 6) ggplot(au, aes(.hat, .cooksd)) + geom_vline(xintercept = 0, colour = NA) + geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") + geom_smooth(se = FALSE) + geom_point() # columnwise models a < matrix(rnorm(20), nrow = 10) b < a + rnorm(length(a)) result < lm(b ~ a) tidy(result)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.