Multiple Linear Regression Using Matrix Algebra: Demand Function Example

Introduction: Multiple Linear Regression Using Matrix

The matrix approach to regression becomes especially powerful once we move beyond a single explanatory variable. It gives us numerical estimates of our multiple linear regression parameters more efficiently. In this post, we estimate a demand function using matrix algebra, where quantity demanded (Y) depends on two explanatory variables: consumers’ income (X_1) and the price of the commodity (X_2). We solve every step — from setting up the matrices to coefficients, R^2, elasticities, standard errors, and t-values – and finally present the results in standard academic/research-paper format.

Step 1: The Data

Obs.Y (Qty. Demanded)X₁ (Income)X₂ (Price)
11406022
21556225
31596724
41797020
51927115
62007214
72127514
82157811

We have n = 8 observations and k = 3 parameters to estimate (\beta_0, \beta_1, \beta_2). The population regression model is:

Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + u

Step 2: Setting Up the Matrices — Y, X, Beta, and Error

In matrix notation, the model is written as Y = X\beta + u. The four matrices are defined as follows.

The Y matrix (8 \times 1) — the dependent variable, quantity demanded:

Y = \begin{bmatrix} 140 \\ 155 \\ 159 \\ 179 \\ 192 \\ 200 \\ 212 \\ 215 \end{bmatrix}

The X matrix (8 \times 3) — a column of 1’s (for the intercept), followed by income (X_1) and price (X_2):

X = \begin{bmatrix} 1 & 60 & 22 \\ 1 & 62 & 25 \\ 1 & 67 & 24 \\ 1 & 70 & 20 \\ 1 & 71 & 15 \\ 1 & 72 & 14 \\ 1 & 75 & 14 \\ 1 & 78 & 11 \end{bmatrix}

The \beta matrix (3 \times 1) — the unknown population parameters to be estimated:

\beta = \begin{bmatrix} \beta_0 \\ \beta_1 \\ \beta_2 \end{bmatrix}

The error matrix (8 \times 1) — the disturbance term for each observation:

u = \begin{bmatrix} u_1 \\ u_2 \\ u_3 \\ u_4 \\ u_5 \\ u_6 \\ u_7 \\ u_8 \end{bmatrix}

The complete model in matrix form is therefore Y = X\beta + u, and OLS estimation gives us the sample counterpart Y = X\hat{\beta} + e, where \hat{\beta} is the OLS coefficient vector and e is the residual vector.

Step 3: Computing X'X

The transpose X' is a (3 \times 8) matrix:

X' = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 60 & 62 & 67 & 70 & 71 & 72 & 75 & 78 \\ 22 & 25 & 24 & 20 & 15 & 14 & 14 & 11 \end{bmatrix}

Multiplying X'X gives a symmetric (3 \times 3) matrix built from the sums n, \sum X_1, \sum X_2, \sum X_1^2, \sum X_2^2, and \sum X_1X_2:

X'X = \begin{bmatrix} n & \sum X_1 & \sum X_2 \\ \sum X_1 & \sum X_1^2 & \sum X_1X_2 \\ \sum X_2 & \sum X_1X_2 & \sum X_2^2 \end{bmatrix}

From the data: \sum X_1 = 555, \sum X_2 = 145, \sum X_1^2 = 38{,}767, \sum X_2^2 = 2{,}823, and \sum X_1X_2 = 9{,}859. So:

X'X = \begin{bmatrix} 8 & 555 & 145 \\ 555 & 38{,}767 & 9{,}859 \\ 145 & 9{,}859 & 2{,}823 \end{bmatrix}

Step 4: Computing X'Y

Multiplying X'Y gives a (3 \times 1) vector built from \sum Y, \sum X_1Y, and \sum X_2Y:

X'Y = \begin{bmatrix} \sum Y \\ \sum X_1Y \\ \sum X_2Y \end{bmatrix}

From the data: \sum Y = 1{,}452, \sum X_1Y = 101{,}895, and \sum X_2Y = 25{,}364. So:

