Tutorials › VEE Mathematical Statistics › Least Squares with Replicated Predictor Values

VEE Mathematical Statistics · Least Squares Estimation · Tutorial 727 of 880

Least Squares with Replicated Predictor Values

Use replicate counts as frequency weights to compute the same least-squares slope and intercept as the full data, and see what group means alone cannot tell you.

Intermediate 10 min read

What You'll Learn

  • Define replicate groups by their shared predictor value, response mean, and count.
  • Derive weighted summary formulas that reproduce the full-data least-squares coefficients.
  • Recognize why each group mean must be weighted by its replicate count.
  • Separate the between-group part of squared error from within-group variation.
  • Identify when grouping preserves the fitted line and when it loses useful information.

Why Replicated Predictor Values Can Be Grouped

In a simple linear regression data set, several observations may share the same predictor value. For example, a hypothetical rating study might record multiple responses from participants exposed to the same dose. Repeated predictor values create a useful computational shortcut: instead of carrying every response through each coefficient calculation, we can summarize each predictor group by its response mean and its count.

The shortcut is exact for the ordinary least-squares slope and intercept, provided each original observation receives equal weight. The count matters: a group based on many observations must contribute more to the fit than a group based on only a few. Grouping does not, however, preserve all information about the responses within each group.

Definition: Suppose the observed data are \((x_i,y_i)\), \(i=1,\ldots,N\), and the distinct predictor values are \(x_1^*,\ldots,x_g^*\). For group \(j\), let \(n_j\) be the number of observations with predictor \(x_j^*\), and let \(\bar{y}_j\) be their response mean. These are replicated predictor values. The group counts satisfy \(\sum_{j=1}^g n_j=N\).

The ordinary least-squares line with an intercept chooses \(b_0\) and \(b_1\) to minimize the sum of squared errors \(\sum_{i=1}^N(y_i-b_0-b_1x_i)^2\). When using group summaries, the equivalent objective for finding the coefficients is \(\sum_{j=1}^g n_j(\bar{y}_j-b_0-b_1x_j^*)^2\). The factor \(n_j\) is a frequency weight: it represents how many original observations the group stands for.

Weighted Formulas for the Least-Squares Line

Assume the data are fitted by ordinary least squares with an intercept, and every original observation has equal weight. Define the total number of observations and the weighted means of the predictors and responses as follows:

$$ N=\sum_{j=1}^g n_j,\qquad \bar{x}=\frac{\sum_{j=1}^g n_jx_j^*}{N},\qquad \bar{y}=\frac{\sum_{j=1}^g n_j\bar{y}_j}{N}. $$

The slope and intercept can then be computed from the weighted centered sums:

$$ S_{xx}=\sum_{j=1}^g n_j(x_j^*)^2-\frac{\left(\sum_{j=1}^g n_jx_j^*\right)^2}{N}, \qquad S_{xy}=\sum_{j=1}^g n_jx_j^*\bar{y}_j- \frac{\left(\sum_{j=1}^g n_jx_j^*\right)\left(\sum_{j=1}^g n_j\bar{y}_j\right)}{N}. $$
$$ \hat{b}_1=\frac{S_{xy}}{S_{xx}},\qquad \hat{b}_0=\bar{y}-\hat{b}_1\bar{x}. $$

These formulas require at least two distinct predictor values, so that \(S_{xx}>0\). If every observation has the same predictor value, the data determine a fitted response level but do not determine a unique slope when an intercept is included.

Important: The formulas use \(n_j\) as a frequency weight, not as an instruction to give every group equal influence. Regressing the group means while ignoring their counts generally changes the fitted line.

Why the Grouped Fit Is Exactly the Same

The key fact is that the total squared error separates into a part depending only on variation within groups and a part depending on the fitted line and the group means. Write \(y_{ij}\) for response \(i\) in group \(j\); by definition, \(\sum_i(y_{ij}-\bar{y}_j)=0\). For any candidate line \(b_0+b_1x\), split each response's deviation from the line into its deviation from its group mean and the group's deviation from the line:

$$ y_{ij}-b_0-b_1x_j^* =(y_{ij}-\bar{y}_j)+(\bar{y}_j-b_0-b_1x_j^*). $$

