Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Forked from johnjosephhorton/simualte_bartik.R
Created January 30, 2018 21:00
Show Gist options
  • Save apoorvalal/8ab63e48dbbafa6d28a6ac97e01bde99 to your computer and use it in GitHub Desktop.
Save apoorvalal/8ab63e48dbbafa6d28a6ac97e01bde99 to your computer and use it in GitHub Desktop.
library(lfe)
library(stargazer)
set.seed(6152011)
# Number of cities
L <- 1000
# Number of industries
K <- 3
# National growth rates by industry
g.raw <- runif(K)
g.k <- g.raw/sum(g.raw)
CreateCity <- function(g.k, eta.s){
# Give each city some industry shares
K <- length(g.k)
z.raw <- runif(K)
z <- z.raw / sum(z.raw)
# Give each city some idiosyncratic growth by industry
g.kl.tilde <- g.raw/sum(g.raw)
# Change in employement is inner product of the national + idiosyncratic component with industry share
x <- (g.kl.tilde + g.k) %*% z
# Bartik instrument (national growth & city industry share inner product)
B <- g.k %*% z
# Realized change in wage
y <- (1/eta.s) * x + rnorm(1,0,1/10)
list("y" = y, "B" = B, "x" = x)
}
# Create a city data frame
city.data <- rep(CreateCity(g.k, 1), L)
df <- data.frame(matrix(unlist(city.data), ncol = 3))
colnames(df) <- c("y", "B", "x")
# Estimates
m.ols <- felm(y ~ x | 0 | 0 | 0, data = df)
m.iv <- felm(y ~ 1 | 0 | (x ~ B) | 0, data = df)
stargazer(m, m.iv, type = "text")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment