Tutorials › Regression Analysis (Undergraduate) › Why the Matrix Product Gives the Fitted Value

Building and Reading Model Terms · Tutorial 140 of 991

Why the Matrix Product Gives the Fitted Value

See how the matrix product for one record is exactly the familiar intercept-plus-terms calculation, including when the row contains indicator variables.

Intermediate 9 min read

What You'll Learn

  • Identify the entries and dimensions of a single design-matrix row and its coefficient vector
  • Expand a row-by-column product into a sum of matched predictor terms
  • Explain why the intercept is represented by a row entry of 1
  • Calculate fitted outcomes using numeric predictors and a binary indicator
  • Check that design-row entries and coefficient estimates are in the same order
  • Distinguish the algebra of a fitted value from assumptions used for statistical inference

From a Regression Equation to a Matrix Product

A fitted value can be calculated in the familiar way: start with an intercept, multiply each predictor value by its coefficient estimate, and add the results. A design matrix packages the predictor values into a row, while a coefficient vector packages the corresponding estimates into a column. The product of those two objects is a compact way to write the same calculation.

Understanding why the product works is useful when reading regression notation or checking a hand calculation. The key is that the multiplication pairs entries in the same positions: the first row entry multiplies the first coefficient, the second row entry multiplies the second coefficient, and so on. The resulting sum is the intercept-plus-terms expression, not a different prediction rule.

The Model and Its Ingredients

Consider a regression model with \(p\) predictor columns. For record \(i\), let \(y_i\) be the observed outcome and let \(x_{ij}\) be the value in predictor column \(j\). In an additive model with an intercept, the model is

$$ y_i=\beta_0+\beta_1x_{i1}+\beta_2x_{i2}+\cdots+\beta_px_{ip}+\varepsilon_i. $$

Here \(\beta_0,\beta_1,\ldots,\beta_p\) are the model coefficients, and \(\varepsilon_i\) is the error for record \(i\). A standard mean-model condition is that the errors have conditional mean zero given the predictor values: \(E(\varepsilon_i\mid x_{i1},\ldots,x_{ip})=0\). This condition gives the model's conditional mean the intercept-plus-terms form. Other assumptions may be needed for particular statistical procedures, but they are not needed to verify the multiplication that calculates a fitted value.

After estimating the coefficients, write their estimates as \(\hat{\beta}_0,\hat{\beta}_1,\ldots,\hat{\beta}_p\). The fitted value for record \(i\), written \(\hat{y}_i\), is the value obtained by inserting its predictor values and the estimated coefficients into the regression equation:

$$ \hat{y}_i=\hat{\beta}_0+\hat{\beta}_1x_{i1}+\hat{\beta}_2x_{i2}+\cdots+\hat{\beta}_px_{ip}. $$

The fitted value is not the observed outcome \(y_i\). The observed outcome may differ from the fitted value; their difference is the residual. The calculation below concerns only how the fitted value is written as a matrix product.

Definition: For a model with an intercept and \(p\) predictor columns, the design row for record \(i\) is the row vector \(\mathbf{x}_i^\mathsf{T}=(1,x_{i1},x_{i2},\ldots,x_{ip})\). The coefficient vector is the column vector \(\hat{\boldsymbol{\beta}}=(\hat{\beta}_0,\hat{\beta}_1,\ldots,\hat{\beta}_p)^\mathsf{T}\). The fitted value is their product: \(\hat{y}_i=\mathbf{x}_i^\mathsf{T}\hat{\boldsymbol{\beta}}\).

The superscript \(\mathsf{T}\) indicates a transpose: it makes the displayed sequence of entries a row rather than a column. There are \(p+1\) entries in the design row because it includes one intercept entry as well as the \(p\) predictor entries. The coefficient vector also has \(p+1\) entries, in the corresponding order. Thus the product is defined: a \(1\)-by-\((p+1)\) row multiplied by a \((p+1)\)-by-\(1\) column gives a \(1\)-by-\(1\) number.

Why the Product Reproduces the Equation

For a row vector multiplied by a column vector, the rule is to multiply entries in matching positions and add those products. Applying that rule to the design row and estimated coefficient vector gives

