UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Textbook Examples
Computer-Aided Multivariate Analysis by Afifi and Clark
Chapter 17: Log-linear Analysis

Table 17.1, page 412.
use http://www.ats.ucla.edu/stat/stata/examples/cama3/depress, clear
gen incomecat = 0
replace incomecat = 1 if income >= 20
tab2 sex incomecat

           |       incomecat
       sex |         0          1 |     Total
-----------+----------------------+----------
      male |        54         57 |       111 
    female |       125         58 |       183 
-----------+----------------------+----------
     Total |       179        115 |       294
Table 17.2, page 413.
table treat sex incomecat

--------------------------------------------
has a     |
doctor    |
prescribe |
d or      |
recommend |
ed that   |
you take  |
medicine, |        incomecat and sex        
medical   | ------ 0 -----    ------ 1 -----
treatment |   male  female      male  female
----------+---------------------------------
      yes |     20      73        21      34
       no |     34      52        36      24
--------------------------------------------
Table 17.3, page 414.
gen cesdcat = 0
replace cesdcat = 1 if cesd >=11
table treat sex incomecat, by(cesdcat)

--------------------------------------------
cesdcat   |
and has a |
doctor    |
prescribe |
d or      |
recommend |
ed that   |
you take  |
medicine, |        incomecat and sex        
medical   | ------ 0 -----    ------ 1 -----
treatment |   male  female      male  female
----------+---------------------------------
0         |
      yes |     16      48        16      20
       no |     23      33        30      20
----------+---------------------------------
1         |
      yes |      4      25         5      14
       no |     11      19         6       4
--------------------------------------------
Page 416 middle of the page.
NOTE: You will need to download the tabchi ado, which can be installed by typing findit tabchi in the command line (see How can I use the findit command to search for programs and get additional help? for more information about using findit).
tabchi sex incomecat

          observed frequency
          expected frequency

----------------------------
          |    incomecat    
      sex |       0        1
----------+-----------------
     male |      54       57
          |  67.582   43.418
          | 
   female |     125       58
          | 111.418   71.582
----------------------------

          Pearson chi2(1) =  11.2104   Pr = 0.001
 likelihood-ratio chi2(1) =  11.1467   Pr = 0.001
Page 419 middle of the page.
tab2 sex incomecat, chi2

           |       incomecat
       sex |         0          1 |     Total
-----------+----------------------+----------
      male |        54         57 |       111 
    female |       125         58 |       183 
-----------+----------------------+----------
     Total |       179        115 |       294 

          Pearson chi2(1) =  11.2104   Pr = 0.001
Table 17.1, page 420.
NOTE: You will need to download xi3, which can be installed by typing findit xi3 in the command line (see How can I use the findit command to search for programs and get additional help? for more information about using findit).
collapse (count) id, by(sex incomecat)
rename id count
recode sex 1=0 2=1
xi3: glm count e.sex*e.incomecat, fam(pois)

d.sex             _Isex_0-1           (naturally coded; _Isex_0 omitted)
d.incomecat       _Iincomecat_0-1     (naturally coded; _Iincomecat_0 omitted)
d.sex*d.incom~t   _IsexXinc_#_#       (coded as above)

Iteration 0:   log likelihood = -12.201957  
Iteration 1:   log likelihood =  -12.14127  
Iteration 2:   log likelihood = -12.141259  
Iteration 3:   log likelihood = -12.141259  

Generalized linear models                          No. of obs      =         4
Optimization     : ML: Newton-Raphson              Residual df     =         0
                                                   Scale param     =         1
Deviance         =  4.44089e-16                    (1/df) Deviance =         .
Pearson          =  1.72033e-27                    (1/df) Pearson  =         .

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -12.14125876                    AIC             =  8.070629
BIC              =  4.44089e-16

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     _Isex_1 |   .2141804    .061899     3.46   0.001     .0928606    .3355001
_Iincomeca~1 |  -.1784509    .061899    -2.88   0.004    -.2997707   -.0571311
_IsexXinc_~1 |  -.2054845    .061899    -3.32   0.001    -.3268043   -.0841647
       _cons |   4.230198    .061899    68.34   0.000     4.108878    4.351518
------------------------------------------------------------------------------
Page 427.
use http://www.ats.ucla.edu/stat/stata/examples/cama3/depress, clear
gen incomecat = 0
replace incomecat = 1 if income >= 20
(115 real changes made)

collapse (count) beddays, by(sex incomecat treat )
rename beddays count
NOTE:  These are in the reverse order so that the subtraction is done correctly.  The lrtest commands give the likelihood ratio tests and the display commands give the Pearson tests.  To determine the number of degrees of freedom shown in the display chi2tail commands, you need to subtract the number of degrees of freedom for the two models in the comparison.
xi3: glm count sex*incomecat*treat, fam(poi)

This is an experimental version of xi3
Please view results with some caution

Iteration 0:   log likelihood = -21.561163  
Iteration 1:   log likelihood = -21.421271  
Iteration 2:   log likelihood = -21.421201  
Iteration 3:   log likelihood = -21.421201  

Generalized linear models                          No. of obs      =         8
Optimization     : ML: Newton-Raphson              Residual df     =         0
                                                   Scale param     =         1
Deviance         =  1.44329e-14                    (1/df) Deviance =         .
Pearson          =  8.09169e-24                    (1/df) Pearson  =         .

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -21.42120107                    AIC             =    7.3553
BIC              =  1.44329e-14

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         sex |   2.164571   .5508512     3.93   0.000     1.084923     3.24422
   incomecat |   .8358518   1.422674     0.59   0.557    -1.952538    3.624242
       treat |   1.400472    .592095     2.37   0.018     .2399874    2.560957
     _IseXin |  -.7954299   .8252301    -0.96   0.335    -2.412851    .8219915
     _IseXtr |   -.869844   .3351733    -2.60   0.009    -1.526772   -.2129163
     _IinXtr |   .0258275   .8504365     0.03   0.976    -1.640997    1.692652
  _IseXinXtr |  -.0174592     .50874    -0.03   0.973    -1.014571    .9796529
       _cons |   .3005329   .9958275     0.30   0.763    -1.651253    2.252319
------------------------------------------------------------------------------

lrtest, saving(m0)
xi3: glm count sex*incomecat sex*treat incomecat*treat, fam(poi)

This is an experimental version of xi3
Please view results with some caution

Iteration 0:   log likelihood = -21.554923  
Iteration 1:   log likelihood =  -21.42186  
Iteration 2:   log likelihood =  -21.42179  
Iteration 3:   log likelihood =  -21.42179  

Generalized linear models                          No. of obs      =         8
Optimization     : ML: Newton-Raphson              Residual df     =         1
                                                   Scale param     =         1
Deviance         =   .001177818                    (1/df) Deviance =  .0011778
Pearson          =  .0011777657                    (1/df) Pearson  =  .0011778

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -21.42178998                    AIC             =  7.105447
BIC              = -2.078263724

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         sex |   2.176457   .4288301     5.08   0.000     1.335965    3.016948
   incomecat |   .8798374   .6176395     1.42   0.154    -.3307138    2.090389
     _IseXin |  -.8223852   .2533633    -3.25   0.001    -1.318968   -.3258023
       treat |   1.413416   .4567266     3.09   0.002     .5182485    2.308584
     _IseXtr |   -.877427    .252167    -3.48   0.001    -1.371665   -.3831887
     _IinXtr |  -.0020759   .2493924    -0.01   0.993     -.490876    .4867242
       _cons |   .2799075   .7951472     0.35   0.725    -1.278552    1.838367
------------------------------------------------------------------------------

lrtest, using(m0)

Glm:  likelihood-ratio test                           chi2(1)     =       0.00
                                                      Prob > chi2 =     0.9726

di .0011777657 - 0
.00117777

di chi2tail(1, .00117777)
.97262305

lrtest, saving(m1)
glm count sex incomecat treat, fam(poi)

Iteration 0:   log likelihood = -33.613736  
Iteration 1:   log likelihood = -33.459453  
Iteration 2:   log likelihood = -33.459367  
Iteration 3:   log likelihood = -33.459367  

Generalized linear models                          No. of obs      =         8
Optimization     : ML: Newton-Raphson              Residual df     =         4
                                                   Scale param     =         1
Deviance         =  24.07633241                    (1/df) Deviance =  6.019083
Pearson          =   24.6518747                    (1/df) Pearson  =  6.162969

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -33.45936727                    AIC             =  9.364842
BIC              =  15.75856624

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         sex |    .499956   .1203058     4.16   0.000     .2641609     .735751
   incomecat |  -.4424537   .1195083    -3.70   0.000    -.6766857   -.2082216
       treat |  -.0136057   .1166451    -0.12   0.907    -.2422258    .2150145
       _cons |   3.040618   .2723247    11.17   0.000     2.506872    3.574365
