Skip to content

Instantly share code, notes, and snippets.

@vincentarelbundock
Created April 11, 2020 17:14
Show Gist options
  • Save vincentarelbundock/cc5b9198a49a0f862fd23efd26ae43ec to your computer and use it in GitHub Desktop.
Save vincentarelbundock/cc5b9198a49a0f862fd23efd26ae43ec to your computer and use it in GitHub Desktop.
library(lme4)
library(gt)
library(modelsummary)
library(tidyverse)
# y dependent variable
# x regressor varies at the county-year level
# z regressor varies at the county level
url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/plm/Crime.csv'
dat <- read_csv(url) %>%
select(county, year,
y = crmrte,
x = density,
z = pctmin) %>%
group_by(county) %>%
mutate(xbar = mean(x)) %>%
ungroup
mod <- list()
mod[['Pooling']] <- lm(y ~ x + z, data = dat)
mod[['FE 1']] <- lm(y ~ x + factor(county), data = dat)
mod[['FE 2']] <- lm(y ~ I(x - xbar) - 1, data = dat)
mod[['MLM']] <- lmer(y ~ x + z + (1 | county), data = dat)
mod[['Mundlak']] <- lmer(y ~ I(x - xbar) + xbar + z + (1 | county), data = dat)
tab <- msummary(mod, fmt = '%.10f', coef_omit = 'county') %>%
tab_style(cell_fill('pink'), cells_body('Mundlak', 7)) %>%
tab_style(cell_fill('lightcyan'), cells_body('Mundlak', c(5, 11))) %>%
tab_style(cell_fill('pink'), cells_body('FE 1', 3)) %>%
tab_style(cell_fill('pink'), cells_body('FE 2', 7))
tab
tab %>% gtsave('mundlak.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment