Last chance! 50% off unlimited learning
Sale ends in
## S3 method for class 'lm':
tidy(x, conf.int = FALSE, conf.level = 0.95,
exponentiate = FALSE, ...)## S3 method for class 'lm':
augment(x, data = x$model, newdata, type.predict, type.residuals,
...)
## S3 method for class 'lm':
glance(x, ...)
conf.int=TRUE
predict.glm
residuals.glm
data.frame
without rownames.
The structure depends on the method chosen.tidy.lm
returns one row for each coefficient, with five columns:
cont.int=TRUE
, it also includes columns for conf.low
and
conf.high
, computed with confint
.When newdata
is not supplied augment.lm
returns
one row for each observation, with seven columns added to the original
data:
cooks.distance
newdata
is supplied, augment.lm
returns one row for each
observation, with three columns added to the new data:glance.lm
returns a one-row data.frame with the columnsna.action = na.exclude
.If conf.int=TRUE
, the confidence interval is computed with
the confint
function.
When the modeling was performed with na.action = "na.omit"
(as is the typical default), rows with NA in the initial data are omitted
entirely from the augmented data frame. When the modeling was performed
with na.action = "na.exclude"
, one should provide the original data
as a second argument, at which point the augmented data will contain those
rows (typically with NAs in place of the new columns). If the original data
is not provided to augment
and na.action = "na.exclude"
, a
warning is raised and the incomplete rows are dropped.
summary.lm
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() + 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, geom = "bar",
stat="identity")
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()
Run the code above in your browser using DataLab