Version info: Code for this page was tested in Stata 12.1.
Table 17.1, page 433.
use https://stats.idre.ucla.edu/stat/stata/examples/pma5/depress, clear gen incomecat = income >= 20 tab2 sex incomecat -> tabulation of sex by incomecat | incomecat sex | 0 1 | Total -----------+----------------------+---------- male | 54 57 | 111 female | 125 58 | 183 -----------+----------------------+---------- Total | 179 115 | 294
Table 17.2, page 434.
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 435.
gen cesdcat = 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 437 middle of the page.
tab sex incomecat, chi2 expected +--------------------+ | Key | |--------------------| | frequency | | expected frequency | +--------------------+ | incomecat sex | 0 1 | Total -----------+----------------------+---------- male | 54 57 | 111 | 67.6 43.4 | 111.0 -----------+----------------------+---------- female | 125 58 | 183 | 111.4 71.6 | 183.0 -----------+----------------------+---------- Total | 179 115 | 294 | 179.0 115.0 | 294.0 Pearson chi2(1) = 11.2104 Pr = 0.001
Table 17.7, page 441.
collapse (count) id, by(sex incomecat) recode sex 1=0 2=1 glm id sex##incomecat, fam(poisson) 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 Residual df = 0 Scale parameter = 1 Deviance = 1.48386e-13 (1/df) Deviance = . Pearson = 1.24285e-13 (1/df) Pearson = . Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 8.070629 Log likelihood = -12.14125876 BIC = 1.48e-13 ------------------------------------------------------------------------------- | OIM id | Coef. Std. Err. z P>|z| [95% Conf. Interval] --------------+---------------------------------------------------------------- 1.sex | .8393297 .1628451 5.15 0.000 .5201592 1.1585 1.incomecat | .0540672 .189901 0.28 0.776 -.3181318 .4262663 | sex#incomecat | 1 1 | -.8219379 .247596 -3.32 0.001 -1.307217 -.3366588 | _cons | 3.988984 .1360828 29.31 0.000 3.722267 4.255701 -------------------------------------------------------------------------------
Page 441.
use https://stats.idre.ucla.edu/stat/stata/examples/pma5/depress, clear gen incomecat = income >= 20 collapse (count) beddays, by(sex incomecat treat)
NOTE: These are in the reverse order so that the subtraction is done correctly. The lrtest commands give the likelihood ratio tests.
glm beddays sex##incomecat##treat, fam(poisson) estimates store m0 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 Residual df = 0 Scale parameter = 1 Deviance = 2.22045e-16 (1/df) Deviance = . Pearson = 8.10392e-24 (1/df) Pearson = . Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 7.3553 Log likelihood = -21.42120107 BIC = 2.22e-16 ------------------------------------------------------------------------------------- | OIM beddays | Coef. Std. Err. z P>|z| [95% Conf. Interval] --------------------+---------------------------------------------------------------- 2.sex | 1.294727 .2523859 5.13 0.000 .8000599 1.789394 1.incomecat | .0487902 .3124405 0.16 0.876 -.5635819 .6611622 | sex#incomecat | 2 1 | -.8128891 .3751392 -2.17 0.030 -1.548148 -.0776297 | 2.treat | .5306283 .2818009 1.88 0.060 -.0216914 1.082948 | sex#treat | 2 2 | -.869844 .3351733 -2.60 0.009 -1.526772 -.2129163 | incomecat#treat | 1 2 | .0083682 .3934572 0.02 0.983 -.7627937 .7795302 | sex#incomecat#treat | 2 1 2 | -.0174592 .50874 -0.03 0.973 -1.014571 .9796529 | _cons | 2.995732 .2236068 13.40 0.000 2.557471 3.433994 ------------------------------------------------------------------------------------- glm beddays sex##incomecat sex##treat incomecat##treat, fam(poisson) estimates store m1 lrtest (m0) (m1) 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 Residual df = 1 Scale parameter = 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] AIC = 7.105447 Log likelihood = -21.42178998 BIC = -2.078264 --------------------------------------------------------------------------------- | OIM beddays | Coef. Std. Err. z P>|z| [95% Conf. Interval] ----------------+---------------------------------------------------------------- 2.sex | 1.29903 .2193141 5.92 0.000 .8691819 1.728878 1.incomecat | .0553764 .2465739 0.22 0.822 -.4278996 .5386523 | sex#incomecat | 2 1 | -.8223852 .2533633 -3.25 0.001 -1.318968 -.3258023 | 2.treat | .5359893 .2347033 2.28 0.022 .0759794 .9959992 | sex#treat | 2 2 | -.877427 .252167 -3.48 0.001 -1.371665 -.3831887 | incomecat#treat | 1 2 | -.0020759 .2493924 -0.01 0.993 -.490876 .4867242 | _cons | 2.992353 .2011046 14.88 0.000 2.598196 3.386511 --------------------------------------------------------------------------------- . estimates store m1 . lrtest (m0) (m1) Likelihood-ratio test LR chi2(1) = 0.00 (Assumption: m1 nested in m0) Prob > chi2 = 0.9726 glm beddays sex incomecat treat, fam(poisson) estimates store m2 lrtest (m0) (m2) 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 Residual df = 4 Scale parameter = 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] AIC = 9.364842 Log likelihood = -33.45936728 BIC = 15.75857 ------------------------------------------------------------------------------ | OIM beddays | 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 ------------------------------------------------------------------------------ . estimates store m2 . lrtest (m0) (m2) Likelihood-ratio test LR chi2(4) = 24.08 (Assumption: m2 nested in m0) Prob > chi2 = 0.0001 glm beddays, fam(poisson) estimates store m3 lrtest (m0) (m3) 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 Residual df = 7 Scale parameter = 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] AIC = 12.59872 Log likelihood = -49.39488135 BIC = 41.39127 ------------------------------------------------------------------------------ | OIM beddays | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- _cons | 3.604138 .0583212 61.80 0.000 3.489831 3.718446 ------------------------------------------------------------------------------ . estimates store m3 . lrtest (m0) (m3) Likelihood-ratio test LR chi2(7) = 55.95 (Assumption: m3 nested in m0) Prob > chi2 = 0.0000Page 461 table in the middle of the page.
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 glm beddays sex##treat incomecat##treat sex##incomecat, fam(poisson) estimates store m0 quietly glm beddays sex##treat incomecat##treat, fam(poisson) estimates store m1 lrtest (m0) (m1) Likelihood-ratio test LR chi2(1) = 10.67 (Assumption: m1 nested in m0) Prob > chi2 = 0.0011 quietly glm beddays sex##treat incomecat##sex, fam(poisson) estimates store m2 lrtest (m0) (m2) Likelihood-ratio test LR chi2(1) = 0.00 (Assumption: m2 nested in m0) Prob > chi2 = 0.9934 quietly glm beddays sex##incomecat incomecat##treat, fam(poisson) estimates store m3 lrtest (m0) (m3) Likelihood-ratio test LR chi2(1) = 12.45 (Assumption: m3 nested in m0) Prob > chi2 = 0.0004Marginal association:
tab sex incomecat [weight=beddays], chi2 (frequency weights assumed) | 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 tab sex treat [weight=beddays], chi2 (frequency weights assumed) | has a doctor | prescribed or | recommended that you | take medicine, | medical treatment sex | yes no | Total -----------+----------------------+---------- male | 41 70 | 111 female | 107 76 | 183 -----------+----------------------+---------- Total | 148 146 | 294 Pearson chi2(1) = 12.8149 Pr = 0.000 tab incomecat treat [weight=beddays], chi2 (frequency weights assumed) | has a doctor | prescribed or | recommended that you | take medicine, | medical treatment incomecat | yes no | Total -----------+----------------------+---------- 0 | 93 86 | 179 1 | 55 60 | 115 -----------+----------------------+---------- Total | 148 146 | 294 Pearson chi2(1) = 0.4776 Pr = 0.490Page 451 table in the middle of the page.
NOTE: For the first four entries in the table, the glm 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:
use https://stats.idre.ucla.edu/stat/stata/examples/pma5/depress, clear gen incomecat = income >= 20 gen cesdcat = cesd >= 11 collapse (count) beddays, by(sex incomecat treat cesdcat) glm beddays incomecat, fam(poisson) Iteration 0: log likelihood = -87.262043 Iteration 1: log likelihood = -86.914885 Iteration 2: log likelihood = -86.914684 Iteration 3: log likelihood = -86.914684 Generalized linear models No. of obs = 16 Optimization : ML Residual df = 14 Scale parameter = 1 Deviance = 101.3627278 (1/df) Deviance = 7.240195 Pearson = 97.97707068 (1/df) Pearson = 6.998362 Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 11.11434 Log likelihood = -86.91468402 BIC = 62.54649 ------------------------------------------------------------------------------ | OIM beddays | 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 ------------------------------------------------------------------------------ glm beddays sex, fam(poisson) Iteration 0: log likelihood = -85.36959 Iteration 1: log likelihood = -85.030304 Iteration 2: log likelihood = -85.03012 Iteration 3: log likelihood = -85.03012 Generalized linear models No. of obs = 16 Optimization : ML Residual df = 14 Scale parameter = 1 Deviance = 97.59359914 (1/df) Deviance = 6.970971 Pearson = 95.83429331 (1/df) Pearson = 6.845307 Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 10.87876 Log likelihood = -85.03011971 BIC = 58.77736 ------------------------------------------------------------------------------ | OIM beddays | 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 ------------------------------------------------------------------------------ glm beddays treat, fam(poisson) Iteration 0: log likelihood = -94.276466 Iteration 1: log likelihood = -93.930174 Iteration 2: log likelihood = -93.929955 Iteration 3: log likelihood = -93.929955 Generalized linear models No. of obs = 16 Optimization : ML Residual df = 14 Scale parameter = 1 Deviance = 115.3932692 (1/df) Deviance = 8.242376 Pearson = 115.5831174 (1/df) Pearson = 8.255937 Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 11.99124 Log likelihood = -93.92995474 BIC = 76.57703 ------------------------------------------------------------------------------ | OIM beddays | 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 ------------------------------------------------------------------------------ glm beddays cesdcat, fam(poisson) Iteration 0: log likelihood = -69.929698 Iteration 1: log likelihood = -69.57608 Iteration 2: log likelihood = -69.575808 Iteration 3: log likelihood = -69.575808 Generalized linear models No. of obs = 16 Optimization : ML Residual df = 14 Scale parameter = 1 Deviance = 66.68497488 (1/df) Deviance = 4.763212 Pearson = 71.12268314 (1/df) Pearson = 5.080192 Variance function: V(u) = u [Poisson] Link function : g(u) = ln(u) [Log] AIC = 8.946976 Log likelihood = -69.57580758 BIC = 27.86873 ------------------------------------------------------------------------------ | OIM beddays | 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:
quietly glm beddays sex##treat sex##incomecat sex##cesdcat /// treat##incomecat treat##cesdcat incomecat##cesdcat, fam(poisson) estimates store m0 quietly glm beddays sex##treat sex##incomecat sex##cesdcat /// treat##cesdcat incomecat##cesdcat, fam(poisson) estimates store m1 quietly glm beddays sex##treat sex##incomecat sex##cesdcat /// treat##incomecat treat##cesdcat, fam(poisson) estimates store m2 quietly glm beddays sex##incomecat sex##cesdcat /// treat##incomecat treat##cesdcat incomecat##cesdcat, fam(poisson) estimates store m3 quietly glm beddays sex##treat sex##incomecat /// treat##incomecat treat##cesdcat incomecat##cesdcat, fam(poisson) estimates store m4 quietly glm beddays sex##treat sex##incomecat sex##cesdcat /// treat##incomecat incomecat##cesdcat, fam(poisson) estimates store m5 lrtest (m0) (m1) lrtest (m0) (m2) lrtest (m0) (m3) lrtest (m0) (m4) lrtest (m0) (m5) . lrtest (m0) (m1) Likelihood-ratio test LR chi2(1) = 0.00 (Assumption: m1 nested in m0) Prob > chi2 = 0.9653 . lrtest (m0) (m2) Likelihood-ratio test LR chi2(1) = 1.17 (Assumption: m2 nested in m0) Prob > chi2 = 0.2799 . lrtest (m0) (m3) Likelihood-ratio test LR chi2(1) = 11.98 (Assumption: m3 nested in m0) Prob > chi2 = 0.0005 . lrtest (m0) (m4) Likelihood-ratio test LR chi2(1) = 2.34 (Assumption: m4 nested in m0) Prob > chi2 = 0.1259 . lrtest (m0) (m5) Likelihood-ratio test LR chi2(1) = 0.32 (Assumption: m5 nested in m0) Prob > chi2 = 0.5733Third-order terms:
quietly glm beddays sex##treat##incomecat sex##treat##cesdcat /// sex##incomecat##cesdcat treat##incomecat##cesdcat, fam(poisson) estimates store m0 quietly glm beddays sex##treat##cesdcat /// sex##incomecat##cesdcat treat##incomecat##cesdcat, fam(poisson) estimates store m1 quietly glm beddays sex##treat##incomecat /// sex##incomecat##cesdcat treat##incomecat##cesdcat, fam(poisson) estimates store m2 quietly glm beddays sex##treat##incomecat sex##treat##cesdcat /// treat##incomecat##cesdcat, fam(poisson) estimates store m3 quietly glm beddays sex##treat##incomecat sex##treat##cesdcat /// sex##incomecat##cesdcat, fam(poisson) estimates store m4 lrtest (m0) (m1) lrtest (m0) (m2) lrtest (m0) (m3) lrtest (m0) (m4) . lrtest (m0) (m1) Likelihood-ratio test LR chi2(1) = 0.01 (Assumption: m1 nested in m0) Prob > chi2 = 0.9274 . lrtest (m0) (m2) Likelihood-ratio test LR chi2(1) = 1.23 (Assumption: m2 nested in m0) Prob > chi2 = 0.2680 . lrtest (m0) (m3) Likelihood-ratio test LR chi2(1) = 0.01 (Assumption: m3 nested in m0) Prob > chi2 = 0.9205 . lrtest (m0) (m4) Likelihood-ratio test LR chi2(1) = 4.74 (Assumption: m4 nested in m0) Prob > chi2 = 0.0294Page 455
use https://stats.idre.ucla.edu/stat/stata/examples/pma5/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 glm count sex##treat treat##incomecat sex##incomecat, fam(poisson) 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 Residual df = 1 Scale parameter = 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] AIC = 7.105447 Log likelihood = -21.42178998 BIC = -2.078264 --------------------------------------------------------------------------------- | OIM count | Coef. Std. Err. z P>|z| [95% Conf. Interval] ----------------+---------------------------------------------------------------- 1.sex | .4007824 .2253418 1.78 0.075 -.0408794 .8424442 1.treat | .3435135 .2270006 1.51 0.130 -.1013995 .7884266 | sex#treat | 1 1 | -.877427 .252167 -3.48 0.001 -1.371665 -.3831887 | 1.incomecat | .7690847 .2156807 3.57 0.000 .3463583 1.191811 | treat#incomecat | 1 1 | -.0020759 .2493924 -0.01 0.993 -.490876 .4867242 | sex#incomecat | 1 1 | -.8223852 .2533633 -3.25 0.001 -1.318968 -.3258023 | _cons | 3.180861 .1867582 17.03 0.000 2.814821 3.5469 ---------------------------------------------------------------------------------Page 457 top of the page.
use https://stats.idre.ucla.edu/stat/stata/examples/pma5/depress, clear gen incomecat = 0 replace incomecat = 1 if income >= 20 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 Iteration 3: log likelihood = -197.31424 Logistic regression 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 .252167 -3.48 0.001 -1.371665 -.3831887 incomecat | -.0020759 .2493924 -0.01 0.993 -.490876 .4867242 _cons | .5359893 .2347033 2.28 0.022 .0759794 .9959992 ------------------------------------------------------------------------------