------------------------------------------------------------------------------

di 24.6518747 - .0011777657 
24.650697

di chi2tail(3, 24.650697)
.00001827

lrtest, using(m1)
Glm:  likelihood-ratio test                           chi2(3)     =      24.08
                                                      Prob > chi2 =     0.0000

lrtest, saving(m2)
glm count, fam(poi)

Iteration 0:   log likelihood = -49.483935  
Iteration 1:   log likelihood = -49.394894  
Iteration 2:   log likelihood = -49.394881  
Iteration 3:   log likelihood = -49.394881  

Generalized linear models                          No. of obs      =         8
Optimization     : ML: Newton-Raphson              Residual df     =         7
                                                   Scale param     =         1
Deviance         =  55.94736055                    (1/df) Deviance =   7.99248
Pearson          =  61.31972823                    (1/df) Pearson  =  8.759961

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -49.39488135                    AIC             =  12.59872
BIC              =  41.39126976

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |   3.604138   .0583212    61.80   0.000     3.489831    3.718446
------------------------------------------------------------------------------

lrtest, using(m2)
Glm:  likelihood-ratio test                           chi2(3)     =      31.87
                                                      Prob > chi2 =     0.0000

di 61.31972823 - 24.6518747 
36.667854

di chi2tail(3, 36.667854)
5.409e-08
Page 428.
NOTE: The first poisson and lrtest commands are used to calculate and save the model to which the other three models will be compared. We have used the quietly command to suppress the output of the poisson command, as it is not needed.
Partial association
quietly xi3: poisson count sex*treat  incomecat*treat sex*incomecat
lrtest, saving(m0)
quietly xi3: poisson count sex*treat  incomecat*treat incomecat
lrtest, using(m0)

Poisson:  likelihood-ratio test                       chi2(1)     =      10.67
                                                      Prob > chi2 =     0.0011

quietly xi3: poisson count sex*treat  incomecat*sex incomecat
lrtest, using(m0)

Poisson:  likelihood-ratio test                       chi2(1)     =       0.00
                                                      Prob > chi2 =     0.9934

quietly xi3: poisson count sex*incomecat  incomecat*treat incomecat
lrtest, using(m0)

Poisson:  likelihood-ratio test                       chi2(1)     =      12.45
                                                      Prob > chi2 =     0.0004
Marginal association
tabchi sex incomecat [weight=count]

(frequency weights assumed)

          observed frequency
          expected frequency

----------------------------
          |    incomecat    
      sex |       0        1
----------+-----------------
     male |      54       57
          |  67.582   43.418
          | 
   female |     125       58
          | 111.418   71.582
----------------------------

          Pearson chi2(1) =  11.2104   Pr = 0.001
 likelihood-ratio chi2(1) =  11.1467   Pr = 0.001

tabchi sex treat [weight=count]

(frequency weights assumed)

          observed frequency
          expected frequency

--------------------------
          |  has a doctor 
          | prescribed or 
          |  recommended  
          | that you take 
          |   medicine,   
          |    medical    
          |   treatment   
      sex |    yes      no
----------+---------------
     male |     41      70
          | 55.878  55.122
          | 
   female |    107      76
          | 92.122  90.878
--------------------------

          Pearson chi2(1) =  12.8149   Pr = 0.000
 likelihood-ratio chi2(1) =  12.9284   Pr = 0.000

tabchi incomecat treat [weight=count]

(frequency weights assumed)

          observed frequency
          expected frequency

--------------------------
          |  has a doctor 
          | prescribed or 
          |  recommended  
          | that you take 
          |   medicine,   
          |    medical    
          |   treatment   
incomecat |    yes      no
----------+---------------
        0 |     93      86
          | 90.109  88.891
          | 
        1 |     55      60
          | 57.891  57.109
--------------------------

          Pearson chi2(1) =   0.4776   Pr = 0.490
 likelihood-ratio chi2(1) =   0.4777   Pr = 0.489
Page 430.
NOTE: For the first four entries in the table, the poisson command can be used to generate the likelihood ratio chi-squared.  For the rest of the table, the lrtest command provides the entries in the table.  We have used the quietly command for the corresponding poisson command to suppress the output, as we are only interested in the output of the lrtest commands.
The first-order terms:
poisson count incomecat

