Tutorials › Exam SRM - Statistics for Risk Modeling › Binary Predictors and Indicator Coding

Exam SRM · Logistic Regression · Tutorial 305 of 926

Binary Predictors and Indicator Coding

Represent a two-category predictor with an indicator and interpret its logistic-regression coefficient as the conditional log-odds difference between categories.

Intermediate 10 min read

What You'll Learn

  • Define an indicator variable for a binary categorical predictor
  • Write a logistic model with an indicator and other predictors
  • Derive the coefficient’s conditional log-odds interpretation
  • Convert the coefficient to an odds ratio and compare fitted probabilities
  • Recognize the effect of reversing the zero and one coding
  • Avoid confusing a log-odds contrast with a probability difference

Why Code a Binary Predictor?

A logistic regression can include a predictor that records a category rather than a numerical measurement. For a predictor with two categories, an indicator variable gives each observation a numerical code: zero for one category and one for the other. This lets the model express a direct comparison between the two groups while retaining the logistic model’s interpretation in terms of odds.

For example, a hypothetical insurer might study whether a policyholder files a claim during a year and record whether the policyholder selected an optional roadside feature. The feature is categorical, but it can be represented by an indicator. The resulting coefficient compares the conditional log-odds of a claim for policyholders with the feature against those without it. The comparison is conditional on any other predictors included in the model.

Definition: For a binary categorical predictor, an indicator variable \(Z\) takes the value 1 when an observation belongs to a designated category and 0 when it belongs to the other category. The category coded 0 is the comparison category; the category coded 1 is the designated category.

The zero and one labels are coding choices, not measurements of quantity. If \(Z=0\) means “feature not selected” and \(Z=1\) means “feature selected,” then the value 1 does not mean one unit more of the feature. It identifies a category. The interpretation of the fitted coefficient must follow the category definitions.

Indicator Coding in a Logistic Model

Let \(Y\) be a binary response, and let \(p=P(Y=1\mid Z,x)\), where \(x\) represents any other predictors held in the model. A logistic regression with one binary indicator and no interaction between \(Z\) and \(x\) can be written as

$$ \operatorname{logit}(p) =\log\left(\frac{p}{1-p}\right) =\beta_0+\beta_1Z+\beta_2x. $$

Here, \(\beta_0\) is the intercept, \(\beta_1\) is the indicator coefficient, and \(\beta_2\) is the coefficient of \(x\). If there are no other predictors, the \(\beta_2x\) term is omitted. The model assumes that, conditional on the included predictors, the response is Bernoulli with success probability \(p\), and that the log-odds are represented by the stated linear predictor. When observations are modeled as independent, their conditional likelihood factors into their Bernoulli likelihood contributions.

The key interpretation is obtained by calculating the fitted log-odds at each indicator value. For \(Z=0\), the indicator contributes zero. For \(Z=1\), it contributes \(\beta_1\). Thus, at the same value of \(x\), the category coded 1 has log-odds \(\beta_1\) higher than the category coded 0. Exponentiating this contrast gives the corresponding conditional odds ratio.

Result: In a logistic model with a binary indicator and no interaction involving that indicator, \(\beta_1\) is the conditional log-odds for \(Z=1\) minus the conditional log-odds for \(Z=0\), at the same values of the other predictors. The conditional odds ratio for \(Z=1\) versus \(Z=0\) is \(\exp(\beta_1)\).

Why the Coefficient Is a Log-Odds Contrast

The result follows directly from the model, rather than from a special rule about indicator variables. Write the model’s linear predictor as \(\eta(Z,x)=\beta_0+\beta_1Z+\beta_2x\). Because the logit link equates the log-odds to the linear predictor, substituting the two possible values of \(Z\) gives

$$ \begin{aligned} \log\left(\frac{p_1}{1-p_1}\right) &=\beta_0+\beta_1+\beta_2x,\\ \log\left(\frac{p_0}{1-p_0}\right) &=\beta_0+\beta_2x. \end{aligned} $$

Here \(p_1=P(Y=1\mid Z=1,x)\) and \(p_0=P(Y=1\mid Z=0,x)\). Subtract the second equation from the first. The intercept and the contribution from \(x\) cancel, leaving

$$ \log\left(\frac{p_1}{1-p_1}\right) -\log\left(\frac{p_0}{1-p_0}\right)=\beta_1. $$

The left side is the difference between the two conditional log-odds. Exponentiating both sides shows that the ratio of the conditional odds is \(\exp(\beta_1)\). This derivation also shows why holding other predictors fixed matters: only then do their contributions cancel. If the model includes an interaction between \(Z\) and another predictor, the contrast can depend on that predictor; the simple \(\beta_1\) interpretation no longer applies on its own.

Worked Example: Turning a Fitted Indicator Model into Group Comparisons