Squaring and summing within group \(j\) gives a cross-product term proportional to \(\sum_i(y_{ij}-\bar{y}_j)\), which is zero. Summing the resulting equality across groups yields:

$$ \sum_{j=1}^g\sum_i(y_{ij}-b_0-b_1x_j^*)^2 = \sum_{j=1}^g\sum_i(y_{ij}-\bar{y}_j)^2 +\sum_{j=1}^g n_j(\bar{y}_j-b_0-b_1x_j^*)^2. $$

The first term on the right does not depend on \(b_0\) or \(b_1\). Therefore, minimizing the full-data sum of squared errors is equivalent to minimizing the count-weighted squared deviations of the group means from the line. This proves why the grouped calculation returns the same least-squares coefficients. It also shows what grouping omits: the group means and counts alone do not give the within-group sum of squared deviations.

Worked Example: Two Predictor Groups

A hypothetical assessment records responses 1 and 3 at predictor value \(x=0\), and responses 4 and 6 at \(x=2\). Find the ordinary least-squares line with an intercept using group summaries, then check it using the original observations.

Method. Calculate each response mean and count, then use the frequency-weighted summary formulas. This avoids treating the two group means as if they were the only two original observations.

At \(x=0\), the count is \(n_1=2\) and the mean is \(\bar{y}_1=(1+3)/2=2\). At \(x=2\), the count is \(n_2=2\) and the mean is \(\bar{y}_2=(4+6)/2=5\). Thus \(N=4\), \(\sum n_jx_j^*=4\), \(\sum n_j\bar{y}_j=14\), \(\sum n_j(x_j^*)^2=8\), and \(\sum n_jx_j^*\bar{y}_j=20\).

The centered sums are \(S_{xx}=8-4^2/4=4\) and \(S_{xy}=20-(4)(14)/4=6\). Therefore \(\hat{b}_1=6/4=3/2\). The weighted means are \(\bar{x}=4/4=1\) and \(\bar{y}=14/4=7/2\), so \(\hat{b}_0=7/2-(3/2)(1)=2\). The fitted line is \(\hat{y}=2+\frac{3}{2}x\).

Check. The line predicts 2 at \(x=0\) and 5 at \(x=2\), which are the two group means. In the original data, the residuals are \(-1,1,-1,1\); their sum of squares is \(1+1+1+1=4\). The coefficients and direct residual calculation are consistent. The common error here is to report slope 1: the difference between group means is 3, but the predictor values differ by 2, so the slope is \(3/2\).

Worked Example: Three Groups with Unequal Counts

A hypothetical study has groups summarized by \((x_j^*,\bar{y}_j,n_j)=(0,1,1),(1,2,2),(3,5,1)\). Find the least-squares slope and intercept for all four observations.

Method. Use weighted totals, with each mean repeated according to its count. This avoids giving the group with two observations the same influence as either single-observation group.

The total count is \(N=1+2+1=4\). The weighted totals are \(\sum n_jx_j^*=0+2+3=5\), \(\sum n_j\bar{y}_j=1+4+5=10\), \(\sum n_j(x_j^*)^2=0+2+9=11\), and \(\sum n_jx_j^*\bar{y}_j=0+4+15=19\).

Thus \(S_{xx}=11-5^2/4=19/4\), and \(S_{xy}=19-(5)(10)/4=13/2\). The slope is \(\hat{b}_1=(13/2)/(19/4)=26/19\). Also, \(\bar{x}=5/4\) and \(\bar{y}=10/4=5/2\), so:

$$ \hat{b}_0=\frac{5}{2}-\frac{26}{19}\left(\frac{5}{4}\right) =\frac{95}{38}-\frac{65}{38} =\frac{15}{19}. $$

The fitted line is \(\hat{y}=\frac{15}{19}+\frac{26}{19}x\). As a check, the weighted means of \(x\) and \(y\) are \(5/4\) and \(5/2\); substituting \(x=5/4\) into the line gives \(15/19+ (26/19)(5/4)=5/2\). The common mistake is to replace the frequency-weighted regression with an unweighted regression on the three means; that would give different coefficients.

Worked Example: Grouping Preserves the Line, Not All the Error Information

