# Package 'broom' needs to be installed to run these examples.
# We check availability before running them to avoid errors.
if (requireNamespace("broom", quietly = TRUE)) {
broom.installed <- TRUE
library(broom)
library(quantreg)
# Inspecting the returned data using geom_debug()
if (requireNamespace("gginnards", quietly = TRUE)) {
library(gginnards)
# Regression by panel, inspecting data
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(colour = factor(cyl))) +
stat_fit_augment(method = "lm",
method.args = list(formula = y ~ x),
geom = "debug",
summary.fun = colnames)
}
}
# Regression by panel example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(colour = factor(cyl))) +
stat_fit_augment(method = "lm",
method.args = list(formula = y ~ x))
# Residuals from regression by panel example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_hline(yintercept = 0, linetype = "dotted") +
stat_fit_augment(geom = "point",
method = "lm",
method.args = list(formula = y ~ x),
y.out = ".resid")
# Regression by group example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) +
geom_point() +
stat_fit_augment(method = "lm",
method.args = list(formula = y ~ x))
# Residuals from regression by group example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) +
geom_hline(yintercept = 0, linetype = "dotted") +
stat_fit_augment(geom = "point",
method.args = list(formula = y ~ x),
y.out = ".resid")
# Weighted regression example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg, weight = cyl)) +
geom_point(aes(colour = factor(cyl))) +
stat_fit_augment(method = "lm",
method.args = list(formula = y ~ x,
weights = quote(weight)))
# Residuals from weighted regression example
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg, weight = cyl)) +
geom_hline(yintercept = 0, linetype = "dotted") +
stat_fit_augment(geom = "point",
method.args = list(formula = y ~ x,
weights = quote(weight)),
y.out = ".resid")
# Quantile regression
if (broom.installed)
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
stat_fit_augment(method = "rq")
Run the code above in your browser using DataLab