Overview

When analysts and researchers present regression output, the conventional method is to create a regression table with name, coefficients estimates and associated standard error. However, table is often not the best way to visualize the data.

I am using broom package to first extract coefficient level data as tidy data format. And then use ggplot to visualize the estimates and uncertainty around them.

library(broom)
library(ggplot2)

Generate Fake Data

a <- 5
b <- 3
x1 <- rnorm(1000, 40, 25)
x2 <- rep(seq(1:100), 10)
n_over <- sum()
# x2[x2>70] <- rnorm()
e <- rnorm(1000, 5, 10)

y <-a*x1 - b*x2 + e
data <- data.frame(y,x1,x2)

Fit the Model and Extract coefficients

Once we fit the model, we can use broom::tidy to extract coefficients and the confidence intervals.

m1 <- lm(y ~ ., data)
coef <- tidy(m1, conf.int = TRUE)
coef
##          term  estimate  std.error   statistic      p.value  conf.low
## 1 (Intercept)  6.010247 0.77135189    7.791836 1.653438e-14  4.496588
## 2          x1  4.979117 0.01214598  409.939402 0.000000e+00  4.955282
## 3          x2 -3.011474 0.01029750 -292.447072 0.000000e+00 -3.031682
##   conf.high
## 1  7.523907
## 2  5.002951
## 3 -2.991267

Graph the Model Out

ggplot(coef, aes(term, estimate))+
  geom_point()+
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high))+
  labs(title = "Coefficients of a linear regression model")