A hypothetical logistic model for a one-year claim indicator is \(\operatorname{logit}(p)=-\log(4)+\log(3)Z\), where \(Z=1\) means that an optional feature was selected and \(Z=0\) means it was not. Find each group’s fitted probability, the log-odds contrast, the odds ratio, and the probability difference. Name the method and a mistake to avoid.

Method. Substitute \(Z=0\) and \(Z=1\) into the linear predictor. Convert each fitted log-odds to odds, then convert odds \(o\) to probability \(o/(1+o)\).

For \(Z=0\), the fitted log-odds are \(-\log(4)=\log(1/4)\). The odds are \(1/4=0.25\), so the probability is \(0.25/(1+0.25)=0.25/1.25=0.2000\).

For \(Z=1\), the fitted log-odds are \(-\log(4)+\log(3)=\log(3/4)\). The odds are \(3/4=0.75\), so the probability is \(0.75/(1+0.75)=0.75/1.75=3/7=0.4286\), rounded to four decimal places.

The log-odds contrast is \(\log(3/4)-\log(1/4)=\log(3)\). The odds ratio is \(\exp(\log(3))=3\). The probability difference is \(3/7-1/5=15/35-7/35=8/35=0.2286\), rounded to four decimal places. The calculation checks: the odds \(0.75\) for \(Z=1\) are three times the odds \(0.25\) for \(Z=0\).

Common mistake avoided: The coefficient \(\log(3)\) is not a probability increase of \(\log(3)\), nor is the probability increase 3. It is a log-odds contrast; the odds are multiplied by 3, and the probability difference here is 0.2286.

Adding Other Predictors

When the model also includes predictors, the indicator coefficient compares the two categories at the same values of those predictors. For example, in \(\operatorname{logit}(p)=\beta_0+\beta_1Z+\beta_2x\), the conditional odds ratio is \(\exp(\beta_1)\) at every fixed value of \(x\), provided there is no interaction term involving \(Z\). This is an odds comparison, not a claim that the groups have the same baseline probability at every value of \(x\).

The fitted probabilities still depend on the other predictors. Even when the odds ratio is constant, the probability difference between categories can change with \(x\), because the logit transformation maps odds to probabilities nonlinearly.

Worked Example: Comparing Categories at a Fixed Predictor Value

For a hypothetical model, \(\operatorname{logit}(p)=-2+0.5x+\log(2)Z\), where \(Z=1\) indicates enrollment in a wellness program. Compare fitted probabilities for \(Z=0\) and \(Z=1\) at \(x=2\), and interpret the indicator coefficient.

Method. Fix \(x=2\) for both categories, calculate each linear predictor, and convert from log-odds to odds and then probability.

At \(Z=0\), the linear predictor is \(-2+0.5(2)+\log(2)(0)=-2+1=-1\). The odds are \(\exp(-1)=1/e=0.3679\), using \(e\approx2.7183\). The probability is \(0.3679/(1+0.3679)=0.2689\), rounded to four decimal places.

At \(Z=1\), the linear predictor is \(-2+0.5(2)+\log(2)=-1+\log(2)\). The odds are \(\exp(-1+\log(2))=2/e=0.7358\). The probability is \(0.7358/(1+0.7358)=0.4239\), rounded to four decimal places.

The log-odds contrast is \(\log(2)\), so the conditional odds ratio is \(\exp(\log(2))=2\). At \(x=2\), the fitted probability difference is \(0.4239-0.2689=0.1550\), using the displayed rounded probabilities. Using unrounded probabilities gives approximately \(0.1549\); the difference rounded from the unrounded values is therefore \(0.1549\). The check is that the odds \(0.7358\) are twice \(0.3679\), apart from rounding.

Common mistake avoided: The coefficient \(\log(2)\) does not mean that program enrollment adds \(\log(2)\) to the probability. It doubles the odds at fixed \(x\); the probabilities must be calculated separately.

Obtaining the Contrast from Grouped Outcomes

For a model with an intercept and one binary predictor only, the two indicator values define the two groups. If both groups contain observed successes and failures, the fitted group probabilities equal the observed proportions in those groups. The indicator coefficient can then be found as the difference between the groups’ observed logits. This calculation is a special case of fitting a logistic model; it does not replace the general model-fitting procedure when other predictors are included.

Worked Example: Estimating the Indicator Contrast from Counts

In a hypothetical study of a device-maintenance option, 12 of 60 units without the option experience an event, while 30 of 70 units with the option experience the event. Fit the two-group logistic model with \(Z=0\) for no option and \(Z=1\) for the option. Find the fitted probabilities and the indicator coefficient and odds ratio.

Method. Calculate each group’s event proportion, convert each proportion to odds, then take the difference in log-odds. This is the intercept-and-indicator logistic model with no additional predictors.

