Statistical Computing Seminars
Logistic Regression with Stata

This page has been updated to Stata 12.

The purpose of this seminar is to help you increase your skills in using logistic regression analysis with Stata.  The seminar does not teach logistic regression, per se, but focuses on how to perform logistic regression analyses and interpret the results using Stata.  It is assumed that you are familiar with Logistic Regression (e.g., have had a class in logistic regression or have read about logistic regression, see our Statistics Books for Loan for books you can borrow on logistic regression).

In addition to the built-in Stata commands we will be demonstrating the use of a number on user-written ados, in particular, listcoef, fitstat, prchange, prtab, prgen, etc. To find out more about these programs or to download them type findit followed by the program name in the Stata command window (example: findit listcoef). These add-on programs ease the running and interpretation of ordinal logistic models. Or, you can download the complete spostado package by typing the following in the Stata command window:

We will use the hsbdemo dataset with the binary response variable honors (enrolled in an honors program). We will looks at some descriptive statistics before we begin our first logistic regression model.

use http://www.ats.ucla.edu/stat/data/hsbdemo, clear

describe honors read female prog
 
              storage  display     value
variable name   type   format      label      variable label
---------------------------------------------------------------------------------
honors          float  %19.0g      honlab     honors english
read            float  %9.0g                  reading score
female          float  %9.0g       fl         
prog            float  %9.0g       sel        type of program

summarize honors read female prog
 
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      honors |       200        .265    .4424407          0          1
        read |       200       52.23    10.25294         28         76
      female |       200        .545    .4992205          0          1
        prog |       200       2.025    .6904772          1          3

tab1 honors female prog

-> tabulation of honors  

      honors |
     english |      Freq.     Percent        Cum.
-------------+-----------------------------------
not enrolled |        147       73.50       73.50
    enrolled |         53       26.50      100.00
-------------+-----------------------------------
       Total |        200      100.00

-> tabulation of female  

     female |      Freq.     Percent        Cum.
------------+-----------------------------------
       male |         91       45.50       45.50
     female |        109       54.50      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of prog  

    type of |
    program |      Freq.     Percent        Cum.
------------+-----------------------------------
    general |         45       22.50       22.50
   academic |        105       52.50       75.00
   vocation |         50       25.00      100.00
------------+-----------------------------------
      Total |        200      100.00

logit honors read i.female i.prog

Iteration 0:   log likelihood = -115.64441  
Iteration 1:   log likelihood = -86.845312  
Iteration 2:   log likelihood = -84.560995  
Iteration 3:   log likelihood = -84.542357  
Iteration 4:   log likelihood = -84.542348  
Iteration 5:   log likelihood = -84.542348  

Logistic regression                               Number of obs   =        200
                                                  LR chi2(4)      =      62.20
                                                  Prob > chi2     =     0.0000
Log likelihood = -84.542348                       Pseudo R2       =     0.2689

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   .1352861   .0242218     5.59   0.000     .0878123      .18276
    1.female |    1.08343   .4094357     2.65   0.008     .2809511    1.885909
             |
        prog |
          2  |   .5559416   .5053125     1.10   0.271    -.4344527    1.546336
          3  |   .0016408   .6611702     0.00   0.998    -1.294229     1.29751
             |
       _cons |   -9.41691   1.481922    -6.35   0.000    -12.32142   -6.512397
------------------------------------------------------------------------------

contrast prog  // multi degree of freedom test

Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
        prog |          2        1.78     0.4101
------------------------------------------------

estimates store m1

/* alternative to contrast: Wald test */

test 2.prog 3.prog
 ( 1)  [honors]2.prog = 0
 ( 2)  [honors]3.prog = 0

           chi2(  2) =    1.78
         Prob > chi2 =    0.4101
/* Likelihood ratio-test */

logit honors read i.female, nolog
 
Logistic regression                               Number of obs   =        200
                                                  LR chi2(2)      =      60.40
                                                  Prob > chi2     =     0.0000
Log likelihood =  -85.44372                       Pseudo R2       =     0.2612

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   .1443657   .0233338     6.19   0.000     .0986322    .1900991
    1.female |   1.120926   .4081043     2.75   0.006     .3210558    1.920795
       _cons |  -9.603364   1.426412    -6.73   0.000    -12.39908   -6.807647
------------------------------------------------------------------------------
 
lrtest m1 .
 
Likelihood-ratio test                                 LR chi2(2)  =      1.80
(Assumption: . nested in m1)                          Prob > chi2 =    0.4060
  
/* rerun full model */

logit honors read i.female i.prog, nolog  /* displays coefficients */
Logistic regression                               Number of obs   =        200
                                                  LR chi2(4)      =      62.20
                                                  Prob > chi2     =     0.0000
Log likelihood = -84.542348                       Pseudo R2       =     0.2689

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   .1352861   .0242218     5.59   0.000     .0878123      .18276
    1.female |    1.08343   .4094357     2.65   0.008     .2809511    1.885909
             |
        prog |
          2  |   .5559416   .5053125     1.10   0.271    -.4344527    1.546336
          3  |   .0016408   .6611702     0.00   0.998    -1.294229     1.29751
             |
       _cons |   -9.41691   1.481922    -6.35   0.000    -12.32142   -6.512397
------------------------------------------------------------------------------
 
logit, or  /* display odds ratios  */

Logistic regression                               Number of obs   =        200
                                                  LR chi2(4)      =      62.20
                                                  Prob > chi2     =     0.0000
Log likelihood = -84.542348                       Pseudo R2       =     0.2689

------------------------------------------------------------------------------
      honors | Odds Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   1.144864   .0277307     5.59   0.000     1.091783    1.200526
    1.female |   2.954798     1.2098     2.65   0.008     1.324389    6.592347
             |
        prog |
          2  |   1.743582   .8810538     1.10   0.271      .647619    4.694239
          3  |   1.001642   .6622559     0.00   0.998     .2741091    3.660173
             |
       _cons |   .0000813   .0001205    -6.35   0.000     4.46e-06    .0014849
------------------------------------------------------------------------------

Now we will demonstrate some of the utilities developed by J. Scott Long & Jeremy Freese.

listcoef
 
logit (N=200): Factor Change in Odds 

  Odds of: enrolled vs not_enro

----------------------------------------------------------------------
      honors |      b         z     P>|z|    e^b    e^bStdX      SDofX
-------------+--------------------------------------------------------
        read |   0.13529    5.585   0.000   1.1449   4.0031    10.2529
    1.female |   1.08343    2.646   0.008   2.9548   1.7175     0.4992
      2.prog |   0.55594    1.100   0.271   1.7436   1.3209     0.5006
      3.prog |   0.00164    0.002   0.998   1.0016   1.0007     0.4341
----------------------------------------------------------------------
  
listcoef, percent
 
logit (N=200): Percentage Change in Odds 

  Odds of: enrolled vs not_enro

----------------------------------------------------------------------
      honors |      b         z     P>|z|      %      %StdX      SDofX
-------------+--------------------------------------------------------
        read |   0.13529    5.585   0.000     14.5    300.3    10.2529
    1.female |   1.08343    2.646   0.008    195.5     71.8     0.4992
      2.prog |   0.55594    1.100   0.271     74.4     32.1     0.5006
      3.prog |   0.00164    0.002   0.998      0.2      0.1     0.4341
----------------------------------------------------------------------
 
 fitstat
  
Measures of Fit for logit of honors

Log-Lik Intercept Only:       -115.644   Log-Lik Full Model:            -84.542
D(193):                        169.085   LR(4):                          62.204
                                         Prob > LR:                       0.000
McFadden's R2:                   0.269   McFadden's Adj R2:               0.208
ML (Cox-Snell) R2:               0.267   Cragg-Uhler(Nagelkerke) R2:      0.390
McKelvey & Zavoina's R2:         0.435   Efron's R2:                      0.284
Variance of y*:                  5.820   Variance of error:               3.290
Count R2:                        0.800   Adj Count R2:                    0.245
AIC:                             0.915   AIC*n:                         183.085
BIC:                          -853.491   BIC':                          -41.011
BIC used by Stata:             195.576   AIC used by Stata:             179.085

