Opens in a new tab

Binary Logistic Regression in Stata
Odds Ratios, Coding and Fit Checks

Binary logistic regression in Stata runs through logistic, which prints odds ratios, or logit, which prints log-odds. The outcome has to be coded 0 and 1, and rows can drop out with only a short note when a predictor predicts the outcome perfectly.

What You'll Need
A two-category outcome variable
Predictors with value labels
Which outcome value is the event

IN SHORT

What logistic and logit Estimate From a 0/1 Outcome

A logistic model for a two-category outcome estimates the probability of one category as a function of several predictors, and its coefficients are read as log-odds or, once exponentiated, as odds ratios. To run binary logistic regression in Stata, type logistic followed by the outcome and the predictors for odds ratios, or logit for coefficients on the log-odds scale, with the or option added if ratios are wanted. Stata treats zero as no event and any other nonmissing value as the event, so the coding is checked first. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.

In This Guide

1
Check How the Outcome Is Coded
2
Fit With logistic or logit
3
Read the Perfect-Prediction Note
4
Run estat gof With Groups
5
Check Discrimination and Cutoff
6
Record the Event in the Do-File

ANALYSIS SUPPORT

Analysis Support for This Method

Sparse cells and perfect prediction break the standard fit. A penalized method may then return estimates that stay finite, and the choice rests on the events and predictors, not on a p-value.

Logistic Regression

Odds ratios with intervals for a two-category outcome, with the model, its fit and its discrimination set out for your design and the output Stata produces.

Explore Logistic Regression
Firth's Logistic Regression

A penalized likelihood estimate for sparse data or perfect separation, where the usual fit drops variables or returns very large coefficients with wide intervals.

Explore Firth's Logistic Regression

BEFORE YOU RUN IT

What the Outcome and Predictors Must Settle First

Whether the odds ratios that print mean what the text will say depends on four settings made before fitting, and the output announces none of them.

Zero and One in the Outcome Stata reads zero as no event and every other nonmissing value as the event. A variable coded 1 and 2 makes every row an event, and the command stops with an error.

Events Relative to Predictors The count of rows in the rarer outcome category limits how many parameters the model can carry. Few events with many predictors give unstable odds ratios, however large the file is.

A Reference Group per Factor Odds ratios for a categorical predictor compare each level with its base. Factor-variable notation with a chosen base, such as ib1.group, makes that comparison explicit and reportable.

Sparse Cells Examined Cross-tabulate each categorical predictor with the outcome first. An empty cell shows that a predictor may predict the outcome perfectly, and Stata then drops it with a note.

IN STATA

How to Run Binary Logistic Regression in Stata

logistic and logit fit the same model and both belong to official Stata, so choosing between them changes the display and not the estimates.

1

Check How the Outcome Is Coded

Tabulate the outcome and confirm that it holds only 0, 1 and missing, with the event coded 1.

Check: A label saying yes or no does not show the code underneath. Tabulate with the nolabel option to see the numbers Stata reads, because only the numbers decide which category is the event.

2

Fit With logistic or logit

Fit the model with logistic to get odds ratios, or with logit to get log-odds coefficients, adding or to show ratios.

Tip: The two commands give identical estimates and the same log likelihood, so a coefficient from logit exponentiates to the odds ratio from logistic. Report the scale named in the methods.

3

Read the Perfect-Prediction Note

Look above the table for any note that a predictor was dropped and that a number of observations were not used.

Watch: Those rows are not in the estimates, so the sample in the header is smaller than the file. Report the loss, and consider recategorizing the variable or using a penalized method.

4

Run estat gof With Groups

Run estat gof with group(10) and table to see observed and expected events across ten risk groups.

Watch: A non-significant Hosmer-Lemeshow result does not show that the model fits well. It has little power in small samples and can change with the number of groups, so read the table beside it.

5

Check Discrimination and Cutoff

Run lroc for the area under the ROC curve and estat classification for the table of correct and incorrect calls at a cutoff.

Tip: Both are computed on the data that fitted the model, so they flatter it. The default cutoff of 0.5 also suits rare events badly, so state the cutoff that was used.

6

Record the Event in the Do-File

Save the commands in a do-file with a comment naming the category coded 1 and the base level of each factor.

Tip: An odds ratio for the wrong event is a common reporting error, and a comment beside the model makes the coding checkable by a collaborator without opening the data.

Stata Commands
tabulate outcome, nolabel
logistic outcome c.age i.group
logit outcome c.age i.group, or
estat gof, group(10) table
lroc
estat classification

READING THE OUTPUT

Reading the Header, the Ratios and the Fit Tables

A logistic fit prints a likelihood header, a table of ratios and, only on request, the fit tables, and each carries its own caution.

