# NOT RUN {
library(ggplot2)
library(dplyr)
mod <- lm(mpg ~ wt + qsec, data = mtcars)
tidy(mod)
glance(mod)
# coefficient plot
d <- tidy(mod) %>% mutate(low = estimate - std.error,
high = estimate + std.error)
ggplot(d, aes(estimate, term, xmin = low, xmax = high, height = 0)) +
geom_point() +
geom_vline(xintercept = 0) +
geom_errorbarh()
head(augment(mod))
head(augment(mod, mtcars))
# predict on new data
newdata <- mtcars %>% head(6) %>% mutate(wt = wt + 1)
augment(mod, newdata = newdata)
au <- augment(mod, data = mtcars)
plot(mod, which = 1)
qplot(.fitted, .resid, data = au) +
geom_hline(yintercept = 0) +
geom_smooth(se = FALSE)
qplot(.fitted, .std.resid, data = au) +
geom_hline(yintercept = 0) +
geom_smooth(se = FALSE)
qplot(.fitted, .std.resid, data = au,
colour = factor(cyl))
qplot(mpg, .std.resid, data = au, colour = factor(cyl))
plot(mod, which = 2)
qplot(sample =.std.resid, data = au, stat = "qq") +
geom_abline()
plot(mod, which = 3)
qplot(.fitted, sqrt(abs(.std.resid)), data = au) + geom_smooth(se = FALSE)
plot(mod, which = 4)
qplot(seq_along(.cooksd), .cooksd, data = au)
plot(mod, which = 5)
qplot(.hat, .std.resid, data = au) + geom_smooth(se = FALSE)
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)
qplot(.hat, .std.resid, data = au, size = .cooksd) +
geom_smooth(se = FALSE, size = 0.5)
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()
qplot(.hat, .cooksd, size = .cooksd / .hat, data = au) + scale_size_area()
# column-wise models
a <- matrix(rnorm(20), nrow = 10)
b <- a + rnorm(length(a))
result <- lm(b ~ a)
tidy(result)
# }
Run the code above in your browser using DataLab