Another fit indicator is the Hosmer & Lemeshow goodness-of-fit given by the lfit command which is part of Stata.

estat gof, group(10)

Logistic model for honors, goodness-of-fit test

  (Table collapsed on quantiles of estimated probabilities)

       number of observations =       200
             number of groups =        10
      Hosmer-Lemeshow chi2(8) =        15.12
                  Prob > chi2 =         0.0568

Next, let's look at some logistic regression diagnostics.

predict pprob            // predicted probabilities 
predict r, resid         // pearson residulas 
predict h, hat           // leverage
predict db, dbeta        // pregobon dbeta 
predict dx2, dx2         // hosmer & lemeshow influence                     

scatter h r, xline(0) msym(Oh) jitter(2)  // leverage vs residual plot 



twoway (scatter dx2 pprob if honors==1, msym(Oh) jitter(2)) ///
       (scatter dx2 pprob if honors==0, msym(Oh) jitter(2))



scatter dx2 pprob if honors==1 [w=db], msym(Oh) jitter(2)


 
summarize pprob
 
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       pprob |       200        .265    .2444744   .0053708   .9244364

histogram pprob
(bin=14, start=.01473261, width=.0701496)

Some researchers find log-odds and odds ratios difficult to interpret and prefer to interpret their results in terms of predicted probabilities.

list honors pprob in 110/125
 
     +-------------------+
     | honors      pprob |
     |-------------------|
110. |      0   .2405516 |
111. |      1   .1948044 |
112. |      0   .2405516 |
113. |      0    .416587 |
114. |      0   .1387049 |
     |-------------------|
115. |      0   .1948044 |
116. |      0   .0449239 |
117. |      1   .1946255 |
118. |      0   .6781951 |
119. |      0   .4834491 |
     |-------------------|
120. |      1   .5841008 |
121. |      1   .3496613 |
122. |      0   .3224231 |
123. |      1   .1948044 |
124. |      1   .1948044 |
     |-------------------|
125. |      0   .0756818 |
     +-------------------+

Here is another utility (leastlikely) by Jeremy Freese that is not part of the spostado package (findit leastlikely).

leastlikely honors read female prog 
 
Outcome: 0 (not enrolled)

     +----------------------------------------------------+
     |     Prob         honors   read   female       prog |
     |----------------------------------------------------|
134. | .2657953   not enrolled     65   female   academic |
157. | .2657953   not enrolled     65   female   academic |
165. | .1943608   not enrolled     68   female   academic |
166. |   .29575   not enrolled     68   female   vocation |
180. | .1945395   not enrolled     76     male   academic |
     +----------------------------------------------------+

Outcome: 1 (enrolled)

     +------------------------------------------------+
     |     Prob     honors   read   female       prog |
     |------------------------------------------------|
 59. | .1220252   enrolled     47   female   vocation |
135. | .1387049   enrolled     52     male   academic |
145. | .1556703   enrolled     53     male   academic |
155. | .1723344   enrolled     50   female    general |
160. | .0756818   enrolled     47     male   academic |
     +------------------------------------------------+
  
margins, dydx(read female prog)   // as observed 

Average marginal effects                          Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
dy/dx w.r.t. : read 1.female 2.prog 3.prog

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |    .018305   .0022549     8.12   0.000     .0138856    .0227245
    1.female |   .1456766    .051693     2.82   0.005     .0443602    .2469929
             |
        prog |
          2  |   .0768896   .0680412     1.13   0.258    -.0564686    .2102479
          3  |   .0002073   .0835493     0.00   0.998    -.1635463     .163961
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
 
 margins, dydx(read female prog) atmeans  // holding variables at their mean value

