Tutorials › Regression Analysis (Undergraduate) › Coding a Two-Level Factor With an Indicator

Coding Categorical Predictors · Tutorial 296 of 991

Coding a Two-Level Factor With an Indicator

Learn to build and verify a 0/1 indicator for a two-level factor, then use algebra to see what happens to the coefficients when the coding is reversed.

Beginner 9 min read

What You'll Learn

  • Define a 0/1 indicator for two mutually exclusive factor levels.
  • Identify the category assigned 0 and the category assigned 1.
  • Write a regression expression that uses a binary indicator.
  • Derive how reversing the 0/1 assignment changes the coefficients.
  • Check a recoding by verifying its values for both categories.
  • Avoid interpreting a coefficient’s sign without checking the coding.

Why a Coding Choice Matters

A categorical predictor records group membership, not a numerical amount. To include a factor with two levels in a regression expression, we can represent membership using an indicator that takes the value 0 for one level and 1 for the other. The arithmetic is simple, but the assignment matters: reversing which level gets 0 and which gets 1 reverses the sign of the indicator’s coefficient.

For example, suppose a hypothetical data set records whether each participant followed a new exercise plan or a standard plan. The labels “new plan” and “standard plan” are categories. Coding those labels as 1 and 0 gives the regression model a numerical predictor, but the numbers are codes for membership, not measurements of how much of a plan someone followed.

Definition: For a factor with exactly two mutually exclusive and exhaustive levels, a 0/1 indicator is a variable defined to equal 1 for one specified level and 0 for the other. The level assigned 0 is the reference category under this coding; the level assigned 1 is the other category. State the assignment explicitly whenever you write or interpret the indicator.

Defining the Indicator Before Using It

The safest procedure is to name the factor, list its two levels, and state which level receives each value. For instance, let \(D_i=1\) if participant \(i\) followed the new plan and \(D_i=0\) if participant \(i\) followed the standard plan. The subscript \(i\) identifies the participant. Once the mapping is defined, the data value \(D_i\) is determined by that participant’s category.

The coding itself requires no assumption that 0 or 1 is a measured quantity. It only requires that each observation belongs to exactly one of the two stated levels, so every observation receives one and only one indicator value. If a record has a missing or ambiguous category, its indicator cannot be assigned from this definition alone.

A simple regression model using the indicator can be written as

$$ E(Y_i\mid D_i)=\beta_0+\beta_1D_i. $$

Here \(Y_i\) is the response for observation \(i\), and \(\beta_0\) and \(\beta_1\) are model coefficients. For the expression to represent the conditional mean, assume its mean structure is appropriate and the model error has conditional mean zero given the indicator. The assignment of the indicator is a coding decision; it does not, by itself, establish that the model is appropriate or that a relationship is causal. No probability distribution assumption is needed to define the code or to derive how its coefficients transform.

Worked Example: Coding Two Exercise-Plan Levels

In a hypothetical data set, each participant followed either the standard plan or the new plan. Define \(D_i=1\) for participants following the new plan and \(D_i=0\) for participants following the standard plan. Assign the indicator values for four records.

ParticipantPlan category\(D_i\)
AStandard0
BNew1
CNew1
DStandard0

Apply the definition one record at a time. Participant A is in the standard-plan category, which was assigned 0, so \(D_A=0\). Participant B is in the new-plan category, assigned 1, so \(D_B=1\). The same rule gives \(D_C=1\) and \(D_D=0\). The assignment is exhaustive and unambiguous because each participant has exactly one of the two plan categories.

The values do not rank the plans or measure plan intensity. They simply distinguish the two categories according to the stated rule.

What Reversing the Coding Does

To reverse a 0/1 coding, assign 1 to the category that previously received 0, and assign 0 to the category that previously received 1. If the original indicator is \(D_i\), the reversed indicator is

$$ D_i^*=1-D_i. $$

Check the formula against both possible original values. If \(D_i=0\), then \(D_i^*=1-0=1\). If \(D_i=1\), then \(D_i^*=1-1=0\). Thus the two categories exchange indicator values.

The effect on coefficients follows by rewriting the model, not by changing the response data. Start with the original conditional-mean expression and substitute \(D_i=1-D_i^*\):

$$ \begin{aligned} E(Y_i\mid D_i) &=\beta_0+\beta_1D_i\\ &=\beta_0+\beta_1(1-D_i^*)\\ &=(\beta_0+\beta_1)-\beta_1D_i^*. \end{aligned} $$

Therefore, when the same linear expression is written using the reversed indicator, its coefficients are

$$ \beta_0^*=\beta_0+\beta_1, \qquad \beta_1^*=-\beta_1. $$

The indicator coefficient changes sign and keeps the same magnitude. The constant term also changes. This derivation is an algebraic statement about the chosen coding; it does not mean the response data or the relationship being represented has changed. It also shows why a coefficient’s sign cannot be interpreted without first checking which category is coded 1.

