Tutorials › Regression Analysis (Undergraduate) › The Difference in Means as a Regression

Regression in Randomized Studies · Tutorial 894 of 991

The Difference in Means as a Regression

Learn how a regression with a 0–1 treatment indicator reproduces the difference between treatment and control means, and what its coefficients represent.

Intermediate 9 min read

What You'll Learn

  • Define a two-arm regression model using a 0–1 treatment indicator
  • Derive algebraically why the fitted treatment coefficient equals the difference in sample means
  • Interpret the intercept, treatment coefficient, fitted values, and residuals in context
  • Work through examples with balanced and unequal group sizes
  • Distinguish the coefficient identity from the assumptions needed for inference and causal interpretation

Why a Difference in Means Can Be Written as a Regression

In a two-arm randomized study, a basic summary of the outcome is the difference between the treatment-group mean and the control-group mean. The same comparison can be expressed as a regression: code treatment assignment as 1 and control assignment as 0, then regress the outcome on that indicator. This connection is useful because it shows exactly what the regression coefficient means, while providing a framework that can later accommodate other predictors.

Here the regression has only an intercept and a binary treatment indicator. Its treatment coefficient is not a change in outcome for a one-unit increase along a continuous scale. It is the estimated change between the two groups, in the direction set by the coding.

The Two-Arm Regression Model

Definition: For individual \(i\), let \(Y_i\) be the outcome and let \(T_i\) be the treatment-assignment indicator, coded \(T_i=1\) for treatment and \(T_i=0\) for control. The two-group regression model is $$ Y_i=\beta_0+\beta_1T_i+\varepsilon_i. $$ Here \(\beta_0\) is the model mean for the control group, \(\beta_1\) is the difference between the treatment and control model means, and \(\varepsilon_i\) is the model error (or disturbance). The fitted coefficients are denoted \(b_0\) and \(b_1\); the fitted residual for individual \(i\) is \(e_i=Y_i-(b_0+b_1T_i)\).

The model implies a separate mean for each assignment group. When \(T_i=0\), the model mean is \(\beta_0\). When \(T_i=1\), it is \(\beta_0+\beta_1\). Thus \(\beta_1\) is treatment mean minus control mean on the model scale. The sample regression coefficients \(b_0\) and \(b_1\) estimate the corresponding quantities.

Before interpreting a fitted regression, distinguish the model error from the fitted residual. The model error \(\varepsilon_i\) is defined relative to the model’s population or theoretical mean. The residual \(e_i\) is calculated after fitting the sample coefficients. They are related, but they are not the same quantity.

What Is Required, and What Is Not

For the coefficient identity proved below, both groups must be represented in the data, and ordinary least squares must fit an intercept and the 0–1 treatment indicator. The identity is an algebraic fact about the observed outcomes. It does not require equal group sizes, equal variances, normally distributed outcomes, or a causal interpretation.

Additional conditions matter for other claims. A conventional model-based standard error relies on assumptions about the errors, commonly independent errors with a common variance; exact small-sample tests may also assume normally distributed errors within groups. If observations are dependent, the uncertainty calculation must account for that dependence. The algebraic equality of the point estimate and difference in means still holds, but it does not make an inappropriate standard error appropriate.

In a randomized study, treatment assignment is randomized to support a causal comparison of assigned groups. Interpreting the observed mean difference as an estimate of a causal treatment effect also requires a suitable study setup, including a well-defined assignment and outcome and no interference between participants. Regression algebra alone does not establish those design conditions.

Deriving the Coefficients

Ordinary least squares chooses the intercept and treatment coefficient to minimize the sum of squared residuals. It is helpful to rewrite the model in terms of its two fitted group means: \(\mu_0=\beta_0\) for control and \(\mu_1=\beta_0+\beta_1\) for treatment. This is only a change of parameterization, since \(\beta_0=\mu_0\) and \(\beta_1=\mu_1-\mu_0\).

$$ Q(\mu_0,\mu_1) = \sum_{i:T_i=0}(Y_i-\mu_0)^2 + \sum_{i:T_i=1}(Y_i-\mu_1)^2. $$

Each group mean can be optimized separately. For any group with observations \(y_1,\ldots,y_m\) and sample mean \(\bar{y}\), the following identity holds:

$$ \sum_{j=1}^{m}(y_j-c)^2 = \sum_{j=1}^{m}(y_j-\bar{y})^2 + m(c-\bar{y})^2. $$

To verify it, write \(y_j-c=(y_j-\bar{y})+(\bar{y}-c)\), square and sum. The cross-product is zero because \(\sum_j(y_j-\bar{y})=0\). The final term, \(m(c-\bar{y})^2\), is nonnegative and is minimized when \(c=\bar{y}\). Applying this result separately to the control and treatment terms shows that the least-squares fitted control mean is the control sample mean and the fitted treatment mean is the treatment sample mean.

$$ b_0=\bar{Y}_0, \qquad b_0+b_1=\bar{Y}_1, \qquad b_1=\bar{Y}_1-\bar{Y}_0. $$

The notation \(\bar{Y}_0\) and \(\bar{Y}_1\) here means the sample mean outcome among observations with \(T_i=0\) and \(T_i=1\), respectively. The fitted treatment coefficient is exactly the treatment-minus-control difference in sample means.

Worked Example: A Balanced Two-Arm Trial

Hypothetical data. Suppose a trial assigns four participants to control and four to treatment. The outcome is a score measured in points.

AssignmentOutcomes (points)Group mean (points)
Control, \(T=0\)10, 12, 9, 1110.5
Treatment, \(T=1\)13, 14, 12, 1513.5

