fortify.lm {ggplot2} | R Documentation |
Supplement the data fitted to a linear model with model fit statistics.
fortify.lm(model, data = model$model, ...)
model |
|
data |
|
... |
The following statistics will be added to the data frame:
cooks.distance
If you have missing values in your model data, you may need to refit
the model with na.action = na.preserve
.
Hadley Wickham <h.wickham@gmail.com>
mod <- lm(mpg ~ wt, data = mtcars) fortify(mod) fortify(mod, mtcars) plot(mod, which = 1) qplot(.fitted, .resid, data = mod) + geom_hline() + geom_smooth(se = FALSE) qplot(.fitted, .stdresid, data = mod) + geom_hline() + geom_smooth(se = FALSE) qplot(.fitted, .stdresid, data = fortify(mod, mtcars), colour = factor(cyl)) qplot(mpg, .stdresid, data = fortify(mod, mtcars), colour = factor(cyl)) plot(mod, which = 2) # qplot(sample =.stdresid, data = mod, stat = "qq") + geom_abline() plot(mod, which = 3) qplot(.fitted, sqrt(abs(.stdresid)), data = mod) + geom_smooth(se = FALSE) plot(mod, which = 4) qplot(seq_along(.cooksd), .cooksd, data = mod, geom = "bar", stat="identity") plot(mod, which = 5) qplot(.hat, .stdresid, data = mod) + geom_smooth(se = FALSE) ggplot(mod, aes(.hat, .stdresid)) + geom_vline(size = 2, colour = "white") + geom_hline(size = 2, colour = "white") + geom_point() + geom_smooth(se = FALSE) qplot(.hat, .stdresid, data = mod, size = .cooksd) + geom_smooth(se = FALSE, size = 0.5) plot(mod, which = 6) ggplot(mod, aes(.hat, .cooksd, data = mod)) + geom_vline(colour = NA) + geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") + geom_smooth(se = FALSE) + geom_point() qplot(.hat, .cooksd, size = .cooksd / .hat, data = mod) + scale_area()