With many regressors, writing OLS one coefficient at a time becomes hopeless. Matrix notation handles any number of regressors with one formula, and it is the language of every graduate econometrics text and statistical package.
We will set up the normal equations and finish with solving for β̂.
Step 1: The normal equations
Stack the observations: y is an n-vector and X an n × k matrix, with n rows and k columns, whose first column is ones. Minimizing the sum of squared residuals (y − Xβ)ᵀ(y − Xβ) gives the normal equations, Xᵀ(y − Xβ̂) = 0: the residuals are orthogonal to every column of X. Rearranged,
Example: x = (1, 2, 3) and y = (2, 3, 7) give n = 3, Σx = 6, Σx² = 14, Σy = 12 and Σxy = 29. The system has a unique solution when XᵀX is invertible: no regressor is an exact linear combination of the others. Including a 0/1 indicator for every category together with an intercept breaks this, since the indicators add up to the column of ones.
Step 2: Solving for β̂
Stack the regressors into X, compute XᵀX and Xᵀy, invert XᵀX, and multiply:
In the example, b₁ = (87 − 72) ÷ (42 − 36) = 2.5 and b₀ = (12 − 15)/3 = −1. The fitted values 1.5, 4 and 6.5 leave residuals 0.5, −1 and 0.5, which sum to zero, as the normal equations require.