Conditional marginal effects                      Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
dy/dx w.r.t. : read 1.female 2.prog 3.prog
at           : read            =       52.23 (mean)
               0.female        =        .455 (mean)
               1.female        =        .545 (mean)
               1.prog          =        .225 (mean)
               2.prog          =        .525 (mean)
               3.prog          =         .25 (mean)

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   .0205865   .0035592     5.78   0.000     .0136105    .0275624
    1.female |   .1606615   .0586157     2.74   0.006     .0457769    .2755462
             |
        prog |
          2  |   .0839404    .071436     1.18   0.240    -.0560715    .2239524
          3  |   .0002055   .0828337     0.00   0.998    -.1621455    .1625566
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.

 
margins female prog

Predictive margins                                Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      female |
          0  |   .1888093   .0345352     5.47   0.000     .1211215    .2564971
          1  |   .3344859   .0382269     8.75   0.000     .2595625    .4094092
             |
        prog |
          1  |   .2181091   .0562519     3.88   0.000     .1078574    .3283608
          2  |   .2949988   .0364807     8.09   0.000      .223498    .3664996
          3  |   .2183165   .0636938     3.43   0.001      .093479    .3431539
------------------------------------------------------------------------------


margins female, at(read=(30 50 70) prog=1) vsquish  // for prog equal 1
 
lAdjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
1._at        : read            =          30
               prog            =           1
2._at        : read            =          50
               prog            =           1
3._at        : read            =          70
               prog            =           1

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
  _at#female |
        1 0  |   .0046867   .0038796     1.21   0.227    -.0029172    .0122906
        1 1  |   .0137226    .009905     1.39   0.166    -.0056908     .033136
        2 0  |   .0658288   .0322765     2.04   0.041      .002568    .1290896
        2 1  |   .1723344   .0683062     2.52   0.012     .0384568    .3062121
        3 0  |    .513277     .14321     3.58   0.000     .2325905    .7939635
        3 1  |   .7570458   .1152195     6.57   0.000     .5312198    .9828718
------------------------------------------------------------------------------
 
margins female, at(read=(30 50 70) prog=2) vsquish  // for prog equal 2
 
Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
1._at        : read            =          30
               prog            =           2
2._at        : read            =          50
               prog            =           2
3._at        : read            =          70
               prog            =           2

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
  _at#female |
        1 0  |   .0081433   .0066016     1.23   0.217    -.0047956    .0210822
        1 1  |   .0236848   .0160775     1.47   0.141    -.0078265     .055196
        2 0  |   .1094218   .0412597     2.65   0.008     .0285542    .1902894
        2 1  |   .2663481   .0632006     4.21   0.000     .1424772     .390219
        3 0  |   .6477268   .0912298     7.10   0.000     .4689197    .8265339
        3 1  |   .8445516   .0579974    14.56   0.000     .7308788    .9582245
------------------------------------------------------------------------------
 
margins female, at(read=(30 50 70) prog=3) vsquish  // for prog equal 3

Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
1._at        : read            =          30
               prog            =           3
2._at        : read            =          50
               prog            =           3
3._at        : read            =          70
               prog            =           3

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
  _at#female |
        1 0  |   .0046944   .0038948     1.21   0.228    -.0029393     .012328
        1 1  |   .0137448   .0096791     1.42   0.156    -.0052258    .0327154
        2 0  |   .0659298   .0353531     1.86   0.062     -.003361    .1352205
        2 1  |   .1725686   .0720298     2.40   0.017     .0313927    .3137445
        3 0  |   .5136869    .164499     3.12   0.002     .1912747     .836099
        3 1  |   .7573474   .1257846     6.02   0.000     .5108141    1.003881
------------------------------------------------------------------------------
 
margins prog, at(read=(20(10)70) female==1) vsquish  // for females
 
Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(honors), predict()
1._at        : read            =          20
               female          =           1
2._at        : read            =          30
               female          =           1
3._at        : read            =          40
               female          =           1
4._at        : read            =          50
               female          =           1
5._at        : read            =          60
               female          =           1
