Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add a statistic to a collection of post-estimation results: csdid example

    Hello,

    Can you please help me on how I could add the sample size of the dependent variable to a collection of post-estimation results and stack multiple collections? I am interested in collecting the ATT, its confidence interval and the sample size for multiple dependent variables. For each dependent variable, I run a DiD regression using the csdid command based on the Callaway and Sant'Anna (2021) approach. The following command gives me a table for the ATT and its confidence interval for the first dependent variable.
    Code:
    use https://friosavila.github.io/playingwithstata/drdid/mpdta.dta, clear
    csdid  lemp lpop , ivar(countyreal) time(year) gvar(first_treat) method(dripw)
    collect _r_b _r_ci: estat simple
    I want to add the sample size of the dependent variable (example, by running "sum lemp") to the collection so that I can have a table that looks like the following.
    ATT -0.042
    CI (-0.064,-0.019)
    N [2500]

    I also have another dependent variable, say lother and want to stack the results below the above table
    Code:
    set seed 123456
    gen lother = lemp*runiform()
    csdid  lother lpop , ivar(countyreal) time(year) gvar(first_treat) method(dripw)
    collect _r_b _r_ci: estat simple
    I want to stack the two collections and create a table that looks like the following
    ATT(lemp) -0.042
    CI(lemp) (-0.064,-0.019)
    N(lemp) [2500]
    ATT(lother) 0.043
    CI(lother) (-0.407,0.492)
    N(lother) [2500]

    I appreciate your help.

    Thanks,
    Dani

  • #2

    Here is how I would use collect to do this in Stata 17 or 18.
    Code:
    use https://friosavila.github.io/playingwithstata/drdid/mpdta.dta, clear
    
    * first model
    csdid lemp lpop , ivar(countyreal) time(year) gvar(first_treat) method(dripw)
    * use e() to collect e(N) and e(level) for CI labels, and r() for r(table) values
    collect e() r() : estat simple
    
    * turn off vertical border
    collect style cell border_block[row-header], border(right, pattern(none))
    
    * add tag for N result, to simplify the layout specification
    collect addtags colname[ATT], fortags(result[N]#cmdset[1])
    
    * custom formats 
    collect style cell result[_r_b _r_ci], nformat(%5.3f)
    collect style cell result[_r_ci], sformat("(%s)") cidelimiter(", ")
    collect style cell result[N], sformat("[%s]")
    
    * specify the items we want to see
    collect layout (cmdset#colname[ATT]#result[_r_b _r_ci N]) 
    
    * second model
    csdid lemp lpop , ivar(countyreal) time(year) gvar(first_treat) method(dripw)
    set seed 123456
    gen lother = lemp*runiform()
    csdid lother lpop , ivar(countyreal) time(year) gvar(first_treat) method(dripw)
    
    * use e() to collect e(N) and e(level) for CI labels, and r() for r(table) values
    collect e() r() : estat simple
    
    * add tag for N result, like we did above
    collect addtags colname[ATT], fortags(result[N]#cmdset[2])
    
    * add labels for the cmdset levels
    collect label levels cmdset 1 "lemp" 2 "lother"
    
    * verify we get the items from both results sets
    collect preview
    
    * add a new dimension with special levels/labels that we want to see in
    * the row header
    collect addtags mylab["ATT(lemp)"], fortags(cmdset[1]#colname[ATT]#result[_r_b])
    collect addtags mylab["CI(lemp)"], fortags(cmdset[1]#colname[ATT]#result[_r_lb _r_ub])
    collect addtags mylab["N(lemp)"], fortags(cmdset[1]#colname[ATT]#result[N])
    collect addtags mylab["ATT(lother)"], fortags(cmdset[2]#colname[ATT]#result[_r_b])
    collect addtags mylab["CI(lother)"], fortags(cmdset[2]#colname[ATT]#result[_r_lb _r_ub])
    collect addtags mylab["N(lother)"], fortags(cmdset[2]#colname[ATT]#result[N])
    
    * suppress levels we do not want to see in the row headers
    collect style header cmdset, level(hide)
    collect style header colname, level(hide)
    collect style header result[_r_b _r_ci N], level(hide)
    
    * add new dimension to the layout; and verify all is as requested
    collect layout (cmdset#colname[ATT]#result[_r_b _r_ci N]#mylab)
    Here is the final table.
    Code:
    ----------------------------
    ATT(lemp)             -0.042
    CI(lemp)    (-0.064, -0.019)
    N(lemp)               [2500]
    ATT(lother)            0.043
    CI(lother)   (-0.407, 0.492)
    N(lother)             [2500]
    ----------------------------

    Comment


    • #3
      This is perfect!


      Thanks, Jeff!
      Dani

      Comment

      Working...
      X