Search code examples
statisticsstatalogistic-regressionmlogit

Error while estimating marginal effects


I have a problem in retrieving marginal effects for a fitted logistic regression model in Stata 15. The outcome variable mathtsbv is binary, a gender variable sex is also dummy and recorded ethnicity eth variable is categorical with values ranging from 0 to 5. All missing values have been excluded.

Here is an excerpt from my do-file:

logit mathtsbv sex eth sex##i.eth if (mathtsbv>=0&mathtsbv<.)&(sex>=0&sex<.)&(eth>=0&eth<.)
margins, dydx(sex eth sex##i.eth) atmeans

This is the error I get in Stata's logs:

. margins, dydx(sex eth sex##i.eth) atmeans
    invalid dydx() option;
    variable sex may not be present in model as factor and continuous predictor

I spent more than an hour Googling and experimenting: removing sex from the model and keeping only eth, and adding a continuous variable to the list of predictors. Unfortunately none of those brought a problem resolution.


Solution

  • You can calculate contrasts of average marginal effects that will get you something similar to what you want: how does the change in probability of success when you alter one variable vary when a second variable changes.

    Here's a replicable example in Stata:

    . webuse lbw, clear
    (Hosmer & Lemeshow data)
    
    . qui logit low i.smoke##i.race
    
    . margins r.smoke#r.race
    
    Contrasts of adjusted predictions
    Model VCE    : OIM
    
    Expression   : Pr(low), predict()
    
    ---------------------------------------------------------------------------
                                            |         df        chi2     P>chi2
    ----------------------------------------+----------------------------------
                                 smoke#race |
    (smoker vs nonsmoker) (black vs white)  |          1        0.00     0.9504
    (smoker vs nonsmoker) (other vs white)  |          1        1.59     0.2070
                                     Joint  |          2        1.67     0.4332
    ---------------------------------------------------------------------------
    
    -----------------------------------------------------------------------------------------
                                            |            Delta-method
                                            |   Contrast   Std. Err.     [95% Conf. Interval]
    ----------------------------------------+------------------------------------------------
                                 smoke#race |
    (smoker vs nonsmoker) (black vs white)  |   .0130245   .2092014     -.3970027    .4230517
    (smoker vs nonsmoker) (other vs white)  |  -.2214452   .1754978     -.5654146    .1225242
    -----------------------------------------------------------------------------------------
    

    For example, the effect of smoking on the probability of having a low weight child is 22 percentage points lower for other compared to white. This difference is not significant.

    These results are identical to what you would get with a fully saturated OLS model where you can interpret the interaction coefficients directly:

    . reg low i.smoke##i.race, robust
    
    Linear regression                               Number of obs     =        189
                                                    F(5, 183)         =       5.09
                                                    Prob > F          =     0.0002
                                                    R-squared         =     0.0839
                                                    Root MSE          =     .45072
    
    -------------------------------------------------------------------------------
                  |               Robust
              low |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
            smoke |
          smoker  |   .2744755   .0809029     3.39   0.001     .1148531    .4340979
                  |
             race |
           black  |   .2215909   .1257293     1.76   0.080    -.0264745    .4696563
           other  |   .2727273   .0792791     3.44   0.001     .1163086    .4291459
                  |
       smoke#race |
    smoker#black  |   .0130245   .2126033     0.06   0.951    -.4064443    .4324933
    smoker#other  |  -.2214452   .1783516    -1.24   0.216    -.5733351    .1304447
                  |
            _cons |   .0909091    .044044     2.06   0.040     .0040098    .1778083
    -------------------------------------------------------------------------------