Checking a Regression Model: Assumptions, Diagnostics and What to Do When They Fail

On this page
อ่านฉบับภาษาไทย (Thai version)
Abstract
A regression model is only as good as the promises it makes about the data. This article takes one synthetic cohort of 1,200 adults admitted with pneumonia, 327 of them with diabetes, and checks each model fitted on it. For every model it names the assumption, the Stata command that checks it, what the output looks like when the check fails, and the model to use next. Residual normality and constant variance belong to linear regression only. Logistic regression needs good calibration, the right link and a straight line for age, and it breaks under separation, when one variable predicts the outcome perfectly; Firth's penalised likelihood and exact logistic regression then give a finite answer. Ordinal regression needs proportional odds: oparallel passes on one grade and fails on another, and a ladder of next models leads, last, to mlogit. Count models need a variance close to the mean, and Cox models need hazards that stay proportional.
One cohort, seven outcomes and the question before you report
Our case is a synthetic cohort of 1,200 adults admitted to hospital with pneumonia. Of them, 327 have diabetes, the exposure we study. The same patients carry seven outcomes, from length of stay to death within a year. In part 2 of this set, "Fit it and read it", logistic regression gave an odds ratio of 1.65 for ICU admission with diabetes, adjusted for age and sex.
Before that 1.65 goes into a report, one question is left. Does the model's picture of the data match the data? Every model makes a few promises, which statisticians call its assumptions: about the shape of the outcome, about how age enters, and about patients being independent of each other. When a promise is broken the number can be wrong, and Stata will usually still print it.
This article checks those promises one model at a time. The data are built by code, so no real patient is in them, and the numbers in the text are rounded from the Stata output in the code panes.
Two jobs belong to the other parts of this set, and each has its own article.
One residual block does not fit every model
A common habit is to paste one "assumption check" block after every model: a residual histogram, a normal quantile plot (qnorm) and residuals against fitted values (rvfplot). Those pictures test two promises of linear regression only: equal spread at every fitted value and a bell shape.
A logistic outcome is 0 or 1, so its residuals never form a bell, and a plot of them says nothing about the odds ratio of 1.65. The same holds for ordinal, multinomial, count and Cox models, so each model below is checked against its own promises.
Every model: are the patients independent?
One promise is shared by every model here: each row is a separate patient whose outcome does not depend on another row. It holds in our cohort by construction: each of the 1,200 patients appears once and no clusters exist. No output line tests it; it comes from how the data were collected.
Three designs break it: matched case-control sets, where each case comes with its own controls; patients clustered in hospitals; and repeated measures, where one patient gives several rows. Matched sets need a model that compares each case only with its own controls, called conditional logistic regression (clogit). Clusters need standard errors that allow for the cluster. Repeated measures need a mixed model or GEE (generalized estimating equations), which averages the effect over patients while letting the rows of one patient be alike.
The full lessons are Why Do Logistic and Conditional Logistic Regression Give Different Odds Ratios? for matched sets, Understanding the VCE: Robust Standard Errors, the Sandwich Estimator and vce(cluster id) for cluster standard errors and Population-Averaged Alternatives: GLS, GEE, and Where the Linear Model's Luck Runs Out for mixed models against GEE.
Linear regression: equal spread and a bell shape
Length of stay is the continuous outcome. The model is regress los i.diabetes c.age i.female, and the checks below run straight after it. Linear regression models the mean stay plus a random scatter around it. It promises that the scatter is equally wide for short and long predicted stays, and that it has a bell shape.
Equal width is called constant variance. When it fails, the residuals fan out as the fitted value grows, and that fan is called heteroskedasticity. A test asks whether the spread of the residuals grows with the fitted value; this is the Breusch-Pagan test (estat hettest). It gives chi2 = 0.02 with p = 0.8918, so there is no fan.
For the shape, save the residuals with predict. Skewness measures lean and is 0 for a perfect bell; here it is 0.0012. Kurtosis measures heavy tails and is 3 for a perfect bell; here it is 2.95.
A formal test asks whether the residuals could plausibly come from a bell shape; this is the Shapiro-Wilk test, and it agrees (W = 0.99869, p = 0.52666). With 1,200 patients it would flag even a harmless departure, so read it beside the skewness and kurtosis.
In your own analysis also draw rvfplot and qnorm. A fan makes the standard errors wrong, not the mean difference, so refit with vce(robust). A strongly skewed outcome such as cost may suit a generalized linear model with a gamma family and a log link, which models a ratio of means; see Link and Family: One Picks Your Effect Measure, the Other Your Standard Error.
estat hettest
* read: Prob > chi2 (Breusch-Pagan; small p = a fan, unequal spread)
predict los_residual, residuals
label variable los_residual "Residual of regress los (days)"
summarize los_residual, detail
* read: Skewness (near 0) and Kurtosis (near 3) of the residuals
Assumption: Normal error terms
Variable: Fitted values of los
H0: Constant variance
chi2(1) = 0.02
Prob > chi2 = 0.8918
. * read: Prob > chi2 (Breusch-Pagan; small p = a fan, unequal spread)
. predict los_residual, residuals
. label variable los_residual "Residual of regress los (days)"
. summarize los_residual, detail
Residual of regress los (days)
-------------------------------------------------------------
Percentiles Smallest
1% -5.124617 -6.218624
5% -3.586443 -6.022003
10% -2.762799 -5.899545 Obs 1,200
25% -1.444301 -5.818961 Sum of wgt. 1,200
50% -.0090843 Mean -2.08e-09
Largest Std. dev. 2.14977
75% 1.481376 6.301649
90% 2.860263 6.341672 Variance 4.621512
95% 3.517357 6.558582 Skewness .0012484
99% 4.749958 7.27951 Kurtosis 2.951542
swilk los_residual
* read: W and Prob>z (with n = 1,200 even tiny departures give small p)
Variable | Obs W V z Prob>z
-------------+------------------------------------------------------
los_residual | 1,200 0.99869 0.974 -0.067 0.52666
Logistic regression: calibration and the link
ICU admission is yes or no. The model is logit icu i.diabetes c.age i.female, or, which gave the odds ratio of 1.65 for diabetes, and the checks below run straight after it. Its residuals have no bell to check. Instead the model promises three things: its predicted risks agree with what happened (calibration), the logit link is the right scale, and age enters as a straight line on the log-odds scale.
To check the first promise, sort the 1,200 patients into 10 groups by predicted risk and compare predicted with observed ICU admissions in each group; this is the Hosmer-Lemeshow test. A large p means no evidence of misfit: here chi2(8) = 3.05 with p = 0.9310.
The area under the ROC curve, 0.6413, is not an assumption check: it says how well the model ranks patients, and a correct model can still rank modestly.
linktest checks the second promise. It refits ICU admission on the predicted log-odds and on its square. If the model is right, the square adds nothing. Its row _hatsq has p = 0.902 here; a small p would point to a wrong link or a missing curve.
A risk ratio from glm y x, family(binomial) link(log) eform needs one more check, because this log-binomial model often fails without an error. The second pane below shows our ICU fit: the diabetes risk ratio prints with a dot for its standard error, the footer says "Warning: Convergence not achieved", and display e(converged) shows 0. Then step down to glm y x, family(poisson) link(log) vce(robust) eform, the modified Poisson model, as Getting a Risk Ratio for a Common Outcome: The Step-Down When Log-Binomial Hits the Wall explains.
logit icu i.diabetes c.age i.female, or
estimates store model_icu_logit
* quietly hides the long table; the two display lines print the test.
quietly estat gof, group(10)
display as text "Hosmer-Lemeshow chi2(" r(df) ") = " %4.2f r(chi2)
display as text "Prob > chi2 = " %6.4f r(p)
* read: Prob > chi2 (large p = no misfit seen)
lroc, nograph
* read: Area under ROC curve (ranking, not an assumption check)
linktest
* read: _hatsq row, P>|z| (small p = wrong link or a missing curve)
. logit icu i.diabetes c.age i.female, or
Iteration 0: Log likelihood = -684.51086
Iteration 1: Log likelihood = -655.55088
Iteration 2: Log likelihood = -655.09143
Iteration 3: Log likelihood = -655.09116
Iteration 4: Log likelihood = -655.09116
Logistic regression Number of obs = 1,200
LR chi2(3) = 58.84
Prob > chi2 = 0.0000
Log likelihood = -655.09116 Pseudo R2 = 0.0430
------------------------------------------------------------------------------
icu | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.646897 .2387784 3.44 0.001 1.239519 2.188162
age | 1.03403 .0061759 5.60 0.000 1.021996 1.046206
|
female |
Female | .636516 .0885237 -3.25 0.001 .4846499 .8359696
_cons | .0356252 .0151098 -7.86 0.000 .0155144 .0818048
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. estimates store model_icu_logit
. * quietly hides the long table; the two display lines print the test.
. quietly estat gof, group(10)
. display as text "Hosmer-Lemeshow chi2(" r(df) ") = " %4.2f r(chi2)
Hosmer-Lemeshow chi2(8) = 3.05
. display as text "Prob > chi2 = " %6.4f r(p)
Prob > chi2 = 0.9310
. * read: Prob > chi2 (large p = no misfit seen)
. lroc, nograph
Logistic model for icu
Number of observations = 1200
Area under ROC curve = 0.6413
. * read: Area under ROC curve (ranking, not an assumption check)
. linktest
Iteration 0: Log likelihood = -684.51086
Iteration 1: Log likelihood = -656.06145
Iteration 2: Log likelihood = -655.08728
Iteration 3: Log likelihood = -655.0835
Iteration 4: Log likelihood = -655.0835
Logistic regression Number of obs = 1,200
LR chi2(2) = 58.85
Prob > chi2 = 0.0000
Log likelihood = -655.0835 Pseudo R2 = 0.0430
------------------------------------------------------------------------------
icu | Coefficient Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
_hat | .9501319 .425214 2.23 0.025 .1167278 1.783536
_hatsq | -.0247296 .2001229 -0.12 0.902 -.4169633 .367504
_cons | -.0189437 .2151431 -0.09 0.930 -.4406163 .402729
------------------------------------------------------------------------------
* Stata does not stop here. Read the footer and e(converged).
glm icu i.diabetes c.age i.female, family(binomial) link(log) eform nolog
display as text "log-binomial fit: e(converged) = " e(converged)
* read: Warning: Convergence not achieved; e(converged) = 0 (1 = yes)
. glm icu i.diabetes c.age i.female, family(binomial) link(log) eform nolog
convergence not achieved
Generalized linear models Number of obs = 1,200
Optimization : ML Residual df = 1,199
Scale parameter = 1
Deviance = 1652.086292 (1/df) Deviance = 1.377887
Pearson = 100001576.4 (1/df) Pearson = 83404.15
Variance function: V(u) = u*(1-u) [Bernoulli]
Link function : g(u) = ln(u) [Log]
AIC = 1.378405
Log likelihood = -826.0431461 BIC = -6848.916
------------------------------------------------------------------------------
| OIM
icu | Risk ratio std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.224657 . . . . .
age | 1.014894 1.03e-10 1.5e+08 0.000 1.014894 1.014894
|
female |
Female | .3517312 .029269 -12.56 0.000 .2987988 .4140406
_cons | .2067673 . . . . .
------------------------------------------------------------------------------
Note: _cons estimates baseline risk.
Warning: Parameter estimates produce inadmissible mean estimates in one or
more observations.
Warning: Convergence not achieved.
. display as text "log-binomial fit: e(converged) = " e(converged)
log-binomial fit: e(converged) = 0
Is age a straight line on the log-odds scale?
Age is continuous, and the model assumes each extra year multiplies the odds of ICU by the same factor: a straight line on the log-odds scale. To test it, let the line bend and see whether the bend helps.
A restricted cubic spline does this. mkspline cuts age at four knots, here 48, 63, 73 and 87 years, and builds three pieces. The first piece is the straight line; the other two can only bend it. If the two bending pieces add nothing, the straight line is enough.
The joint test of the bending pieces gives chi2(2) = 2.69 with p = 0.2604, so age stays linear. When the test fails, keep the spline pieces and report predicted risks at chosen ages with margins, because no single odds ratio per year describes a bent line.
mkspline age_spline = age, cubic nknots(4) displayknots
logit icu i.diabetes age_spline1 age_spline2 age_spline3 i.female, or
estimates store model_icu_logit_age_spline
test age_spline2 age_spline3
. mkspline age_spline = age, cubic nknots(4) displayknots
| knot1 knot2 knot3 knot4
-------------+--------------------------------------------
age | 48 63 73 87
. logit icu i.diabetes age_spline1 age_spline2 age_spline3 i.female, or
Iteration 0: Log likelihood = -684.51086
Iteration 1: Log likelihood = -654.90475
Iteration 2: Log likelihood = -653.65701
Iteration 3: Log likelihood = -653.65185
Iteration 4: Log likelihood = -653.65185
Logistic regression Number of obs = 1,200
LR chi2(5) = 61.72
Prob > chi2 = 0.0000
Log likelihood = -653.65185 Pseudo R2 = 0.0451
------------------------------------------------------------------------------
icu | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.640798 .23825 3.41 0.001 1.234404 2.180985
age_spline1 | 1.079703 .0299725 2.76 0.006 1.022528 1.140076
age_spline2 | .8982303 .0598333 -1.61 0.107 .788292 1.023501
age_spline3 | 1.443484 .350857 1.51 0.131 .8964309 2.324381
|
female |
Female | .6380295 .0887579 -3.23 0.001 .485767 .8380183
_cons | .0034373 .0053226 -3.66 0.000 .0001653 .0714943
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. estimates store model_icu_logit_age_spline
. test age_spline2 age_spline3
( 1) [icu]age_spline2 = 0
( 2) [icu]age_spline3 = 0
chi2( 2) = 2.69
Prob > chi2 = 0.2604
Separation: when one variable predicts the outcome perfectly
The cohort is too well behaved to show the next problem, so the do-file builds a small demo inside itself. It has 30 patients, and 10 of them took drug X. All 10 who took the drug had the outcome. Among the 20 who did not, 8 had it and 12 did not.
Try the odds ratio by hand. On the drug the odds of the outcome are 10 to 0, which is infinite. So no finite odds ratio fits these data best: the larger the number, the better the fit, without end. This is called separation: a predictor, or a mix of predictors, splits the outcome with no overlap.
The drug shows quasi-complete separation, because the patients without the drug have both outcomes. Stata does not stop. It prints a note, drops the drug and its 10 patients, and fits what is left: note: drug != 0 predicts success perfectly; drug omitted and 10 obs not used. The header then says Number of obs = 20, not 30, and the exposure has silently left the model.
Complete separation is the stronger kind. The demo biomarker is above 2.2 in every patient with the outcome and at or below 2.2 in every patient without it. Here Stata stops with r(2000) and the message outcome = biomarker > 2.2 predicts data perfectly. The pane below runs it under capture noisily, so the do-file prints the message and carries on.
tabulate outcome drug
* (a) Quasi-complete: Stata prints a note, drops drug and 10 patients
logit outcome drug
* read: note: drug != 0 predicts success perfectly
* (b) Complete: Stata stops with r(2000); capture noisily goes on
capture noisily logit outcome biomarker
display as text "return code: r(" _rc ")"
. tabulate outcome drug
DEMO: | DEMO: took drug X
outcome | 0 1 | Total
-----------+----------------------+----------
0 | 12 0 | 12
1 | 8 10 | 18
-----------+----------------------+----------
Total | 20 10 | 30
.
. * (a) Quasi-complete: Stata prints a note, drops drug and 10 patients
. logit outcome drug
note: drug != 0 predicts success perfectly;
drug omitted and 10 obs not used.
Iteration 0: Log likelihood = -13.460233
Iteration 1: Log likelihood = -13.460233
Logistic regression Number of obs = 20
LR chi2(0) = 0.00
Prob > chi2 = .
Log likelihood = -13.460233 Pseudo R2 = 0.0000
------------------------------------------------------------------------------
outcome | Coefficient Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
drug | 0 (omitted)
_cons | -.4054651 .4564355 -0.89 0.374 -1.300062 .489132
------------------------------------------------------------------------------
. * read: note: drug != 0 predicts success perfectly
.
. * (b) Complete: Stata stops with r(2000); capture noisily goes on
. capture noisily logit outcome biomarker
outcome = biomarker > 2.2 predicts data perfectly
. display as text "return code: r(" _rc ")"
return code: r(2000)
Two remedies: Firth and exact logistic regression
Two remedies keep every patient and give a finite answer. The first is Firth's penalised likelihood. It adds a small penalty to the usual fit, and the penalty pulls an estimate that would run to infinity back to a finite value. It is user-written: install it once with ssc install firthlogit.
For the drug, firthlogit outcome drug, or gives an odds ratio of 30.9 with a 95% CI of 1.59 to 600.7. The interval is very wide: the data say the drug raises the odds a lot, but not by how much. For the biomarker it gives 34.1 per unit (95% CI 2.33 to 499.0).
Both are Wald intervals, the estimate plus or minus about two standard errors on the log-odds scale. Heinze and Schemper recommend a profile penalised-likelihood interval instead, which can differ.
The second remedy is exact logistic regression, official Stata as exlogistic. It lists every way the data could have come out with the same totals and counts how extreme the observed table is. The usual estimate is +inf, so Stata reports the middle value among the odds ratios the exact calculation cannot rule out, called the median unbiased estimate. It gives an odds ratio of 17.6, a 95% CI from 2.36 to infinity, and p = 0.0029.
Exact methods suit small data like this demo and slow down fast as covariates are added. Two moves do not help: dropping the variable, which the note already did, and switching to clogit, which answers a matched design, not separation.
* Remedy 1: Firth penalised likelihood (ssc install firthlogit)
firthlogit outcome drug, or
* read: drug, odds ratio and 95% CI (finite, unlike plain logit)
firthlogit outcome biomarker, or
* read: biomarker, odds ratio and 95% CI
* Remedy 2: exact logistic regression (official Stata)
exlogistic outcome drug
* read: drug, odds ratio marked * = median unbiased estimate
. firthlogit outcome drug, or
Initial: Penalized log likelihood = -18.968308
Rescale: Penalized log likelihood = -18.968308
Iteration 0: Penalized log likelihood = -18.968308
Iteration 1: Penalized log likelihood = -13.906352
Iteration 2: Penalized log likelihood = -13.564489
Iteration 3: Penalized log likelihood = -13.557624
Iteration 4: Penalized log likelihood = -13.557623
Number of obs = 30
Wald chi2(1) = 5.13
Penalized log likelihood = -13.557623 Prob > chi2 = 0.0235
------------------------------------------------------------------------------
outcome | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
drug | 30.88233 46.76286 2.27 0.023 1.587807 600.6515
_cons | .68 .3023111 -0.87 0.386 .2845023 1.625294
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. * read: drug, odds ratio and 95% CI (finite, unlike plain logit)
. firthlogit outcome biomarker, or
Initial: Penalized log likelihood = -17.98391
Rescale: Penalized log likelihood = -17.98391
Iteration 0: Penalized log likelihood = -17.98391
Iteration 1: Penalized log likelihood = -1.9472574
Iteration 2: Penalized log likelihood = -1.5886269 (not concave)
Iteration 3: Penalized log likelihood = -1.4682851
Iteration 4: Penalized log likelihood = -1.4002217
Iteration 5: Penalized log likelihood = -1.3739669
Iteration 6: Penalized log likelihood = -1.3685597
Iteration 7: Penalized log likelihood = -1.3685335
Iteration 8: Penalized log likelihood = -1.3685335
Number of obs = 30
Wald chi2(1) = 6.65
Penalized log likelihood = -1.3685335 Prob > chi2 = 0.0099
------------------------------------------------------------------------------
outcome | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
biomarker | 34.09847 46.68389 2.58 0.010 2.330037 499.0074
_cons | .0000725 .0002783 -2.48 0.013 3.91e-08 .1343629
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. * read: biomarker, odds ratio and 95% CI
.
. * Remedy 2: exact logistic regression (official Stata)
. exlogistic outcome drug
Enumerating sample-space combinations:
Observation 1: Enumerations = 2
Observation 2: Enumerations = 3
Observation 3: Enumerations = 4
Observation 4: Enumerations = 5
Observation 5: Enumerations = 6
Observation 6: Enumerations = 7
Observation 7: Enumerations = 8
Observation 8: Enumerations = 9
Observation 9: Enumerations = 10
Observation 10: Enumerations = 11
Observation 11: Enumerations = 22
Observation 12: Enumerations = 33
Observation 13: Enumerations = 43
Observation 14: Enumerations = 52
Observation 15: Enumerations = 60
Observation 16: Enumerations = 67
Observation 17: Enumerations = 73
Observation 18: Enumerations = 78
Observation 19: Enumerations = 81
Observation 20: Enumerations = 82
Observation 21: Enumerations = 81
Observation 22: Enumerations = 78
Observation 23: Enumerations = 73
Observation 24: Enumerations = 67
Observation 25: Enumerations = 60
Observation 26: Enumerations = 52
Observation 27: Enumerations = 43
Observation 28: Enumerations = 33
Observation 29: Enumerations = 22
Observation 30: Enumerations = 11
note: CMLE estimate for drug is +inf; computing MUE.
Exact logistic regression Number of obs = 30
Model score = 9.666667
Pr >= score = 0.0041
---------------------------------------------------------------------------
outcome | Odds ratio Suff. 2*Pr(Suff.) [95% conf. interval]
-------------+-------------------------------------------------------------
drug | 17.63132* 10 0.0029 2.359387 +inf
---------------------------------------------------------------------------
(*) median unbiased estimates (MUE)
Ordinal logistic regression: one odds ratio for every split?
Severity has three ordered grades: mild, moderate and severe. There are two ways to split them in two. Split 1 puts moderate or severe against mild (Y > 0). Split 2 puts severe against mild or moderate (Y > 1).
Ordinal logistic regression fits one odds ratio for diabetes that must serve both splits. The model is ologit severity i.diabetes c.age i.female, or, and the checks below run straight after it. In the cohort that single cumulative odds ratio is 1.90. It says the odds of being in a higher grade, against that grade or lower, are 1.90 times as high with diabetes, at either split.
That "one odds ratio for every split" is the proportional odds assumption. The plain way to see it is to fit one ordinary logistic regression per split and compare the two odds ratios. If they are close, one number is a fair summary; if they are far apart, it hides the difference.
Which formal test runs depends on your Stata. estat parallel after ologit needs StataNow; plain Stata 18 answers "estat parallel not valid", r(321). brant needs the SPost13 package, absent in the Stata 18 used here, so Stata answers "command brant is unrecognized", r(199). The user-written oparallel (ssc install oparallel) runs on Stata 18 and gives five tests at once.
On severity all five agree: the likelihood-ratio test gives chi2 = 3.435 on 3 degrees of freedom with p = 0.329, and the Brant test p = 0.331. There is no evidence against proportional odds, as expected from grades generated on one underlying scale.
A second check, the user-written gologit2 (ssc install gologit2) with autofit, gives a covariate its own odds ratio per split only when the data demand it. On severity it keeps diabetes on one shared odds ratio (p = 0.1605), agreeing with oparallel.
ologit severity i.diabetes c.age i.female, or
estimates store model_severity_ologit
* estat parallel needs StataNow; plain Stata 18 refuses it.
capture noisily estat parallel
display as text "return code: r(" _rc ") (0 = StataNow)"
* brant needs the SPost13 package.
capture noisily brant
display as text "return code: r(" _rc ") (199 = brant not installed)"
* oparallel (ssc install oparallel) works on Stata 18 after ologit
oparallel
* read: all five tests; large p = no evidence against proportional odds
* Partial proportional odds (ssc install gologit2): autofit frees only
* the variables that fail the parallel-lines test.
gologit2 severity i.diabetes c.age i.female, autofit or
* read: Step lines (P Value of each variable) and the final model
. ologit severity i.diabetes c.age i.female, or
Iteration 0: Log likelihood = -1202.5936
Iteration 1: Log likelihood = -1160.4847
Iteration 2: Log likelihood = -1160.281
Iteration 3: Log likelihood = -1160.281
Ordered logistic regression Number of obs = 1,200
LR chi2(3) = 84.63
Prob > chi2 = 0.0000
Log likelihood = -1160.281 Pseudo R2 = 0.0352
------------------------------------------------------------------------------
severity | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.898396 .2341316 5.20 0.000 1.490758 2.417501
age | 1.03446 .0051357 6.82 0.000 1.024443 1.044575
|
female |
Female | 1.081509 .122423 0.69 0.489 .8663174 1.350153
-------------+----------------------------------------------------------------
/cut1 | 2.638261 .3472221 1.957719 3.318804
/cut2 | 4.161577 .3599673 3.456054 4.8671
------------------------------------------------------------------------------
Note: Estimates are transformed only in the first equation to odds ratios.
. estimates store model_severity_ologit
.
. * estat parallel needs StataNow; plain Stata 18 refuses it.
. capture noisily estat parallel
estat parallel not valid
. display as text "return code: r(" _rc ") (0 = StataNow)"
return code: r(321) (0 = StataNow)
.
. * brant needs the SPost13 package.
. capture noisily brant
command brant is unrecognized
. display as text "return code: r(" _rc ") (199 = brant not installed)"
return code: r(199) (199 = brant not installed)
.
. * oparallel (ssc install oparallel) works on Stata 18 after ologit
. oparallel
Tests of the parallel regression assumption
| Chi2 df P>Chi2
-----------------+----------------------
Wolfe Gould | 3.514 3 0.319
Brant | 3.424 3 0.331
score | 3.426 3 0.330
likelihood ratio | 3.435 3 0.329
Wald | 3.432 3 0.330
. * read: all five tests; large p = no evidence against proportional odds
.
. * Partial proportional odds (ssc install gologit2): autofit frees only
. * the variables that fail the parallel-lines test.
. gologit2 severity i.diabetes c.age i.female, autofit or
------------------------------------------------------------------------------
Testing parallel lines assumption using the .05 level of significance...
Step 1: Constraints for parallel lines imposed for 1.female (P Value = 0.8843
> )
Step 2: Constraints for parallel lines imposed for age (P Value = 0.2353)
Step 3: Constraints for parallel lines imposed for 1.diabetes (P Value = 0.16
> 05)
Step 4: All explanatory variables meet the pl assumption
Wald test of parallel lines assumption for the final model:
( 1) [Mild]1.female - [Moderate]1.female = 0
( 2) [Mild]1.diabetes - [Moderate]1.diabetes = 0
( 3) [Mild]age - [Moderate]age = 0
chi2( 3) = 3.43
Prob > chi2 = 0.3297
When proportional odds fails
To see a failure, the do-file builds a second grade, severity_npo, for the same 1,200 patients. Diabetes matters little for leaving mild but a lot for reaching severe. With diabetes 40.37% are severe, against 13.40% without. For mild the gap is small: 40.06% against 44.79%.
ologit still prints one tidy cumulative odds ratio, 1.92, and nothing in its table warns you. oparallel does. Every test gives p = 0.000, and the likelihood-ratio test gives chi2 = 83.17. Large samples make these tests reject even small departures, so a small p starts a decision rather than ending one.
tabulate severity_npo diabetes, column
* The same check as for severity, now on severity_npo:
* here oparallel should reject.
ologit severity_npo i.diabetes c.age i.female, or
estimates store model_severity_npo_ologit
oparallel
* read: all five tests; small p = proportional odds fails
. tabulate severity_npo diabetes, column
+-------------------+
| Key |
|-------------------|
| frequency |
| column percentage |
+-------------------+
DEMO: |
severity, |
non-propor |
tional | Diabetes (exposure)
odds | No Yes | Total
-----------+----------------------+----------
Mild | 391 131 | 522
| 44.79 40.06 | 43.50
-----------+----------------------+----------
Moderate | 365 64 | 429
| 41.81 19.57 | 35.75
-----------+----------------------+----------
Severe | 117 132 | 249
| 13.40 40.37 | 20.75
-----------+----------------------+----------
Total | 873 327 | 1,200
| 100.00 100.00 | 100.00
.
. * The same check as for severity, now on severity_npo:
. * here oparallel should reject.
. ologit severity_npo i.diabetes c.age i.female, or
Iteration 0: Log likelihood = -1267.3789
Iteration 1: Log likelihood = -1228.6462
Iteration 2: Log likelihood = -1228.5084
Iteration 3: Log likelihood = -1228.5083
Ordered logistic regression Number of obs = 1,200
LR chi2(3) = 77.74
Prob > chi2 = 0.0000
Log likelihood = -1228.5083 Pseudo R2 = 0.0307
------------------------------------------------------------------------------
severity_npo | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.917858 .2462288 5.07 0.000 1.49119 2.466608
age | 1.031426 .0048408 6.59 0.000 1.021982 1.040958
|
female |
Female | .9445405 .104028 -0.52 0.604 .761154 1.172111
-------------+----------------------------------------------------------------
/cut1 | 1.946518 .3233202 1.312822 2.580213
/cut2 | 3.632491 .3362017 2.973548 4.291434
------------------------------------------------------------------------------
Note: Estimates are transformed only in the first equation to odds ratios.
. estimates store model_severity_npo_ologit
. oparallel
Tests of the parallel regression assumption
| Chi2 df P>Chi2
-----------------+----------------------
Wolfe Gould | 83.58 3 0.000
Brant | 92.38 3 0.000
score | 103.5 3 0.000
likelihood ratio | 83.17 3 0.000
Wald | 95.97 3 0.000
The ladder, step by step
Step 1: look at the split odds ratios. Separate logits give 1.11 for split 1 and 4.11 for split 2. An odds ratio near 1 against one near 4 changes the clinical message: diabetes barely moves patients out of mild, but it strongly pushes them to severe. The single 1.92 hides that.
Step 2: free only what fails. gologit2 with autofit tests each covariate and frees only the ones that break the assumption. Here it keeps age and sex on one shared odds ratio and frees diabetes only (p = 0.00000). Diabetes now has two odds ratios: 1.09 for split 1 and 4.15 for split 2.
This is the partial proportional odds model. Its output names each equation after the category below the split: the equation labelled Mild is split 1, and Moderate is split 2.
Step 3: keep the order another way. The stereotype logit slogit (official) gives each grade its own score on one shared scale, so the effect of diabetes can be stronger for some grades than for others. The continuation-ratio model ocratio (ssc install ocratio) asks, grade by grade, whether a patient who has reached a grade goes beyond it. Neither is run here.
Step 4: mlogit, last. It drops the order and fits one equation per category, so use it only when the order does not matter for your question. The full comparison of the two families is in Multinomial vs Ordinal Logistic Regression [mlogit & ologit]: Choosing the Right Model for Categorical Outcomes.
* The two splits as separate logits: compare the diabetes rows.
* Split 1: moderate or severe vs mild. Split 2: severe vs the rest.
generate npo_not_mild = severity_npo >= 1 if !missing(severity_npo)
generate npo_severe = severity_npo == 2 if !missing(severity_npo)
logit npo_not_mild i.diabetes c.age i.female, or
estimates store model_severity_npo_split_1
logit npo_severe i.diabetes c.age i.female, or
estimates store model_severity_npo_split_2
* gologit2 with autofit frees proportional odds only where it fails.
gologit2 severity_npo i.diabetes c.age i.female, autofit or
* read: Step 3 (diabetes not constrained); diabetes in Mild vs Moderate
* gologit2 names each equation after the category BELOW the split:
* "Mild" = Y > 0, "Moderate" = Y > 1.
. generate npo_not_mild = severity_npo >= 1 if !missing(severity_npo)
. generate npo_severe = severity_npo == 2 if !missing(severity_npo)
. logit npo_not_mild i.diabetes c.age i.female, or
Iteration 0: Log likelihood = -821.60786
Iteration 1: Log likelihood = -802.95219
Iteration 2: Log likelihood = -802.93078
Iteration 3: Log likelihood = -802.93078
Logistic regression Number of obs = 1,200
LR chi2(3) = 37.35
Prob > chi2 = 0.0000
Log likelihood = -802.93078 Pseudo R2 = 0.0227
------------------------------------------------------------------------------
npo_not_mild | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.107484 .1488367 0.76 0.447 .8510268 1.441226
age | 1.030276 .0052966 5.80 0.000 1.019947 1.040709
|
female |
Female | .9466345 .1128865 -0.46 0.646 .7493353 1.195882
_cons | .171901 .060506 -5.00 0.000 .0862326 .3426774
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. estimates store model_severity_npo_split_1
. logit npo_severe i.diabetes c.age i.female, or
Iteration 0: Log likelihood = -612.75056
Iteration 1: Log likelihood = -551.7707
Iteration 2: Log likelihood = -547.96772
Iteration 3: Log likelihood = -547.95799
Iteration 4: Log likelihood = -547.95798
Logistic regression Number of obs = 1,200
LR chi2(3) = 129.59
Prob > chi2 = 0.0000
Log likelihood = -547.95798 Pseudo R2 = 0.1057
------------------------------------------------------------------------------
npo_severe | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 4.114223 .6282828 9.26 0.000 3.050007 5.549767
age | 1.038772 .0070581 5.60 0.000 1.02503 1.052698
|
female |
Female | .9934492 .1515543 -0.04 0.966 .7367018 1.339675
_cons | .0112169 .0055606 -9.06 0.000 .0042453 .0296375
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
. estimates store model_severity_npo_split_2
.
. * gologit2 with autofit frees proportional odds only where it fails.
. gologit2 severity_npo i.diabetes c.age i.female, autofit or
------------------------------------------------------------------------------
Testing parallel lines assumption using the .05 level of significance...
Step 1: Constraints for parallel lines imposed for 1.female (P Value = 0.8916
> )
Step 2: Constraints for parallel lines imposed for age (P Value = 0.2443)
Step 3: Constraints for parallel lines are not imposed for
1.diabetes (P Value = 0.00000)
Wald test of parallel lines assumption for the final model:
( 1) [Mild]1.female - [Moderate]1.female = 0
( 2) [Mild]age - [Moderate]age = 0
chi2( 2) = 1.38
Prob > chi2 = 0.5023
An insignificant test statistic indicates that the final model
does not violate the proportional odds/ parallel lines assumption
If you re-estimate this exact same model with gologit2, instead
of autofit you can save time by using the parameter
pl(0b.female 1.female 0b.diabetes age)
------------------------------------------------------------------------------
Generalized Ordered Logit Estimates Number of obs = 1,200
LR chi2(4) = 159.53
Prob > chi2 = 0.0000
Log likelihood = -1187.6123 Pseudo R2 = 0.0629
( 1) [Mild]1.female - [Moderate]1.female = 0
( 2) [Mild]age - [Moderate]age = 0
------------------------------------------------------------------------------
severity_npo | Odds ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
Mild |
diabetes |
Yes | 1.091097 .1466934 0.65 0.517 .8383454 1.420051
|
age | 1.032936 .0049305 6.79 0.000 1.023318 1.042645
|
female |
Female | .9568249 .1062962 -0.40 0.691 .7696092 1.189583
|
_cons | .1450439 .0474641 -5.90 0.000 .0763752 .2754522
-------------+----------------------------------------------------------------
Moderate |
diabetes |
Yes | 4.14703 .629851 9.37 0.000 3.079333 5.584929
|
age | 1.032936 .0049305 6.79 0.000 1.023318 1.042645
|
female |
Female | .9568249 .1062962 -0.40 0.691 .7696092 1.189583
|
_cons | .0169816 .0059244 -11.68 0.000 .0085708 .0336463
------------------------------------------------------------------------------
Note: _cons estimates baseline odds.
Multinomial logistic regression: cells, base and IIA
Discharge destination has three unordered categories: home, rehabilitation unit and nursing home. mlogit fits one equation per category except the base, all together, each comparing that category with the base. That costs parameters: its likelihood-ratio test has 6 degrees of freedom, against 3 for ologit with the same covariates. The model is mlogit dispo i.diabetes c.age i.female, written here without baseoutcome() on purpose.
So first check the cells with a plain cross-table. Among the 327 patients with diabetes, 53 went to a rehabilitation unit, the smallest cell, and 74 went to a nursing home. A cell of a handful of patients gives an unstable equation: merge categories or say that it is too sparse.
Then check the base. Without baseoutcome(), Stata picks the most frequent category, not the lowest code. Home is both code 0 and the most frequent (854 patients), so the do-file recodes home to 2 and fits again, and Stata still picks home: e(baseout) = 2. Write baseoutcome(#) every time.
Age must be a straight line in each equation, so the spline check above applies to each. IIA, the independence of irrelevant alternatives, means that adding or removing one category should not change the odds between the other two; with home, rehabilitation and nursing home as fixed choices that is rarely a worry.
tabulate dispo diabetes
* Without baseoutcome() Stata uses the most frequent category.
mlogit dispo i.diabetes c.age i.female
display as text "base outcome chosen by Stata, e(baseout) = " e(baseout)
* Home is both code 0 and the most frequent here, so the line above
* cannot tell "lowest code" from "most frequent". Recode home to 2
* (the highest code) and fit the same model again.
generate dispo_home_coded_2 = 2 - dispo // 0 nursing, 1 rehab, 2 home
label define dispo_home_coded_2_label 0 "Nursing home" 1 "Rehabilitation unit" 2 "Home"
label values dispo_home_coded_2 dispo_home_coded_2_label
label variable dispo_home_coded_2 "DEMO: destination, home = 2"
tabulate dispo_home_coded_2
mlogit dispo_home_coded_2 i.diabetes c.age i.female
display as text "base after recoding, e(baseout) = " e(baseout)
. tabulate dispo diabetes
Discharge |
destination | Diabetes (exposure)
(unordered) | No Yes | Total
--------------------+----------------------+----------
Home | 654 200 | 854
Rehabilitation unit | 138 53 | 191
Nursing home | 81 74 | 155
--------------------+----------------------+----------
Total | 873 327 | 1,200
. * Without baseoutcome() Stata uses the most frequent category.
. mlogit dispo i.diabetes c.age i.female
Iteration 0: Log likelihood = -958.73585
Iteration 1: Log likelihood = -901.67976
Iteration 2: Log likelihood = -896.44756
Iteration 3: Log likelihood = -896.433
Iteration 4: Log likelihood = -896.433
Multinomial logistic regression Number of obs = 1,200
LR chi2(6) = 124.61
Prob > chi2 = 0.0000
Log likelihood = -896.433 Pseudo R2 = 0.0650
------------------------------------------------------------------------------
dispo | Coefficient Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
Home | (base outcome)
-------------+----------------------------------------------------------------
Rehabilita~t |
diabetes |
Yes | .1748404 .1821159 0.96 0.337 -.1821003 .5317811
age | .0265636 .0071455 3.72 0.000 .0125586 .0405686
|
female |
Female | .1312631 .1617033 0.81 0.417 -.1856695 .4481957
_cons | -3.406471 .5028751 -6.77 0.000 -4.392088 -2.420854
-------------+----------------------------------------------------------------
Nursing_home |
diabetes |
Yes | .9931329 .1877955 5.29 0.000 .6250605 1.361205
age | .0749393 .0088318 8.49 0.000 .0576293 .0922493
|
female |
Female | -.1243209 .1873336 -0.66 0.507 -.4914881 .2428462
_cons | -7.330181 .6683567 -10.97 0.000 -8.640136 -6.020226
------------------------------------------------------------------------------
. display as text "base outcome chosen by Stata, e(baseout) = " e(baseout)
base outcome chosen by Stata, e(baseout) = 0
.
. * Home is both code 0 and the most frequent here, so the line above
. * cannot tell "lowest code" from "most frequent". Recode home to 2
. * (the highest code) and fit the same model again.
. generate dispo_home_coded_2 = 2 - dispo // 0 nursing, 1 rehab, 2 home
. label define dispo_home_coded_2_label 0 "Nursing home" 1 "Rehabilitation unit
> " 2 "Home"
. label values dispo_home_coded_2 dispo_home_coded_2_label
. label variable dispo_home_coded_2 "DEMO: destination, home = 2"
. tabulate dispo_home_coded_2
DEMO: destination, |
home = 2 | Freq. Percent Cum.
--------------------+-----------------------------------
Nursing home | 155 12.92 12.92
Rehabilitation unit | 191 15.92 28.83
Home | 854 71.17 100.00
--------------------+-----------------------------------
Total | 1,200 100.00
. mlogit dispo_home_coded_2 i.diabetes c.age i.female
Iteration 0: Log likelihood = -958.73585
Iteration 1: Log likelihood = -901.67976
Iteration 2: Log likelihood = -896.44756
Iteration 3: Log likelihood = -896.433
Iteration 4: Log likelihood = -896.433
Multinomial logistic regression Number of obs = 1,200
LR chi2(6) = 124.61
Prob > chi2 = 0.0000
Log likelihood = -896.433 Pseudo R2 = 0.0650
------------------------------------------------------------------------------
dispo_home~2 | Coefficient Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
Nursing_home |
diabetes |
Yes | .9931329 .1877955 5.29 0.000 .6250605 1.361205
age | .0749393 .0088318 8.49 0.000 .0576293 .0922493
|
female |
Female | -.1243209 .1873336 -0.66 0.507 -.4914881 .2428462
_cons | -7.330181 .6683567 -10.97 0.000 -8.640136 -6.020226
-------------+----------------------------------------------------------------
Rehabilita~t |
diabetes |
Yes | .1748404 .1821159 0.96 0.337 -.1821003 .5317811
age | .0265636 .0071455 3.72 0.000 .0125586 .0405686
|
female |
Female | .1312631 .1617033 0.81 0.417 -.1856695 .4481957
_cons | -3.406471 .5028751 -6.77 0.000 -4.392088 -2.420854
-------------+----------------------------------------------------------------
Home | (base outcome)
------------------------------------------------------------------------------
. display as text "base after recoding, e(baseout) = " e(baseout)
base after recoding, e(baseout) = 2
Count and rate models: is the variance close to the mean?
Readmissions are counts: 0, 1, 2 or more per patient, over follow-up that differs between patients. The model is the Poisson rate model poisson readmit_n i.diabetes c.age i.female, exposure(fu_years) irr. It assumes that, among patients with the same covariates and the same follow-up, the variance of the count equals its mean.
As a first look, the raw mean is 0.563 readmissions per patient and the raw variance is 0.943. The counts look more spread out than Poisson allows, and that excess spread is called overdispersion. This raw comparison is only a hint, because follow-up time and covariates also move the variance; the tests below decide. Overdispersion barely moves the rate ratio, but it makes the standard errors, the interval and the p-value too small.
After poisson, estat gof gives two tests of fit. Both reject: deviance 1443.185 and Pearson 1769.635, each on 1196 degrees of freedom, with p = 0.0000. With counts this small, a mean of 0.563, these chi2 tests are only approximate, so they support the decision rather than make it.
The check that decides comes from the negative binomial model (nbreg). It adds one parameter, alpha, for the extra spread; alpha = 0 is Poisson. Its last output line tests exactly that: chibar2(01) = 77.42 with p = 0.000. For the rate model with exposure(fu_years), alpha is 0.754 (95% CI 0.541 to 1.049), so prefer nbreg.
The rate ratio for diabetes barely moves, 1.58 under Poisson and 1.51 under nbreg, whose interval, 1.24 to 1.85, reflects the uncertainty better. Keeping Poisson with vce(robust) also fixes the standard errors. Avoid glm with family(nbinomial), which fixes alpha at 1 unless you write family(nbinomial ml). The rate model itself is taught in Rates, Poisson Regression, and the Missing Lambda Backbone.
tabstat readmit_n, statistics(mean variance)
* first look only: follow-up and covariates also move the raw variance
poisson readmit_n i.diabetes c.age i.female, exposure(fu_years) irr
estimates store model_readmit_rate_poisson
estat gof
* read: Deviance and Pearson, Prob > chi2 (rough with small counts)
nbreg readmit_n i.diabetes c.age i.female, exposure(fu_years) irr
estimates store model_readmit_rate_nbreg
* read: LR test of alpha=0 (small p = extra spread, prefer nbreg)
. tabstat readmit_n, statistics(mean variance)
Variable | Mean Variance
-------------+--------------------
readmit_n | .5633333 .9434418
----------------------------------
. * first look only: follow-up and covariates also move the raw variance
. poisson readmit_n i.diabetes c.age i.female, exposure(fu_years) irr
Iteration 0: Log likelihood = -1206.2177
Iteration 1: Log likelihood = -1206.2177
Poisson regression Number of obs = 1,200
LR chi2(3) = 43.24
Prob > chi2 = 0.0000
Log likelihood = -1206.2177 Pseudo R2 = 0.0176
------------------------------------------------------------------------------
readmit_n | IRR Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.577806 .1267972 5.67 0.000 1.347871 1.846965
age | 1.008897 .00335 2.67 0.008 1.002353 1.015485
|
female |
Female | 1.068134 .082401 0.85 0.393 .9182479 1.242486
_cons | .2297324 .0538322 -6.28 0.000 .1451323 .3636474
ln(fu_years) | 1 (exposure)
------------------------------------------------------------------------------
Note: _cons estimates baseline incidence rate.
. estimates store model_readmit_rate_poisson
. estat gof
Deviance goodness-of-fit = 1443.185
Prob > chi2(1196) = 0.0000
Pearson goodness-of-fit = 1769.635
Prob > chi2(1196) = 0.0000
. * read: Deviance and Pearson, Prob > chi2 (rough with small counts)
. nbreg readmit_n i.diabetes c.age i.female, exposure(fu_years) irr
Fitting Poisson model:
Iteration 0: Log likelihood = -1206.2177
Iteration 1: Log likelihood = -1206.2177
Fitting constant-only model:
Iteration 0: Log likelihood = -1181.1677
Iteration 1: Log likelihood = -1180.6536
Iteration 2: Log likelihood = -1180.6531
Iteration 3: Log likelihood = -1180.6531
Fitting full model:
Iteration 0: Log likelihood = -1168.134
Iteration 1: Log likelihood = -1167.5105
Iteration 2: Log likelihood = -1167.5091
Iteration 3: Log likelihood = -1167.5091
Negative binomial regression Number of obs = 1,200
LR chi2(3) = 26.29
Dispersion: mean Prob > chi2 = 0.0000
Log likelihood = -1167.5091 Pseudo R2 = 0.0111
------------------------------------------------------------------------------
readmit_n | IRR Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.514983 .1535783 4.10 0.000 1.241992 1.847977
age | 1.010164 .0041202 2.48 0.013 1.002121 1.018272
|
female |
Female | 1.062921 .1008768 0.64 0.520 .882506 1.28022
_cons | .2142607 .0612126 -5.39 0.000 .1223942 .37508
ln(fu_years) | 1 (exposure)
-------------+----------------------------------------------------------------
/lnalpha | -.2828409 .1688926 -.6138643 .0481825
-------------+----------------------------------------------------------------
alpha | .7536397 .1272842 .5412552 1.049362
------------------------------------------------------------------------------
Note: Estimates are transformed only in the first equation to incidence-rate
ratios.
Note: _cons estimates baseline incidence rate.
LR test of alpha=0: chibar2(01) = 77.42 Prob >= chibar2 = 0.000
Cox regression: do the hazards stay proportional?
Time to death within 365 days ends the list, and 222 patients died. The data are declared with stset time_d, failure(died) and the model is stcox i.diabetes c.age i.female. It gives one hazard ratio for diabetes, 1.92, and assumes it holds from the first day to the last. That is the proportional hazards assumption.
After stcox, estat phtest, detail tests it. At each death it takes the gap between the covariate value of the patient who died and the average among those still at risk; these gaps are called Schoenfeld residuals. A trend of the gaps with time means the effect changes over time, and a small p flags that trend. For diabetes rho = 0.00696 with p = 0.9167, and the global test gives chi2 = 0.65 on 3 degrees of freedom with p = 0.8860.
So the hazards stay proportional here. When the test fails, let the effect change with time: fit stcox with tvc(diabetes) texp(ln(_t)), or split follow-up with stsplit and estimate one hazard ratio per period. The flexible parametric model stpm2 (ssc install stpm2) also handles this; it is not installed on the Stata 18 used here, so it is not run. The Cox model itself is taught in Cox Proportional Hazards Regression and Related Survival-Analysis Concepts.
stset time_d, failure(died)
stcox i.diabetes c.age i.female
estimates store model_death_cox
estat phtest, detail
* read: each covariate row and Global test (small p = not proportional)
. stset time_d, failure(died)
Survival-time data settings
Failure event: died!=0 & died<.
Observed time interval: (0, time_d]
Exit on or before: failure
--------------------------------------------------------------------------
1,200 total observations
0 exclusions
--------------------------------------------------------------------------
1,200 observations remaining, representing
222 failures in single-record/single-failure data
391,391 total analysis time at risk and under observation
At risk from t = 0
Earliest observed entry t = 0
Last observed exit t = 365
. stcox i.diabetes c.age i.female
Failure _d: died
Analysis time _t: time_d
Iteration 0: Log likelihood = -1552.2399
Iteration 1: Log likelihood = -1509.9605
Iteration 2: Log likelihood = -1509.6317
Iteration 3: Log likelihood = -1509.6317
Refining estimates:
Iteration 0: Log likelihood = -1509.6317
Cox regression with Breslow method for ties
No. of subjects = 1,200 Number of obs = 1,200
No. of failures = 222
Time at risk = 391,391
LR chi2(3) = 85.22
Log likelihood = -1509.6317 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
_t | Haz. ratio Std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
diabetes |
Yes | 1.918519 .2619999 4.77 0.000 1.46799 2.507317
age | 1.039789 .0062316 6.51 0.000 1.027647 1.052075
|
female |
Female | .5688773 .0813645 -3.94 0.000 .4298078 .7529445
------------------------------------------------------------------------------
. estimates store model_death_cox
. estat phtest, detail
Test of proportional-hazards assumption
Time function: Analysis time
--------------------------------------------------------
| rho chi2 df Prob>chi2
-------------+------------------------------------------
0b.diabetes | . . 1 .
1.diabetes | 0.00696 0.01 1 0.9167
age | -0.00683 0.01 1 0.9202
0b.female | . . 1 .
1.female | 0.05311 0.63 1 0.4282
-------------+------------------------------------------
Global test | 0.65 3 0.8860
--------------------------------------------------------
Every model on one line
| Model | Assumption | Check command | Sign of failure | Next step |
|---|---|---|---|---|
| regress (length of stay) | Constant variance; bell-shaped residuals | estat hettest; rvfplot; qnorm of the residuals | Small p; a fan; strongly skewed residuals | vce(robust); glm with a log link |
| logit (ICU) | Calibration; the right link; continuous X linear in the log-odds | estat gof, group(10); linktest; mkspline then test | Small p; a significant _hatsq; spline pieces needed | Keep the spline pieces; report margins |
| logit, small or sparse data | No separation | Read the log for the note or r(2000) | predicts success perfectly; obs not used | firthlogit (user-written); exlogistic |
| glm, binomial family, log link | The fit converged | display e(converged) | Convergence not achieved; e(converged) = 0 | glm y x, family(poisson) link(log) vce(robust) eform |
| ologit (severity) | Proportional odds: one odds ratio for every split | oparallel (user-written, Stata 18); estat parallel (StataNow) | Small p and split odds ratios far apart | gologit2, autofit (user-written); slogit or ocratio; mlogit last |
| mlogit (discharge) | Enough patients in every cell; a base you chose | tabulate y x; display e(baseout) | Tiny cells; a base you did not mean | Merge categories; write baseoutcome(#) |
| poisson (readmissions) | Variance equals the mean, given covariates and follow-up | LR test of alpha = 0 after nbreg (tabstat: a first look only) | Small p for alpha = 0 | nbreg; or poisson with vce(robust) |
| stcox (death) | Proportional hazards | estat phtest, detail | Small p for a covariate or the global test | tvc() with texp(); stsplit; stpm2 (user-written) |
| Every model | Independent patients | The study design, not a test | Matched sets, clusters, repeated rows | clogit; vce(cluster id); mixed or GEE |
References
- Firth D. Bias reduction of maximum likelihood estimates. Biometrika 1993;80(1):27-38. https://doi.org/10.1093/biomet/80.1.27
- Heinze G, Schemper M. A solution to the problem of separation in logistic regression. Statistics in Medicine 2002;21(16):2409-2419. https://doi.org/10.1002/sim.1047
- Brant R. Assessing proportionality in the proportional odds model for ordinal logistic regression. Biometrics 1990;46(4):1171-1178. https://doi.org/10.2307/2532457
- Williams R. Generalized ordered logit/partial proportional odds models for ordinal dependent variables. The Stata Journal 2006;6(1):58-82. https://doi.org/10.1177/1536867X0600600104
- Grambsch PM, Therneau TM. Proportional hazards tests and diagnostics based on weighted residuals. Biometrika 1994;81(3):515-526. https://doi.org/10.1093/biomet/81.3.515
Key takeaways
- Residual normality and constant variance are checks for regress only; every other model has its own.
- Separation is silent or fatal: read the log for predicts success perfectly, then refit with firthlogit or exlogistic.
- For ologit on Stata 18 run oparallel and compare the split odds ratios; free only the failing variable with gologit2 before you drop the order.
- A raw variance above the mean only hints at overdispersion; the nbreg test of alpha = 0 decides.
- Independence comes from the design: matched sets need clogit, clusters need vce(cluster id), mixed or GEE.
Related in the wiki: Link and Family: One Picks Your Effect Measure, the Other Your Standard Error · Multinomial vs Ordinal Logistic Regression [mlogit & ologit]: Choosing the Right Model for Categorical Outcomes · Why Do Logistic and Conditional Logistic Regression Give Different Odds Ratios? · Understanding the VCE: Robust Standard Errors, the Sandwich Estimator and vce(cluster id) · Population-Averaged Alternatives: GLS, GEE, and Where the Linear Model's Luck Runs Out · Getting a Risk Ratio for a Common Outcome: The Step-Down When Log-Binomial Hits the Wall · Rates, Poisson Regression, and the Missing Lambda Backbone · Cox Proportional Hazards Regression and Related Survival-Analysis Concepts