Generates an augmented data frame from a linear model object, including fitted values, residuals, leverage, Cook's distance, prediction intervals, and outlier/influence flags. This function prepares model diagnostics for plotting.
Details
The returned data frame contains key statistics for each observation:
.id
: Observation identifier.sequence
: Sequence index.fits
: Fitted/predicted values.resid
: Residuals.obs
: Observed values.sigma
: Estimated standard deviation for each residual.std.resid
: Standardized residuals.stud.resid
: Studentized residuals.lower.pi
,.upper.pi
: Lower/upper 95% prediction interval bounds.cooksd
: Cook's distance.hat
: Leverage (diagonal of the hat matrix).cooksd.is.infl
,.hat.is.infl
: Logical flags for influential points.quantile
: Theoretical normal quantile of residualsoutlier
: Flag for ordinary residual outlier ("outl" or "reg").stud.outl
: Flag for studentized residual influential point ("infl" or "reg")
Examples
mdl <- lm(Sepal.Length ~ Sepal.Width, data = iris)
df <- lm_plot.df(mdl)
head(df)
#> .id .sequence .fits .resid .obs .sigma .std.resid .stud.resid
#> 1 1 1 5.744459 -0.6444588 5.1 0.8261667 -0.7864325 -0.7854139
#> 2 2 2 5.856139 -0.9561394 4.9 0.8241081 -1.1627712 -1.1641660
#> 3 3 3 5.811467 -1.1114672 4.7 0.8227693 -1.3520774 -1.3559020
#> 4 4 4 5.833803 -1.2338033 4.6 0.8215777 -1.5004021 -1.5068286
#> 5 5 5 5.722123 -0.7221227 5.0 0.8257158 -0.8827646 -0.8821026
#> 6 6 6 5.655114 -0.2551144 5.4 0.8276221 -0.3142222 -0.3132634
#> .lower.pi .upper.pi .cooksd .hat .cooksd.is.infl .hat.is.infl
#> 1 4.102926 7.385992 0.004260169 0.013589132 FALSE FALSE
#> 2 4.220127 7.492152 0.004616605 0.006782791 FALSE FALSE
#> 3 4.174965 7.447969 0.006801185 0.007385705 FALSE FALSE
#> 4 4.197833 7.469773 0.007627752 0.006730978 FALSE FALSE
#> 5 4.077773 7.366472 0.006766613 0.017070023 FALSE FALSE
#> 6 3.998939 7.311290 0.001618927 0.031751938 FALSE FALSE
#> .quantile outlier .stud.outl
#> 1 -0.6744898 reg reg
#> 2 -1.0954185 reg reg
#> 3 -1.5273795 reg reg
#> 4 -1.7907506 reg reg
#> 5 -0.7609839 reg reg
#> 6 -0.2104284 reg reg