X'Y = \begin{bmatrix} 1{,}452 \\ 101{,}895 \\ 25{,}364 \end{bmatrix}

Step 5: Finding (X'X)^{-1}

Determinant of X'X (cofactor expansion along the first row):

|X'X| = 8\begin{vmatrix} 38{,}767 & 9{,}859 \\ 9{,}859 & 2{,}823 \end{vmatrix} - 555\begin{vmatrix} 555 & 9{,}859 \\ 145 & 2{,}823 \end{vmatrix} + 145\begin{vmatrix} 555 & 38{,}767 \\ 145 & 9{,}859 \end{vmatrix}

= 8(38{,}767 \times 2{,}823 - 9{,}859 \times 9{,}859) - 555(555 \times 2{,}823 - 9{,}859 \times 145) + 145(555 \times 9{,}859 - 38{,}767 \times 145)

= 8(12{,}239{,}360) - 555(137{,}210) + 145(-149{,}470)

= 97{,}914{,}880 - 76{,}151{,}550 - 21{,}673{,}150 = 90{,}180

So |X'X| = 90{,}180.

Cofactors of X'X:

C_{11} = (38{,}767)(2{,}823) - (9{,}859)^2 = 12{,}239{,}360
C_{12} = -[(555)(2{,}823) - (9{,}859)(145)] = -137{,}210
C_{13} = (555)(9{,}859) - (38{,}767)(145) = -149{,}470
C_{22} = (8)(2{,}823) - (145)^2 = 1{,}559
C_{23} = -[(8)(9{,}859) - (145)(555)] = 1{,}603
C_{33} = (8)(38{,}767) - (555)^2 = 2{,}111

Since X'X is symmetric, its cofactor (and adjugate) matrix is also symmetric. The inverse is (X'X)^{-1} = \frac{1}{|X'X|}\text{adj}(X'X):

