Tutorials › Exam PA - Predictive Analytics › Encoding a Binary Factor With an Indicator

Exam PA · Categorical Encoding and Interactions · Tutorial 232 of 966

Encoding a Binary Factor With an Indicator

Build a clear zero-one indicator for a binary factor and connect its regression coefficient to the fitted means of the two groups.

Intermediate 9 min read

What You'll Learn

  • Define which category receives indicator value zero and which receives one
  • Fit an intercept-and-indicator linear regression model
  • Derive why the coefficient equals the difference in category means
  • Calculate fitted group means and check them against the observed data
  • Recognize how reversing the indicator changes the coefficient’s sign
  • Distinguish this unadjusted mean difference from interpretations in other models

Why Encode a Binary Factor?

A categorical predictor with two levels can be represented in a linear regression model by a single zero-one indicator. This makes the coding explicit: the value zero identifies one category, and the value one identifies the other. The choice matters because it determines the direction of the coefficient, but it does not change the underlying fitted group means.

The key result is specific to a linear model with an intercept and this one binary predictor: the intercept is the fitted mean for the zero-coded category, and the indicator coefficient is the fitted mean for the one-coded category minus the fitted mean for the zero-coded category. This tutorial develops that result and its practical checks, in a form aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Definition: For a factor with two categories, define an indicator \(z\) by assigning \(z=0\) to a chosen category and \(z=1\) to the other category. In the ordinary least squares model \(Y_i=\beta_0+\beta_1z_i+\varepsilon_i\), \(\beta_0\) is the intercept and \(\beta_1\) is the indicator coefficient. The fitted response is \(\hat{Y}_i=\hat{\beta}_0+\hat{\beta}_1z_i\).

The assignment should be stated in words before interpreting the coefficient. For instance, if \(z=0\) means “standard service” and \(z=1\) means “expedited service,” then a positive \(\hat{\beta}_1\) indicates that the fitted response is higher for expedited service. If the two values are swapped, the coefficient changes sign.

This result concerns an ordinary least squares model for a numeric response with an intercept and no other predictors. It does not say that every observation in one category differs from every observation in the other by the same amount. It also does not directly transfer to a model with a different response scale, such as a generalized linear model with a nonlinear link.

Deriving the Difference-in-Means Result

Suppose there are \(n_0\) observations with \(z_i=0\) and \(n_1\) observations with \(z_i=1\), where both counts are positive. Write \(\bar{Y}_0\) and \(\bar{Y}_1\) for the observed response means in those categories. Ordinary least squares chooses \(\beta_0\) and \(\beta_1\) to minimize the residual sum of squares:

$$ \sum_{i:z_i=0}(Y_i-\beta_0)^2+ \sum_{i:z_i=1}(Y_i-\beta_0-\beta_1)^2. $$

The model assigns the same fitted value, \(\beta_0\), to each observation in the zero-coded category and the same fitted value, \(\beta_0+\beta_1\), to each observation in the one-coded category. For any group of numbers, the sum of squared deviations from a proposed constant is minimized when that constant is the group mean. Applying this fact separately to the two groups gives the fitted values and coefficient below.

$$ \hat{\beta}_0=\bar{Y}_0,\qquad \hat{\beta}_0+\hat{\beta}_1=\bar{Y}_1,\qquad \hat{\beta}_1=\bar{Y}_1-\bar{Y}_0. $$

The last equality follows by subtracting the first fitted mean from the second. It also shows why the indicator’s coding direction matters: the coefficient is the mean for the category coded one minus the mean for the category coded zero. The derivation uses the least-squares objective and the presence of both categories; it does not require normally distributed errors. Error-distribution assumptions are relevant to particular inference procedures, not to this algebraic identity.

Theorem: In an ordinary least squares regression of a numeric response on an intercept and a zero-one indicator, with observations from both indicator values, the fitted value for each category equals that category’s response mean. The indicator coefficient is \(\bar{Y}_1-\bar{Y}_0\), using the stated coding.

Worked Calculations

Worked Example: Comparing Two Hypothetical Service Types

