-------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Q:\C-modelling\runmlwin\website\logfiles\2024-10-11\18\7.4.smcl
  log type:  smcl
 opened on:  11 Oct 2024, 18:41:07

. ****************************************************************************
. * Module 7: Multilevel Models for Binary Responses Stata Practicals
. *
. *     P7.4: Predicted Probabilities from a Multilevel Model
. *
. *           George Leckie
. *           Centre for Multilevel Modelling, 2010
. ****************************************************************************
. *     Stata do-file to replicate all analyses using runmlwin
. *
. *     George Leckie
. *     Centre for Multilevel Modelling, 2013
. *     http://www.bristol.ac.uk/cmm/software/runmlwin/
. ****************************************************************************
. 
. use "http://www.bristol.ac.uk/cmm/media/runmlwin/7.4.dta", clear

. 
. runmlwin antemed cons magec magecsq meduc2 meduc3 wealthc, ///
>         level2(comm: cons) ///
>         level1(womid:) ///
>         discrete(distribution(binomial) link(logit) denominator(cons) pql2) /
> //
>         nopause
 
MLwiN 3.13 multilevel model                     Number of obs      =      5366
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
           comm |      361          3       14.9         25
-----------------------------------------------------------

Run time (seconds)   =       3.44
Number of iterations =          5
------------------------------------------------------------------------------
     antemed |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -.4546834   .0827282   -5.50    0.000    -.6168277   -.2925392
       magec |  -.0003881    .006554   -0.06    0.953    -.0132337    .0124574
     magecsq |  -.0010068   .0006828   -1.47    0.140     -.002345    .0003314
      meduc2 |   .5478987   .0845976    6.48    0.000     .3820905    .7137069
      meduc3 |   1.309241   .0977836   13.39    0.000     1.117589    1.500893
     wealthc |   .3977514    .029586   13.44    0.000     .3397638    .4557389
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: comm                |
                   var(cons) |   .8538518   .0935791      .6704402    1.037263
------------------------------------------------------------------------------

. 
. replace magec = 0
(5,366 real changes made)

. 
. replace magecsq = 0
(5,366 real changes made)

.         
. predict predxb

. 
. generate medianpredprob = invlogit(predxb)

. 
. egen pickone = tag(wealth meduc)

. 
. sort wealth meduc

. 
. list wealth meduc medianpredprob if pickone==1, abbreviate(14) sepby(wealth)

      +---------------------------------+
      | wealth   meduc   medianpredprob |
      |---------------------------------|
 543. |      1       1         .2221039 |
 973. |      1       2         .3305831 |
1135. |      1       3         .5139447 |
      |---------------------------------|
1491. |      2       1         .2982393 |
1685. |      2       2         .4236524 |
2121. |      2       3         .6114817 |
      |---------------------------------|
2221. |      3       1         .3874735 |
2689. |      3       2         .5224733 |
2964. |      3       3          .700839 |
      |---------------------------------|
3253. |      4       1         .4849561 |
3613. |      4       2         .6195657 |
3939. |      4       3          .777135 |
      |---------------------------------|
4186. |      5       1         .5835972 |
4514. |      5       2         .7079517 |
4845. |      5       3         .8384579 |
      +---------------------------------+

. 
. generate medianpredlogit = logit(medianpredprob)

. 
. keep if pickone==1
(5,351 observations deleted)

. 
. keep wealth meduc medianpredprob medianpredlogit

. 
. list, abbreviate(15) sepby(wealth)

     +---------------------------------------------------+
     | meduc   wealth   medianpredprob   medianpredlogit |
     |---------------------------------------------------|
  1. |     1        1         .2221039         -1.253448 |
  2. |     2        1         .3305831         -.7055489 |
  3. |     3        1         .5139447          .0557934 |
     |---------------------------------------------------|
  4. |     1        2         .2982393         -.8556963 |
  5. |     2        2         .4236524         -.3077976 |
  6. |     3        2         .6114817          .4535449 |
     |---------------------------------------------------|
  7. |     1        3         .3874735         -.4579449 |
  8. |     2        3         .5224733          .0899537 |
  9. |     3        3          .700839          .8512962 |
     |---------------------------------------------------|
 10. |     1        4         .4849561         -.0601936 |
 11. |     2        4         .6195657          .4877051 |
 12. |     3        4          .777135          1.249048 |
     |---------------------------------------------------|
 13. |     1        5         .5835972          .3375579 |
 14. |     2        5         .7079517          .8854565 |
 15. |     3        5         .8384579          1.646799 |
     +---------------------------------------------------+

. 
. expand 1000
(14,985 observations created)

. 
. generate u = rnormal(0,sqrt(0.889))

. 
. generate meanpredprob = invlogit(medianpredlogit + u)

. 
. collapse (mean) meanpredprob, by(wealth meduc medianpredprob)

. 
. list wealth meduc medianpredprob meanpredprob, abbreviate(14) sepby(wealth)

     +------------------------------------------------+
     | wealth   meduc   medianpredprob   meanpredprob |
     |------------------------------------------------|
  1. |      1       1         .2221039       .2607134 |
  2. |      1       2         .3305831        .351727 |
  3. |      1       3         .5139447       .5015319 |
     |------------------------------------------------|
  4. |      2       1         .2982393       .3314112 |
  5. |      2       2         .4236524       .4415864 |
  6. |      2       3         .6114817       .5943345 |
     |------------------------------------------------|
  7. |      3       1         .3874735       .4119753 |
  8. |      3       2         .5224733       .5235564 |
  9. |      3       3          .700839       .6627779 |
     |------------------------------------------------|
 10. |      4       1         .4849561        .497875 |
 11. |      4       2         .6195657         .59526 |
 12. |      4       3          .777135       .7437027 |
     |------------------------------------------------|
 13. |      5       1         .5835972       .5741999 |
 14. |      5       2         .7079517       .6912331 |
 15. |      5       3         .8384579       .8028967 |
     +------------------------------------------------+

. 
end of do-file