UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

Stata Textbook Examples
Multilevel Analysis by Tom Snijders and Roel Bosker
Chapter 14: Discrete Dependent Variables


Figure 14.1 on page 211.
use snijder_chap14, clear
preserve
collapse (mean) cohab, by(reg)
gen one = 1
gen zero = 0
twoway rspike one zero cohab, xlab(.2(.2) .8) ylab(0 5) ///
plotregion(style(none)) yscale(off) ysize(1.5) xsize(4)
restore



Chi-square test for equality of proportions and variance calculation on page 211.
tab cohab reg, chi2
           |                                           reg
     cohab |         1          2          3          4          5          6          7          8 |     Total
-----------+----------------------------------------------------------------------------------------+----------
         0 |        73        102        135         41         54         58         54         48 |     1,177 
         1 |        34        113        100         42         37         53         44         27 |       902 
-----------+----------------------------------------------------------------------------------------+----------
     Total |       107        215        235         83         91        111         98         75 |     2,079 


           |                                           reg
     cohab |         9         10         11         12         14         15         16         17 |     Total
-----------+----------------------------------------------------------------------------------------+----------
         0 |        38         51         96        121         32         67         63         34 |     1,177 
         1 |        19         23         66         79         23         54         46         21 |       902 
-----------+----------------------------------------------------------------------------------------+----------
     Total |        57         74        162        200         55        121        109         55 |     2,079 


           |               reg
     cohab |        18         19         20 |     Total
-----------+---------------------------------+----------
         0 |        62         32         16 |     1,177 
         1 |        63         39         19 |       902 
-----------+---------------------------------+----------
     Total |       125         71         35 |     2,079 

         Pearson chi2(18) =  35.4000   Pr = 0.008

xtmixed cohab ||reg:, var

Performing EM optimization: 

Performing gradient-based optimization: 

Iteration 0:   log restricted-likelihood = -1491.4981  
Iteration 1:   log restricted-likelihood =  -1491.498  

Computing standard errors:

Mixed-effects REML regression                   Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235


                                                Wald chi2(0)       =         .
Log restricted-likelihood =  -1491.498          Prob > chi2        =         .

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   .4313919   .0157033    27.47   0.000     .4006139    .4621698
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0021911   .0015606      .0005425    .0088501
-----------------------------+------------------------------------------------
               var(Residual) |   .2437321   .0075941      .2292934    .2590801
------------------------------------------------------------------------------
LR test vs. linear regression: chibar2(01) =     5.35 Prob >= chibar2 = 0.0103

Table 14.1 on page 214.
xtmelogit cohab ||reg:, var mle

Refining starting values: 

Iteration 0:   log likelihood = -1436.0778  (not concave)
Iteration 1:   log likelihood = -1420.7298  
Iteration 2:   log likelihood = -1420.5007  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -1420.5007  
Iteration 1:   log likelihood = -1420.4928  
Iteration 2:   log likelihood = -1420.4928  

Mixed-effects logistic regression               Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235

Integration points =   7                        Wald chi2(0)       =         .
Log likelihood = -1420.4928                     Prob > chi2        =         .

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |  -.2778168   .0626587    -4.43   0.000    -.4006257   -.1550079
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0324699   .0241911      .0075389    .1398473
------------------------------------------------------------------------------
LR test vs. logistic regression: chibar2(01) =     4.64 Prob>=chibar2 = 0.0156

Table 14.2 on page 217, model 1.
gen x1 = age - 20
gen x2 = (age-20)^2
gen x3 = 0
replace x3 = (age-30)^2 if age>30
gen x4 = 0
replace x4 = (age-40)^2 if age>40

xtmelogit cohab x* ||reg:, var 

Refining starting values: 

Iteration 0:   log likelihood = -1135.7448  (not concave)
Iteration 1:   log likelihood = -1123.9337  
Iteration 2:   log likelihood = -1123.8488  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -1123.8488  
Iteration 1:   log likelihood = -1123.8486  
Iteration 2:   log likelihood = -1123.8486  

Mixed-effects logistic regression               Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235

Integration points =   7                        Wald chi2(4)       =    424.21
Log likelihood = -1123.8486                     Prob > chi2        =    0.0000

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .5453759   .0513839    10.61   0.000     .4446653    .6460865
          x2 |  -.0292833   .0037238    -7.86   0.000    -.0365818   -.0219847
          x3 |   .0242597    .005472     4.43   0.000     .0135348    .0349846
          x4 |   .0068919   .0024996     2.76   0.006     .0019928     .011791
       _cons |  -1.214272   .1551446    -7.83   0.000     -1.51835   -.9101941
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0627891   .0396416      .0182172    .2164145
------------------------------------------------------------------------------
LR test vs. logistic regression: chibar2(01) =     8.03 Prob>=chibar2 = 0.0023