Iteration 0:   log likelihood = -86.914684  
Iteration 1:   log likelihood = -86.914684  

Poisson regression                                Number of obs   =         16
                                                  LR chi2(1)      =      14.04
                                                  Prob > chi2     =     0.0002
Log likelihood = -86.914684                       Pseudo R2       =     0.0748

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
   incomecat |  -.4424537   .1195083    -3.70   0.000    -.6766857   -.2082216
       _cons |   3.107944   .0747435    41.58   0.000      2.96145    3.254439
------------------------------------------------------------------------------

poisson count sex

Iteration 0:   log likelihood =  -85.03012  
Iteration 1:   log likelihood =  -85.03012  

Poisson regression                                Number of obs   =         16
                                                  LR chi2(1)      =      17.81
                                                  Prob > chi2     =     0.0000
Log likelihood =  -85.03012                       Pseudo R2       =     0.0948

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         sex |    .499956   .1203058     4.16   0.000     .2641609     .735751
       _cons |   2.130133   .2037168    10.46   0.000     1.730855     2.52941
------------------------------------------------------------------------------

poisson count treat

Iteration 0:   log likelihood = -93.929955  
Iteration 1:   log likelihood = -93.929955  

Poisson regression                                Number of obs   =         16
                                                  LR chi2(1)      =       0.01
                                                  Prob > chi2     =     0.9071
Log likelihood = -93.929955                       Pseudo R2       =     0.0001

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       treat |  -.0136057   .1166451    -0.12   0.907    -.2422258    .2150145
       _cons |   2.931376   .1840553    15.93   0.000     2.570635    3.292118
------------------------------------------------------------------------------

poisson count cesdcat

Iteration 0:   log likelihood = -69.575822  
Iteration 1:   log likelihood = -69.575808  
Iteration 2:   log likelihood = -69.575808  

Poisson regression                                Number of obs   =         16
                                                  LR chi2(1)      =      48.72
                                                  Prob > chi2     =     0.0000
Log likelihood = -69.575808                       Pseudo R2       =     0.2593

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     cesdcat |  -.8505394   .1273499    -6.68   0.000    -1.100141   -.6009381
       _cons |   3.248435   .0696733    46.62   0.000     3.111877    3.384992
------------------------------------------------------------------------------
The second-order terms:
NOTE: The first lrtest is used to save the model to which all of the other models in this section will be compared.
quietly xi3: poisson count sex*treat sex*incomecat sex*cesdcat treat*incomecat ///
	treat*cesdcat incomecat*cesdcat
lrtest, saving(m2)
quietly poisson count sex treat incomecat cesdcat _IseXtr _IseXce _ItrXin _ItrXce _IinXce
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =       9.91
                                                      Prob > chi2 =     0.0016
                                                      
quietly poisson count sex treat incomecat cesdcat _IseXtr _IseXin _IseXce _ItrXce _IinXce
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =       0.00
                                                      Prob > chi2 =     0.9653

quietly poisson count sex treat incomecat cesdcat _IseXtr _IseXin _IseXce _ItrXin _ItrXce 
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =       1.17
                                                      Prob > chi2 =     0.2799

quietly poisson count sex treat incomecat cesdcat _IseXin _IseXce _ItrXin _ItrXce _IinXce
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =      11.98
                                                      Prob > chi2 =     0.0005

quietly poisson count sex treat incomecat cesdcat _IseXtr _IseXin _ItrXin _ItrXce _IinXce
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =       2.34
                                                      Prob > chi2 =     0.1259

quietly poisson count sex treat incomecat cesdcat _IseXtr _IseXce _IseXin _ItrXin _IinXce
lrtest, using(m2)

Poisson:  likelihood-ratio test                       chi2(1)     =       0.32
                                                      Prob > chi2 =     0.5733
Third-order terms:
quietly xi3: poisson count sex*treat*incomecat sex*treat*cesdcat ///
	sex*incomecat*cesdcat treat*incomecat*cesdcat 
lrtest, saving(m3)
quietly poisson count sex treat incomecat cesdcat _IseXin _IseXtr _IseXce _ItrXin _ItrXce _IinXce ///
	_IseXtrXce _IseXinXce _ItrXinXce
lrtest, using(m3)

