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.

## Arguments

- x
A model fit object, such as those returned by

`lm()`

or`glm()`

. See the broom documentation for the full list of model types supported.- ...
Additional arguments passed to

`broom::augment()`

.

## Value

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.

## Details

The name comes by analogy to `tidyr::pivot_longer()`

, and the concept of long
versus wide data formats.

## Limitations

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.

## Examples

```
fit <- lm(mpg ~ cyl + disp + hp, data = mtcars)
# each observation appears 3 times, once per predictor:
augment_longer(fit)
#> # A tibble: 96 × 11
#> .predictor_name .predictor_value .rownames mpg .fitted .resid .hat .sigma
#> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 cyl 6 Mazda RX4 21 22.2 -1.19 0.0837 3.10
#> 2 disp 160 Mazda RX4 21 22.2 -1.19 0.0837 3.10
#> 3 hp 110 Mazda RX4 21 22.2 -1.19 0.0837 3.10
#> 4 cyl 6 Mazda RX… 21 22.2 -1.19 0.0837 3.10
#> 5 disp 160 Mazda RX… 21 22.2 -1.19 0.0837 3.10
#> 6 hp 110 Mazda RX… 21 22.2 -1.19 0.0837 3.10
#> 7 cyl 4 Datsun 7… 22.8 25.9 -3.08 0.0868 3.05
#> 8 disp 108 Datsun 7… 22.8 25.9 -3.08 0.0868 3.05
#> 9 hp 93 Datsun 7… 22.8 25.9 -3.08 0.0868 3.05
#> 10 cyl 6 Hornet 4… 21.4 20.3 1.05 0.0775 3.10
#> # ℹ 86 more rows
#> # ℹ 3 more variables: .cooksd <dbl>, .std.resid <dbl>, .obs <chr>
```