Announcement

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

  • Table export after margins contrast

    I noticed a rather peculiar problem when trying to export output tables after using margins with the contrast option:

    Code:
    sysuse nlsw88, clear
    reg never_married i.race##i.collgrad
    margins r.collgrad@race, contrast(nowald pveffects) post saving(results, replace)
    outreg2 using "test.tex", replace label tex(fragment) sideway
    Here, outreg2 exports the nice sideways table displayed by margins, with the Contrast, SE, z-score, and p-value all presented next to each other in separate columns.
    However, despite specifying the label option, outreg2 does not seem able to attach the labels, as the output in the first column shows raw text such as "[email protected]", "[email protected]", etc. Is there a way to preserve the labels that margins is correctly displaying?
    I further tried using esttab/estout as an alternative, but I do not succeed in exporting the table fully in sideways style; instead, the significance stars are placed in separate rows underneath the other results:

    Code:
    sysuse nlsw88, clear
    logit never_married i.race##i.collgrad
    margins r.collgrad@race, contrast(nowald pveffects) post saving(results, replace)
    esttab using "test.tex", replace fragment label cells("b(star fmt(3)) se(fmt(3))")
    Is there a known fix for that?

  • #2
    estout and outreg2 are from SSC, as you are asked to explain (FAQ Advice #12). It does not appear that margins stores the labels for the contrasts, so you will probably need to add them manually. The issue with the stars in estout is related to how rows of the pvalues matrix are named following margins, contrast. You can retrieve the p-values from r(table) and add them to e() using estadd.


    Code:
    sysuse nlsw88, clear
    logit never_married i.race##i.collgrad
    margins r.collgrad@race, contrast(nowald pveffects) post saving(results, replace)
    estadd mat pval= r(table)["pvalue", 1...] 
    esttab , cells("b(star fmt(3) pvalue(pval)) se(fmt(3))") nonumb nomtitles ///
    coeflabels([email protected] "(College grad vs Not college grad) White" ///
    [email protected] "(College grad vs Not college grad) Black" ///
    [email protected] "(College grad vs Not college grad) Other") ///
    varwidth(40) noabbrev collab( Coefficient "Std. Error")
    Res.:

    Code:
    
    . margins r.collgrad@race, contrast(nowald pveffects) post saving(results, replace)
    
    Contrasts of adjusted predictions                        Number of obs = 2,246
    Model VCE: OIM
    
    Expression: Pr(never_married), predict()
    
    ----------------------------------------------------------------------------------
                                              |            Delta-method
                                              |   Contrast   std. err.      z    P>|z|
    ------------------------------------------+---------------------------------------
                                collgrad@race |
    (College grad vs Not college grad) White  |   .0918281   .0183609     5.00   0.000
    (College grad vs Not college grad) Black  |  -.0064927    .041344    -0.16   0.875
    (College grad vs Not college grad) Other  |   .1633987   .1498701     1.09   0.276
    ----------------------------------------------------------------------------------
    
    . estadd mat pval= r(table)["pvalue", 1...]
    
    added matrix:
                   e(pval) :  1 x 3
    
    . esttab , cells("b(star fmt(3) pvalue(pval)) se(fmt(3))") nonumb nomtitles ///
    > coeflabels([email protected] "(College grad vs Not college grad) White" ///
    > [email protected] "(College grad vs Not college grad) Black" ///
    > [email protected] "(College grad vs Not college grad) Other") ///
    > varwidth(40) noabbrev collab( Coefficient "Std. Error")
    
    ---------------------------------------------------------------------
                                              Coefficient      Std. Error
    ---------------------------------------------------------------------
    (College grad vs Not college grad) White        0.092***        0.018
    (College grad vs Not college grad) Black       -0.006           0.041
    (College grad vs Not college grad) Other        0.163           0.150
    ---------------------------------------------------------------------
    N                                                2246                
    ---------------------------------------------------------------------
    
    .

    Comment

    Working...
    X