6._at        : read            =          70
               female          =           1

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
    _at#prog |
        1 1  |   .0035837   .0033206     1.08   0.280    -.0029246    .0100921
        1 2  |   .0062319   .0057058     1.09   0.275    -.0049512    .0174151
        1 3  |   .0035896    .003226     1.11   0.266    -.0027332    .0099124
        2 1  |   .0137226    .009905     1.39   0.166    -.0056908     .033136
        2 2  |   .0236848   .0160775     1.47   0.141    -.0078265     .055196
        2 3  |   .0137448   .0096791     1.42   0.156    -.0052258    .0327154
        3 1  |    .051075   .0275788     1.85   0.064    -.0029784    .1051285
        3 2  |   .0857952   .0380499     2.25   0.024     .0112186    .1603717
        3 3  |   .0511546   .0276057     1.85   0.064    -.0029516    .1052608
        4 1  |   .1723344   .0683062     2.52   0.012     .0384568    .3062121
        4 2  |   .2663481   .0632006     4.21   0.000     .1424772     .390219
        4 3  |   .1725686   .0720298     2.40   0.017     .0313927    .3137445
        5 1  |   .4461322    .124094     3.60   0.000     .2029123     .689352
        5 2  |   .5841008   .0733682     7.96   0.000     .4403019    .7278998
        5 3  |   .4465376   .1359971     3.28   0.001     .1799882    .7130871
        6 1  |   .7570458   .1152195     6.57   0.000     .5312198    .9828718
        6 2  |   .8445516   .0579974    14.56   0.000     .7308788    .9582245
        6 3  |   .7573474   .1257846     6.02   0.000     .5108141    1.003881
------------------------------------------------------------------------------
 
marginsplot

Let's rerun our model.

logit honors read i.female i.prog

Iteration 0:   log likelihood = -115.64441  
Iteration 1:   log likelihood = -86.845312  
Iteration 2:   log likelihood = -84.560995  
Iteration 3:   log likelihood = -84.542357  
Iteration 4:   log likelihood = -84.542348  
Iteration 5:   log likelihood = -84.542348  

Logistic regression                               Number of obs   =        200
                                                  LR chi2(4)      =      62.20
                                                  Prob > chi2     =     0.0000
Log likelihood = -84.542348                       Pseudo R2       =     0.2689

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        read |   .1352861   .0242218     5.59   0.000     .0878123      .18276
    1.female |    1.08343   .4094357     2.65   0.008     .2809511    1.885909
             |
        prog |
          2  |   .5559416   .5053125     1.10   0.271    -.4344527    1.546336
          3  |   .0016408   .6611702     0.00   0.998    -1.294229     1.29751
             |
       _cons |   -9.41691   1.481922    -6.35   0.000    -12.32142   -6.512397
------------------------------------------------------------------------------

Next, we will demonstrate some alternative coding systems.

logit honors i.ses

Iteration 0:   log likelihood = -115.64441  
Iteration 1:   log likelihood = -108.69882  
Iteration 2:   log likelihood = -108.53558  
Iteration 3:   log likelihood = -108.53555  
Iteration 4:   log likelihood = -108.53555  

Logistic regression                               Number of obs   =        200
                                                  LR chi2(2)      =      14.22
                                                  Prob > chi2     =     0.0008
Log likelihood = -108.53555                       Pseudo R2       =     0.0615

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         ses |
          2  |  -.4112355   .4402784    -0.93   0.350    -1.274165    .4516944
          3  |   .9779843   .4340489     2.25   0.024     .1272642    1.828704
             |
       _cons |  -1.185624   .3445096    -3.44   0.001     -1.86085   -.5103972
------------------------------------------------------------------------------

contrast ses
 
Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
         ses |          2       13.94     0.0009
------------------------------------------------
 
contrast p.ses  // orthogonal polynomials

Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
         ses |
   (linear)  |          1        5.08     0.0242
(quadratic)  |          1        6.63     0.0100
      Joint  |          2       13.94     0.0009
------------------------------------------------

--------------------------------------------------------------
             |   Contrast   Std. Err.     [95% Conf. Interval]
-------------+------------------------------------------------
         ses |
   (linear)  |   .3992604   .1771997      .0519554    .7465655
(quadratic)  |   .4243714   .1648284      .1013136    .7474291
--------------------------------------------------------------

