UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Manually generate predicted probabilities from a multinomial logistic regression in Stata

Occasionally, there might be a need for generating the predicted probabilities manually from a multinomial logistic regression. The code generates the predicted probabilities using a little bit of matrix calculation.

use http://www.ats.ucla.edu/stat/data/stata/mlogit_demo_data, clear
mlogit y write female math, nolog

Multinomial logistic regression                   Number of obs   =        200
                                                  LR chi2(9)      =      31.76
                                                  Prob > chi2     =     0.0002
Log likelihood = -159.59108                       Pseudo R2       =     0.0905

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
1            |
       write |  -.0592852   .0313425    -1.89   0.059    -.1207155     .002145
      female |   .0253633   .5020617     0.05   0.960    -.9586595    1.009386
        math |  -.0525018   .0349184    -1.50   0.133    -.1209407    .0159371
       _cons |   3.818145   1.534455     2.49   0.013     .8106693    6.825621
-------------+----------------------------------------------------------------
2            |
       write |   .0339394   .0554246     0.61   0.540    -.0746909    .1425697
      female |   .7612508   .7302687     1.04   0.297    -.6700495    2.192551
        math |   .0238891   .0448113     0.53   0.594    -.0639394    .1117177
       _cons |  -6.297374     2.5963    -2.43   0.015    -11.38603    -1.20872
-------------+----------------------------------------------------------------
3            |
       write |  -.0414901   .0347647    -1.19   0.233    -.1096276    .0266475
      female |   .6969475   .5588378     1.25   0.212    -.3983546     1.79225
        math |  -.0748394   .0388773    -1.93   0.054    -.1510375    .0013586
       _cons |   3.471735   1.698489     2.04   0.041     .1427571    6.800713
------------------------------------------------------------------------------
(y==4 is the base outcome)

predict p1 p2 p3 p4, p

list in 1/4

     +-----------------------------------------------------------------------+
     | y   female   math   write         p1         p2         p3         p4 |
     |-----------------------------------------------------------------------|
  1. | 4     male     41      52   .1678438   .0198305   .1198363   .6924894 |
  2. | 4   female     53      59   .0674177   .0798579    .081622   .7711025 |
  3. | 4     male     54      33   .2449829   .0132924   .0932966   .6484281 |
  4. | 4     male     47      44   .1942302    .017215    .105188   .6833668 |
     +-----------------------------------------------------------------------+

mat a = e(b)
mat list a

a[1,12]
             1:          1:          1:          1:          2:          2:          2:          2:
         write      female        math       _cons       write      female        math       _cons
y1  -.05928522   .02536326  -.05250177   3.8181451   .03393942   .76125081   .02388913  -6.2973737

             3:          3:          3:          3:
         write      female        math       _cons
y1  -.04149009   .69694747  -.07483944    3.471735
 
preserve 
clear
input y female math cons
52 0 41 1
59 1 53 1
end
* converting everything to matrices
mkmat y female math cons, mat(X)

mat a1 = a[1, "1:"]
mat a2 = a[1, "2:"]
mat a3 = a[1, "3:"]
mat biga = (a1 \ a2 \a3)'

mat list biga

biga[4,3]
                  y1          y1          y1
 1:write  -.05928522   .03393942  -.04149009
1:female   .02536326   .76125081   .69694747
  1:math  -.05250177   .02388913  -.07483944
 1:_cons   3.8181451  -6.2973737    3.471735

mat xb = X*biga
mat list xb

xb[2,3]
            y1          y1          y1
r1  -1.4172591  -3.5530693  -1.7541665
r2  -2.4369136   -2.267573  -2.2457229
* converting back to data
svmat xb

gen denom = 1+exp(xb1) + exp(xb2) + exp(xb3)
gen p1 = exp(xb1)/denom
gen p2 = exp(xb2)/denom
gen p3 = exp(xb3)/denom
 
list p1 p2 p3

     +--------------------------------+
     |       p1         p2         p3 |
     |--------------------------------|
  1. | .1678438   .0198305   .1198363 |
  2. | .0674177   .0798579    .081622 |
     +--------------------------------+
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.