Announcement

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

  • How?: Table with incremental marginal effects after logit model with interaction

    Hi everyone!
    How can I output to Latex (e.g. with esttab), after a logit model with interaction term (!), the respective incremental marginal effects of each variable (especially the interaction) instead of the logit coefficients (such that the interpretation of the incremental marginal effects is analogous to the interpretation of coefficients from a linear OLS regression)?
    I want to output the incremental marginal probabilities of the logit model in a table with esttabalongside other tobit, OLS models.

    Consider the following example.
    Code:
    sysuse auto, clear
    gen binaryoutcome = weight>2000
    
    //use reg instead of logit to see that margins below are calculated properly:
    eststo mainmodel: logit binaryoutcome i.foreign##c.trunk
    
    margins, dydx(i.foreign) at(c.trunk==0)
    margins, dydx(c.trunk) at(foreign==0)
    
    //effect of 1 'trunk' unit when foreign==1 in addition (!) to the effect of 1 'trunk' unit when foreign==0 and in addition (!) to
    //the differential offect of moving from foreign==0 to foreign==1
    margins, dydx(c.trunk) over(foreign) pwcompare(effects)
    
    esttab mainmodel, nobaselevels nonotes noobs nomtitle nonumber noline

    I want to produce this table (here produced by hand); the coefficient values and z-stats are from the three margins commands from above:
    Code:
    Pr[binaryoutcome==1] 
    1.foreign           0.4430546  
                       (0.69)  
    
    trunk               0.0138894  
                       (1.08)  
    
    1.foreign#trunk    -.0109257  
                      (-0.36)
    I have tried everything that I considered remotely possible:
    • saving the margins as a model and tabulating -- cannot assign the third value to the interaction "variable"; can only save a single margin
    • erepost -- can replace coefficients in mainmodel in e(b)[i,j], but don't know how to replace z and p values
    • estadd margins -- can only add a single margin ("e(margins_level) already defined" on second estadd margin); esttab shows non-incremental effects for i.foreign==0, i.foreign==1 when tabulating
    • estpost margins -- can only add a single margin ("option dydx() not allowed"); if using estimates restore mainmodel previous margins are gone; margins not showing after esttab
    • extract values and 'manually' replace in r(coefs), but don't know how to get original labels, titles, formats, z-stat below coef when tabulating matrix with estout
    If this were a model without interaction I would simply, after the logit command, save the 'marginal effects at the means' as model and then tabulate:
    Code:
    eststo marginsmodel: margins, dydx(*) atmeans post
    esttab marginsmodel

    Please help me:
    1. Is my understanding of the marginal effects as calculated correct and my desired presentatin appropriate for a logit model with interaction?
    2. How do I get the incremental marginal effects from Stata into Latex, alongside other models?
    Your help is much appreciated, I've been trying only this for 1.5 days now. Thank you all!
    Last edited by Alexander Schumann; 03 Jun 2021, 02:29.

  • #2
    estout and erepost are from SSC (FAQ Advice #12). Thanks for the reproducible example. You need the -post- option to export margins results. Trying to output results from different models and making them appear as if they are from the same model is not an easy task with esttab (and should not be!). At best, it is confusing to the reader and worse, it may be purposeful manipulation. However, you can store the results and then replace estimates from a single model. Also, search the forum for mergemodels by Ben Jann which can be used to combine estimates from different models.

    Code:
    sysuse auto, clear
    gen binaryoutcome = weight>2000
    
    //use reg instead of logit to see that margins below are calculated properly:
    eststo mainmodel: logit binaryoutcome i.foreign##c.trunk
    
    margins, dydx(i.foreign) at(c.trunk==0) post
    mat b1= e(b)[1,2]
    mat V1=e(V)[2,2]
    
    
    est restore mainmodel
    margins, dydx(c.trunk) at(foreign==0) post
    
    mat b2= e(b)
    mat V2=e(V)
    
    est restore mainmodel
    margins, dydx(c.trunk) over(foreign) pwcompare(effects) post
    mat b3=e(b_vs)
    mat V3= e(V_vs)
    
    mat b= b1,b2,b3
    mat V= V1, 0, 0\ 0, V2, 0\ 0, 0, V3
    
    qui logit binaryoutcome 1.foreign##c.trunk, nocons 
    
    mat colname b= `:colname e(b)'
    mat colname V= `:colname e(V)'
    mat coleq b= ""
    mat coleq V= ""
    
    capt prog drop replace_bV
    program replace_bV, eclass
    erepost b= b, rename
    erepost V= V, rename
    end
    replace_bV
    
    eststo wanted
    esttab wanted, noobs nonumbers mlab("Pr[`e(depvar)']=1")
    Res.:

    Code:
    
    . esttab wanted, noobs nonumbers mlab("Pr[`e(depvar)']=1")
    
    ----------------------------
                 Pr[binaryo~1  
    ----------------------------
    1.foreign           0.443  
                       (0.69)  
    
    trunk              0.0139  
                       (1.08)  
    
    1.foreign#~k      -0.0109  
                      (-0.36)  
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Amazing, this totally works, thanks a bunch!
      In my actual code I figured I should rather use what would be analogous to the following code (although in the auto file there are no observations with trunk==0, but my data has a majority of observations where the continuous variable ==0):
      Code:
      margins, dydx(i.foreign) at(c.trunk==0)
      margins if trunk>0, dydx(c.trunk) at(foreign==0)
      margins if trunk>0, dydx(c.trunk) over(foreign) pwcompare(effects)

      It would be helpful if someone could further comment on my point 1. from post #1, i.e. whether the marginal effects calculated in this manner can be interpreted analogously to the coefficients from an OLS regression (in fact, In my real data, I also do OLS regression of my binaryoutcome on the full interaction, and the OLS coefficients are reassuringly close to the margins calculated as I propose in this post).

      Comment

      Working...
      X