Learn R Programming

pubrplot (version 0.0.1)

plot_lm: Plot Linear Regression Estimates with Confidence Intervals

Description

This function fits univariate and multivariate linear regression models for a given outcome and a set of predictors. It returns a ggplot showing point estimates and 95% confidence intervals for each predictor. Reference levels of factors can optionally be added, and univariate and multivariate results are plotted side by side.

Usage

plot_lm(
  data,
  outcome,
  predictors,
  label_vjust = -0.8,
  label_hjust = 0.4,
  label_size = 3.5,
  label_color = "black",
  point_color = c("steelblue", "firebrick"),
  point_shape = 15,
  ref = TRUE
)

Value

A ggplot object showing regression estimates with 95% confidence intervals for both univariate and multivariate models. @import broom

Arguments

data

A data frame containing the outcome and predictor variables.

outcome

A string specifying the outcome (dependent) variable.

predictors

A character vector of predictor (independent) variables.

label_vjust

Vertical adjustment for text labels. Default is -0.8.

label_hjust

Horizontal adjustment for text labels. Default is 0.4.

label_size

Size of text labels. Default is 3.5.

label_color

Color of text labels. Default is "black".

point_color

Vector of colors for the points. Default is c("steelblue", "firebrick").

point_shape

Shape of the points. Default is 15.

ref

Logical; if TRUE, adds reference levels for factor variables. Default is TRUE.

Examples

Run this code
mtcars2 <- dplyr::mutate(
  mtcars,
  cyl = factor(cyl),
  am = factor(am, labels = c("Automatic", "Manual")),
  gear = factor(gear)
)

plot_lm(
  data = mtcars2,
  outcome = "mpg",
  predictors = c("cyl", "hp", "wt", "am", "gear"),
  point_shape = 18
)

plot_lm(
  data = mtcars2,
  outcome = "mpg",
  predictors = c("cyl", "hp", "wt", "am", "gear"),
  point_shape = 18
)

Run the code above in your browser using DataLab