Last active
May 3, 2021 20:10
-
-
Save kylebutts/3829464a22fff16a0e70b82ac558f897 to your computer and use it in GitHub Desktop.
Export did aggregation to tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| reg_format <- function(pt, se, crit) { | |
| pt_str <- sprintf("%0.4f", pt) | |
| t_stat <- abs(pt/se) | |
| if(t_stat > crit) { | |
| pt_str <- paste0("$", pt_str, "^{**}$") | |
| } | |
| return(pt_str) | |
| } | |
| export_did_effects <- function(obj) { | |
| tex <- "" | |
| type <- obj$type | |
| # Overall Row | |
| if(type == "simple"){ | |
| att <- obj$overall.att | |
| se <- obj$overall.se | |
| # p-value | |
| p <- qnorm(1 - obj$DIDparams$alp/2) | |
| att_str <- reg_format(att, se, p) | |
| tex <- paste(tex, glue::glue("ATT & {att_str} & {format(se, digits=3)} & {format(att - 1.96 * se, digits=3)} & {format(att + 1.96 * se, digits=3)}")) | |
| tex <- paste(tex, "\n") | |
| } | |
| if(type %in% c("group", "calendar", "dynamic")) { | |
| egt <- obj$egt | |
| att.egt <- obj$att.egt | |
| se.egt <- obj$se.egt | |
| crit.val.egt <- obj$crit.val.egt | |
| for(i in 1:length(att.egt)){ | |
| att_str <- reg_format(att.egt[i], se.egt[i], crit.val.egt) | |
| tex <- paste(tex, glue::glue("ATT({egt[i]}) & {att_str} & {format(se.egt[i], digits=3)} & {format(att.egt[i] - crit.val.egt * se.egt[i], digits=3)} & {format(att.egt[i] - crit.val.egt * se.egt[i], digits=3)}")) | |
| if(i != length(att.egt)) { | |
| tex <- paste(tex, "\\\\") | |
| } | |
| tex <- paste(tex, "\n") | |
| } | |
| } | |
| return(tex) | |
| } | |
| # Example Code | |
| library(tidyverse) | |
| library(did) | |
| data(mpdta) | |
| out <- att_gt(yname = "lemp", | |
| gname = "first.treat", | |
| idname = "countyreal", | |
| tname = "year", | |
| xformla = ~1, | |
| data = mpdta, | |
| est_method = "reg" | |
| ) | |
| group_effects <- aggte(out, type = "group") | |
| export_did_effects(group_effects) %>% cat() | |
| dynamic_effects <- aggte(out, type="dynamic") | |
| export_did_effects(dynamic_effects) %>% cat() | |
| calendar_effects <- aggte(out, type="calendar") | |
| export_did_effects(calendar_effects) %>% cat() | |
| simple_effects <- aggte(out, type="simple") | |
| export_did_effects(simple_effects) %>% cat() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

To write to file just change
%>% cat()to%>% cat(file = "filename.tex")Created on 2021-05-03 by the reprex package (v0.3.0)