Figure 14.6 on page 217.
predict xb, xb
sort age
twoway line xb age if age>=20


Table 14.2 on page 217, model 2.
gen religion = 1
replace religion = 0 if relserv >=3.5
xtmelogit cohab x* religion ||reg:, var 

note: xb dropped because of collinearity

Refining starting values: 

Iteration 0:   log likelihood = -1098.0525  (not concave)
Iteration 1:   log likelihood = -1086.0279  
Iteration 2:   log likelihood = -1085.6553  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -1085.6553  
Iteration 1:   log likelihood = -1085.6197  
Iteration 2:   log likelihood = -1085.6197  

Mixed-effects logistic regression               Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235

Integration points =   7                        Wald chi2(5)       =    449.04
Log likelihood = -1085.6197                     Prob > chi2        =    0.0000

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .5488541   .0519315    10.57   0.000     .4470703    .6506379
          x2 |  -.0291627   .0037768    -7.72   0.000    -.0365651   -.0217603
          x3 |   .0236239   .0055663     4.24   0.000     .0127142    .0345336
          x4 |   .0075683   .0025535     2.96   0.003     .0025635    .0125731
    religion |  -1.869635   .2430106    -7.69   0.000    -2.345927   -1.393343
       _cons |  -1.117111   .1537198    -7.27   0.000    -1.418396    -.815826
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0505109    .036407      .0122988     .207448
------------------------------------------------------------------------------
LR test vs. logistic regression: chibar2(01) =     5.23 Prob>=chibar2 = 0.0111

Table 14.3 on page 222 using dataset for this table.
use table_14_3, clear
xtmelogit foe ego pfa ||respondant: pfa, var cov(un)

Refining starting values: 

Iteration 0:   log likelihood = -798.74841  
Iteration 1:   log likelihood = -791.53546  
Iteration 2:   log likelihood = -791.06141  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -791.06141  
Iteration 1:   log likelihood = -791.05138  
Iteration 2:   log likelihood = -791.05138  

Mixed-effects logistic regression               Number of obs      =      1683
Group variable: respondant                      Number of groups   =       426

                                                Obs per group: min =         1
                                                               avg =       4.0
                                                               max =        14

Integration points =   7                        Wald chi2(2)       =      1.53
Log likelihood = -791.05138                     Prob > chi2        =    0.4651

------------------------------------------------------------------------------
         foe |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         ego |   .2798584   .2320657     1.21   0.228     -.174982    .7346987
         pfa |  -.3306332   .7571286    -0.44   0.662    -1.814578    1.153312
       _cons |   -1.92459   .1402628   -13.72   0.000    -2.199501    -1.64968
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
respondant: Unstructured     |
                    var(pfa) |   5.290383   4.937787      .8492134    32.95774
                  var(_cons) |   1.587789   .3909177      .9799971    2.572532
              cov(pfa,_cons) |  -.1469777   .9823098      -2.07227    1.778314
------------------------------------------------------------------------------
LR test vs. logistic regression:     chi2(3) =    62.58   Prob > chi2 = 0.0000

Note: LR test is conservative and provided only for reference.
 
di -2*e(ll)

1582.1028

estimates store m1
xtmelogit foe ego pfa ||respondant: , var cov(un)

Note: single-variable random-effects specification; covariance structure set to identity

Refining starting values: 

Iteration 0:   log likelihood = -799.52695  
Iteration 1:   log likelihood =  -793.9659  
Iteration 2:   log likelihood =  -793.8014  

Performing gradient-based optimization: 

Iteration 0:   log likelihood =  -793.8014  
Iteration 1:   log likelihood = -793.80109  
Iteration 2:   log likelihood = -793.80109  

Mixed-effects logistic regression               Number of obs      =      1683
Group variable: respondant                      Number of groups   =       426

                                                Obs per group: min =         1
                                                               avg =       4.0
                                                               max =        14

Integration points =   7                        Wald chi2(2)       =      3.07
Log likelihood = -793.80109                     Prob > chi2        =    0.2159

------------------------------------------------------------------------------
         foe |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         ego |   .2473459   .2169032     1.14   0.254    -.1777765    .6724683
         pfa |   .3176939   .2424613     1.31   0.190    -.1575216    .7929093
       _cons |  -1.877151   .1315776   -14.27   0.000    -2.135038   -1.619263
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
respondant: Identity         |
                  var(_cons) |   1.403913   .3313099      .8840256    2.229541
------------------------------------------------------------------------------
LR test vs. logistic regression: chibar2(01) =    57.08 Prob>=chibar2 = 0.0000

di -2*e(ll)

1587.6022

estimates store m2
* the result is two-sided, need to adjusted to one-sided
lrtest m1 m2


Likelihood-ratio test                                  LR chi2(2)  =      5.50
(Assumption: m2 nested in m1)                          Prob > chi2 =    0.0639

Note: LR test is conservative

xi: xtmelogit foe i.ego*i.pfa col sup sub nei ||respondant: pfa, var cov(un) 

i.ego             _Iego_0-1           (naturally coded; _Iego_0 omitted)
i.pfa             _Ipfa_0-1           (naturally coded; _Ipfa_0 omitted)
i.ego*i.pfa       _IegoXpfa_#_#       (coded as above)

Refining starting values: 

Iteration 0:   log likelihood = -768.38146  
Iteration 1:   log likelihood = -760.95373  
Iteration 2:   log likelihood = -760.45097  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -760.45097  
Iteration 1:   log likelihood = -760.07085  
Iteration 2:   log likelihood =  -759.9334  
Iteration 3:   log likelihood = -759.93322  
Iteration 4:   log likelihood = -759.93322  

Mixed-effects logistic regression               Number of obs      =      1683
Group variable: respondant                      Number of groups   =       426

                                                Obs per group: min =         1
                                                               avg =       4.0
                                                               max =        14

Integration points =   7                        Wald chi2(7)       =     53.29
Log likelihood = -759.93322                     Prob > chi2        =    0.0000

------------------------------------------------------------------------------
         foe |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     _Iego_1 |   .2506568   .2388512     1.05   0.294     -.217483    .7187966
     _Ipfa_1 |  -.1059095   .7358454    -0.14   0.886     -1.54814    1.336321
_IegoXpfa_~1 |  -1.089537   .9800973    -1.11   0.266    -3.010492    .8314183
         col |   1.189454   .2338061     5.09   0.000     .7312024    1.647706
         sup |   1.333307    .254923     5.23   0.000     .8336667    1.832947
         sub |  -.1984302   .7449843    -0.27   0.790    -1.658573    1.261712
         nei |   2.299506   .3585229     6.41   0.000     1.596814    3.002198
       _cons |  -2.965708   .2430836   -12.20   0.000    -3.442143   -2.489273
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
respondant: Unstructured     |
                    var(pfa) |   5.260331   5.140629      .7747848    35.71453
                  var(_cons) |   1.682331   .4236274         1.027    2.755829
              cov(pfa,_cons) |    .211198    1.03089     -1.809309    2.231705
------------------------------------------------------------------------------
LR test vs. logistic regression:     chi2(3) =    60.77   Prob > chi2 = 0.0000

Note: LR test is conservative and provided only for reference.

Results listed on page 224.
use snijder_chap14, clear

xtmixed cohab ||reg:, var

Performing EM optimization: 

Performing gradient-based optimization: 

Iteration 0:   log restricted-likelihood = -1491.4981  
Iteration 1:   log restricted-likelihood =  -1491.498  

Computing standard errors:

Mixed-effects REML regression                   Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235


                                                Wald chi2(0)       =         .
Log restricted-likelihood =  -1491.498          Prob > chi2        =         .

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   .4313919   .0157033    27.47   0.000     .4006139    .4621698
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0021911   .0015606      .0005425    .0088501
-----------------------------+------------------------------------------------
               var(Residual) |   .2437321   .0075941      .2292934    .2590801
------------------------------------------------------------------------------
LR test vs. linear regression: chibar2(01) =     5.35 Prob >= chibar2 = 0.0103

di exp(_b[lns1_1_1:_cons])^2 /(exp(_b[lns1_1_1:_cons])^2 + _b[cohab:_cons]*(1-_b[cohab:_cons]))

.00885335

xtmelogit cohab ||reg:, var

Refining starting values: 

Iteration 0:   log likelihood = -1436.0778  (not concave)
Iteration 1:   log likelihood = -1420.7298  
Iteration 2:   log likelihood = -1420.5007  

Performing gradient-based optimization: 

Iteration 0:   log likelihood = -1420.5007  
Iteration 1:   log likelihood = -1420.4928  
Iteration 2:   log likelihood = -1420.4928  

Mixed-effects logistic regression               Number of obs      =      2079
Group variable: reg                             Number of groups   =        19

                                                Obs per group: min =        35
                                                               avg =     109.4
                                                               max =       235

Integration points =   7                        Wald chi2(0)       =         .
Log likelihood = -1420.4928                     Prob > chi2        =         .

------------------------------------------------------------------------------
       cohab |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |  -.2778168   .0626587    -4.43   0.000    -.4006257   -.1550079
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
reg: Identity                |
                  var(_cons) |   .0324699   .0241911      .0075389    .1398473
------------------------------------------------------------------------------
LR test vs. logistic regression: chibar2(01) =     4.64 Prob>=chibar2 = 0.0156

di exp(_b[lns1_1_1:_cons])^2 /(exp(_b[lns1_1_1:_cons])^2 + _pi^2/3)

.0097732

How to cite this page

Report an error on this page or leave a comment

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.