Tutorials › Regression Analysis (Undergraduate) › Calculating Fitted Means for Two Categories

Coding Categorical Predictors · Tutorial 299 of 991

Calculating Fitted Means for Two Categories

Learn to substitute each category’s indicator value into the fitted model, interpret the resulting group predictions, and verify their difference.

Beginner 9 min read

What You'll Learn

  • Write the fitted model for a two-category predictor coded zero and one.
  • Calculate the fitted outcome for the reference category and the comparison category.
  • Verify that the comparison-minus-reference fitted difference equals the indicator coefficient.
  • Interpret the coefficient’s sign and units in the context of the outcome.
  • Distinguish fitted group values from individual outcomes and from causal effects.
  • Check the calculations using a small hypothetical data set.

From a Two-Category Model to Two Fitted Values

A fitted regression equation lets us calculate predicted outcomes for specified predictor values. With a two-category predictor, the calculation is especially direct: the reference category is coded zero, and the comparison category is coded one. Substituting those two values into the same equation gives the fitted outcome for each category. Comparing the results also provides a useful check on the meaning of the indicator coefficient.

Suppose the response \(Y\) is a quantitative outcome and the two-level factor is coded with an indicator \(x\): \(x=0\) for the reference category and \(x=1\) for the comparison category. The fitted model is

$$ \hat{y}=b_0+b_1x. $$

Here \(b_0\) is the estimated intercept and \(b_1\) is the estimated indicator coefficient. The fitted value \(\hat{y}\) is the outcome predicted by the model for the specified category. It is not a guarantee of the outcome for every individual in that category: individuals can have outcomes above or below the fitted value.

Definition: In the fitted model \(\hat{y}=b_0+b_1x\), with the reference category coded \(x=0\) and the comparison category coded \(x=1\), the fitted reference-category outcome is \(b_0\), and the fitted comparison-category outcome is \(b_0+b_1\). Their comparison-minus-reference difference is \(b_1\).

The coefficient is a difference in the outcome’s units. A positive \(b_1\) means the comparison category has the larger fitted outcome; a negative \(b_1\) means it has the smaller fitted outcome. The sign describes the comparison relative to the chosen reference category, so category labels and coding must be stated when interpreting it.

Calculate Each Fitted Outcome

The calculation follows the coding. For the reference category, substitute \(x=0\). For the comparison category, substitute \(x=1\). Then subtract the reference fitted value from the comparison fitted value to check the coefficient.

1
Identify the coding.
Record which category is the reference category (\(x=0\)) and which is the comparison category (\(x=1\)).
2
Calculate the reference fitted value.
Substitute zero: \(\hat{y}_{\text{reference}}=b_0+b_1(0)=b_0\).
3
Calculate the comparison fitted value.
Substitute one: \(\hat{y}_{\text{comparison}}=b_0+b_1(1)=b_0+b_1\).
4
Check the difference.
Subtract in the stated order: \(\hat{y}_{\text{comparison}}-\hat{y}_{\text{reference}}=(b_0+b_1)-b_0=b_1\).

This procedure assumes the model being used is the stated two-category model, with no additional predictors or interaction terms. It also assumes the indicator is coded as described. The arithmetic does not require a significance test or a claim that the category itself causes a change in the outcome. The calculation describes fitted values from this model.

Worked Example: Fitted Exam Scores for Two Study Groups

Consider a hypothetical model for exam score, measured in points. Students in the usual-preparation category are the reference group, coded \(x=0\); students in the additional-preparation category are coded \(x=1\). Suppose the fitted equation is

$$ \hat{y}=72+6.5x. $$

For usual preparation, substitute \(x=0\):

$$ \hat{y}_{\text{usual}}=72+6.5(0)=72 \text{ points}. $$

For additional preparation, substitute \(x=1\):

$$ \hat{y}_{\text{additional}}=72+6.5(1)=78.5 \text{ points}. $$

Now subtract the reference fitted value from the comparison fitted value:

$$ 78.5-72=6.5 \text{ points}. $$

The difference equals the indicator coefficient, \(b_1=6.5\). In this fitted model, the additional-preparation category has a predicted score 6.5 points higher than the usual-preparation category. This is a model-based group comparison; by itself, it does not establish that additional preparation caused a score increase.

Why the Difference Must Equal the Coefficient

The equality is a direct consequence of substitution and subtraction, not a coincidence in a particular data set. Write the comparison fitted value minus the reference fitted value, using the fitted model:

$$ \begin{aligned} \hat{y}_{\text{comparison}}-\hat{y}_{\text{reference}} &=(b_0+b_1\cdot 1)-(b_0+b_1\cdot 0)\\ &=(b_0+b_1)-b_0\\ &=b_1. \end{aligned} $$

The intercept appears in both fitted values and therefore cancels when they are subtracted. The indicator coefficient is what remains. This is why the coefficient represents the fitted difference for the comparison category relative to the reference category under this coding.

The subtraction order matters. Comparison minus reference gives \(b_1\); reference minus comparison gives \(-b_1\). Neither calculation is inherently wrong, but its sign must match the stated order. A coefficient of \(-8.2\), for example, means the comparison fitted outcome is 8.2 outcome units lower than the reference fitted outcome, not 8.2 units higher.