$$ \begin{aligned} \mathbf{x}_i^\mathsf{T}\hat{\boldsymbol{\beta}} &=(1,x_{i1},x_{i2},\ldots,x_{ip}) (\hat{\beta}_0,\hat{\beta}_1,\hat{\beta}_2,\ldots,\hat{\beta}_p)^\mathsf{T}\\ &=1\hat{\beta}_0+x_{i1}\hat{\beta}_1+x_{i2}\hat{\beta}_2+\cdots+x_{ip}\hat{\beta}_p\\ &=\hat{\beta}_0+\hat{\beta}_1x_{i1}+\hat{\beta}_2x_{i2}+\cdots+\hat{\beta}_px_{ip}\\ &=\hat{y}_i. \end{aligned} $$

The first equality applies the definition of the design row and coefficient vector. The second follows from the row-by-column multiplication rule. In the third, the intercept term simplifies because \(1\hat{\beta}_0=\hat{\beta}_0\); the other products are written in the same order as the regression equation. The result is exactly the definition of the fitted value.

This also explains why an intercept model includes a 1 in every design row. That entry ensures the intercept estimate is included in the product. Without it, there would be no product term for \(\hat{\beta}_0\). The row entries need not all be measurements in their original form: a predictor column can be an indicator or another constructed term. Whatever its meaning, its entry must be paired with the coefficient estimate for that column.

1
Write the model terms in order.
List the intercept first, followed by each predictor column in the model's specified order.
2
Build the matching row and vector.
Put 1 and the record's predictor values into the row. Put the estimated coefficient for each corresponding term into the same position in the column vector.
3
Multiply matching entries and add.
The sum of those products is the fitted value for that record.

Worked Example: Two Numeric Predictors

Suppose a hypothetical model predicts a quiz score, in points, from hours of practice and number of missed classes. The estimated equation is \(\hat{y}=42+3.2x_1-1.5x_2\), where \(x_1\) is practice hours and \(x_2\) is missed classes. For a hypothetical student with 5 hours of practice and 2 missed classes, find the fitted score using the matrix product.

The predictor columns are ordered as practice hours, then missed classes. The design row and estimated coefficient vector in that order are

$$ \mathbf{x}_i^\mathsf{T}=(1,5,2), \qquad \hat{\boldsymbol{\beta}}=(42,3.2,-1.5)^\mathsf{T}. $$

Multiplying matching entries and adding gives

$$ \hat{y}_i=(1,5,2)(42,3.2,-1.5)^\mathsf{T} =1(42)+5(3.2)+2(-1.5) =42+16-3 =55. $$

The fitted quiz score is 55 points. This is the same calculation as substituting into \(42+3.2x_1-1.5x_2\). The negative sign belongs to the estimate for missed classes, so the contribution from two missed classes is \(2(-1.5)=-3\) points.

Indicator Columns Also Follow the Same Rule

An indicator column records membership in a group, often using 1 for one group and 0 for the other. Its coefficient estimate is multiplied by that 0-or-1 entry just like any other coefficient estimate is multiplied by its predictor value. If the entry is 0, that term contributes zero; if it is 1, the coefficient estimate is included in the sum. The multiplication rule does not change for a categorical predictor represented by indicator columns.

Worked Example: A Binary Indicator and a Numeric Predictor

Suppose a hypothetical model predicts weekly support hours using age in years and an indicator for enrollment in a support program. The indicator \(D\) equals 1 for enrolled people and 0 otherwise. The estimated equation is \(\hat{y}=8+0.4x+5.5D\), where \(x\) is age. Find the fitted weekly support hours for a hypothetical 60-year-old enrolled person.

The terms are ordered as intercept, age, and program indicator. Since this person is enrolled, \(D=1\). The matching row and coefficient vector are

$$ \mathbf{x}_i^\mathsf{T}=(1,60,1), \qquad \hat{\boldsymbol{\beta}}=(8,0.4,5.5)^\mathsf{T}. $$

The matrix product is

$$ \hat{y}_i=(1,60,1)(8,0.4,5.5)^\mathsf{T} =1(8)+60(0.4)+1(5.5) =8+24+5.5 =37.5. $$