We continue with logistic regression models with multiple categorical predictors. These models are logistic equivalents of analysis of variance models. The next example is the logistic equivalent of a 3-by-2 factorial anova and is followed by the equivalent of a 3-by-2 factorial ancova.

logit honors i.prog##i.female

Iteration 0:   log likelihood = -115.64441  
Iteration 1:   log likelihood = -105.80227  
Iteration 2:   log likelihood = -105.03687  
Iteration 3:   log likelihood = -105.01869  
Iteration 4:   log likelihood = -105.01869  

Logistic regression                               Number of obs   =        200
                                                  LR chi2(5)      =      21.25
                                                  Prob > chi2     =     0.0007
Log likelihood = -105.01869                       Pseudo R2       =     0.0919

------------------------------------------------------------------------------
      honors |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        prog |
          2  |   1.493604   .8065652     1.85   0.064    -.0872346    3.074443
          3  |  -.8397523   1.264154    -0.66   0.507    -3.317448    1.637944
             |
    1.female |   .9162888   .8973641     1.02   0.307    -.8425125     2.67509
             |
 prog#female |
        2 1  |  -.4362348   .9866519    -0.44   0.658    -2.370037    1.497567
        3 1  |   .6931488   1.447816     0.48   0.632    -2.144518    3.530816
             |
       _cons |   -2.25129   .7433913    -3.03   0.002     -3.70831   -.7942696
------------------------------------------------------------------------------


contrast r.prog r.female r.prog#r.female  // reference group coding
 
Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------------
                   |         df        chi2     P>chi2
-------------------+----------------------------------
              prog |
         (2 vs 1)  |          1        6.68     0.0097
         (3 vs 1)  |          1        0.46     0.4957
            Joint  |          2       13.39     0.0012
                   |
            female |          1        3.99     0.0458
                   |
       prog#female |
(2 vs 1) (1 vs 0)  |          1        0.20     0.6584
(3 vs 1) (1 vs 0)  |          1        0.23     0.6321
            Joint  |          2        0.97     0.6150
------------------------------------------------------

--------------------------------------------------------------------
                   |   Contrast   Std. Err.     [95% Conf. Interval]
-------------------+------------------------------------------------
              prog |
         (2 vs 1)  |   1.275487   .4933259      .3085856    2.242388
         (3 vs 1)  |  -.4931779    .723908     -1.912011    .9256557
                   |
            female |
         (1 vs 0)  |   1.001927   .5015964      .0188158    1.985038
                   |
       prog#female |
(2 vs 1) (1 vs 0)  |  -.4362348   .9866519     -2.370037    1.497567
(3 vs 1) (1 vs 0)  |   .6931488   1.447816     -2.144518    3.530816
--------------------------------------------------------------------

logit hiwrite i.prog##i.female read

Iteration 0:   log likelihood = -131.79114  
Iteration 1:   log likelihood = -84.462377  
Iteration 2:   log likelihood = -82.994215  
Iteration 3:   log likelihood =   -82.9704  
Iteration 4:   log likelihood = -82.970367  
Iteration 5:   log likelihood = -82.970367  

Logistic regression                               Number of obs   =        200
                                                  LR chi2(6)      =      97.64
                                                  Prob > chi2     =     0.0000
Log likelihood = -82.970367                       Pseudo R2       =     0.3704

------------------------------------------------------------------------------
     hiwrite |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        prog |
          2  |   1.479148   .6851452     2.16   0.031     .1362886    2.822008
          3  |  -1.543036   .9866088    -1.56   0.118    -3.476754    .3906817
             |
    1.female |    2.56807   .8096941     3.17   0.002     .9810989    4.155041
             |
 prog#female |
        2 1  |   -1.83987   .9600168    -1.92   0.055    -3.721468    .0417288
        3 1  |     .85677   1.194374     0.72   0.473    -1.484161    3.197701
             |
        read |   .1637165   .0291826     5.61   0.000     .1065197    .2209134
       _cons |   -9.14064   1.699912    -5.38   0.000    -12.47241   -5.808874
------------------------------------------------------------------------------

contrast a.prog a.female r.prog#r.female  // reference group coding
 
Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------------
                   |         df        chi2     P>chi2