A hypothetical service team records resolution time, in minutes, for six requests. Three used standard service and took 4, 6, and 8 minutes. Three used expedited service and took 9, 11, and 13 minutes. Define \(z=0\) for standard and \(z=1\) for expedited. Find the fitted model and interpret its coefficient.

Method. Calculate each category mean, then use the difference-in-means result. The response is resolution time, so the coefficient is measured in minutes.

Standard mean. The total is \(4+6+8=18\), so \(\bar{Y}_0=18/3=6\) minutes.

Expedited mean. The total is \(9+11+13=33\), so \(\bar{Y}_1=33/3=11\) minutes.

Therefore, \(\hat{\beta}_0=6\) and \(\hat{\beta}_1=11-6=5\). The fitted model is \(\hat{Y}=6+5z\). For standard service, \(z=0\) and the fitted time is \(6+5(0)=6\) minutes. For expedited service, \(z=1\) and the fitted time is \(6+5(1)=11\) minutes.

Check. The fitted values are the two observed category means. The residuals are \(-2,0,2\) in each category, and each category’s residuals sum to zero. Thus the calculation is consistent with the least-squares fit.

Common mistake avoided. The coefficient is not the expedited mean itself; it is the difference, \(5\) minutes. The intercept, \(6\), is the standard-service mean under this coding.

Worked Example: An Indicator With Unequal Group Sizes

A hypothetical analyst compares the number of daily applications completed through two channels. The in-person channel has observed counts 12, 15, and 18; the online channel has counts 14, 17, 20, 23, and 26. Define \(z=0\) for in-person and \(z=1\) for online. Determine the regression coefficients, including whether the unequal sample sizes change the result.

Method. Fit an intercept and indicator by ordinary least squares. Group sizes may differ; the fitted values are still the separate observed means.

In-person mean. The total is \(12+15+18=45\), and there are three observations, so \(\bar{Y}_0=45/3=15\).

Online mean. The total is \(14+17+20+23+26=100\), and there are five observations, so \(\bar{Y}_1=100/5=20\).

Thus \(\hat{\beta}_0=15\), and \(\hat{\beta}_1=20-15=5\). The fitted equation is \(\hat{Y}=15+5z\). It predicts 15 completed applications for the in-person channel and 20 for the online channel. The fitted difference is \(5\) applications per day.

Check. The in-person residuals are \(-3,0,3\), with sum \(0\). The online residuals are \(-6,-3,0,3,6\), also with sum \(0\). Unequal group sizes affect the amount of data in each group, but not the identity between each fitted group value and its sample mean.

Common mistake avoided. Do not pool the ten observations into one overall mean and use it as the intercept. The intercept is the mean for the zero-coded category, here \(15\), not the overall mean.

Worked Example: Reversing the Indicator

A hypothetical claims review records review time, in hours. The routine-review category has times 82, 94, and 106; the priority-review category has times 91, 103, 115, and 127. First code routine as zero and priority as one. Then reverse the coding and explain what changes.

Original coding. The routine total is \(82+94+106=282\), so its mean is \(282/3=94\). The priority total is \(91+103+115+127=436\), so its mean is \(436/4=109\). With routine coded zero and priority coded one, \(\hat{\beta}_0=94\) and \(\hat{\beta}_1=109-94=15\). The fitted equation is \(\hat{Y}=94+15z\).

Reverse the coding. Now let \(z=0\) mean priority and \(z=1\) mean routine. The zero-coded mean is 109, so the new intercept is 109. The new coefficient is \(94-109=-15\), and the fitted equation is \(\hat{Y}=109-15z\).

Check. Under the original coding, routine has fitted value \(94+15(0)=94\) and priority has fitted value \(94+15(1)=109\). Under the reversed coding, priority has fitted value \(109-15(0)=109\) and routine has fitted value \(109-15(1)=94\). The fitted values are unchanged for each category; only the parameter labels and coefficient direction change.

Common mistake avoided. A negative coefficient after reversing the coding does not mean that the model’s fitted times have changed. It means the difference is now calculated as routine minus priority rather than priority minus routine.

Using the Coding in R

