Learn R Programming

jtools (version 0.9.0)

plot_summs: Plot Regression Summaries

Description

plot_summs and plot_coefs create regression coefficient plots with ggplot2.

Usage

plot_summs(..., ci_level = 0.95, model.names = NULL, coefs = NULL,
  omit.coefs = "(Intercept)", color.class = "Set2")

plot_coefs(..., ci_level = 0.95, model.names = NULL, coefs = NULL, omit.coefs = "(Intercept)", color.class = "Set2")

Arguments

...

regression model(s).

ci_level

The desired width of confidence intervals for the coefficients. Default: 0.95

model.names

If plotting multiple models simultaneously, you can provide a vector of names here. If NULL, they will be named sequentially as "Model 1", "Model 2", and so on. Default: NULL

coefs

If you'd like to include only certain coefficients, provide them as a vector. If it is a named vector, then the names will be used in place of the variable names. See details for examples. Default: NULL

omit.coefs

If you'd like to specify some coefficients to not include in the plot, provide them as a vector. This argument is overridden by coefs if both are provided. By default, the intercept term is omitted. To include the intercept term, just set omit.coefs to NULL.

color.class

A color class understood by ggplot2::scale_colour_brewer() for differentiating multiple models. Not used if only one model is plotted. Default: 'Set2'

Value

A ggplot object.

Details

A note on the distinction between plot_summs and plot_coefs: plot_summs only accepts models supported by summ() and allows users to take advantage of the standardization and robust standard error features (among others as may be relevant). plot_coefs supports any models that have a broom::tidy() method defined in the broom package, but of course lacks any additional features like robust standard errors. To get a mix of the two, you can pass summ objects to plot_coefs too.

For coefs, if you provide a named vector of coefficients, then the plot will refer to the selected coefficients by the names of the vector rather than the coefficient names. For instance, if I want to include only the coefficients for the hp and mpg but have the plot refer to them as "Horsepower" and "Miles/gallon", I'd provide the argument like this: c("Horsepower" = "hp", "Miles/gallon" = "mpg")

Examples

Run this code
# NOT RUN {
states <- as.data.frame(state.x77)
fit1 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))
fit2 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))
fit3 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))

# Plot all 3 regressions with custom predictor labels,
# standardized coefficients, and robust standard errors
plot_summs(fit1, fit2, fit3,
           coefs = c("Frost Days" = "Frost", "% Illiterate" = "Illiteracy",
                     "Murder Rate" = "Murder"),
           scale = TRUE, robust = TRUE)

# }

Run the code above in your browser using DataLab