-------------------+----------------------------------
              prog |
         (2 vs 1)  |          1        1.44     0.2296
         (3 vs 1)  |          1        3.48     0.0620
            Joint  |          2        9.68     0.0079
                   |
            female |          1       20.72     0.0000
                   |
       prog#female |
(2 vs 1) (1 vs 0)  |          1        3.67     0.0553
(3 vs 1) (1 vs 0)  |          1        0.51     0.4732
            Joint  |          2        7.32     0.0258
------------------------------------------------------

--------------------------------------------------------------------
                   |   Contrast   Std. Err.     [95% Conf. Interval]
-------------------+------------------------------------------------
              prog |
         (2 vs 1)  |   .5592136   .4655076     -.3531646    1.471592
         (3 vs 1)  |  -1.114651   .5972122     -2.285166    .0558634
                   |
            female |
         (1 vs 0)  |    2.24037   .4921307      1.275812    3.204929
                   |
       prog#female |
(2 vs 1) (1 vs 0)  |   -1.83987   .9600168     -3.721468    .0417288
(3 vs 1) (1 vs 0)  |     .85677   1.194374     -1.484161    3.197701
--------------------------------------------------------------------

contrast a.prog a.female a.prog#g.female  // difference from adjacent group

Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------------
                   |         df        chi2     P>chi2
-------------------+----------------------------------
              prog |
         (1 vs 2)  |          1        1.44     0.2296
         (2 vs 3)  |          1        9.60     0.0019
            Joint  |          2        9.68     0.0079
                   |
            female |          1       20.72     0.0000
                   |
       prog#female |
(1 vs 2) (0 vs 1)  |          1        3.67     0.0553
(2 vs 3) (0 vs 1)  |          1        5.97     0.0146
            Joint  |          2        7.32     0.0258
------------------------------------------------------

--------------------------------------------------------------------
                   |   Contrast   Std. Err.     [95% Conf. Interval]
-------------------+------------------------------------------------
              prog |
         (1 vs 2)  |  -.5592136   .4655076     -1.471592    .3531646
         (2 vs 3)  |   1.673865   .5401886      .6151145    2.732615
                   |
            female |
         (0 vs 1)  |   -2.24037   .4921307     -3.204929   -1.275812
                   |
       prog#female |
(1 vs 2) (0 vs 1)  |   -1.83987   .9600168     -3.721468    .0417288
(2 vs 3) (0 vs 1)  |    2.69664   1.103841      .5331519    4.860127
--------------------------------------------------------------------

contrast g.prog g.female g.prog#g.female  // difference from grand mean; effect coding

Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------------------
                         |         df        chi2     P>chi2
-------------------------+----------------------------------
                    prog |
            (1 vs mean)  |          1        0.36     0.5480
            (2 vs mean)  |          1        7.55     0.0060
            (3 vs mean)  |          1        7.20     0.0073
                  Joint  |          2        9.68     0.0079
                         |
                  female |
            (0 vs mean)  |          1       20.72     0.0000
            (1 vs mean)  |          1       20.72     0.0000
                  Joint  |          1       20.72     0.0000
                         |
             prog#female |
(1 vs mean) (0 vs mean)  |          1        0.28     0.5981
(1 vs mean) (1 vs mean)  |          1        0.28     0.5981
(2 vs mean) (0 vs mean)  |          1        7.21     0.0072
(2 vs mean) (1 vs mean)  |          1        7.21     0.0072
(3 vs mean) (0 vs mean)  |          1        2.89     0.0891
(3 vs mean) (1 vs mean)  |          1        2.89     0.0891
                  Joint  |          2        7.32     0.0258
------------------------------------------------------------

--------------------------------------------------------------------------
                         |   Contrast   Std. Err.     [95% Conf. Interval]
-------------------------+------------------------------------------------
                    prog |
            (1 vs mean)  |   .1851458   .3082055     -.4189259    .7892176
            (2 vs mean)  |   .7443595    .270871      .2134621    1.275257
            (3 vs mean)  |  -.9295053   .3464478     -1.608531     -.25048
                         |
                  female |
            (0 vs mean)  |  -1.120185   .2460653     -1.602464   -.6379059
            (1 vs mean)  |   1.120185   .2460653      .6379059    1.602464
                         |
             prog#female |
