-------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Q:\C-modelling\runmlwin\website\logfiles\2024-10-11\18\9_Logistic_
> Models_for_Binary_and_Binomial_Responses.smcl
  log type:  smcl
 opened on:  11 Oct 2024, 16:41:15

. *****************************************************************************
> ***
. *     MLwiN User Manual 
. *
. * 9   Logistic Models for Binary and Binomial Responses                      
> 117
. *
. *     Rasbash, J., Steele, F., Browne, W. J. and Goldstein, H. (2012). A User
> 's 
. *     Guide to MLwiN, v2.26. Centre for Multilevel Modelling, University of 
. *     Bristol.
. *****************************************************************************
> ***
. *     Stata do-file to replicate all analyses using runmlwin
. *
. *     George Leckie and Chris Charlton, 
. *     Centre for Multilevel Modelling, 2012
. *     https://www.bristol.ac.uk/cmm/software/runmlwin/
. *****************************************************************************
> ***
. 
. * 9.1 Introduction and description of the example data . . . . . . . . . . . 
> 117
. 
. use "https://www.bristol.ac.uk/cmm/media/runmlwin/bang.dta", clear

. 
. describe

Contains data from https://www.bristol.ac.uk/cmm/media/runmlwin/bang.dta
 Observations:         2,867                  
    Variables:            12                  12 Mar 2014 22:22
-------------------------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
-------------------------------------------------------------------------------
woman           float   %9.0g                 Identifying code for each woman
                                                (level 1 unit)
district        float   %9.0g                 Identifying code for each
                                                district (level 2 unit)
use             float   %9.0g                 Contraceptive use status at a
                                                time of survey (1 = using
                                                contraception, 2 = not u
use4            float   %9.0g      use4       Contraceptive use status and
                                                method (1 = Sterilization, 2 =
                                                Modern reversible me
lc              float   %9.0g      lc         Number of living children at time
                                                of survey (0 = None, 1 = 1
                                                child, 2 = 2 childr
age             float   %9.0g                 Age of woman at time of survey
                                                (in years), centred on the
                                                sample mean of 30 year
urban           float   %9.0g                 Type of region of residence (1 =
                                                Urban, 0 = Rural)
educ            float   %9.0g      educ       Womans level of education (1 =
                                                None, 2 = Lower primary, 3 =
                                                Upper primary, 4 = S
hindu           float   %9.0g                 Womans religion (1 = Hindu, 0 =
                                                Muslim)
d_lit           float   %9.0g                 Proportion of women in district
                                                who are literate
d_pray          float   %9.0g                 Proportion of Muslim women in
                                                district who pray every day (a
                                                measure of religios
cons            float   %9.0g                 constant vector
-------------------------------------------------------------------------------
Sorted by: 

. 
. 
. * 9.2 Single-level logistic regression . . . . . . . . . . . . . . . . . . . 
> 119
. 
. * Link functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
> 119
. 
. * Interpretation of coeficients . . . . . . . . . . . . . . . . . . . . . . .
> 120
. 
. * Fitting a single-level logit model in MLwiN . . . . . . . . . . . . . . . .
> 121
. 
. tabulate lc use, row

+----------------+
| Key            |
|----------------|
|   frequency    |
| row percentage |
+----------------+

 Number of |
    living |
  children |
at time of |   Contraceptive use
 survey (0 |  status at a time of
 = None, 1 |   survey (1 = using
= 1 child, |  contraception, 2 =
     2 = 2 |         not u
    childr |         0          1 |     Total
-----------+----------------------+----------
       lc0 |       584        190 |       774 
           |     75.45      24.55 |    100.00 
-----------+----------------------+----------
       lc1 |       283        234 |       517 
           |     54.74      45.26 |    100.00 
-----------+----------------------+----------
       lc2 |       234        227 |       461 
           |     50.76      49.24 |    100.00 
-----------+----------------------+----------
   lc3plus |       627        488 |     1,115 
           |     56.23      43.77 |    100.00 
-----------+----------------------+----------
     Total |     1,728      1,139 |     2,867 
           |     60.27      39.73 |    100.00 

. 
. generate lc1 = (lc==1)

. 
. generate lc2 = (lc==2)

. 
. generate lc3plus = (lc==3)

. 
. runmlwin use cons lc1 lc2 lc3plus, level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons)) nopaus
> e
 
MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, MQL1

Run time (seconds)   =       3.19
Number of iterations =          4
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -1.122876   .0834809  -13.45    0.000    -1.286496   -.9592566
         lc1 |   .9327503    .121557    7.67    0.000      .694503    1.170998
         lc2 |   1.092505   .1250913    8.73    0.000     .8473305    1.337679
     lc3plus |   .8722449     .10302    8.47    0.000     .6703295     1.07416
------------------------------------------------------------------------------


.         
. test [FP1]lc1 = [FP1]lc2

 ( 1)  [FP1]lc1 - [FP1]lc2 = 0

           chi2(  1) =    1.55
         Prob > chi2 =    0.2134

. 
. 
. 
. * A probit model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
> 126
. 
. runmlwin use cons lc1 lc2 lc3plus, level1(woman) ///
>         discrete(distribution(binomial) link(probit) denominator(cons)) nopau
> se
 
MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial probit response model (hierarchical)
Estimation algorithm: IGLS, MQL1

Run time (seconds)   =       3.04
Number of iterations =          4
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |   -.688789   .0491538  -14.01    0.000    -.7851287   -.5924492
         lc1 |   .5697222   .0739598    7.70    0.000     .4247637    .7146807
         lc2 |    .669757   .0763146    8.78    0.000     .5201832    .8193308
     lc3plus |    .531905   .0619477    8.59    0.000     .4104897    .6533203
------------------------------------------------------------------------------


. 
. runmlwin use cons lc1 lc2 lc3plus age, level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons)) nopaus
> e
 
MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, MQL1

Run time (seconds)   =       3.13
Number of iterations =          4
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -1.255983   .0977567  -12.85    0.000    -1.447583   -1.064383
         lc1 |   .9913076   .1237629    8.01    0.000     .7487368    1.233878
         lc2 |   1.223558   .1348033    9.08    0.000     .9593484    1.487768
     lc3plus |   1.116551   .1382418    8.08    0.000      .845602      1.3875
         age |  -.0162877   .0060931   -2.67    0.008    -.0282299   -.0043456
------------------------------------------------------------------------------


. 
. 
. 
. * 9.3 A two-level random intercept model . . . . . . . . . . . . . . . . . . 
> 128
. 
. * Model specification . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> 128
. 
. * Estimation procedures . . . . . . . . . . . . . . . . . . . . . . . . . . .
> 128
. 
. * Fitting a two-level random intercept model in MLwiN . . . . . . . . . . . .
> 129
. 
. runmlwin use cons lc1 lc2 lc3plus age, ///
>         level2(district: cons) level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons)) nopaus
> e
 
MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, MQL1

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          3       47.8        173
-----------------------------------------------------------

Run time (seconds)   =       2.95
Number of iterations =          5
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -1.367111   .1233779  -11.08    0.000    -1.608927   -1.125295
         lc1 |   .9899751   .1264296    7.83    0.000     .7421777    1.237773
         lc2 |   1.275233   .1381648    9.23    0.000     1.004435    1.546031
     lc3plus |   1.215676   .1424506    8.53    0.000     .9364783    1.494874
         age |  -.0187756   .0062489   -3.00    0.003    -.0310232   -.0065281
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .2740886   .0713836      .1341793    .4139978
------------------------------------------------------------------------------

. 
. runmlwin use cons lc1 lc2 lc3plus age, ///
>         level2(district: cons) level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons) pql2) /
> //
>         initsprevious ///
>         nopause
 
Model fitted using initial values specified as parameter estimates from previou
> s model

MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          3       47.8        173
-----------------------------------------------------------

Run time (seconds)   =       3.16
Number of iterations =          5
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -1.466065   .1279216  -11.46    0.000    -1.716787   -1.215343
         lc1 |   1.062866   .1288259    8.25    0.000     .8103723    1.315361
         lc2 |   1.370127   .1416822    9.67    0.000     1.092435    1.647819
     lc3plus |   1.303942   .1459562    8.93    0.000     1.017873    1.590011
         age |  -.0200488   .0064053   -3.13    0.002    -.0326029   -.0074947
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .3078618   .0791322      .1527655    .4629581
------------------------------------------------------------------------------

. 
. test [RP2]var(cons)=0

 ( 1)  [RP2]var(cons) = 0

           chi2(  1) =   15.14
         Prob > chi2 =    0.0001

. 
. 
. 
. * Variance partition coeficient . . . . . . . . . . . . . . . . . . . . . . .
> 131
. 
. preserve

. 
.         set obs 5000
Number of observations (_N) was 2,867, now 5,000.

. 
.         set seed 12345

. 
.         generate u = sqrt([RP2]var(cons))*invnormal(uniform())

. 
.         generate p1 = invlogit(_b[cons] + u)

. 
.         generate p2 = invlogit(_b[cons] + _b[lc3plus] + _b[age]*-9.7 + u)

. 
.         generate p3 = invlogit(_b[cons] + _b[age]*15.3 + u)

. 
.         forvalues p = 1/3 {
  2. 
.                 generate v`p' = p`p'*(1 - p`p')
  3. 
.                 quietly summarize p`p'
  4. 
.                 scalar lev2var`p' = r(sd)^2
  5. 
.                 quietly summarize v`p'
  6. 
.                 scalar lev1var`p' =  r(mean)
  7. 
.         }

. 
.         display "VPC = " lev2var1/(lev2var1 + lev1var1)
VPC = .04701588

. 
.         display "VPC for a young women with 3+ children (low probability use)
>  = " lev2var2/(lev2var2 + lev1var2)
VPC for a young women with 3+ children (low probability use) = .06713939

. 
.         display "VPC for an old woman with no children (high probability use)
>  = " lev2var3/(lev2var3 + lev1var3)
VPC for an old woman with no children (high probability use) = .04011108

. 
. restore

. 
. 
. 
. * Adding further explanatory variables . . . . . . . . . . . . . . . . . . . 
> 134
. 
. tabulate educ

     Womans |
   level of |
  education |
 (1 = None, |
  2 = Lower |
 primary, 3 |
    = Upper |
 primary, 4 |
        = S |      Freq.     Percent        Cum.
------------+-----------------------------------
    ed_none |      1,806       62.99       62.99
   ed_lprim |        357       12.45       75.44
   ed_uprim |        265        9.24       84.69
 ed_secplus |        439       15.31      100.00
------------+-----------------------------------
      Total |      2,867      100.00

. 
. generate ed_lprim = (educ==2)

. 
. generate ed_uprim = (educ==3)

. 
. generate ed_secplus = (educ==4)

. 
. runmlwin use cons lc1 lc2 lc3plus age urban ///
>         ed_lprim ed_uprim ed_secplus hindu, ///
>         level2(district: cons) level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons) pql2) /
> //
>         initsprevious ///
>         nopause
 
Model fitted using initial values specified as parameter estimates from previou
> s model

MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          3       47.8        173
-----------------------------------------------------------

Run time (seconds)   =       3.41
Number of iterations =          5
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -2.052701   .1382067  -14.85    0.000    -2.323581   -1.781821
         lc1 |   1.151532   .1341357    8.58    0.000     .8886307    1.414433
         lc2 |   1.512435   .1473516   10.26    0.000     1.223631    1.801239
     lc3plus |   1.502071   .1527244    9.84    0.000     1.202737    1.801405
         age |  -.0173581    .006648   -2.61    0.009     -.030388   -.0043282
       urban |   .5331155   .1048243    5.09    0.000     .3276636    .7385674
    ed_lprim |   .2465644   .1283691    1.92    0.055    -.0050344    .4981633
    ed_uprim |    .724403   .1438131    5.04    0.000     .4425344    1.006271
  ed_secplus |   1.170312   .1271726    9.20    0.000     .9210582    1.419565
       hindu |   .4328669   .1276563    3.39    0.001     .1826652    .6830686
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .2336909   .0656384      .1050419    .3623398
------------------------------------------------------------------------------

. 
. 
. 
. * 9.4 A two-level random coeficient model . . . . . . . . . . . . . . . . . .
> 135
. 
. runmlwin use cons lc1 lc2 lc3plus age urban ///
>         ed_lprim ed_uprim ed_secplus hindu, ///
>         level2(district: cons urban) level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons) pql2) /
> //
>         initsprevious ///
>         nopause
 
Model fitted using initial values specified as parameter estimates from previou
> s model

MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          3       47.8        173
-----------------------------------------------------------

Run time (seconds)   =       3.81
Number of iterations =          6
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -2.093553   .1482139  -14.13    0.000    -2.384047   -1.803059
         lc1 |    1.16591   .1348819    8.64    0.000     .9015459    1.430273
         lc2 |   1.526202   .1483987   10.28    0.000     1.235346    1.817058
     lc3plus |   1.522709    .154066    9.88    0.000     1.220745    1.824673
         age |  -.0181749   .0066994   -2.71    0.007    -.0313055   -.0050444
       urban |   .5741606   .1363773    4.21    0.000     .3068661    .8414551
    ed_lprim |   .2451684   .1295018    1.89    0.058    -.0086505    .4989873
    ed_uprim |   .7332251   .1453219    5.05    0.000     .4483995    1.018051
  ed_secplus |   1.179629    .128384    9.19    0.000     .9280011    1.431257
       hindu |   .5094263   .1329043    3.83    0.000     .2489387    .7699139
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .3596003   .0985204      .1665039    .5526967
             cov(cons,urban) |  -.2575624   .1112809     -.4756689   -.0394559
                  var(urban) |   .3479358     .17282      .0092148    .6866568
------------------------------------------------------------------------------

. 
. test ([RP2]cov(cons\urban)=0) ([RP2]var(urban)=0), mtest

 ( 1)  [RP2]cov(cons\urban) = 0
 ( 2)  [RP2]var(urban) = 0

--------------------------------------
       |        chi2     df   p > chi2
-------+------------------------------
   (1) |        5.36      1     0.0206*
   (2) |        4.05      1     0.0441*
-------+------------------------------
   All |        5.47      2     0.0648
--------------------------------------
* Unadjusted p-values

. 
. runmlwin use cons lc1 lc2 lc3plus age urban ///
>         ed_lprim ed_uprim ed_secplus hindu d_lit d_pray, ///
>         level2(district: cons urban) level1(woman) ///
>         discrete(distribution(binomial) link(logit) denominator(cons) pql2) /
> //
>         initsprevious ///
>         nopause
 
Model fitted using initial values specified as parameter estimates from previou
> s model

MLwiN 3.13 multilevel model                     Number of obs      =      2867
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          3       47.8        173
-----------------------------------------------------------

Run time (seconds)   =       3.92
Number of iterations =          6
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -1.722902   .2632283   -6.55    0.000     -2.23882   -1.206984
         lc1 |   1.170099   .1350131    8.67    0.000     .9054781     1.43472
         lc2 |   1.533891   .1485973   10.32    0.000     1.242646    1.825136
     lc3plus |   1.528153   .1542348    9.91    0.000     1.225858    1.830448
         age |  -.0181398   .0066973   -2.71    0.007    -.0312663   -.0050133
       urban |    .528236   .1380133    3.83    0.000     .2577349    .7987371
    ed_lprim |   .2376898   .1299372    1.83    0.067    -.0169824     .492362
    ed_uprim |    .742241   .1455787    5.10    0.000      .456912     1.02757
  ed_secplus |   1.195861   .1289305    9.28    0.000     .9431618     1.44856
       hindu |   .5093639   .1324719    3.85    0.000     .2497238     .769004
       d_lit |   2.074494   1.705624    1.22    0.224    -1.268468    5.417455
      d_pray |  -1.407985   .5337808   -2.64    0.008    -2.454176   -.3617942
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .3047796   .0879458       .132409    .4771502
             cov(cons,urban) |   -.232913   .1050934     -.4388924   -.0269337
                  var(urban) |   .3502613   .1731283       .010936    .6895866
------------------------------------------------------------------------------

. 
. 
. 
. * 9.5 Modelling binomial data . . . . . . . . . . . . . . . . . . . . . . . .
> 139
. 
. * Modelling district-level variation with district-level proportions . . . . 
> 139
. 
. * Creating a district-level data set . . . . . . . . . . . . . . . . . . . . 
> 140
. 
. collapse (mean) use cons (sum) denom = cons, by(district d_lit d_pray)

. 
. 
. 
. * Fitting the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> 142
. 
. runmlwin use cons d_lit d_pray, ///
>         level2(district: cons) level1(district) ///
>         discrete(distribution(binomial) link(logit) denominator(denom)) nopau
> se
 
MLwiN 3.13 multilevel model                     Number of obs      =        60
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, MQL1

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          1        1.0          1
-----------------------------------------------------------

Run time (seconds)   =       2.78
Number of iterations =          4
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -.3979703   .2339486   -1.70    0.089    -.8565012    .0605606
       d_lit |   3.759683   1.617902    2.32    0.020     .5886535    6.930713
      d_pray |  -1.195713   .5010576   -2.39    0.017    -2.177768   -.2136579
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .2106125   .0589008       .095169     .326056
------------------------------------------------------------------------------

. 
. runmlwin use cons d_lit d_pray, ///
>         level2(district: cons) level1(district) ///
>         discrete(distribution(binomial) link(logit) denominator(denom) pql2) 
> ///
>         initsprevious ///
>         nopause
 
Model fitted using initial values specified as parameter estimates from previou
> s model

MLwiN 3.13 multilevel model                     Number of obs      =        60
Binomial logit response model (hierarchical)
Estimation algorithm: IGLS, PQL2

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       district |       60          1        1.0          1
-----------------------------------------------------------

Run time (seconds)   =       3.01
Number of iterations =          6
------------------------------------------------------------------------------
         use |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |  -.4266337   .2406829   -1.77    0.076    -.8983636    .0450961
       d_lit |   3.997347   1.687761    2.37    0.018     .6893952    7.305298
      d_pray |  -1.250717   .5219554   -2.40    0.017    -2.273731   -.2277034
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 2: district            |
                   var(cons) |   .2251485    .062199      .1032407    .3470563
------------------------------------------------------------------------------

. // Note: The screenshot in the manual incorrectly dispalys the PQL1 estimates
. // when it should show the PQL2 estimates.
. 
. 
.         
. * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . . . .
> 143
. 
. 
. 
. ****************************************************************************
. exit

end of do-file