(X'X)^{-1} = \frac{1}{90{,}180}\begin{bmatrix} 12{,}239{,}360 & -137{,}210 & -149{,}470 \\ -137{,}210 & 1{,}559 & 1{,}603 \\ -149{,}470 & 1{,}603 & 2{,}111 \end{bmatrix}

(X'X)^{-1} = \begin{bmatrix} 135.7214 & -1.5215 & -1.6575 \\ -1.5215 & 0.017288 & 0.017776 \\ -1.6575 & 0.017776 & 0.023409 \end{bmatrix}

Step 6: Solving for the Coefficient Vector \hat{\beta} = (X'X)^{-1}X'Y

Using the exact integer numerators over the common determinant 90{,}180 (to avoid rounding error), each element of \hat{\beta} is found as the dot product of a row of the adjugate matrix with X'Y:

\hat{\beta}_0 = \frac{(12{,}239{,}360)(1{,}452) + (-137{,}210)(101{,}895) + (-149{,}470)(25{,}364)}{90{,}180} = \frac{-619{,}310}{90{,}180} = -6.8675

\hat{\beta}_1 = \frac{(-137{,}210)(1{,}452) + (1{,}559)(101{,}895) + (1{,}603)(25{,}364)}{90{,}180} = \frac{283{,}877}{90{,}180} = 3.1479

\hat{\beta}_2 = \frac{(-149{,}470)(1{,}452) + (1{,}603)(101{,}895) + (2{,}111)(25{,}364)}{90{,}180} = \frac{-149{,}351}{90{,}180} = -1.6561

So the estimated coefficient vector is:

\hat{\beta} = \begin{bmatrix} \hat{\beta}_0 \\ \hat{\beta}_1 \\ \hat{\beta}_2 \end{bmatrix} = \begin{bmatrix} -6.8675 \\ 3.1479 \\ -1.6561 \end{bmatrix}

Step 7: The Estimated Regression Equation

\hat{Y} = -6.8675 + 3.1479X_1 - 1.6561X_2

This says that, holding price constant, a one-unit increase in income raises quantity demanded by about 3.15 units, while holding income constant, a one-unit increase in price reduces quantity demanded by about 1.66 units — both consistent with standard demand theory (a normal good with a downward-sloping demand curve).

Step 8: Finding the Predicted Values \hat{Y} = X\hat{\beta}

Each predicted value is the dot product of a row of X with \hat{\beta}, i.e. \hat{Y}_i = \hat{\beta}_0 + \hat{\beta}_1X_{1i} + \hat{\beta}_2X_{2i}:

\hat{Y}_1 = -6.8675 + 3.1479(60) - 1.6561(22) = 145.571
\hat{Y}_2 = -6.8675 + 3.1479(62) - 1.6561(25) = 146.898
\hat{Y}_3 = -6.8675 + 3.1479(67) - 1.6561(24) = 164.294
\hat{Y}_4 = -6.8675 + 3.1479(70) - 1.6561(20) = 180.362
\hat{Y}_5 = -6.8675 + 3.1479(71) - 1.6561(15) = 191.791
\hat{Y}_6 = -6.8675 + 3.1479(72) - 1.6561(14) = 196.595
\hat{Y}_7 = -6.8675 + 3.1479(75) - 1.6561(14) = 206.038
\hat{Y}_8 = -6.8675 + 3.1479(78) - 1.6561(11) = 220.451

So the predicted values matrix is:

\hat{Y} = X\hat{\beta} = \begin{bmatrix} 145.571 \\ 146.898 \\ 164.294 \\ 180.362 \\ 191.791 \\ 196.595 \\ 206.038 \\ 220.451 \end{bmatrix}

Step 9: Finding the Residual Matrix e = Y - X\hat{\beta}

The residual vector is obtained by subtracting the predicted values matrix (Step 8) from the actual Y matrix, element by element:

e = Y - X\hat{\beta} = \begin{bmatrix} 140 \\ 155 \\ 159 \\ 179 \\ 192 \\ 200 \\ 212 \\ 215 \end{bmatrix} - \begin{bmatrix} 145.571 \\ 146.898 \\ 164.294 \\ 180.362 \\ 191.791 \\ 196.595 \\ 206.038 \\ 220.451 \end{bmatrix} = \begin{bmatrix} -5.571 \\ 8.102 \\ -5.294 \\ -1.362 \\ 0.209 \\ 3.405 \\ 5.962 \\ -5.451 \end{bmatrix}

Obs.Y\hat{Y}e = Y - \hat{Y}e^2
1140145.571-5.57131.04
2155146.8988.10265.64
3159164.294-5.29428.03
4179180.362-1.3621.86
5192191.7910.2090.04
6200196.5953.40511.59
7212206.0385.96235.54
8215220.451-5.45129.71

The residuals sum to approximately zero (\sum e_i \approx 0), confirming the OLS property that \mathbf{1}'e = 0 whenever an intercept is included.

Step 10: Residual Sum of Squares (RSS) via Matrices

Direct method: e'e = \sum e_i^2 = 31.04+65.64+28.03+1.86+0.04+11.59+35.54+29.71 = 203.44

Matrix shortcut: RSS = Y'Y - \hat{\beta}'X'Y. First find Y'Y = \sum Y_i^2:

Y'Y = 140^2+155^2+159^2+179^2+192^2+200^2+212^2+215^2 = 268{,}980

Next, \hat{\beta}'X'Y:

\hat{\beta}'X'Y = (-6.8675)(1{,}452) + (3.1479)(101{,}895) + (-1.6561)(25{,}364) = 268{,}776.56

RSS = Y'Y - \hat{\beta}'X'Y = 268{,}980 - 268{,}776.56 = 203.44

Both approaches agree: RSS = 203.44.

Step 11: Total Sum of Squares (TSS) via Matrices

TSS = Y'Y - n\bar{Y}^2, where \bar{Y} = \frac{\sum Y_i}{n} = \frac{1{,}452}{8} = 181.5.

n\bar{Y}^2 = 8(181.5)^2 = 8(32{,}942.25) = 263{,}538

TSS = 268{,}980 - 263{,}538 = 5{,}442

Step 12: Finding R^2 Using Matrices

With RSS and TSS already obtained purely from matrix operations, R^2 follows directly:

R^2 = 1 - \frac{RSS}{TSS} = 1 - \frac{203.44}{5{,}442} = 1 - 0.0374 = 0.9626

This means 96.26% of the variation in quantity demanded is jointly explained by income and price, indicating a very good fit.

Step 13: Variance and the Variance-Covariance Matrix of \hat{\beta}

The estimated variance of the error term is:

\hat{\sigma}^2 = \frac{e'e}{n-k} = \frac{203.44}{8-3} = \frac{203.44}{5} = 40.69

The variance-covariance matrix of the coefficient vector is Var(\hat{\beta}) = \hat{\sigma}^2(X'X)^{-1}:

Var(\hat{\beta}) = 40.69\begin{bmatrix} 135.7214 & -1.5215 & -1.6575 \\ -1.5215 & 0.017288 & 0.017776 \\ -1.6575 & 0.017776 & 0.023409 \end{bmatrix} = \begin{bmatrix} 5{,}522.28 & -61.91 & -67.44 \\ -61.91 & 0.7034 & 0.7233 \\ -67.44 & 0.7233 & 0.9525 \end{bmatrix}

The standard errors of the coefficients are the square roots of the diagonal elements:

se(\hat{\beta}_0) = \sqrt{5{,}522.28} = 74.312
se(\hat{\beta}_1) = \sqrt{0.7034} = 0.839
se(\hat{\beta}_2) = \sqrt{0.9525} = 0.976

Step 14: t-Values and Tests of Significance

Each t-statistic is computed as t_{\hat{\beta}_j} = \hat{\beta}_j / se(\hat{\beta}_j), with n-k = 5 degrees of freedom:

t_{\hat{\beta}_0} = \dfrac{-6.8675}{74.312} = -0.092
t_{\hat{\beta}_1} = \dfrac{3.1479}{0.839} = 3.753
t_{\hat{\beta}_2} = \dfrac{-1.6561}{0.976} = -1.697

The critical t-value at the 5% level of significance (two-tailed, df=5) is t_{0.025,5} = 2.571, and at the 10% level it is t_{0.05,5} = 2.015.

Interpretation

Intercept (\hat{\beta}_0): |t| = 0.092 < 2.571, so the intercept is statistically insignificant. This is not unusual, since the intercept has no direct economic meaning here (income and price are never realistically zero).

Income coefficient (\hat{\beta}_1): |t| = 3.753 > 2.571, so income is statistically significant at the 5% level. Higher consumer income significantly raises quantity demanded, as expected for a normal good.

Price coefficient (\hat{\beta}_2): |t| = 1.697 < 2.571, so price is not significant at the conventional 5% two-tailed level. However, economic theory strongly predicts a negative price effect, which justifies a one-tailed test. The one-tailed 10% critical value at df=5 is t_{0.10,5} = 1.476; since 1.697 > 1.476, the price effect is significant at the 10% level (one-tailed), though it remains a weaker result than the income effect.

Step 15: Income and Price Elasticity of Demand

Elasticities are calculated at the mean values of the variables, using \bar{X}_1 = 69.375, \bar{X}_2 = 18.125, and \bar{Y} = 181.5.

Income elasticity of demand:

E_{Y,X_1} = \hat{\beta}_1 \times \frac{\bar{X}_1}{\bar{Y}} = 3.1479 \times \frac{69.375}{181.5} = 3.1479 \times 0.3822 = 1.2032

Since E_{Y,X_1} = 1.20 > 1, demand is income-elastic — the commodity behaves like a luxury good, with quantity demanded rising proportionally more than income.

Price elasticity of demand:

E_{Y,X_2} = \hat{\beta}_2 \times \frac{\bar{X}_2}{\bar{Y}} = -1.6561 \times \frac{18.125}{181.5} = -1.6561 \times 0.09986 = -0.1654

Since |E_{Y,X_2}| = 0.165 < 1, demand is price-inelastic — a 1% increase in price reduces quantity demanded by only about 0.165%, suggesting the commodity has few close substitutes or is a necessity.

Regression Results (Academic / Research Paper Format)

The estimated demand function, with standard errors in parentheses and t-statistics in brackets below each coefficient, is reported as follows:

\hat{Y}_i = \underset{(74.312)}{-6.8675} + \underset{(0.839)}{3.1479}\,X_{1i} - \underset{(0.976)}{1.6561}\,X_{2i}

t: \quad (-0.092) \qquad (3.753) \qquad (-1.697)

R^2 = 0.9626, \quad n = 8, \quad df = 5

Table 1. OLS Estimates of the Demand Function

VariableCoefficientStd. Errort-StatisticSignificance (5%)
Intercept-6.867574.312-0.092Not significant
Income (X_1)3.14790.8393.753Significant
Price (X_2)-1.65610.976-1.697Not sig. at 5% (two-tailed); sig. at 10% (one-tailed)

Note: R^2 = 0.9626; n = 8; degrees of freedom = 5. Income elasticity of demand = 1.2032; price elasticity of demand = -0.1654.

Overall Interpretation of the Model

The estimated model shows that consumer income and commodity price jointly explain about 96.26% of the variation in quantity demanded (R^2 = 0.9626), indicating a strong overall fit.

Income has a positive and statistically significant effect on demand, consistent with the good being a normal good; in fact, with an income elasticity greater than one, it behaves as a luxury good.

Price has the theoretically expected negative sign, but its effect is only marginally significant at conventional levels, and demand is price-inelastic, implying that consumers do not adjust their purchases much in response to price changes, possibly because the good has few substitutes or is a routine purchase.

The intercept itself is not economically meaningful and is statistically insignificant, which is expected since income and price are never realistically zero in practice.

Frequently Asked Questions

Q1: Why do we still add a column of 1’s in the X matrix for multiple regression?
Just as in simple regression, the column of 1’s lets matrix multiplication generate the intercept \beta_0 automatically, alongside the slope coefficients for each explanatory variable.

Q2: How is (X'X)^{-1} found for a 3\times3 matrix?
It is found using the cofactor/adjugate method: compute the determinant via cofactor expansion, find the cofactor for each element, arrange them into the adjugate matrix (transpose of the cofactor matrix), and divide every element by the determinant.

Q3: Why is the intercept insignificant while income is significant?
The intercept represents the predicted value of Y when both X_1 and X_2 are zero — a scenario with no real-world meaning here, so it is common for the intercept’s t-value to be small and statistically insignificant. Income, by contrast, has direct economic relevance to demand and shows a strong, significant relationship.

Q4: What does an income elasticity greater than 1 tell us?
An income elasticity above 1 (here, 1.20) means the commodity is a luxury good — demand grows proportionally faster than income. Elasticity between 0 and 1 would indicate a normal necessity good, and a negative value would indicate an inferior good.

Q5: Why might price be statistically insignificant even though its coefficient has the expected negative sign?
With a small sample (n=8) and a low price elasticity (demand is inelastic), the standard error on the price coefficient can be large relative to its estimate, reducing the t-value. This doesn’t necessarily mean price has no effect — it may reflect limited sample size or genuinely weak price sensitivity for this commodity.

About the author

Picture of Muhammad Minhaj Akhtar

Muhammad Minhaj Akhtar

Muhammad Minhaj Akhtar is a Lecturer in Economics at Government Graduate College Jauharabad, Pakistan. He holds an M.Phil. in Economics from Quaid-i-Azam University, Islamabad, and an MSc in Economics from the University of Sargodha, where he earned a Silver Medal. His academic passion lies in Econometrics, with a strong focus on applying empirical methods to real-world economic issues. Through MinhajMetrixHub, he shares learning resources, research guidance, and practical econometric insights for students and researchers.

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts