install.packages('didimputation')
didimputation
R package for the imputation estimator (Borusyak et al 2021)
didimputation is a package that implements the imputation DiD estimator proposed by Borusyak, Jaravel, and Speiss (2021) that solves the issues with TWFE in staggered settings. Documentation can be found here.
Install the package as follows:
sample code
Start by loading packages and the data:
library(didimputation)
library(ggplot2) # for plotting
library(readr) # for importing data
= read_csv('df.csv') df
We use the did_imputation()
function to run the model.
= did_imputation(
mod data = df,
yname = "outcome",
gname = "cohort",
tname = "time",
idname = "id",
first_stage = ~ covar | id + time # can delete entire arg if no covars
)
|> print() mod
#> # A tibble: 1 × 6
#> lhs term estimate std.error conf.low conf.high
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 outcome treat -1.09 0.127 -1.34 -0.842
We can also estimate event-study dynamic effects for pre and post-treatment periods.
= did_imputation(
mod data = df,
yname = "outcome",
gname = "cohort",
tname = "time",
idname = "id",
first_stage = ~ covar | id + time, # can delete entire arg if no covars
horizon = T, # do not change
pretrends = -4:-1 # how many pre-treatment period to include
)
There is no simple built-in plot function for the package, so we will need to manually create a ggplot.
# convert output into table
= mod |> as.data.table()
tbl
# filter for only treatment coefficients
$term = tbl$term |> as.numeric()
tbl= tbl |> na.omit()
tbl
# ggplot
|>
tbl ggplot(aes(x = term, y = estimate)) +
geom_vline(xintercept = -0.5, color = "gray") +
geom_hline(yintercept = 0, color = "gray") +
geom_point() +
geom_linerange(aes(ymin = conf.low, ymax = conf.high)) +
labs(title = "Dynamic Treatment Effects") +
xlab("Time to Treatment") +
ylab("Estimate") +
theme_light()