augment_longer | R Documentation |
Use broom::augment()
to augment a model fit with residual and fit
information, then reformat the resulting data frame into a "long" format with
one row per predictor per observation, to facilitate plotting of the result.
augment_longer(x, ...)
x |
A model fit object, such as those returned by |
... |
Additional arguments passed to |
The name comes by analogy to tidyr::pivot_longer()
, and the concept of long
versus wide data formats.
A data frame (tibble) in similar form to those produced by
broom::augment()
, but expanded to have one row per predictor per
observation. Columns .predictor_name
and .predictor_value
identify the
predictor and its value. An additional column .obs
records the original
observation numbers so results can be matched to observations in the
original model data.
Factor predictors (as factors, logical, or character vectors) can't coexist
with numeric variables in the .predictor_value
column. If there are some
numeric and some factor predictors, the factor predictors will automatically
be omitted. If all predictors are factors, they will be combined into one
factor with all levels. However, if a numeric variable is converted to factor
in the model formula, such as with y ~ factor(x)
, the function cannot
determine the appropriate types and will raise an error. Create factors as
needed in the source data frame before fitting the model to avoid this
issue.
partial_residuals()
, binned_residuals()
fit <- lm(mpg ~ cyl + disp + hp, data = mtcars)
# each observation appears 3 times, once per predictor:
augment_longer(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.