UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Textbook Examples
Applied Longitudinal Data Analysis: Modeling Change and Event Occurrence
by Judith D. Singer and John B. Willett
Chapter 3: Introducing the Multilevel Model for Change

Please note that the early_int data file (which is used in Chapter 3) is not included among the data files. This was done at the request of the researcher who contributed this data file to ensure the privacy of the participants in the study. Although the web page shows how to obtain the results with this data file, we regret that visitors do not have access to this file to be able to replicate the results for themselves.
Table 3.1, page 48.
use earlyint_pp, clear

list id age cog program in 1/12
 
            id        age        cog    program
  1.        68          1        103          1
  2.        68        1.5        119          1
  3.        68          2         96          1
  4.        70          1        106          1
  5.        70        1.5        107          1
  6.        70          2         96          1
  7.        71          1        112          1
  8.        71        1.5         86          1
  9.        71          2         73          1
 10.        72          1        100          1
 11.        72        1.5         93          1
 12.        72          2         87          1
 
list id age cog program in 175/186
 
            id        age        cog    program
175.       902          1        119          0
176.       902        1.5         93          0
177.       902          2         99          0
178.       904          1        112          0
179.       904        1.5         98          0
180.       904          2         79          0
181.       906          1         89          0
182.       906        1.5         66          0
183.       906          2         81          0
184.       908          1        117          0
185.       908        1.5         90          0
186.       908          2         76          0
Figure 3.1, page 50.
generate select = inlist(id,68,70,71,72,902,904,906,908)
keep if select

graph twoway (lfit cog age)(scatter cog age), by(id)

Figure 3.3, page 57.
use earlyint_pp, clear

egen grp=group(id)

generate p1=.
forvalues i = 1/103 {
  quietly regress cog age if grp==`i'
  quietly predict p
  quietly replace p1=p if grp==`i'
  quietly drop p
 }
 
graph twoway (scatter p1 age, msym(i) connect(L))(lfit cog age), legend(off)



statsby _b[_cons] _b[time] (e(rmse)^2), by(id): regress cog time

stem _stat_1, round(1)

Stem-and-leaf plot for _stat_1 (_b[_cons])

_stat_1 rounded to integers

_stat_1 rounded to integers

   5. | 7
   6* | 
   6. | 
   7* | 
   7. | 7
   8* | 34
   8. | 89
   9* | 344
   9. | 6666677799
  10* | 0012222244
  10. | 55666788999
  11* | 000111112222333334444
  11. | 55677777888999
  12* | 12233344
  12. | 5556778999
  13* | 0013
  13. | 55568
  14* | 0
  
stem _stat_2

Stem-and-leaf plot for _stat_2 (_b[time])

 -4* | 443111
 -3. | 987
 -3* | 443322100000
 -2. | 9999877776655
 -2* | 44322211110000
 -1. | 99888877666655
 -1* | 4333322211000
 -0. | 99998888777765
 -0* | 4444332
  0* | 134
  0. | 79
  1* | 0
  1. | 
  2* | 0

stem _stat_3, round(1) lines(1)

Stem-and-leaf plot for _stat_3 (e(rmse)^2)

_stat_3 rounded to integers

   0* | 0000111122233334444444466668
   1* | 111114447
   2* | 044448888
   3* | 33338888888
   4* | 3338
   5* | 44
   6* | 777
   7* | 44
   8* | 111888
   9* | 6666
  10* | 444
  11* | 33
  12* | 2
  13* | 1
  14* | 
  15* | 
  16* | 000
  17* | 11
  18* | 
  19* | 3
  20* | 
  21* | 
  22* | 8
  23* | 
  24* | 1
  25* | 444
  26* | 7
  27* | 
  28* | 
  29* | 4
  30* | 
  31* | 
  32* | 3
  33* | 
  34* | 
  35* | 
  36* | 8
  37* | 
  38* | 
  39* | 
  40* | 00
  41* | 
  42* | 
  43* | 
  44* | 
  45* | 
  46* | 8
Figure 3.4, page 59.
use earlyint_pp, clear

egen grp=group(id)

generate p1=.
forvalues i = 1/103 {
  quietly regress cog age if grp==`i'
  quietly predict p
  quietly replace p1=p if grp==`i'
  quietly drop p
 }
 
graph twoway (scatter p1 age if program==0, msym(i) connect(L))(lfit cog age if program==0), legend(off)



graph twoway (scatter p1 age if program==1, msym(i) connect(L))(lfit cog age if program==1), legend(off)

Table 3.3, page 69.
Note: The xtmixed command is new to Stata 9.
generate prg_time = program*time

xtmixed cog program time prg_time || id: time, variance cov(un) mle

Performing EM optimization: 

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -1186.0251  
[Iterations ommitted ]
Iteration 9:   log likelihood = -1184.9703  

Computing standard errors:

Mixed-effects ML regression                     Number of obs      =       309
Group variable: id                              Number of groups   =       103

                                                Obs per group: min =         3
                                                               avg =       3.0
                                                               max =         3


                                                Wald chi2(3)       =    242.63
Log likelihood = -1184.9703                     Prob > chi2        =    0.0000

------------------------------------------------------------------------------
         cog |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     program |   6.854662    2.71105     2.53   0.011     1.541101    12.16822
        time |  -21.13333   1.883386   -11.22   0.000     -24.8247   -17.44196
    prg_time |   5.271264   2.509829     2.10   0.036     .3520907    10.19044
       _cons |   107.8407   2.034384    53.01   0.000     103.8534    111.8281
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
id: Unstructured             |
                   var(time) |   10.08915   8.747843      1.844236    55.19411
                  var(_cons) |   123.9371   25.82037      82.38856    186.4387
             cov(time,_cons) |  -35.36098   18.02242     -70.68427   -.0376942
-----------------------------+------------------------------------------------
               var(Residual) |   74.76618   7.370867      61.62955    90.70295
------------------------------------------------------------------------------
LR test vs. linear regression:       chi2(3) =    89.46   Prob > chi2 = 0.0000

Note: LR test is conservative and provided only for reference


estat ic

------------------------------------------------------------------------------
       Model |    Obs    ll(null)   ll(model)     df          AIC         BIC
-------------+----------------------------------------------------------------
|    309           .    -1184.97      8     2385.941    2415.807
------------------------------------------------------------------------------

Figure 3.5 on page 71.

predict pred
sort prog age
twoway scatter cog pred age, msymbol(i i) connect(. L) ///
                 ylabel(50(25)150) xlabel(1(.5)2) legend(off)
                 

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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