Start by checking the factor’s two observed labels and deciding which one should receive zero. Then create a numeric indicator with that mapping stated explicitly. For example, if a factor called channel has levels “InPerson” and “Online,” and the intended coding is InPerson as zero and Online as one, the R expression is `as.integer(channel == "Online")`. The equality test is TRUE for Online and FALSE for InPerson; converting those logical values to integers produces one and zero, respectively.

Fit the numeric response using an intercept and the indicator, for example `lm(y ~ z)`. Check the mapping before interpreting the output: the intercept refers to \(z=0\), and the coefficient refers to the change from \(z=0\) to \(z=1\). Also check that both categories are represented among the observations used by the model. If a category is absent, its group mean and the two-category comparison cannot be estimated from that data.

If the response has missing values, the fitting procedure can only use records with observed response and predictor values. The relevant group means must therefore be calculated over the same records used to fit the model. A missing response should not be silently replaced with zero, since that would change the observed group mean and hence the fitted coefficient.

Limits of the Interpretation and Exam Traps

The difference-in-means interpretation depends on the model being an ordinary least squares regression with an intercept and only this binary predictor. If additional predictors are included, the indicator coefficient is generally an adjusted comparison conditional on those predictors; it need not equal the raw difference between the two overall category means. If the response is modeled on another scale, the coefficient also need not be a raw response-mean difference.

For the specified two-category linear model, the coefficient describes a fitted difference, not a causal effect. A causal conclusion would require an appropriate study design and assumptions about how the categories were assigned, among other considerations. Regression coding alone does not establish those conditions.

A reliable calculation follows four checks: name the zero-coded category, name the one-coded category, calculate each group mean using the relevant observations, and subtract in the order “one-coded mean minus zero-coded mean.” A sign check is useful: if the one-coded group has a larger response mean, the coefficient must be positive.

1
Define the mapping.
Write down the factor level assigned zero and the level assigned one.
2
Calculate the means.
Find the response mean separately for the zero-coded and one-coded observations.
3
Identify the parameters.
The intercept is the zero-coded mean; the coefficient is the one-coded mean minus the zero-coded mean.
4
Check fitted values and sign.
Substitute zero and one into the fitted equation and confirm that the predictions equal the two group means.
Key takeaway: With an intercept and a single zero-one predictor in an ordinary least squares model, the fitted category means are \(\hat{\beta}_0\) and \(\hat{\beta}_0+\hat{\beta}_1\). Therefore, \(\hat{\beta}_1\) is the fitted mean for the category coded one minus the fitted mean for the category coded zero.

Check Your Understanding

For each problem, identify the zero-coded category and compute the requested fitted quantities.

  1. A hypothetical sensor records readings of 3, 5, and 7 for mode A, and 6, 8, and 10 for mode B. Code A as zero and B as one. Find the intercept and indicator coefficient. (Answer: \(\hat{\beta}_0=5\), \(\hat{\beta}_1=3\).)
  2. A hypothetical processing-time response is 20, 22, and 26 for category East, and 17 and 23 for category West. Code East as zero and West as one. Find both coefficients, rounded to two decimals. (Answer: \(\hat{\beta}_0=22.67\), \(\hat{\beta}_1=-2.67\).)
  3. A model uses \(z=0\) for basic coverage and \(z=1\) for extended coverage. The observed average response is 8 for basic and 10 for extended. State the fitted equation and the prediction for extended coverage. (Answer: \(\hat{Y}=8+2z\); the prediction for extended coverage is 10.)
  4. A hypothetical audit has outcomes 1, 3, 5, and 7 for route A, and 2, 6, and 10 for route B. Code A as zero and B as one. Calculate both group means, the coefficient, and the fitted response for route B. (Answer: means 4 and 6; \(\hat{\beta}_1=2\); fitted response for route B is 6.)
  5. A binary-indicator model has fitted equation \(\hat{Y}=31-4z\), with \(z=1\) assigned to the “remote” category and \(z=0\) to “onsite.” Give each fitted category mean and state the fitted difference, remote minus onsite. (Answer: onsite mean 31, remote mean 27, remote minus onsite \(=-4\).)
Clinical Biostats