The fitted value is 37.5 support hours per week. The indicator entry of 1 makes the program term contribute 5.5 hours. For a person with the same age but \(D=0\), that term would contribute \(0(5.5)=0\); all other entries would stay the same.

Order Is Part of the Calculation

A matrix product pairs positions, not labels. If a design row lists the predictor columns in one order but the coefficient vector lists their estimates in another, the calculation can be wrong even if every number appears somewhere in the two objects. In practice, the order is determined by the model's columns. Write it down before multiplying, especially when a model has several numeric predictors or indicator columns.

For a categorical predictor represented by more than one indicator, each indicator has its own design-row entry and corresponding coefficient. A zero in one indicator column does not remove the other columns from the product; it simply makes that particular product zero. The intercept remains a separate first term in an intercept model.

Worked Example: Matching Two Indicator Columns to Their Coefficients

Suppose a hypothetical model predicts a rating score from the number of sessions attended and a three-category program type. Program A is the reference category; \(D_B=1\) identifies Program B and \(D_C=1\) identifies Program C. The estimated equation is \(\hat{y}=12+0.8x+4D_B-2D_C\), where \(x\) is sessions attended. Find the fitted rating for a hypothetical Program B participant who attended 6 sessions.

The column order is intercept, sessions, \(D_B\), then \(D_C\). For this participant, \(D_B=1\) and \(D_C=0\), so

$$ \mathbf{x}_i^\mathsf{T}=(1,6,1,0), \qquad \hat{\boldsymbol{\beta}}=(12,0.8,4,-2)^\mathsf{T}. $$

Now multiply the entries in matching positions:

$$ \hat{y}_i=(1,6,1,0)(12,0.8,4,-2)^\mathsf{T} =1(12)+6(0.8)+1(4)+0(-2) =12+4.8+4+0 =20.8. $$

The fitted rating is 20.8 points. The Program C term is present in the product but contributes zero because this record is not in Program C. This demonstrates that the product reproduces each term of the equation, including terms whose value for a particular record is zero.

What the Product Does—and Does Not—Depend On

The identity \(\hat{y}_i=\mathbf{x}_i^\mathsf{T}\hat{\boldsymbol{\beta}}\) is an algebraic statement about a specified design row and its corresponding coefficient estimates. It does not require the errors to be normally distributed, independent, or to have constant variance. Such conditions can matter when estimating coefficients or drawing conclusions about them, but the row-by-column multiplication itself follows from the definition of matrix multiplication.

The product also does not decide which columns belong in the model, how categorical variables are coded, or whether the model is appropriate for a particular scientific question. It applies once the model terms, their order, the record's values, and the coefficient estimates are specified. The result is a fitted outcome on the outcome's scale; it is not automatically an observed outcome or a causal effect.

Common Mistake: Multiplying the Wrong Positions

A common error is to put the predictor values into a row in one order and the coefficients into a vector in another, then multiply matching positions anyway. For example, if a model's columns are age followed by an indicator, the row \((1,\text{age},D)\) must pair with \((\hat{\beta}_0,\hat{\beta}_{\text{age}},\hat{\beta}_D)^\mathsf{T}\). Pairing the age entry with the indicator coefficient changes the model calculation.

The correction is to align the row and vector by model term before doing arithmetic. Check both the number of entries and their order, then expand the product as a sum. If that expanded sum does not match the intercept-plus-terms equation, the row or coefficient vector has been arranged incorrectly.

Key takeaway: A single design row contains 1 for the intercept followed by the record's predictor-column values. Multiplying it by the coefficient vector pairs each value with its matching estimate and adds those products, reproducing the fitted-value equation exactly.

Check Your Understanding

Use the term order specified in each question.

  1. For \(\hat{y}=10+2x_1-3x_2\), write the design row and coefficient vector for a record with \(x_1=4\) and \(x_2=1\).
  2. Explain why an intercept model includes a 1 as the first entry of each design row.
  3. A model has terms in the order intercept, age, and indicator \(D\). What is the contribution of the indicator term when \(D=0\), and when \(D=1\)?
  4. For \(\hat{y}=6+1.5x\), calculate the fitted value at \(x=8\) by writing the row-by-column product and expanding it.
  5. Why can a row and coefficient vector containing all the correct values still give a wrong fitted value?