Worked Example: Reversing a Positive Coefficient

Suppose a hypothetical fitted model uses \(D_i=1\) for participants following a new plan and \(D_i=0\) for participants following a standard plan. Its fitted expression, with response measured in points, is

$$ \widehat{Y}_i=42+3.5D_i. $$

Now reverse the coding, defining \(D_i^*=1\) for the standard-plan category and \(D_i^*=0\) for the new-plan category. Use \(D_i=1-D_i^*\) in the original expression:

$$ \begin{aligned} \widehat{Y}_i &=42+3.5(1-D_i^*)\\ &=(42+3.5)-3.5D_i^*\\ &=45.5-3.5D_i^*. \end{aligned} $$

The reversed-coding indicator coefficient is \(-3.5\) points, obtained from \(-3.5=-(3.5)\); the constant is \(45.5\) points, obtained from \(42+3.5\). The coefficient has the same magnitude as before but the opposite sign. Here the reversed indicator assigns 1 to the standard-plan category, so the negative sign is attached to that coding direction. The calculation does not imply that the data changed when the labels were recoded.

Checking a Coding and Its Sign

A reliable check has two parts. First, test the definition with one observation from each category: the chosen 1-level must receive 1, and the other level must receive 0. Second, if the coding is reversed, verify that the coefficient transformation follows the algebra. For the model \(\beta_0+\beta_1D_i\), the new coefficient multiplying \(D_i^*\) must be \(-\beta_1\), not \(+\beta_1\).

This is a general algebraic rule for the two possible 0/1 assignments. It applies to a coefficient estimate as well as to a model coefficient: if an estimated coefficient is \(\hat{\beta}_1\), then the coefficient under the reversed coding is \(-\hat{\beta}_1\). Its sign is meaningful only together with the recorded mapping from categories to numbers.

Worked Example: Checking a Negative Coefficient

A hypothetical regression expression for weekly minutes of an activity is

$$ \widehat{Y}_i=310-18D_i, $$

where \(D_i=1\) denotes category A and \(D_i=0\) denotes category B. Reverse the code, so \(D_i^*=1\) denotes category B and \(D_i^*=0\) denotes category A. Substitute \(D_i=1-D_i^*\):

$$ \begin{aligned} \widehat{Y}_i &=310-18(1-D_i^*)\\ &=(310-18)+18D_i^*\\ &=292+18D_i^*. \end{aligned} $$

The original indicator coefficient was \(-18\) minutes; after reversal it is \(+18\) minutes, since \(+18=-(-18)\). Its magnitude remains 18 minutes. The arithmetic also gives the new constant \(292\) minutes, because \(310-18=292\). The reversed sign is not a contradiction: the value 1 now identifies category B rather than category A.

The indicator assignment is valid here because the example specifies two categories and assigns one value to each. No assumption about the distribution of weekly minutes is needed for this recoding calculation.

Common Mistakes and Why the Mapping Matters

  • Leaving the assignment unstated. Writing “group is coded 0/1” does not identify which group is which. State both mappings, such as “standard = 0; new = 1.”
  • Treating the codes as measurements. A code of 1 does not mean twice as much as a code of 0. The values are labels used in the model’s arithmetic.
  • Reversing the code but keeping the coefficient sign. If \(D_i^*=1-D_i\), substitution gives \(-\beta_1D_i^*\), so the coefficient sign must reverse.
  • Reading a sign without checking the code. A positive coefficient is attached to an increase from indicator value 0 to value 1. To connect that direction to named categories, first identify which category received 1.

These checks matter when reading a regression table, comparing analyses, or communicating a result. Two analyses can use opposite indicator assignments, so their indicator coefficients can have opposite signs even when they encode the same categories. Reporting the mapping alongside the coefficient prevents a sign from being mistaken for a conclusion about an unnamed group.

Key takeaway: Define the two category assignments explicitly. Reversing a binary indicator replaces \(D_i\) with \(1-D_i\), which changes the indicator coefficient from \(\beta_1\) to \(-\beta_1\) and shifts the constant from \(\beta_0\) to \(\beta_0+\beta_1\).

Check Your Understanding

Use the stated category assignments and show the arithmetic where appropriate.

  1. A factor has levels “remote” and “in person.” Define an indicator with remote coded 1. What value does in person receive?
  2. If \(D_i=1\) for category A and \(D_i=0\) for category B, write a formula for the reversed indicator and verify it for both categories.
  3. Rewrite \(\widehat{Y}_i=25+4D_i\) using \(D_i^*=1-D_i\). Give both new coefficients.
  4. A coefficient changes from \(+6\) to \(-6\) after the coding is reversed. What changed, and what did not change merely because of the recoding?
  5. Why is it incomplete to report that a binary-indicator coefficient is positive without stating which category was coded 1?