Announcement

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

  • Display Mean and N along with RD estimates

    I am trying to make a table of RD estimates while displaying the mean (of the dependent variable) and number of observations (of the RD sample) under each RD estimate. I have managed to build the table with the RD estimates but am unable to add the latter under the estimates. This is the sample code:

    Code:
    clear all
    
    use "https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata/rdrobust_senate.dta", clear
    
    gen vote1 = vote + runiform()
    gen margin1 = margin + runiform()
    
    collect clear
    
    local outcomes vote vote1
    local running_var margin margin1
    
    foreach x of varlist `outcomes'{
        foreach y of varlist `running_var'{
        collect get e(tau_cl) e(se_tau_cl), tag(col[`x'] row[`y']): rdrobust `x' `y' if -15<=`y' & `y'<=15
        collect get r(mean) r(se), tag(col[`x'] row[`y']): su `x' if -15<=`y' & `y'<=15
        }
    }
    
    collect style column, dups(center)
    collect style cell result[tau_cl], nformat(%9.2f) halign(center)
    collect style cell result[se_tau_cl], nformat(%9.3f) halign(center)
    collect style cell result[mean], nformat(%9.2f) halign(center)
    collect style cell result[N], nformat(%9.0f) halign(center)
    collect style header result, level(hide)
    collect layout (row#result) (col)
    This is the table output:
    HTML Code:
    ---------------------
            |  vote vote1
    --------+------------
    margin  | 12.53 12.42
            | 2.869 2.908
    margin1 |  7.10  7.26
            | 2.864 2.861
    ---------------------
    I would like the table to look like this instead:
    HTML Code:
    ---------------------
            |  vote vote1
    --------+------------
    margin  | 12.53 12.42
            | 2.869 2.908
     mean   | x     x
     N      | x     x
    margin1 |  7.10  7.26
            | 2.864 2.861
     mean   | x     x
     N      | x     x
    ---------------------
    Thanks in advance!

  • #2
    Try: (modifications highlighted in red)

    Code:
    clear all
    
    use "https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata/rdrobust_senate.dta", clear
    
    gen vote1 = vote + runiform()
    gen margin1 = margin + runiform()
    
    collect clear
    
    local outcomes vote vote1
    local running_var margin margin1
    
    foreach x of varlist `outcomes'{
        foreach y of varlist `running_var'{
            collect get e(tau_cl) e(se_tau_cl), tag(col[`x'] row[`y'] cmdset[1]): rdrobust `x' `y' if -15<=`y' & `y'<=15
            collect get r(mean) obs = r(N), tag(col[`x'] row[`y'] cmdset[1]): su `x' if -15<=`y' & `y'<=15
        }
    }
    
    collect style column, dups(center)
    collect style cell result[tau_cl], nformat(%9.2f) halign(center)
    collect style cell result[se_tau_cl], nformat(%9.3f) halign(center)
    collect style cell result[mean], nformat(%9.2f) halign(center)
    collect style cell result[obs], nformat(%9.0f) halign(center)
    collect style header result, level(label)
    collect style autolevels result tau_cl se_tau_cl mean obs, clear
    collect label levels result  tau_cl "tau" se_tau_cl "se" mean "mean" obs "N" , replace
    collect layout (row#result) (col)
    which produces:
    Code:
    . collect preview
    
    ---------------------
            |  vote vote1
    --------+------------
    margin  |            
      tau   | 12.53 12.62
       se   | 2.869 2.868
      mean  | 48.78 49.29
       N    |  607   607 
    margin1 |            
      tau   |  3.82  3.82
       se   | 3.596 3.582
      mean  | 48.57 49.09
       N    |  606   606 
    ---------------------

    Comment


    • #3
      Thank you! That worked.

      Comment

      Working...
      X