Simple Linear Regression Using Matrix: Full Numerical Example
Introduction: Why Use Matrix Algebra for Regression?
When we estimate a Simple Linear Regression Model (SLRM) using matrix algebra, we get a compact and generalizable method that extends easily to multiple regression. In this post, we solve a complete numerical example step by step — from setting up the data matrices to finding the OLS coefficients, predicted values, residuals, variance, standard errors, and
.
Consider data on lot size (
) and man-hours of labor (
) for 10 recent production runs. We want to estimate the OLS regression line
using matrix notation.
Step 1: The Data
| Run | Y (Lot Size) | X (Man-Hours) |
|---|---|---|
| 1 | 73 | 30 |
| 2 | 50 | 20 |
| 3 | 128 | 60 |
| 4 | 170 | 80 |
| 5 | 87 | 40 |
| 6 | 108 | 50 |
| 7 | 135 | 60 |
| 8 | 69 | 30 |
| 9 | 148 | 70 |
| 10 | 132 | 60 |
Here
observations. In matrix form, the model is written as
, where
is an
vector,
is an
matrix (a column of 1’s for the intercept plus the values of the explanatory variable),
is a
vector of parameters, and
is the
error vector.
Step 2: Setting Up the Y Vector and X Matrix
The
vector is:

The
matrix (with a column of 1’s for the intercept term
) is:

Step 3: Computing 
The transpose
is a
matrix:
![]()
Multiplying
gives a
matrix made up of
,
, and
:
![]()
From the data:
and
. So:
![]()
Step 4: Computing 
Multiplying
gives a
vector made up of
and
:
![]()
From the data:
and
. So:
![]()
Step 5: Finding the Inverse 
For a
matrix
, the inverse is
, where
.
First, the determinant of
:
![]()
Now the inverse:
![]()
![]()
Step 6: Solving for the Coefficient Vector 
The OLS coefficient vector is found from
:
![]()
Solving element by element, using the exact fractions (
) to avoid rounding error:
![]()
![]()
![]()
So the coefficient vector is:
![]()
Step 7: The Estimated Regression Equation
Using the coefficients found above, the fitted regression line is:
![]()
This means for every additional man-hour of labor, lot size increases on average by 2 units, and the estimated lot size is 10 units when man-hours are zero (the intercept).
Step 8: Finding the Predicted Values 
The predicted values are obtained by multiplying the
matrix by the coefficient vector
. Each row of
is multiplied (as a dot product) with the column vector
:

Each element of
is computed as
, i.e., row
of
dotted with
:
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
So the predicted values (fitted
) matrix is:

Step 9: Finding the Residual Matrix 
The residual vector is found using the matrix equation
, subtracting the predicted values matrix
(Step 8) from the actual
matrix (Step 2), element by element:

So the residual matrix is:

For reference, the individual squared residuals used later in
are:
| Run | ||||
|---|---|---|---|---|
| 1 | 73 | 70 | 3 | 9 |
| 2 | 50 | 50 | 0 | 0 |
| 3 | 128 | 130 | -2 | 4 |
| 4 | 170 | 170 | 0 | 0 |
| 5 | 87 | 90 | -3 | 9 |
| 6 | 108 | 110 | -2 | 4 |
| 7 | 135 | 130 | 5 | 25 |
| 8 | 69 | 70 | -1 | 1 |
| 9 | 148 | 150 | -2 | 4 |
| 10 | 132 | 130 | 2 | 4 |
Notice that
, which confirms one of the key OLS properties — the sum of residuals is always zero. In matrix terms, this is because
, i.e., the residual vector is orthogonal to the column of 1’s in
.
Step 10: Residual Sum of Squares (RSS) — Two Matrix Approaches
Approach 1: Direct method, ![]()
The residual sum of squares is found by pre-multiplying the residual vector by its transpose,
:
![]()
Approach 2: Matrix shortcut formula, ![]()
This is the standard matrix formula used in econometrics, since
. First find
(the sum of squared
values):
![]()
Next, find
using the
vector and the
vector found in Step 4:

Therefore:
![]()
Both approaches agree exactly:
.
Step 11: Total Sum of Squares (TSS) Using Matrices
The matrix formula for TSS is:
![]()
where
is the column of 1’s transposed and multiplied by
. First compute the correction term:
![]()
Now subtract from
(found in Step 10):
![]()
This matches the direct computation
, confirming the result.
Step 12: Finding
Using Matrices
Once RSS and TSS are both known from matrix operations (Steps 10 and 11),
can be obtained entirely from matrix quantities—no need to touch the raw data again:
![]()
Substituting the matrix results already found:
![]()
Equivalently,
can be written using the Explained Sum of Squares in matrix form,
:
![]()
![]()
Both matrix routes confirm
, meaning approximately 99.56% of the variation in lot size (
) is explained by man-hours of labor (
).
Step 13: Variance and Standard Error of the Regression
The estimated variance of the error term (also called the Mean Square Error) is:
![]()
where
is the degrees of freedom (
observations,
parameters estimated). The standard error of the regression (standard error of the estimate) is:
![]()
Variance-Covariance Matrix of 
The variance-covariance matrix of the coefficient estimates is given by
:
![]()
The standard errors of the coefficients are the square roots of the diagonal elements:
![]()
![]()
Step 14: Testing Significance — The t-Values
To test whether each coefficient is statistically significant, we compute the t-statistic for each parameter using:
![]()
Using
,
(from Step 13):
![]()
Using
,
(from Step 13):
![]()
With
degrees of freedom, the critical value from the t-table at the 5% level of significance (two-tailed test) is
.
Interpretation of Significance
Since
, the intercept
is statistically significant at the 5% level — it is significantly different from zero.
Since
, the slope
is highly statistically significant, well beyond even the 1% level (
). This provides very strong evidence that man-hours of labor has a real and precise positive effect on lot size, and the relationship is extremely unlikely to be due to chance.
In short, both coefficients pass the significance test, but the slope coefficient’s exceptionally high t-value (42.58) reflects the near-perfect linear fit already suggested by
.
Summary of Results
| Statistic | Value |
|---|---|
| Intercept ( | 10 |
| Slope ( | 2 |
| Regression Equation | |
| Residual Sum of Squares (RSS) | 60 |
| Total Sum of Squares (TSS) | 13,660 |
| Explained Sum of Squares (ESS) | 13,600 |
| Variance ( | 7.5 |
| Standard Error of Regression ( | 2.7386 |
| Standard Error of | 2.5029 |
| Standard Error of | 0.0470 |
| Coefficient of Determination ( | 0.9956 |
| t-value of | 3.9953 |
| t-value of | 42.5833 |
| Critical t-value ( | 2.306 |
| Significance | Both coefficients significant at 5% |
Conclusion
Using matrix algebra, we estimated the simple linear regression of lot size on man-hours of labor and found
. The very high
of 0.9956 and the small standard errors of both coefficients suggest that man-hours of labor is a strong and precise predictor of lot size in this production process. This same matrix approach —
— extends directly to multiple regression with any number of explanatory variables, which is why it forms the foundation of econometric estimation.
Frequently Asked Questions
Q1: Why do we add a column of 1’s in the X matrix?
The column of 1’s allows the matrix multiplication to generate the intercept term
automatically as part of the coefficient vector, rather than estimating it separately.
Q2: Why is
in every OLS regression?
This is a mathematical property of the OLS normal equations: since the first normal equation sets
, minimizing the sum of squared residuals always forces residuals to sum to zero when an intercept is included in the model.
Q3: What does a high
value of 0.9956 mean?
It means that 99.56% of the variation in the dependent variable (lot size) is explained by the independent variable (man-hours of labor), leaving only 0.44% unexplained by the model.
Q4: Why divide by
instead of
when calculating variance?
Dividing by
(degrees of freedom) instead of
corrects for the bias introduced by estimating
parameters from the sample, giving an unbiased estimator of the error variance
.
Q5: How is the matrix method different from the direct-formula method of SLRM?
Both methods give identical results for simple regression. The matrix method, however, is more general — it applies directly to multiple regression with several explanatory variables using the same formula
, without needing separate formulas for each parameter.
Q6: Why does the matrix shortcut
give the same answer as
?
Because
is algebraically equal to
, the fitted values dotted with actual
. Since
, and OLS properties make
, the expression simplifies exactly to
.
Q7: What does a very high t-value like 42.58 for the slope mean?
A large t-value means the estimated coefficient is many standard errors away from zero, so we can be extremely confident the true slope is not zero. It signals a strong, precisely estimated relationship between man-hours of labor and lot size, consistent with the very high
.
Suggestions for further readings:





MinhajMetricsHub