For \(Z=0\), there are \(12\) events and \(60-12=48\) non-events. The fitted probability is \(12/60=0.2\), the odds are \(12/48=0.25\), and the log-odds are \(\log(0.25)\). Thus \(\hat{\beta}_0=\log(0.25)\).

For \(Z=1\), there are \(30\) events and \(70-30=40\) non-events. The fitted probability is \(30/70=3/7=0.4286\), rounded to four decimal places. The odds are \(30/40=0.75\), and the log-odds are \(\log(0.75)\).

The estimated indicator coefficient is \(\hat{\beta}_1=\log(0.75)-\log(0.25)=\log(3)=1.0986\), rounded to four decimal places. The estimated odds ratio is \(\exp(\hat{\beta}_1)=3\). The odds check is \(0.75/0.25=3\), matching the exponentiated coefficient.

Common mistake avoided: The coefficient is not the difference in event proportions, \(0.4286-0.2000=0.2286\). That is the probability difference; the coefficient is the difference in log-odds, \(1.0986\).

What Happens If the Coding Is Reversed?

The model’s fitted probabilities do not depend on which category is assigned zero, as long as the intercept and indicator coefficient are changed consistently. If the original coding is \(Z=0\) for category A and \(Z=1\) for category B, reversing the coding makes the new indicator \(W=1-Z\). In a model with no interaction, substituting \(Z=1-W\) gives

$$ \beta_0+\beta_1Z+\beta_2x =(\beta_0+\beta_1)-\beta_1W+\beta_2x. $$

Therefore, the reversed coding has intercept \(\beta_0+\beta_1\) and indicator coefficient \(-\beta_1\). The odds ratio for category A relative to category B is the reciprocal of the original odds ratio. Reversing the coding changes the direction of the comparison, not the underlying fitted comparison between the two groups.

Key takeaway: State explicitly which category receives 0 and which receives 1. With no interaction involving the indicator, its coefficient is the conditional log-odds for the category coded 1 minus the conditional log-odds for the category coded 0; exponentiating it gives the corresponding conditional odds ratio.

Common Exam Traps

  • Reading the coefficient as a probability change. A logistic coefficient is on the log-odds scale. Exponentiate it to obtain an odds ratio; calculate probabilities separately.
  • Omitting the category direction. “The odds ratio is 2” is incomplete unless it is clear which category’s odds are compared with which other category’s odds.
  • Comparing observations with different covariate values. The indicator coefficient is a conditional contrast. Hold other predictors fixed when interpreting it.
  • Reversing the coding but keeping the coefficient unchanged. Reversing the categories changes the coefficient’s sign and reciprocates the odds ratio.
  • Assuming a constant odds ratio means a constant probability difference. The odds ratio can be constant across values of another predictor while the probability difference varies.

Check Your Understanding

For each problem, identify the category comparison as well as the requested numerical result. Probabilities are rounded to four decimal places.

  1. A model is \(\operatorname{logit}(p)=-1+\log(4)Z\), with \(Z=1\) for a selected service and \(Z=0\) otherwise. Find the two fitted probabilities and the odds ratio for selected versus not selected. (Answer: \(p_0=0.2689\), \(p_1=0.5954\), odds ratio \(=4\).)
  2. A model is \(\operatorname{logit}(p)=-2+0.4x+\log(1.5)Z\). At \(x=5\), find the fitted probabilities for \(Z=0\) and \(Z=1\), and their probability difference. (Answer: \(p_0=0.5000\), \(p_1=0.6000\), difference \(=0.1000\).)
  3. In a hypothetical sample, \(18\) of \(90\) observations in the \(Z=0\) category and \(36\) of \(100\) in the \(Z=1\) category have the event. For an intercept-and-indicator model, find the estimated indicator coefficient and odds ratio. (Answer: \(\hat{\beta}_1=\log(2.25)=0.8109\), odds ratio \(=2.25\), for \(Z=1\) versus \(Z=0\).)
  4. An indicator coefficient is \(\log(0.6)\) when \(Z=1\) denotes group B and \(Z=0\) denotes group A. If the coding is reversed, what is the new coefficient and odds ratio for A versus B? (Answer: coefficient \(\log(5/3)=0.5108\), odds ratio \(=5/3\approx1.6667\).)
  5. A model is \(\operatorname{logit}(p)=-1.5+0.25x+\log(3)Z\), where \(Z=1\) identifies a chosen coverage option. At \(x=2\), calculate both fitted probabilities, the conditional odds ratio, and the probability difference. (Answer: \(p_0=0.2689\), \(p_1\approx0.5246\), conditional odds ratio \(=3\), probability difference \(\approx0.2557\).)
Clinical Biostats