Announcement

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

  • using collect to make a table of results from the margins command

    Dear Stata Users,

    I searched the forum for examples showing how to transform the output from the margins command into a table using the new collect command.

    I didn't find anything, but after reading the help files and much experimentation, I found some code that works. Hopefully this will save someone else some time.

    Other users may need to edit the winexec command so it points at excel.exe.

    Jeremy

    Code:
    use https://www.stata-press.com/data/r17/nlswork
    
    xtreg ln_w c.age c.age#c.age c.ttl_exp c.tenure i.not_smsa i.south i.race#c.tenure, fe    
    
    *marginal effects by race
    collect clear
    collect: margins, dydx(tenure) over(race) 
    collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", shownote
    collect title "Marginal Effect of Tenure by Race"
    collect style cell result[_r_se _r_b], nformat(%8.7f)
    collect layout (race[1 2 3]) (result[_r_b _r_se _r_p stars])
    collect export table1.xlsx, replace
    winexec "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" table1.xlsx 
    
    *contrasts of marginal effects by race
    collect clear
    collect: margins, dydx(tenure) over(r.race) contrast(effects)
    collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", shownote
    collect title "Marginal Effect of Tenure compared to whites"
    collect style cell result[_r_se _r_b], nformat(%8.7f)
    *collect label levels race 2 "Black vs. White" 3 "Other vs White", modify // edit row labels
    collect layout (race[2 3]) (result[_r_b _r_se _r_p stars])
    collect export table2.xlsx, replace
    winexec "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" table2.xlsx

  • #2
    Very helpful. Thanks, Jeremy!

    I would like to take this code a step further and combine the margins and the contrast in a single table. My slightly modified code below creates a table of margins for Black and White and a second table showing the contrast for Black vs White. How do I append the second table to the first?

    Thanks,
    Devra

    Code:
    use https://www.stata-press.com/data/r17/nlswork
    
    xtreg ln_w c.age c.age#c.age c.ttl_exp c.tenure i.not_smsa i.south i.race#c.tenure, fe    
    
    *marginal effects by race
    collect clear
    collect create Table1
    quietly collect: margins, dydx(tenure) over(race)
    collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", shownote
    collect title "Marginal Effect of Tenure by Race"
    collect style cell result[_r_se _r_b], nformat(%8.7f)
    collect layout (race[1 2]) (result[_r_b _r_se _r_p stars])
    
    
    *contrasts of marginal effects by race
    collect clear
    collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", shownote
    quietly collect: margins, dydx(tenure) over(r.race) contrast(effects)
    collect style cell result[_r_se _r_b], nformat(%8.7f)
    collect label levels race 2 "Black vs. White" 3 "Other vs White", modify // edit row labels
    collect layout (race[2 ]) (result[_r_b _r_se _r_p stars])
    Devra Golbe
    Professor Emerita, Dept. of Economics
    Hunter College, CUNY

    Comment


    • #3
      Hi Devra,

      I have struggled with appending tables too. The post below show a solution to that problem in a different context. I don't know if it will work in your situation.

      Jeremy

      https://www.statalist.org/forums/for...ollect-command

      Comment


      • #4
        Thanks, Jeremy! I've gotten closer, but I can't figure out how to fix the row label for the contrast. See the third table in the code below. ( I've also amended the code to attach the stars to the coefficients.)

        Devra

        Code:
        use https://www.stata-press.com/data/r17/nlswork
        
        xtreg ln_w c.age c.age#c.age c.ttl_exp c.tenure i.not_smsa i.south i.race#c.tenure, fe    
        
        *marginal effects by race
        collect clear
        collect create Table1
        quietly collect: margins, dydx(tenure) over(race)
        collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", attach(_r_b) shownote
        collect title "Marginal Effect of Tenure by Race"
        collect style cell result[_r_se _r_b], nformat(%8.7f)
        collect layout (race[1 2]) (result[_r_b _r_se _r_p ])
        
        *contrasts of marginal effects by race
        collect clear
        collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", attach(_r_b) shownote
        quietly collect: margins, dydx(tenure) over(r.race) contrast(effects)
        collect style cell result[_r_se _r_b], nformat(%8.7f)
        collect label levels race 2 "Black vs. White" 3 "Other vs White", modify // edit row labels
        collect layout (race[2 ]) (result[_r_b _r_se _r_p ])
        
        *both results in a single table
        collect clear
        collect stars _r_p 0.01 "**" 0.05 "*" 0.1 "+", attach(_r_b) shownote
        quietly collect: margins, dydx(tenure) over(race)
        quietly collect: margins, dydx(tenure) over(r.race) contrast(effects)
        collect style cell result[_r_se _r_b], nformat(%8.7f)
        collect label levels cmdset 1 "Margins" 2 "Contrast", modify
        collect layout (cmdset#race[1 2 ]) (result[_r_b _r_se _r_p ])
        Devra Golbe
        Professor Emerita, Dept. of Economics
        Hunter College, CUNY

        Comment

        Working...
        X