Poisson:  likelihood-ratio test                       chi2(1)     =       0.01
                                                      Prob > chi2 =     0.9274

quietly poisson count sex treat incomecat cesdcat _IseXin _IseXtr _IseXce _ItrXin _ItrXce _IinXce ///
	_IseXtrXin _IseXtrXce _ItrXinXce
lrtest, using(m3)

Poisson:  likelihood-ratio test                       chi2(1)     =       0.01
                                                      Prob > chi2 =     0.9205

quietly poisson count sex treat incomecat cesdcat _IseXin _IseXtr _IseXce _ItrXin _ItrXce _IinXce ///
	_IseXtrXin _IseXtrXce _IseXinXce
lrtest, using(m3)

Poisson:  likelihood-ratio test                       chi2(1)     =       4.74
                                                      Prob > chi2 =     0.0294
 
quietly poisson count sex treat incomecat cesdcat _IseXin _IseXtr _IseXce _ItrXin _ItrXce _IinXce ///
	_IseXtrXin _IseXinXce _ItrXinXce
lrtest, using(m3)
Poisson:  likelihood-ratio test                       chi2(1)     =       1.23
                                                      Prob > chi2 =     0.2680
Page 435.
use http://www.ats.ucla.edu/stat/stata/examples/cama3/depress, clear
gen incomecat = 0
replace incomecat = 1 if income >= 20
collapse (count) id, by(sex incomecat treat)
rename id count
recode sex 2=0
recode treat 2=0
recode incomecat 0=1 1= 0
xi3: glm count d.sex*d.treat d.treat*d.incomecat d.sex*d.incomecat, fam(pois)

d.sex             _Isex_0-1           (naturally coded; _Isex_0 omitted)
d.treat           _Itreat_0-1         (naturally coded; _Itreat_0 omitted)
d.incomecat       _Iincomecat_0-1     (naturally coded; _Iincomecat_0 omitted)

Iteration 0:   log likelihood = -21.554923  
Iteration 1:   log likelihood =  -21.42186  
Iteration 2:   log likelihood =  -21.42179  
Iteration 3:   log likelihood =  -21.42179  

Generalized linear models                          No. of obs      =         8
Optimization     : ML: Newton-Raphson              Residual df     =         1
                                                   Scale param     =         1
Deviance         =   .001177818                    (1/df) Deviance =  .0011778
Pearson          =  .0011777657                    (1/df) Pearson  =  .0011778

Variance function: V(u) = u                        [Poisson]
Link function    : g(u) = ln(u)                    [Log]
Standard errors  : OIM

Log likelihood   = -21.42178998                    AIC             =  7.105447
BIC              = -2.078263724

------------------------------------------------------------------------------
       count |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     _Isex_1 |  -.2245618   .0635601    -3.53   0.000    -.3491373   -.0999863
   _Itreat_1 |  -.0481189   .0627327    -0.77   0.443    -.1710728    .0748349
   _Ise1Xtr1 |  -.2193567   .0630417    -3.48   0.001    -.3429163   -.0957972
_Iincomeca~1 |   .1784271   .0619647     2.88   0.004     .0569785    .2998757
   _Itr1Xin1 |   -.000519   .0623481    -0.01   0.993     -.122719    .1216811
   _Ise1Xin1 |  -.2055963   .0633408    -3.25   0.001     -.329742   -.0814506
       _cons |   3.512079   .0635747    55.24   0.000     3.387475    3.636683
------------------------------------------------------------------------------
Page 437.
use http://www.ats.ucla.edu/stat/stata/examples/cama3/depress, clear
gen treat1 = treat - 1
gen sex1 = sex -1
logit treat1 sex1 incomecat

=Iteration 0:   log likelihood = -203.77847
Iteration 1:   log likelihood = -197.31646
Iteration 2:   log likelihood = -197.31424

Logit estimates                                   Number of obs   =        294
                                                  LR chi2(2)      =      12.93
                                                  Prob > chi2     =     0.0016
Log likelihood = -197.31424                       Pseudo R2       =     0.0317

------------------------------------------------------------------------------
      treat1 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        sex1 |   -.877427   .2521666    -3.48   0.001    -1.371664   -.3831895
   incomecat |  -.0020759   .2493921    -0.01   0.993    -.4908755    .4867237
       _cons |   .5359893   .2347028     2.28   0.022     .0759804    .9959983
------------------------------------------------------------------------------

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