A hypothetical experiment has responses 1 and 3 at \(x=0\), responses 2 and 4 at \(x=1\), and responses 5 and 7 at \(x=3\). Find the fitted line from the group summaries and calculate the total squared error by separating within-group and between-group variation.

Method. First find the frequency-weighted fit. Then apply the squared-error decomposition to account for both the scatter around each group mean and the group means' deviations from the fitted line. This avoids assuming that the group means alone describe every response.

Each group has count 2. The means are 2, 3, and 6, respectively. The totals are \(N=6\), \(\sum n_jx_j^*=0+2+6=8\), \(\sum n_j\bar{y}_j=4+6+12=22\), \(\sum n_j(x_j^*)^2=0+2+18=20\), and \(\sum n_jx_j^*\bar{y}_j=0+6+36=42\).

Therefore \(S_{xx}=20-8^2/6=28/3\), \(S_{xy}=42-(8)(22)/6=38/3\), and \(\hat{b}_1=(38/3)/(28/3)=19/14\). Since \(\bar{x}=8/6=4/3\) and \(\bar{y}=22/6=11/3\), the intercept is \(\hat{b}_0=11/3-(19/14)(4/3)=20/21\). The fitted line is \(\hat{y}=20/21+(19/14)x\).

The within-group sums of squares are \( (1-2)^2+(3-2)^2=2\), \( (2-3)^2+(4-3)^2=2\), and \( (5-6)^2+(7-6)^2=2\), for a total of 6. The fitted values at the three predictor values are \(20/21\), \(20/21+19/14=97/42\), and \(20/21+57/14=191/42\). The group-mean deviations from those fitted values are \(22/21\), \(29/42\), and \(61/42\). The weighted between-group sum of squares is therefore:

$$ 2\left(\frac{22}{21}\right)^2+ 2\left(\frac{29}{42}\right)^2+ 2\left(\frac{61}{42}\right)^2 =\frac{1518+841+3721}{882} =\frac{6080}{882} =\frac{3040}{441}. $$

Adding the within-group contribution gives total squared error \(6+3040/441=5686/441\). This equals the full-data squared error by the decomposition. The check also illustrates the limitation of storing only means and counts: those summaries produce the line, but the within-group contribution of 6 requires information about the individual responses.

Common Mistakes and Practical Use

  • Ignoring the counts. A group mean based on many observations should contribute more to the fit than one based on few observations. Use \(n_j\) in the weighted totals and formulas.
  • Using a mean difference as a slope. The change in response mean must be divided by the corresponding change in predictor. For two groups, the slope is \((\bar{y}_2-\bar{y}_1)/(x_2^*-x_1^*)\), not just the response difference.
  • Assuming the summaries preserve every statistic. Counts and means preserve the ordinary least-squares coefficients, but not the within-group sum of squared errors or the original response values.
  • Changing the observation weights without noticing. The equivalence shown here assumes equal weight per original observation. If the original fit assigns unequal weights to observations, simple counts and ordinary group means do not in general reproduce that fit.

Replicated-value summaries are especially useful for hand calculations and compact data displays. For coefficient estimation, the weighted formulas save work without changing the answer. For any calculation that depends on the individual responses' scatter, retain or separately summarize the within-group variation as well.

Check Your Understanding

For each problem, use the replicate counts as frequency weights unless the question asks for a conceptual explanation.

  1. At \(x=1\), responses are 2 and 4; at \(x=4\), responses are 5 and 7. Find the ordinary least-squares line with an intercept. (Answer: \(\hat{y}=2+x\).)
  2. Three groups have summaries \((x_j^*,\bar{y}_j,n_j)=(0,2,2),(2,5,1),(4,6,3)\). Find the slope and intercept. (Answer: \(\hat{b}_1=57/58,\ \hat{b}_0=64/29\).)
  3. Groups have summaries \((-1,2,2),(1,4,2),(2,6,1)\). Find the fitted line. (Answer: \(\hat{y}=28/9+(11/9)x\).)
  4. Two observations at \(x=0\) have responses 1 and 3, and two at \(x=1\) have responses 2 and 4. Find the fitted line and its total squared error. (Answer: \(\hat{y}=2+x\), total squared error \(=4\).)
Clinical Biostats