UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Code Fragment
Stata Code Fragment: Graphing predicted probabilities with two interaction terms

This example uses the hsb2 data file to illustrate how to graph predicted probabilities against a predictor variable with two interaction terms.
use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear
gen hon = (write>60)
xi: logit hon i.female*math i.ses*math
. xi: logit hon i.female*math i.ses*math, nolog

Logistic regression                               Number of obs   =        200
                                                  LR chi2(7)      =      67.89
                                                  Prob > chi2     =     0.0000
Log likelihood = -77.408332                       Pseudo R2       =     0.3049

------------------------------------------------------------------------------
         hon |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
  _Ifemale_1 |  -1.893752   3.340903    -0.57   0.571    -8.441802    4.654298
        math |   .1195186   .0702875     1.70   0.089    -.0182424    .2572797
_IfemXmath_1 |   .0493777   .0582462     0.85   0.397    -.0647827    .1635382
     _Ises_2 |  -.8678234   3.984084    -0.22   0.828    -8.676484    6.940837
     _Ises_3 |  -1.761901   4.431971    -0.40   0.691    -10.44841    6.924603
_IsesXmath_2 |   .0077363   .0718919     0.11   0.914    -.1331693    .1486419
_IsesXmath_3 |   .0397258   .0785011     0.51   0.613    -.1141335    .1935851
       _cons |    -8.2049   3.886223    -2.11   0.035    -15.82176   -.5880425
------------------------------------------------------------------------------
preserve
*for female = 0
gen yhat1 = _b[_cons] + math*_b[math] /* ses ==1 */
gen p1 = exp(yhat1)/(1+exp(yhat1))

gen yhat2 = _b[_cons] + math*_b[math] + _b[_Ises_2] + math*_b[_IsesXmath_2] /* ses ==2 */
gen p2 = exp(yhat2)/(1+exp(yhat2))

gen yhat3 = _b[_cons] + math*_b[math] + _b[_Ises_3] + math*_b[_IsesXmath_3] /* ses ==3 */
gen p3 = exp(yhat3)/(1+exp(yhat3))

sort math
twoway (line p1 math) (line p2 math) (line p3 math), name(female1, replace)


restore 
preserve 
*for female = 1
gen yhat1 = _b[_cons] + math*_b[math] +_b[_Ifemale_1] /* ses ==1 */
gen p1 = exp(yhat1)/(1+exp(yhat1))

gen yhat2 = _b[_cons] + math*_b[math] + _b[_Ises_2] + math*_b[_IsesXmath_2] ///
        + _b[_Ifemale_1] + math*_b[_IfemXmath_1] /* ses ==2 */
gen p2 = exp(yhat2)/(1+exp(yhat2))

gen yhat3 = _b[_cons] + math*_b[math] + _b[_Ises_3] + math*_b[_IsesXmath_3] ///
         + _b[_Ifemale_1] + math*_b[_IfemXmath_1] /* ses ==3 */
gen p3 = exp(yhat3)/(1+exp(yhat3))

sort math
twoway (line p1 math) (line p2 math) (line p3 math) , name(female2, replace)



restore

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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