kylebutts / did2s

Two-stage Difference-in-Differences package following Gardner (2021)
http://kylebutts.github.io/did2s
Other
96 stars 22 forks source link

Example of `ggplot` plot for `did2s` #14

Closed kylebutts closed 2 years ago

kylebutts commented 2 years ago
library(did2s)
#> Loading required package: fixest
#> ℹ did2s (v0.6.0). For more information on the methodology, visit <https://www.kylebutts.com/did2s>
#> To cite did2s in publications use:
#> 
#>   Butts, Kyle (2021).  did2s: Two-Stage Difference-in-Differences
#>   Following Gardner (2021). R package version 0.6.0.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {did2s: Two-Stage Difference-in-Differences Following Gardner (2021)},
#>     author = {Kyle Butts},
#>     year = {2021},
#>     url = {https://github.com/kylebutts/did2s/},
#>   }
library(ggplot2)

data("df_hom")

es <- did2s(df_hom,
            yname = "dep_var", treatment = "treat", cluster_var = "state",
            first_stage = ~ 0 | unit + year,
            second_stage = ~ i(rel_year, ref=c(-1, Inf)))
#> Running Two-stage Difference-in-Differences
#> • first stage formula `~ 0 | unit + year`
#> • second stage formula `~ i(rel_year, ref = c(-1, Inf))`
#> • The indicator variable that denotes when treatment is on is `treat`
#> • Standard errors will be clustered by `state`

# Plotting with ggplot2

# gather points
pts <- broom::tidy(es)

# convert rel_year terms to relative year number
pts$term <- stringr::str_replace(pts$term, "rel_year::", "") |>
    as.numeric()
pts$ci_lower <- pts$estimate - 1.96 * pts$std.error
pts$ci_upper <- pts$estimate + 1.96 * pts$std.error

ggplot(pts) +
    geom_point(aes(x = term, y = estimate)) +
    geom_errorbar(aes(x = term, ymin = ci_lower, ymax = ci_upper))

Created on 2022-03-07 by the reprex package (v2.0.1)

grantmcdermott commented 2 years ago

FWIW did2s estimation objects should work out of the box with ggiplot. In most cases, a simple ggiplot::ggiplot(object) call should suffice, but it's easy to customise beyond that.

library(did2s)
#> Loading required package: fixest
#> ℹ did2s (v0.4.0). For more information on the methodology, visit <https://www.kylebutts.com/did2s>
#> To cite did2s in publications use:
#> 
#>   Butts, Kyle (2021).  did2s: Two-Stage Difference-in-Differences
#>   Following Gardner (2021). R package version 0.4.0.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {did2s: Two-Stage Difference-in-Differences Following Gardner (2021)},
#>     author = {Kyle Butts},
#>     year = {2021},
#>     url = {https://github.com/kylebutts/did2s/},
#>   }
library(ggiplot)
#> Loading required package: ggplot2

data("df_hom")

es = did2s(df_hom,
                    yname = "dep_var", treatment = "treat", cluster_var = "state",
                    first_stage = ~ 0 | unit + year,
                    second_stage = ~ i(rel_year, ref=c(-1, Inf)))
#> Running Two-stage Difference-in-Differences
#> • first stage formula `~ 0 | unit + year`
#> • second stage formula `~ i(rel_year, ref = c(-1, Inf))`
#> • The indicator variable that denotes when treatment is on is `treat`
#> • Standard errors will be clustered by `state`

ggiplot(es)

Created on 2022-03-31 by the reprex package (v2.0.1)