The interpretation also depends on the model. In the two-category model shown here, both category fitted values share the same intercept and differ only by the indicator term. If a different model includes additional predictors or interactions, its fitted values may depend on those terms as well. The calculation on this page is for the stated model, not a rule to ignore other terms in a larger equation.

Worked Example: A Negative Difference in Waiting Time

Suppose a hypothetical analysis models appointment waiting time in minutes. The standard-scheduling category is the reference (\(x=0\)); the revised-scheduling category is the comparison (\(x=1\)). The fitted model is

$$ \hat{y}=34-8.2x. $$

For standard scheduling:

$$ \hat{y}_{\text{standard}}=34-8.2(0)=34 \text{ minutes}. $$

For revised scheduling:

$$ \hat{y}_{\text{revised}}=34-8.2(1)=25.8 \text{ minutes}. $$

The comparison-minus-reference difference is

$$ 25.8-34=-8.2 \text{ minutes}. $$

This agrees with \(b_1=-8.2\). The fitted waiting time for the revised-scheduling category is 8.2 minutes lower than the fitted waiting time for the standard-scheduling category. The negative sign indicates direction; the unit is minutes. This fitted comparison alone does not demonstrate that changing schedules would cause an individual’s wait to fall.

Fitted Values and Group Averages

When an ordinary least-squares regression includes only an intercept and a two-category indicator, the fitted value for each category equals that category’s sample mean outcome. This fact can help check a calculation, but it applies to this simple model, where the only predictor is the indicator. In models containing other predictors, the fitted category values need not equal the raw group averages.

The reason follows from how least squares chooses the coefficients. For the reference group, the fitted value is \(b_0\); its residual sum of squares is minimized when \(b_0\) is the reference group’s sample mean. For the comparison group, the fitted value is \(b_0+b_1\); its residual sum of squares is minimized when that fitted value is the comparison group’s sample mean. Thus the two fitted values match the two group averages, and their difference is the estimated indicator coefficient.

Worked Example: Checking Coefficients Against Group Means

For a hypothetical data set, four reference-category participants have outcomes 4, 6, 5, and 5 units. Three comparison-category participants have outcomes 7, 8, and 6 units. Fit the intercept-plus-indicator model, using \(x=0\) for reference and \(x=1\) for comparison.

First calculate each group’s sample mean. For the reference group:

$$ \bar{y}_{\text{reference}}=\frac{4+6+5+5}{4}=\frac{20}{4}=5 \text{ units}. $$

For the comparison group:

$$ \bar{y}_{\text{comparison}}=\frac{7+8+6}{3}=\frac{21}{3}=7 \text{ units}. $$

In this model, the reference fitted value is the intercept, so \(b_0=5\). The indicator coefficient is the comparison mean minus the reference mean:

$$ b_1=7-5=2 \text{ units}. $$

The resulting fitted equation is \(\hat{y}=5+2x\). Substitution gives \(\hat{y}_{\text{reference}}=5+2(0)=5\) units and \(\hat{y}_{\text{comparison}}=5+2(1)=7\) units. Their difference is \(7-5=2\) units, exactly the indicator coefficient. This checks both the fitted values and the direction of the group comparison.

Common Mistakes and a Reliable Check

A frequent mistake is to report the intercept as the fitted value for both categories. The intercept is the reference-category fitted value because its indicator is zero; the comparison category’s fitted value includes the indicator coefficient. Another mistake is to call \(b_1\) the comparison fitted value. It is the difference between the two fitted values, not generally the fitted value itself.

  • Wrong: “The comparison group’s fitted outcome is \(b_1\).” Correction: It is \(b_0+b_1\); \(b_1\) is the comparison-minus-reference difference.
  • Wrong: “The comparison group is higher because the coefficient is negative.” Correction: With comparison-minus-reference ordering, a negative coefficient means the comparison fitted outcome is lower.
  • Wrong: “The fitted value is the outcome every member of the group will have.” Correction: It is the model’s predicted value for that category; individual outcomes can vary.
  • Wrong: “A positive fitted difference proves the category caused the outcome to increase.” Correction: A fitted association is not by itself a causal conclusion.

A compact check is to write both substitutions before interpreting anything: \(x=0\) gives \(b_0\), while \(x=1\) gives \(b_0+b_1\). Then subtract in the same order used to describe the coefficient. If the difference is not \(b_1\), check the arithmetic, the coding, and the subtraction order.

Key takeaway: With the reference category coded zero and the comparison category coded one, the fitted outcomes are \(b_0\) and \(b_0+b_1\). Their comparison-minus-reference difference is \(b_1\), measured in the units of the outcome.

Check Your Understanding

Use the model and coding stated in each question. For each calculation, include the outcome units and specify the comparison-minus-reference difference where requested.

  1. A model is \(\hat{y}=18+4x\), with category A coded zero and category B coded one. Calculate each fitted outcome and their difference.
  2. A model is \(\hat{y}=50-3.5x\), with the reference category coded zero. What is the comparison fitted outcome, and how does it differ from the reference fitted outcome?
  3. In a two-category model, a learner says the comparison fitted value equals the indicator coefficient. Explain what is incorrect and give the correct expression.
  4. Why does the intercept cancel when calculating the comparison-minus-reference fitted difference?
  5. In what setting does the fitted value for each category equal that category’s sample mean in an ordinary least-squares model? Name one kind of added model term that would make the simple calculation require further care.