------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\13_Fitting_ > Models_to_Repeated_Measures_Data.smcl log type: smcl opened on: 27 Mar 2020, 17:42:49 . ***************************************************************************** > *** . * MLwiN User Manual . * . * 13 Fitting Models to Repeated Measures Data > 191 . * . * 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 . * http://www.bristol.ac.uk/cmm/software/runmlwin/ . ***************************************************************************** > *** . . * 13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . > 191 . . * 13.2 A basic model . . . . . . . . . . . . . . . . . . . . . . . . . . . . > 194 . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/reading1.dta", clear . . describe Contains data from http://www.bristol.ac.uk/cmm/media/runmlwin/reading1.dta obs: 407 vars: 13 21 Oct 2011 12:19 ------------------------------------------------------------------------------- storage display value variable name type format label variable label ------------------------------------------------------------------------------- ID int %9.0g Student ID AGE1 float %9.0g Age at occasion 1 READ1 float %9.0g Reading score at occasion 1 AGE2 float %9.0g Age at occasion 2 READ2 float %9.0g Reading score at occasion 2 AGE3 float %9.0g Age at occasion 3 READ3 float %9.0g Reading score at occasion 3 AGE4 float %9.0g Age at occasion 4 READ4 float %9.0g Reading score at occasion 4 AGE5 float %9.0g Age at occasion 5 READ5 float %9.0g Reading score at occasion 5 AGE6 float %9.0g Age at occasion 6 READ6 float %9.0g Reading score at occasion 6 ------------------------------------------------------------------------------- Sorted by: . . summarize Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- ID | 407 342.6265 212.4703 1 751 AGE1 | 407 -4.834326 3.545192 -10 -1.7004 READ1 | 407 .018285 6.890005 -10 7.505 AGE2 | 407 -2.194591 2.438846 -10 -.6804 READ2 | 407 4.302185 4.51627 -10 7.1503 -------------+--------------------------------------------------------- AGE3 | 407 -1.656124 3.165415 -10 .2896 READ3 | 407 4.58113 5.585241 -10 9.9068 AGE4 | 407 -2.424712 4.703413 -10 1.2496 READ4 | 407 2.727961 7.945211 -10 9.9077 AGE5 | 407 -2.607457 5.507318 -10 2.2496 -------------+--------------------------------------------------------- READ5 | 407 1.979898 8.968271 -10 10.42 AGE6 | 407 -3.088647 7.109931 -10 4.5096 READ6 | 407 .3621742 10.7083 -10 13.869 . . recode * (-10=.) (ID: 0 changes made) (AGE1: 130 changes made) (READ1: 130 changes made) (AGE2: 36 changes made) (READ2: 36 changes made) (AGE3: 51 changes made) (READ3: 51 changes made) (AGE4: 113 changes made) (READ4: 113 changes made) (AGE5: 145 changes made) (READ5: 145 changes made) (AGE6: 209 changes made) (READ6: 209 changes made) . . summarize Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- ID | 407 342.6265 212.4703 1 751 AGE1 | 277 -2.410003 .1449145 -2.7104 -1.7004 READ1 | 277 4.720007 .6099982 3.8928 7.505 AGE2 | 371 -1.437192 .1539733 -2.0104 -.6804 READ2 | 371 5.689998 .739999 4.3406 7.1503 -------------+--------------------------------------------------------- AGE3 | 356 -.4607933 .1571502 -1.0604 .2896 READ3 | 356 6.67 .8699961 4.955 9.9068 AGE4 | 294 .4868789 .1269735 -.0404 1.2496 READ4 | 294 7.62 .9900053 5.4902 9.9077 AGE5 | 262 1.483837 .1307948 .9596 2.2496 -------------+--------------------------------------------------------- READ5 | 262 8.609994 1.119988 5.4619 10.42 AGE6 | 198 4.206671 .1150215 3.6596 4.5096 READ6 | 198 11.30002 1.470012 6.4509 13.869 . . reshape long AGE READ, i(ID) j(occasion) (note: j = 1 2 3 4 5 6) Data wide -> long ----------------------------------------------------------------------------- Number of obs. 407 -> 2442 Number of variables 13 -> 4 j variable (6 values) -> occasion xij variables: AGE1 AGE2 ... AGE6 -> AGE READ1 READ2 ... READ6 -> READ ----------------------------------------------------------------------------- . . describe Contains data obs: 2,442 vars: 4 ------------------------------------------------------------------------------- storage display value variable name type format label variable label ------------------------------------------------------------------------------- ID int %9.0g Student ID occasion byte %10.0g AGE float %9.0g READ float %9.0g ------------------------------------------------------------------------------- Sorted by: ID occasion Note: Dataset has changed since last saved. . . rename ID student . . rename AGE age . . rename READ reading . . list in 1/5 +----------------------------------------+ | student occasion age reading | |----------------------------------------| 1. | 1 1 -2.6504 6.3009 | 2. | 1 2 -1.6104 6.8948 | 3. | 1 3 -.6204 7.7685 | 4. | 1 4 .3396 8.8804 | 5. | 1 5 1.3396 10.082 | +----------------------------------------+ . . tabstat reading, statistics(count mean sd) by(occasion) columns(statistics) Summary for variables: reading by categories of: occasion occasion | N mean sd ---------+------------------------------ 1 | 277 4.720007 .6099982 2 | 371 5.689998 .739999 3 | 356 6.67 .8699961 4 | 294 7.62 .9900053 5 | 262 8.609994 1.119988 6 | 198 11.30002 1.470012 ---------+------------------------------ Total | 1758 7.125401 2.154428 ---------------------------------------- . . tabstat age, statistics(count mean sd) by(occasion) columns(statistics) Summary for variables: age by categories of: occasion occasion | N mean sd ---------+------------------------------ 1 | 277 -2.410003 .1449145 2 | 371 -1.437192 .1539733 3 | 356 -.4607933 .1571502 4 | 294 .4868789 .1269735 5 | 262 1.483837 .1307948 6 | 198 4.206671 .1150215 ---------+------------------------------ Total | 1758 9.55e-06 1.94346 ---------------------------------------- . . generate cons = 1 . . runmlwin reading cons, level2(student: cons) level1(occasion: cons) nopause MLwiN 3.05 multilevel model Number of obs = 1758 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ student | 407 1 4.3 6 ----------------------------------------------------------- Run time (seconds) = 0.66 Number of iterations = 3 Log likelihood = -3842.8685 Deviance = 7685.737 ------------------------------------------------------------------------------ reading | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | 7.114911 .0530307 134.17 0.000 7.010973 7.21885 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: student | var(cons) | .0776119 .0830937 -.0852487 .2404726 -----------------------------+------------------------------------------------ Level 1: occasion | var(cons) | 4.56149 .1724517 4.22349 4.899489 ------------------------------------------------------------------------------ . . . . * 13.3 A linear growth curve model . . . . . . . . . . . . . . . . . . . . . > 201 . . runmlwin reading cons age, /// > level2(student: cons) level1(occasion: cons) initsprevious nopause Model fitted using initial values specified as parameter estimates from previou > s model MLwiN 3.05 multilevel model Number of obs = 1758 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ student | 407 1 4.3 6 ----------------------------------------------------------- Run time (seconds) = 0.67 Number of iterations = 3 Log likelihood = -1897.7943 Deviance = 3795.5887 ------------------------------------------------------------------------------ reading | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | 7.117101 .0412801 172.41 0.000 7.036193 7.198009 age | .9967589 .0072193 138.07 0.000 .9826093 1.010909 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: student | var(cons) | .6029806 .0484443 .5080315 .6979296 -----------------------------+------------------------------------------------ Level 1: occasion | var(cons) | .3072374 .01181 .2840901 .3303846 ------------------------------------------------------------------------------ . . runmlwin reading cons age, /// > level2(student: cons age, residuals(u)) /// > level1(occasion: cons, residuals(e)) /// > initsprevious nopause Model fitted using initial values specified as parameter estimates from previou > s model MLwiN 3.05 multilevel model Number of obs = 1758 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ student | 407 1 4.3 6 ----------------------------------------------------------- Run time (seconds) = 0.78 Number of iterations = 4 Log likelihood = -1604.6959 Deviance = 3209.3918 ------------------------------------------------------------------------------ reading | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | 7.117256 .0433476 164.19 0.000 7.032296 7.202215 age | .9950642 .0121898 81.63 0.000 .9711727 1.018956 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: student | var(cons) | .6826229 .0530121 .5787211 .7865246 cov(cons,age) | .1232355 .0123613 .0990077 .1474633 var(age) | .0373452 .0038567 .0297861 .0449042 -----------------------------+------------------------------------------------ Level 1: occasion | var(cons) | .1610104 .007046 .1472004 .1748204 ------------------------------------------------------------------------------ . . egen pickone = tag(student) . . summarize u0 if pickone==1 Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- u0 | 407 -9.50e-15 .7821875 -1.827964 2.483674 . . generate u0std = (u0 - r(mean))/r(sd) if pickone==1 (2,035 missing values generated) . . summarize u1 if pickone==1 Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- u1 | 407 -1.57e-15 .1608952 -.5554863 .4252396 . . generate u1std = (u1 - r(mean))/r(sd) if pickone==1 (2,035 missing values generated) . . scatter u1std u0std if pickone==1, /// > yline(0) xline(0) ylabel(-4(1)4) xlabel(-4(1)4) aspectratio(1) . . drop u0* u1* . . summarize e0 Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- e0 | 1,758 -5.85e-16 .3328508 -1.500116 1.501293 . . generate e0std = (e0 - r(mean))/r(sd) if e(sample) (684 missing values generated) . . egen e0rank = rank(e0) if e(sample) (684 missing values generated) . . generate e0uniform = (e0rank - 0.5)/e(N) if e(sample) (684 missing values generated) . . generate e0nscore = invnorm(e0uniform) if e(sample) (684 missing values generated) . . scatter e0std e0nscore if e(sample), /// > yline(0) xline(0) ylabel(-4(1)4) xlabel(-4(1)4) aspectratio(1) . . . . * 13.4 Complex level 1 variation . . . . . . . . . . . . . . . . . . . . . . > 204 . . runmlwin reading cons age, /// > level2(student: cons age) level1(occasion: cons age) nopause MLwiN 3.05 multilevel model Number of obs = 1758 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ student | 407 1 4.3 6 ----------------------------------------------------------- Run time (seconds) = 0.92 Number of iterations = 9 Log likelihood = -1588.5007 Deviance = 3177.0015 ------------------------------------------------------------------------------ reading | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | 7.116961 .0439 162.12 0.000 7.030918 7.203003 age | .9950452 .0123224 80.75 0.000 .9708936 1.019197 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: student | var(cons) | .7048907 .0543704 .5983266 .8114548 cov(cons,age) | .127969 .0127769 .1029267 .1530112 var(age) | .035461 .0040377 .0275473 .0433746 -----------------------------+------------------------------------------------ Level 1: occasion | var(cons) | .1226826 .008252 .1065089 .1388563 cov(cons,age) | .0006717 .0027288 -.0046766 .00602 var(age) | .0145111 .0030457 .0085417 .0204805 ------------------------------------------------------------------------------ . . . . * 13.5 Repeated measures modelling of non-linear polynomial growth . . . . . > 205 . . gen agesq = age^2 (684 missing values generated) . . runmlwin reading cons age agesq, /// > level2(student: cons age agesq, residuals(u)) level1(occasion: cons a > ge) nopause MLwiN 3.05 multilevel model Number of obs = 1758 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ student | 407 1 4.3 6 ----------------------------------------------------------- Run time (seconds) = 1.03 Number of iterations = 8 Log likelihood = -1566.0101 Deviance = 3132.0201 ------------------------------------------------------------------------------ reading | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | 7.115027 .0462868 153.72 0.000 7.024307 7.205747 age | .9945387 .0125887 79.00 0.000 .9698653 1.019212 agesq | .0008364 .0031649 0.26 0.792 -.0053667 .0070396 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: student | var(cons) | .7668458 .0600873 .6490768 .8846148 cov(cons,age) | .1408307 .0139027 .1135819 .1680796 var(age) | .0393893 .00427 .0310203 .0477583 cov(cons,agesq) | -.0143496 .0030599 -.0203469 -.0083523 cov(age,agesq) | -.0017785 .0007679 -.0032836 -.0002734 var(agesq) | .0013288 .0003117 .000718 .0019396 -----------------------------+------------------------------------------------ Level 1: occasion | var(cons) | .1290837 .0085666 .1122933 .145874 cov(cons,age) | -.0060839 .0026723 -.0113215 -.0008462 var(age) | .0003855 .0039208 -.0072992 .0080702 ------------------------------------------------------------------------------ . . generate l2varfn = [RP2]var(cons) + 2*[RP2]cov(cons\age)*age + /// > [RP2]var(age)*age^2 + 2*[RP2]cov(cons\agesq)*agesq /// > + 2*[RP2]cov(age\agesq)*age*agesq + [RP2]var(agesq)*agesq^2 (684 missing values generated) . . generate l1varfn = [RP1]var(cons) + 2*[RP1]cov(cons\age)*age + [RP1]var(age)* > age^2 (684 missing values generated) . . generate totvarfn = l2varfn + l1varfn (684 missing values generated) . . scatter totvarfn age . . predict xb (684 missing values generated) . . gen yhat = xb + u0 + u1*age + u2*agesq (684 missing values generated) . . twoway /// > (line yhat age if student==1) /// > (line yhat age if student==2) /// > (line yhat age if student==3) /// > (line yhat age if student==4), legend(off) . . twoway /// > (line yhat age if student==10) /// > (line yhat age if student==11) /// > (line yhat age if student==12) /// > (line yhat age if student==13) /// > (line yhat age if student==14), legend(off) . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . . . . > 209 . . . . ***************************************************************************** > *** . exit end of do-file