1 Required libraries

library(ggplot2)
library(gt)
library(afcommon)

2 Data Preparation

We’ll use the mtcars dataset to demonstrate the coefficient visualization functions.

# Prepare data
data(mtcars)
mtcars$am <- as.factor(mtcars$am)

# Create multiple models predicting mpg
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- lm(mpg ~ wt + am, data = mtcars)
m3 <- lm(mpg ~ wt + am + hp, data = mtcars)

# Create a named list of models
models <- list(
  "Model A" = m1,
  "Model B" = m2,
  "Model C" = m3
)

3 Generate Coefficient Table

Let’s examine the effect of weight (wt) across our models:

# Create coefficient table for 'wt'
coef_table <- af_coef_and_ci_table(models, "wt")
gt(coef_table)
model_name cov_name coef conf1 conf2
Model A wt -5.34447157272268 -6.48630823741826 -4.20263490802709
Model B wt -5.3528114467999 -6.96495096730801 -3.7406719262918
Model C wt -2.87857541380719 -4.73232352702331 -1.02482730059107

4 Create Coefficient Plot

Now let’s visualize these coefficients and their confidence intervals:

# Create coefficient plot
plot <- af_coef_and_ci_plot(coef_table)
print(plot)

5 Interpretation

The plot shows how the effect of weight (wt) on mpg changes across different model specifications:

  1. Model 1: Simple linear regression with just weight
  2. Model 2: Adds transmission type (am)
  3. Model 3: Adds horsepower (hp)

The black dots represent the coefficient estimates, and the horizontal lines show the 95% confidence intervals. The vertical dashed line at zero helps identify significant effects (intervals that don’t cross zero).