Output element What it tells you What to check
Number of obs and LR chi2(k) Rows used and the likelihood-ratio test that all slopes are zero That the rows used match the plan after dropped observations
Log likelihood Value at the fit, the basis for comparing nested models That compared models use exactly the same rows
Pseudo R2 McFadden’s measure on the likelihood scale That it is not read as variance explained
Odds ratio column (logistic) or Coefficient (logit; Coef. in older releases) Multiplicative change in odds, or change in log-odds That the printed scale is the scale reported
[95% conf. interval] Range compatible with the data, on the scale printed That ratios are read against 1 and coefficients against 0
z and p-value columns Wald test of each coefficient That Wald p-values are not the only evidence in small samples
estat gof table of groups Observed and expected events by risk group That calibration is judged from the table, not only the p-value
lroc area under the ROC curve Discrimination in the fitting sample That its source in the same data is stated

REPORTING

Reporting Odds Ratios, Events and Fit From Stata

The sentence around an odds ratio has to carry the coding, the sample and the scale, none of which the table of ratios states by itself.

Report each predictor as an odds ratio with its 95% CI [___, ___] and p = ___, naming the reference level of every factor and the outcome category coded 1. Give the number of observations and of events used, since rows lost to missing values or to a predictor that predicts the outcome perfectly are not in the estimates. Describe calibration with the table of groups or a plot, and discrimination as the area under the ROC curve = ___, noting that both come from the fitting sample. Say whether the odds ratios are adjusted for the other predictors, and word findings as differences in odds, not as risks. Give the number of predictors beside the events, since that ratio limits what the model can support. Name the command and release if a penalized or exact method replaced the standard fit.

Event Coded 1 NamedReference LevelsEvents and n UsedFit Source StatedOdds, Not Risk

COMMON MISTAKES

How Odds Ratios Lose Their Meaning in Print

Every item here is a detail printed in the output or fixed in the do-file, and none shows in the table of ratios itself.

Odds Ratio Worded as Risk An odds ratio of 2 does not mean twice the risk, and the two diverge when the outcome is common. The sentence names odds, or another estimate is used to report risks.

Wrong Event Modeled If the category coded 1 is the one the text calls the reference, every odds ratio describes the opposite outcome from the one under discussion in the paragraph.

Dropped Observations Missed A predictor that predicts the outcome perfectly is removed with a short note, and the rows it determined are not used. A report listing that predictor, or the full n, misdescribes the model.

Fit Test as Proof of Fit A non-significant estat gof result is read as showing a well-fitting model. It reflects power and grouping as much as fit, so calibration is judged from the groups too.

In-Sample Discrimination Unqualified An area under the curve from lroc is computed on the data that built the model. Without a statement to that effect, or a validation step, it overstates accuracy for new cases.

Pseudo R-Squared Read as Variance The Pseudo R2 in the header is McFadden's measure on the likelihood scale. It is not a share of variance explained, so it is not set against R-squared from linear models.

Ratios printed, coding not yet confirmed? Independent Statistical Review sets the event coding, the rows Stata dropped and the fit checks beside each odds ratio you report.

Request an Assessment

What's Next

An outcome with three or more unordered categories leads to Multinomial Logistic Regression.
Ordered categories such as mild, moderate and severe are covered under Ordinal Regression.
An association between two categorical variables, before any model, is tested with the Chi-Square Test.

FREQUENTLY ASKED QUESTIONS

Questions About logistic, logit and Fit Checks

The printed output settles none of the next three questions by itself, since each turns on how Stata displays, groups or drops what it fitted.

Both fit the same model: logistic displays odds ratios and logit displays log-odds coefficients. Adding the or option to logit gives the same ratios. The log likelihood, z statistics and p-values agree, while the standard error printed by logistic belongs to the odds ratio, so the report names the scale it uses.

Without options it compares observed and expected counts across covariate patterns, and with group(10) it forms the Hosmer-Lemeshow groups. The first form is unreliable when continuous predictors give nearly as many patterns as rows. In either form a large p-value is an absence of detected misfit, not proof of fit, so the table of groups is read too.

It drops the predictor and the observations it determines, and prints a note saying how many were not used. The remaining model describes a smaller sample. Recategorizing the predictor, merging sparse levels or a penalized method such as Firth’s, often fitted with the user-contributed firthlogit unless your release offers its own, are the usual responses.

An in-house PhD-level statistician at OnlineSPSS reviews the coding, the dropped observations and the fit checks against your output, and prepares the odds ratios for reporting. The step belongs to Results Reporting & Interpretation, and the clinical or substantive reading of the ratios remains yours.

Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.

You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.

Still have a question? Find more answers in our full FAQ.

View All FAQs →

NEXT STEP

Is the Event Coded the
Way the Text Says?

An odds ratio quoted for the opposite event reads just as convincingly as the right one, and only the 0/1 coding in the data shows which event the model describes.

No Commitment Required

Strictly Confidential

Fixed Project Fee

In-House PhD-Level Statisticians

Same-Day Response

Serving Researchers Since 2014