Announcement

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

  • Table displaying multiple RD models in a single column along with mean and number of observations

    I’m trying to create a summary table from a set of RD regressions using the collect command. I have several outcome variables and multiple running variables, and I want to stack the results for each outcome into a single column for better presentation. I have successfully run my regressions and collected results for each outcome and running variable and am able to show the different models into one column per outcome. But I also want to show the mean of the outcome variable and the number of observations used in each regression. Here is my code using an example dataset:

    Code:
    clear all
    
    use "https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata/rdrobust_senate.dta", clear
    
    gen vote1 = vote + runiform()
    gen margin1 = margin + runiform()
    
    cap program drop rd_prog
    program define rd_prog
    syntax varlist(min=2 max=2), bw(real)
    cap drop run
    cap drop above
    cap drop above_run_inter
    
    gen run=`2'
    gen above=(run>=0)
    gen above_run_inter=above*run
    
    reg `1' run above above_run_inter if abs(run)<`bw'
    end
    
    
    collect clear
    
    local outcomes vote vote1
    local running_var margin margin1
    
    foreach x of varlist `outcomes'{
        foreach y of varlist `running_var'{
        collect get, tag(col[`x'] row[`y']): rd_prog `x' `y', bw(15)
        }
    }
    
    collect style header colname, level(hide) title(hide)
    collect style header result, level(hide)
    
    collect layout (colname[above]#row#result[_r_b _r_se]) (col)
    The output comes out like this:
    Click image for larger version

Name:	table.png
Views:	1
Size:	4.4 KB
ID:	1768149



    But I would like to add "Mean" (of the dependent var.) and "Number of observations" (used in the regression) under each regression output.
    I can get the values I want using the estpost command but I would like to combine this with the table above:
    Code:
    local outcomes vote vote1
    local running_var margin margin1
    eststo clear
    foreach x of varlist `outcomes'{
        foreach y of varlist `running_var'{
            eststo: qui estpost sum `x' if abs(`y')<15
        }
    }
    
    esttab, cells("mean")
    Thanks in advance!

  • #2
    Thank you for providing example code with data.

    In your program rd_prog, add the results you want in a new e() matrix, then update your layout to include those new results.

    Here is how I modified your example to accommodate showing the outcome mean and sample size. My edits are in blue.
    Code:
    clear all
    
    use "https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata/rdrobust_senate.dta", clear
    
    gen vote1 = vote + runiform()
    gen margin1 = margin + runiform()
    
    cap program drop rd_prog
    program define rd_prog, eclass
    syntax varlist(min=2 max=2), bw(real)
    cap drop run
    cap drop above
    cap drop above_run_inter
    
    gen run=`2'
    gen above=(run>=0)
    gen above_run_inter=above*run
    
    reg `1' run above above_run_inter if abs(run)<`bw'
    summarize `1' if e(sample)
    tempname extra
    matrix `extra' = r(mean), r(N)
    matrix colname `extra' = mean N
    ereturn matrix extra `extra'
    end
    
    
    collect clear
    
    local outcomes vote vote1
    local running_var margin margin1
    
    foreach x of varlist `outcomes'{
        foreach y of varlist `running_var'{
        collect get, tag(col[`x'] row[`y']): rd_prog `x' `y', bw(15)
        }
    }
    
    collect style header colname, level(hide) title(hide)
    collect style header result, level(hide)
    
    * adorn SE values in parens
    collect style cell result[_r_se], sformat("(%s)")
    * show row header for means and sample size
    collect style header colname[mean N], level(value)
    
    * fix row specification to accommodate mean and sample size
    collect layout (row#colname[above mean N]#result[_r_b _r_se extra]) (col)
    Here is the resulting table.
    Code:
    -------------------------------
            |       vote      vote1
    --------+----------------------
    margin  |   6.963837   6.954685
            | (1.503954) (1.507287)
      mean  |   48.78155   49.26791
      N     |        607        607
    margin1 |   6.017279   6.008345
            | (1.496242) (1.499515)
      mean  |   48.65793   49.14279
      N     |        602        602
    -------------------------------

    Comment


    • #3
      Thank you so much! I spent so much time unsuccessfully trying to figure this out.

      Comment

      Working...
      X