The control mean is \((10+12+9+11)/4=42/4=10.5\) points. The treatment mean is \((13+14+12+15)/4=54/4=13.5\) points. Therefore, fitting the two-group regression gives

$$ b_0=10.5, \qquad b_1=13.5-10.5=3.0. $$

The fitted equation is \(\widehat{Y}_i=10.5+3.0T_i\). For a control participant, \(T_i=0\), so the fitted value is 10.5 points. For a treatment participant, \(T_i=1\), so it is \(10.5+3.0=13.5\) points. The treatment coefficient says that the sample treatment mean is 3.0 points higher than the sample control mean. This describes the observed comparison; it does not by itself establish why the groups differed.

Fitted Values and Residuals

With a binary predictor, all participants in a group receive the same fitted value: that group’s sample mean. Their residuals are their observed outcomes minus that fitted group mean. The residuals within each group sum to zero, which is a consequence of fitting a separate mean for each group.

Worked Example: Calculating Fitted Values and Residuals

Use the hypothetical trial data above. The fitted value for each control participant is \(b_0=10.5\) points, and the fitted value for each treatment participant is \(b_0+b_1=13.5\) points. For each person, calculate \(e_i=Y_i-(b_0+b_1T_i)\).

AssignmentObserved outcome \(Y_i\)Fitted valueResidual \(e_i\)
Control1010.5\(10-10.5=-0.5\)
Control1210.5\(12-10.5=1.5\)
Control910.5\(9-10.5=-1.5\)
Control1110.5\(11-10.5=0.5\)
Treatment1313.5\(13-13.5=-0.5\)
Treatment1413.5\(14-13.5=0.5\)
Treatment1213.5\(12-13.5=-1.5\)
Treatment1513.5\(15-13.5=1.5\)

The control residuals sum to \(-0.5+1.5-1.5+0.5=0\) points. The treatment residuals sum to \(-0.5+0.5-1.5+1.5=0\) points. These are fitted residuals calculated from the observed outcomes and fitted coefficients, not the unobserved model errors \(\varepsilon_i\).

Unequal Group Sizes Do Not Change the Result

The difference-in-means identity does not depend on a balanced design. Each group’s fitted value is still its own sample mean, so the fitted coefficient is still the treatment mean minus the control mean.

Worked Example: Unequal Numbers in the Two Groups

Hypothetical data. Suppose three participants are assigned to control, with outcomes 18, 20, and 22 points, and two are assigned to treatment, with outcomes 24 and 26 points.

The control mean is \((18+20+22)/3=60/3=20\) points. The treatment mean is \((24+26)/2=50/2=25\) points. With treatment coded 1 and control coded 0, the regression intercept and treatment coefficient are

$$ b_0=20, \qquad b_1=25-20=5. $$

The fitted value is 20 points for each control participant and 25 points for each treatment participant. The treatment coefficient is 5 points, the difference between these group means. Unequal group sizes affect how many observations contribute to each mean and can affect the precision of the comparison, but they do not change the coefficient identity.

The Direction Depends on the Coding

The reference group determines the intercept, and the order of subtraction determines the sign of the treatment coefficient. With treatment coded 1 and control coded 0, the coefficient is treatment minus control. Reversing the indicator reverses the comparison. The underlying fitted group means do not change.

Worked Example: Reversing the Indicator

Return to the hypothetical unequal-size trial: the control mean is 20 points and the treatment mean is 25 points. Now define \(S_i=1\) for control and \(S_i=0\) for treatment, and fit an intercept plus \(S_i\). When \(S_i=0\), the fitted mean is the treatment mean, so the intercept is \(25\) points. When \(S_i=1\), the fitted mean is \(25\) plus the coefficient. To obtain the control mean of 20 points, the coefficient must be \(20-25=-5\) points.

$$ \widehat{Y}_i=25-5S_i. $$

For treatment, \(S_i=0\) gives a fitted value of 25 points; for control, \(S_i=1\) gives \(25-5=20\) points. The coefficient is now control minus treatment. The negative sign reflects the coding and comparison direction, not a different fitted comparison.

Why This Connection Matters

The binary-indicator regression provides a compact way to express a two-group comparison. Its intercept gives the reference group’s mean, while its treatment coefficient gives the difference from that reference group. This makes the result straightforward to report in outcome units and clarifies how changing the coding changes the coefficient’s interpretation.

The coefficient identity should not be confused with an inference result. The equality \(b_1=\bar{Y}_1-\bar{Y}_0\) tells us what the point estimate is. It does not determine a valid standard error, confidence interval, or test without considering the study design and the assumptions used for uncertainty estimates. Nor does writing the difference as a regression automatically adjust for other variables: this model contains only the intercept and treatment indicator.

Key takeaway: In a regression of an outcome on an intercept and a 0–1 treatment indicator, the intercept is the control-group sample mean and the treatment coefficient is the treatment-group mean minus the control-group mean. This exact algebraic result does not, by itself, justify a causal claim or an inference procedure.

Check Your Understanding

Use treatment coded 1 and control coded 0 unless a question says otherwise.

  1. If the control mean is 42 units and the treatment mean is 47 units, what are the fitted intercept and treatment coefficient? State each in context.
  2. Why does the fitted value remain constant within each assignment group in this model?
  3. For an observed outcome of 51 units in a treatment group whose fitted mean is 47 units, calculate the fitted residual and state its units.
  4. Does an unequal number of participants in the two arms change the identity between the treatment coefficient and the difference in sample means? Explain.
  5. If the indicator is recoded as 1 for control and 0 for treatment, which group mean becomes the intercept, and what difference does the coefficient represent?