Skip to content

Instantly share code, notes, and snippets.

@dhicks
Created December 24, 2021 16:39
Show Gist options
  • Save dhicks/f4fca422774b19d1fc912810473c5a12 to your computer and use it in GitHub Desktop.
Save dhicks/f4fca422774b19d1fc912810473c5a12 to your computer and use it in GitHub Desktop.
Omitted interaction terms don't cause omitted variable bias maybe?
library(tidyverse)
theme_set(theme_minimal())
library(broom)
one_sim = function(id, n = 100) {
dataf = tibble(x1 = rnorm(n, 0, 1),
x2 = rnorm(n, 0, 1),
eps = rnorm(n, 0, .1),
y = 1 + x1 + 3*x1*x2 + eps)
coef_df = lm(y ~ x1 + x2, dataf) |>
tidy(conf.int = TRUE) |>
mutate(id)
return(coef_df)
}
set.seed(2021-12-24)
df = map_dfr(1:1000, one_sim)
df |>
ggplot(aes(estimate)) +
geom_density() +
geom_rug() +
geom_vline(aes(xintercept = value),
data = tribble(
~ term, ~ value,
'(Intercept)', 1,
'x1', 1,
'x2', 0),
color = 'blue') +
facet_wrap(vars(term), scales = 'free')
df |>
ggplot(aes(id, y = estimate, ymin = conf.low, ymax = conf.high)) +
geom_pointrange() +
facet_wrap(vars(term), scales = 'free') +
coord_flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment