MatthieuStigler / multiDiff

Multi-period diff and diff
Other
3 stars 0 forks source link

Add texreg extract method? #13

Open MatthieuStigler opened 1 year ago

MatthieuStigler commented 1 year ago

Here's one possibility, should think about how to add the para test!?

library(multiDiff)
suppressPackageStartupMessages(library(texreg))

extract_mdd_DiD <- function(model, add_para = FALSE, ...) {

  extr_out <- texreg:::extract.fixest(model, ...)

  if(add_para) {
    if(!all(c("para_test_joint_pval", "para_test_joint_pval") %in% names(model))) stop("Ooutput should have added slot 'para_test_joint'")

    extr_out@gof.names <- c(extr_out@gof.names, "Parallel test: Wald stat", "Parallel test: p-val")
    extr_out@gof <- c(extr_out@gof, model$para_test_joint_val, model$para_test_joint_pval)
    extr_out@gof.decimal <- c(extr_out@gof.decimal, TRUE)

  }
  extr_out
}

DID_dat <- mdd_data_format(sim_dat_common(timing_treatment = 5:10))
mdd_out <- mdd_DD_simple(DID_dat)
mdd_event <- mdd_test_pre_trend_event(DID_dat)
mdd_out$para_test_joint_pval <- subset(mdd_event, test =="test_joint")$p.value
mdd_out$para_test_joint_val <- subset(mdd_event, test =="test_joint")$statistic
setMethod("extract", signature = className("mdd_DiD", "multiDiff"), definition = extract_mdd_DiD)

screenreg(mdd_out, add_para=TRUE)
#> 
#> ======================================
#>                           Model 1     
#> --------------------------------------
#> tr                            1.08 ***
#>                              (0.05)   
#> --------------------------------------
#> Num. obs.                 10000       
#> Num. groups: unit          1000       
#> Num. groups: Time            10       
#> R^2 (full model)              0.70    
#> R^2 (proj model)              0.05    
#> Adj. R^2 (full model)         0.66    
#> Adj. R^2 (proj model)         0.05    
#> Parallel test: Wald stat      2.98    
#> Parallel test: p-val          0.40    
#> ======================================
#> *** p < 0.001; ** p < 0.01; * p < 0.05

Created on 2023-06-07 with reprex v2.0.2

MatthieuStigler commented 1 year ago

Also add N treated/control, needs version 0.2.4 !

library(multiDiff)
suppressPackageStartupMessages(library(texreg))

extract_mdd_DiD <- function(model, add_para = FALSE, include.n_treated = TRUE, ...) {

  extr_out <- texreg:::extract.fixest(model, ...)

  if(include.n_treated){
    mdd_slot <- multiDiff:::intrnl_mdd_get_mdd_slot(model)
    if(mdd_slot$n_seq!=2) stop("Arg. `include.n_treated` only works for standard DiD")

    extr_out@gof.names <- c(extr_out@gof.names, "N control:", "N treated:")
    extr_out@gof <- c(extr_out@gof, mdd_slot$n_units-mdd_slot$n_treated, mdd_slot$n_treated)
    extr_out@gof.decimal <- c(extr_out@gof.decimal, FALSE, FALSE)
  }

  if(add_para) {
    if(!all(c("para_test_joint_pval", "para_test_joint_pval") %in% names(model))) stop("Ooutput should have added slot 'para_test_joint'")

    extr_out@gof.names <- c(extr_out@gof.names, "Parallel test: Wald stat", "Parallel test: p-val")
    extr_out@gof <- c(extr_out@gof, model$para_test_joint_val, model$para_test_joint_pval)
    extr_out@gof.decimal <- c(extr_out@gof.decimal, TRUE, TRUE)

  }
  extr_out
}

DID_dat <- mdd_data_format(sim_dat_common(timing_treatment = 5:10))
mdd_out <- mdd_DD_simple(DID_dat)

setMethod("extract", signature = className("mdd_DiD", "multiDiff"), definition = extract_mdd_DiD)

screenreg(mdd_out, include.n_treated=TRUE)
#> 
#> ===================================
#>                        Model 1     
#> -----------------------------------
#> tr                         1.03 ***
#>                           (0.04)   
#> -----------------------------------
#> Num. obs.              10000       
#> Num. groups: unit       1000       
#> Num. groups: Time         10       
#> R^2 (full model)           0.67    
#> R^2 (proj model)           0.05    
#> Adj. R^2 (full model)      0.63    
#> Adj. R^2 (proj model)      0.05    
#> N control:               750       
#> N treated:               250       
#> ===================================
#> *** p < 0.001; ** p < 0.01; * p < 0.05

Created on 2023-06-13 with reprex v2.0.2

MatthieuStigler commented 1 year ago

Function for synthdid:

extract.synth <- function(model) {

  ## this has more slots, but much slower
  # s <- summary(model)
  # names <- rownames(s$coef)

  ## simple way
  out <- tidy(model)
  co <- out$estimate
  se <- out$std.error 
  pval <- out$p.value

  # rs <- s$r.squared
  # adj <- s$adj.r.squared
  # n <- nobs(model)
  # gof <- c(rs, adj, n)
  # gof.names <- c("R$^2$", "Adj.\\ R$^2$", "Num.\\ obs.")

  tr <- createTexreg(
    coef.names = "treat_status",
    coef = co,
    se = se,
    pvalues = pval,
    # gof.names = gof.names,
    # gof = gof
  )
  return(tr)
}

setMethod("extract", signature = className("synthdid_estimate", "multiDiff"), definition = extract.synth) 
MatthieuStigler commented 1 month ago

first attempt with https://github.com/MatthieuStigler/multiDiff/commit/cf549729e03d856e6055e38abf8b7b3e65f6869c, but:

To be used with:

setMethod("extract", signature = "gsynth", definition = multiDiff:::extract.gsynth)
MatthieuStigler commented 1 month ago

latest code for others: see ETH/Projects/Indonesia ZDC effect/IND_ZDC_deforest_impact_ana/code_setup/888_txrg_scripts_texreg.R