Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Last active October 9, 2019 01:12
Show Gist options
  • Save BioSciEconomist/e2cc5f7fb788520a2ce5ccec929cadc4 to your computer and use it in GitHub Desktop.
Save BioSciEconomist/e2cc5f7fb788520a2ce5ccec929cadc4 to your computer and use it in GitHub Desktop.
Example of difference in differences in a regression context with clustered SEs
# *-----------------------------------------------------------------
# | PROGRAM NAME: DiD demo.R
# | DATE: 2/5/19
# | CREATED BY: MATT BOGARD
# | PROJECT FILE:
# *----------------------------------------------------------------
# | PURPOSE: demo of difference-in-difference estimation
# *----------------------------------------------------------------
options("scipen" =100, "digits" = 4) # override R's tendency to use scientific notation
setwd("//econometrics//projects")
df <- read.csv("DID toy dat.csv", na.strings=c(".", "NA", "", "?"), encoding="UTF-8")
# read last year's audit file
summary(lm(steps ~ treat + time + I(time*treat), data = df))
# use the cluster.vcov function to get variance-covariance matrix
library(multiwayvcov) # for cluster robust standard errors from glm model
library(lmtest) # to get coefficient estimates from glm model
m1 <- lm(steps ~ treat + time + I(time*treat), data = df) # save model
m1.vcovCL <- cluster.vcov(m1, df$id) # get variance-covariance matrix
coeftest(m1, m1.vcovCL) # results with clustered standard errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment