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

. ****************************************************************************
. * Module 11: Three and Higher-Level Models - Stata Practical
. *
. *     P11.2: A Three-Level Model of THKS
. *
. *                George Leckie
. *            Centre for Multilevel Modelling, 2011
. ****************************************************************************
. *     Stata do-file to replicate all analyses using runmlwin
. *
. *     George Leckie
. *     Centre for Multilevel Modelling, 2013
. *     http://www.bristol.ac.uk/cmm/software/runmlwin/
. ****************************************************************************
. 
. * P11.2.1 Specifying and fitting the three-level model
. 
. use "http://www.bristol.ac.uk/cmm/media/runmlwin/11.2.dta", clear

. 
. runmlwin postthks cons, ///
>         level3(schoolid: cons, residuals(v)) ///
>         level2(classid: cons, residuals(u)) ///
>         level1(studentid: cons) ///
>         nopause
 
MLwiN 3.13 multilevel model                     Number of obs      =      1600
Normal response model (hierarchical)
Estimation algorithm: IGLS

-----------------------------------------------------------
                |   No. of       Observations per Group
 Level Variable |   Groups    Minimum    Average    Maximum
----------------+------------------------------------------
       schoolid |       28         18       57.1        137
        classid |      135          1       11.9         28
-----------------------------------------------------------

Run time (seconds)   =       3.21
Number of iterations =          4
Log likelihood       = -2750.9908
Deviance             =  5501.9816
------------------------------------------------------------------------------
    postthks |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        cons |   2.663487   .0781224   34.09    0.000      2.51037    2.816604
------------------------------------------------------------------------------

------------------------------------------------------------------------------
   Random-effects Parameters |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
Level 3: schoolid            |
                   var(cons) |   .1103113   .0457034      .0207343    .1998884
-----------------------------+------------------------------------------------
Level 2: classid             |
                   var(cons) |   .0848017   .0327901      .0205343     .149069
-----------------------------+------------------------------------------------
Level 1: studentid           |
                   var(cons) |   1.723674   .0634102      1.599392    1.847955
------------------------------------------------------------------------------

. 
. estimates store model1

. 
. 
. 
. * P11.2.2 Interpretation of the three-level mode;
. 
. 
. 
. * P11.2.3 Calculating variance partition coefficients (VPCs) and intraclass
. *         correlations (ICCs)
. 
. 
. 
. * P11.2.4 Predicting and examining school and classroom effects
. 
. egen v0rank = rank(v0) if pickone_school==1
(1,572 missing values generated)

. 
. egen u0rank = rank(u0) if pickone_class==1
(1,465 missing values generated)

. 
. sort v0rank

. 
. list schoolid v0 v0se v0rank if pickone_school==1, noobs

  +------------------------------------------+
  | schoolid          v0       v0se   v0rank |
  |------------------------------------------|
  |      506    -.536879   .1732999        1 |
  |      513   -.4589806   .2171219        2 |
  |      515   -.3405864   .1453007        3 |
  |      507   -.3385126    .177263        4 |
  |      410   -.3382328   .2159964        5 |
  |------------------------------------------|
  |      199    -.334378   .1931299        6 |
  |      194   -.1455744   .1792061        7 |
  |      409   -.1397455   .1847486        8 |
  |      197   -.1182113   .2132904        9 |
  |      405   -.1078893     .20518       10 |
  |------------------------------------------|
  |      402   -.0978065   .2295873       11 |
  |      505   -.0724921   .1751819       12 |
  |      198    -.071732     .20518       13 |
  |      193   -.0689895   .2546527       14 |
  |      412    .0325697   .2094178       15 |
  |------------------------------------------|
  |      509    .0463831   .1597266       16 |
  |      404    .0669768   .2321721       17 |
  |      408    .0866747   .2293949       18 |
  |      407    .1607906    .198829       19 |
  |      514    .2152646   .1667683       20 |
  |------------------------------------------|
  |      414      .25143   .2077815       21 |
  |      411    .2679052   .2506294       22 |
  |      401    .2747781   .2246474       23 |
  |      196    .2877176    .233327       24 |
  |      510    .3139966   .1616469       25 |
  |------------------------------------------|
  |      508    .3199214   .1837873       26 |
  |      403    .3595921   .2491774       27 |
  |      415    .4860094   .1853557       28 |
  +------------------------------------------+

. 
. summarize u0 if pickone_class==1

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
          u0 |        135    4.88e-17    .1583189  -.4351861   .4740873

. 
. qnorm v0 if pickone_school==1, aspectratio(1)

. 
. qnorm u0 if pickone_class==1, aspectratio(1)

. 
. generate labheight = v0 + 1.96*v0se + 0.05

. 
. serrbar v0 v0se v0rank if pickone_school==1, scale(1.96) yline(0) ///
>         addplot(scatter labheight v0rank, ///
>         msymbol(none) mlabel(schoolid) ///
>         mlabposition(1) mlabangle(vertical) mlabcolor(navy)) ///
>         ytitle("Predicted school effect") xtitle("Rank") ///
>         legend(off)

. 
. serrbar u0 u0se u0rank if pickone_class==1, scale(1.96) yline(0) ///
>         ytitle("Predicted classroom effect") xtitle("Rank")

. 
. count if ((u0 + 1.96*u0se)<0 | (u0 - 1.96*u0se)>0) & pickone_class==1
  1

. 
end of do-file