Stata Textbook Examples
Introduction to Multilevel Modeling by Kreft and de Leeuw
Chapter 2: Overview of Contextual Models

Table 2.1, page 24. Ten selected schools for NELS-88: within-school means.
use http://www.ats.ucla.edu/stat/stata/examples/mlm_imm/imm10, clear

table schnum, cont(freq mean math mean homework) format(%6.2f)

----------------------------------------------------------
group(sch |
id)       |          Freq.      mean(math)  mean(homework)
----------+-----------------------------------------------
        1 |             23           45.74            1.39
        2 |             20           42.15            2.35
        3 |             24           53.25            1.83
        4 |             22           43.55            1.64
        5 |             22           49.86            0.86
        6 |             20           46.40            1.15
        7 |             67           62.82            3.30
        8 |             21           49.67            2.10
        9 |             21           46.33            1.33
       10 |             20           47.85            1.60
----------------------------------------------------------
Table 2.2, page 24. Ten selected schools from NELS-88: within school dispersions and correlations. The variances produced by Stata are not the same as in the book even when adjusted by (n-1)/n. It is unclear why these differences exist.
* Version 8 code
statsby "corr math homework, cov" r(Var_1) r(Var_2) r(cov_12) r(N), by(schnum) 
* _stat_1 mathv
* _stat_2 hmwkv
* _stat_3 cov
* _stat_4 n

* Version 9 code
statsby , by(schnum): corr math homework, cov
(running correlate on estimation sample)

      command:  correlate math homework, cov
            N:  r(N)
       cov_12:  r(cov_12)
        Var_2:  r(Var_2)
        Var_1:  r(Var_1)
           by:  schnum

Statsby groups
----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
.........

clist

        schnum          N     cov_12      Var_2      Var_1
  1.         1         23  -4.438735   1.249012   56.74704
  2.         2         20  -5.002632   1.713158   69.18684
  3.         3         24   10.08696   1.275362   132.8044
  4.         4         22   12.54113   2.242424   100.1645
  5.         5         22  -2.829004   .5995671   71.26624
  6.         6         20  -1.642105   .6605263   18.67369
  7.         7         67   3.251244   2.970149   32.20986
  8.         8         21   8.383333   1.290476   106.8333
  9.         9         21   4.883333   .8333333   91.23333
 10.        10         20   12.93684   2.042105   127.7132
use http://www.ats.ucla.edu/stat/stata/examples/mlm_imm/imm10, clear

* Version 8 code
statsby "corr math homework" r(rho), by(schnum)

* Version 9 code
statsby , by(schnum): corr math homework
(running correlate on estimation sample)

      command:  correlate math homework
            N:  r(N)
          rho:  r(rho)
           by:  schnum

Statsby groups
----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
.........

clist

        schnum          N        rho
  1.         1         23  -.5272352
  2.         2         20  -.4595027
  3.         3         24   .7750634
  4.         4         22   .8367981
  5.         5         22  -.4327856
  6.         6         20  -.4675638
  7.         7         67   .3324038
  8.         8         21   .7139838
  9.         9         21   .5600544
 10.        10         20   .8010717

Table 2.3, page 27. Total regression for 10 schools. Equations for null model and with homework.

use http://www.ats.ucla.edu/stat/stata/examples/mlm_imm/imm10, clear
regress math

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F(  0,   259) =    0.00
       Model |        0.00     0           .           Prob > F      =       .
    Residual |    32116.60   259  124.002317           R-squared     =  0.0000
-------------+------------------------------           Adj R-squared =  0.0000
       Total |    32116.60   259  124.002317           Root MSE      =  11.136

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |       51.3   .6906026    74.28   0.000     49.94009    52.65991
------------------------------------------------------------------------------

regress math homework

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F(  1,   258) =   84.64
       Model |  7933.80702     1  7933.80702           Prob > F      =  0.0000
    Residual |   24182.793   258  93.7317557           R-squared     =  0.2470
-------------+------------------------------           Adj R-squared =  0.2441
       Total |    32116.60   259  124.002317           Root MSE      =  9.6815

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
    homework |   3.571856   .3882366     9.20   0.000      2.80734    4.336372
       _cons |   44.07386    .988641    44.58   0.000     42.12703    46.02069
------------------------------------------------------------------------------
Table 2.4. page 28. Aggregate regression for 10 schools.
egen n=count(schnum),  by(schnum)
sort schnum
by schnum: generate i = _n
egen mmath = mean(math), by(schnum)
egen mhmwk = mean(homework), by(schnum)

regress mmath if i==1 [aw=n]
(sum of wgt is   2.6000e+02)

      Source |       SS       df       MS              Number of obs =      10
-------------+------------------------------           F(  0,     9) =    0.00
       Model |        0.00     0           .           Prob > F      =       .
    Residual |  539.635975     9  59.9595528           R-squared     =  0.0000
-------------+------------------------------           Adj R-squared =  0.0000
       Total |  539.635975     9  59.9595528           Root MSE      =  7.7434

------------------------------------------------------------------------------
       mmath |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |       51.3   2.448664    20.95   0.000     45.76074    56.83926
------------------------------------------------------------------------------

regress mmath mhmwk if i==1 [aw=n]
(sum of wgt is   2.6000e+02)

      Source |       SS       df       MS              Number of obs =      10
-------------+------------------------------           F(  1,     8) =   14.33
       Model |  346.267285     1  346.267285           Prob > F      =  0.0054
    Residual |   193.36869     8  24.1710863           R-squared     =  0.6417
-------------+------------------------------           Adj R-squared =  0.5969
       Total |  539.635975     9  59.9595528           Root MSE      =  4.9164

------------------------------------------------------------------------------
       mmath |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       mhmwk |   7.014745   1.853336     3.78   0.005     2.740944    11.28855
       _cons |   37.10863   4.058993     9.14   0.000     27.74858    46.46869
------------------------------------------------------------------------------
Table 2.5, page 29. Contextual model for 10 schools.
regress math homework mhmwk

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F(  2,   257) =   67.00
       Model |  11006.6159     2  5503.30794           Prob > F      =  0.0000
    Residual |  21109.9841   257  82.1400161           R-squared     =  0.3427
-------------+------------------------------           Adj R-squared =  0.3376
       Total |    32116.60   259  124.002317           Root MSE      =  9.0631

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
    homework |   2.136635   .4326083     4.94   0.000     1.284726    2.988543
       mhmwk |    4.87811    .797556     6.12   0.000     3.307533    6.448687
       _cons |   37.10863   1.467442    25.29   0.000     34.21889    39.99837
------------------------------------------------------------------------------
Table 2.6, page 30. Cronbach model for 10 schools. The intercept given in the book seems to by in error. The printed intercept can be obtained with the model, regress math chmwk mhmwk.
generate chmwk = homework - mhmwk      /* centered on group mean */ 
egen gmhmwk = mean(homework)
generate cghmwk = mhmwk -  gmhmwk      /* centered on grand mean */
regress math chmwk cghmwk

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F(  2,   257) =   67.00
       Model |  11006.6159     2  5503.30795           Prob > F      =  0.0000
    Residual |  21109.9841   257   82.140016           R-squared     =  0.3427
-------------+------------------------------           Adj R-squared =  0.3376
       Total |    32116.60   259  124.002317           Root MSE      =  9.0631

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       chmwk |   2.136635   .4326083     4.94   0.000     1.284726    2.988543
      cghmwk |   7.014744    .670034    10.47   0.000     5.695288      8.3342
       _cons |       51.3   .5620704    91.27   0.000     50.19315    52.40685
------------------------------------------------------------------------------
Table 2.7, page 31. ANCOVA for 10 schools. The first set coefficients are for the null model while the second set are with the homework variable.
tabulate schnum, gen(sch)

group(schid |
          ) |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |         23        8.85        8.85
          2 |         20        7.69       16.54
          3 |         24        9.23       25.77
          4 |         22        8.46       34.23
          5 |         22        8.46       42.69
          6 |         20        7.69       50.38
          7 |         67       25.77       76.15
          8 |         21        8.08       84.23
          9 |         21        8.08       92.31
         10 |         20        7.69      100.00
------------+-----------------------------------
      Total |        260      100.00

regress math sch1-sch10, nocons tsscons

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F(  9,   250) =   21.55
       Model |  14030.5357     9  1558.94841           Prob > F      =  0.0000
    Residual |  18086.0643   250  72.3442573           R-squared     =  0.4369
-------------+------------------------------           Adj R-squared =  0.4166
       Total |    32116.60   259  124.002317           Root MSE      =  8.5055

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        sch1 |   45.73913   1.773528    25.79   0.000     42.24617    49.23209
        sch2 |      42.15   1.901897    22.16   0.000     38.40422    45.89578
        sch3 |      53.25   1.736187    30.67   0.000     49.83058    56.66942
        sch4 |   43.54545   1.813388    24.01   0.000     39.97399    47.11692
        sch5 |   49.86364   1.813388    27.50   0.000     46.29217     53.4351
        sch6 |       46.4   1.901897    24.40   0.000     42.65422    50.14578
        sch7 |    62.8209   1.039117    60.46   0.000     60.77436    64.86744
        sch8 |   49.66667   1.856062    26.76   0.000     46.01116    53.32218
        sch9 |   46.33333   1.856062    24.96   0.000     42.67782    49.98884
       sch10 |      47.85   1.901897    25.16   0.000     44.10422    51.59578
------------------------------------------------------------------------------

regress math sch1-sch10 homework, nocons tsscons

      Source |       SS       df       MS              Number of obs =     260
-------------+------------------------------           F( 10,   249) =   24.83
       Model |  16034.2029    10  1603.42029           Prob > F      =  0.0000
    Residual |  16082.3971   249  64.5879403           R-squared     =  0.4992
-------------+------------------------------           Adj R-squared =  0.4791
       Total |    32116.60   259  124.002317           Root MSE      =  8.0367

------------------------------------------------------------------------------
        math |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        sch1 |   42.76642   1.758701    24.32   0.000     39.30259    46.23025
        sch2 |   37.12891   2.010493    18.47   0.000     33.16917    41.08865
        sch3 |   49.33284   1.784876    27.64   0.000     45.81746    52.84821
        sch4 |   40.04914   1.824791    21.95   0.000     36.45515    43.64314
        sch5 |   48.01836   1.745158    27.52   0.000     44.58121    51.45551
        sch6 |   43.94287   1.850409    23.75   0.000     40.29842    47.58732
        sch7 |   55.77319   1.601596    34.82   0.000     52.61879    58.92759
        sch8 |   45.18991   1.929157    23.42   0.000     41.39036    48.98945
        sch9 |   43.48449   1.826809    23.80   0.000     39.88652    47.08246
       sch10 |   44.43138    1.89898    23.40   0.000     40.69127    48.17149
    homework |   2.136635   .3836129     5.57   0.000     1.381095    2.892174
------------------------------------------------------------------------------

How to cite this page

Report an error on this page or leave a comment

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.