(1 vs mean) (0 vs mean)  |  -.1638499   .3108171     -.7730402    .4453404
(1 vs mean) (1 vs mean)  |   .1638499   .3108171     -.4453404    .7730402
(2 vs mean) (0 vs mean)  |   .7560849   .2815464       .204264    1.307906
(2 vs mean) (1 vs mean)  |  -.7560849   .2815464     -1.307906    -.204264
(3 vs mean) (0 vs mean)  |  -.5922349   .3483436     -1.274976    .0905059
(3 vs mean) (1 vs mean)  |   .5922349   .3483436     -.0905059    1.274976
--------------------------------------------------------------------------
 
* plot interaction for three values of read (30 50 70) 
* beginning with read = 30

margins prog#female, at(read=30) vsquish

Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(hiwrite), predict()
at           : read            =          30

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
 prog#female |
        1 0  |   .0143558   .0129815     1.11   0.269    -.0110874    .0397989
        1 1  |   .1596179   .0889452     1.79   0.073    -.0147114    .3339472
        2 0  |   .0600869   .0414002     1.45   0.147    -.0210559    .1412298
        2 1  |   .1169335    .069781     1.68   0.094    -.0198347    .2537018
        3 0  |   .0031033    .003399     0.91   0.361    -.0035585    .0097651
        3 1  |   .0872775   .0496422     1.76   0.079    -.0100194    .1845744
-----------------------------------------------------------------------------

marginsplot



* for read = 50

margins prog#female, at(read=50) vsquish

Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(hiwrite), predict()
at           : read            =          50

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
 prog#female |
        1 0  |   .2779179   .1149279     2.42   0.016     .0526635    .5031724
        1 1  |   .8338631   .0734785    11.35   0.000     .6898478    .9778784
        2 0  |    .628161   .0912562     6.88   0.000     .4493021    .8070199
        2 1  |   .7777385   .0677238    11.48   0.000     .6450023    .9104747
        3 0  |   .0760091   .0583469     1.30   0.193    -.0383488     .190367
        3 1  |   .7164644   .0946828     7.57   0.000     .5308896    .9020392
------------------------------------------------------------------------------

marginsplot



* for read = 70

margins prog#female, at(read=70) vsquish

Adjusted predictions                              Number of obs   =        200
Model VCE    : OIM

Expression   : Pr(hiwrite), predict()
at           : read            =          70

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
 prog#female |
        1 0  |   .9104806   .0573769    15.87   0.000     .7980239    1.022937
        1 1  |   .9925168   .0066608   149.01   0.000      .979462    1.005572
        2 0  |   .9780902   .0143631    68.10   0.000     .9499391    1.006241
        2 1  |   .9893012   .0077172   128.19   0.000     .9741758    1.004427
        3 0  |   .6849213   .1994977     3.43   0.001     .2939129     1.07593
        3 1  |   .9852452   .0123986    79.46   0.000     .9609443    1.009546
------------------------------------------------------------------------------

marginsplot

Our last example shows how to do simple constrasts and simple main effects in a logistic regression model.

contrast female@prog, effects

Contrasts of marginal linear predictions

Margins      : asbalanced

------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
 female@prog |
          1  |          1       10.06     0.0015
          2  |          1        1.72     0.1899
          3  |          1       12.33     0.0004
      Joint  |          3       21.23     0.0001
------------------------------------------------

--------------------------------------------------------------------------------
               |   Contrast   Std. Err.      z    P>|z|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
   female@prog |
(1 vs base) 1  |    2.56807   .8096941     3.17   0.002     .9810989    4.155041
(1 vs base) 2  |   .7282006   .5554797     1.31   0.190    -.3605197    1.816921
(1 vs base) 3  |    3.42484   .9753415     3.51   0.000     1.513206    5.336474
--------------------------------------------------------------------------------

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.