A delivery takes longer when the route is longer and also when there are more packages to drop off. Long routes often carry more packages, so a line in distance alone mixes the two influences. Multiple linear regression puts both features into one prediction rule with a coefficient for each. This lesson fits such a rule by least squares, shows that its fitted values are a projection in the space of observations, and explains when the coefficients are unique and how software computes them reliably.
Key terms
Multiple linear regression
Definition Multiple linear regression predicts a numerical target from features with
For training cases, collect the parameters in and build the design matrix from a leading column of ones, which carries the intercept, followed by one column per feature. Then is , has entries, and the vector of predictions has entries. Because the ones column already supplies the intercept, no separate intercept is added. Least squares chooses to minimize , the sum of the squared residuals , which on this page are prediction minus observed as in Simple Linear Regression.
In plain words Each feature gets its own coefficient, and the prediction adds the contributions of all features to a baseline. The design matrix holds one case per row, so a single matrix product makes every prediction at once.
Example Four constructed deliveries have distance in kilometers, package count and duration in minutes, given by the rows , , and . With features the design matrix is ,
The least-squares coefficients found below are . For the first delivery the prediction is minutes, a baseline of 4 minutes, plus 2 for its one kilometer, plus 3 for its one package.
Normal equations
Definition For a real matrix and a target vector in , the normal equations are
They come from the objective , whose gradient is
The function is convex, because its Hessian is positive semidefinite, so minimizes exactly when the gradient is zero. The least-squares solutions are therefore exactly the solutions of the normal equations, and at least one always exists. Entry of is the dot product of column with the residual vector, so the equations say that the residual is orthogonal to every column of . With an intercept column, the first equation says that the residuals add to zero.
In plain words At the best coefficients the residual has zero dot product with every column. If it had a nonzero dot product with some column, moving that column’s coefficient a little in the right direction would lower the squared error.
Example For the deliveries,
The vector solves the equations, since , and . The predictions are 9, 15, 13 and 19 minutes, so the residuals are , , and . They add to zero, their sum weighted by distance is , and their sum weighted by package count is . The sum of squared residuals is 4 squared minutes.
Least-squares projection
Definition The column space is the set of all vectors , the prediction vectors the model can produce for the training cases. The least-squares fitted vector is the orthogonal projection of onto . For every , the vector lies in and is orthogonal to it, so
Hence is a least-squares solution exactly when , and the smallest sum of squared residuals is . The fitted vector is unique even when several coefficient vectors produce it. This picture lives in observation space , with one axis per training case, not in feature space, where the fitted model is a plane over the features.
In plain words Treat the whole column of targets as one point in a space with one axis per case. The model can reach only a flat subspace of that space, and least squares picks the point of the subspace closest to the target. The leftover arrow, the residual, meets the subspace at a right angle, and any other choice adds a second, perpendicular piece of error.
Example For the deliveries, and . The alternative coefficients predict 9.5, 14.5, 13.5 and 18.5 minutes. Their squared error is , which splits as , the squared distance from , plus the unavoidable . Since is orthogonal to , also splits as plus . In feature space the same fit is the plane above the distance and count axes.
Full column rank
Definition An matrix has full column rank when , that is, when its columns are linearly independent, so that only for . This requires , so a design with an intercept and features needs at least cases. The matrix is invertible if and only if has full column rank, because gives and hence . With full column rank the least-squares coefficients are unique,
Without it, if is one least-squares solution, the solutions are exactly the vectors with . They all give the same fitted vector, so the predictions for the training cases stay unique while the coefficients do not. A feature that is constant across the cases, for example, repeats the ones column up to a factor and breaks full column rank.
In plain words Full column rank means no column can be built from the others, so each coefficient has its own direction to account for. When one column is a combination of others, the data cannot say how to split the credit between them, although the best predictions are still well defined.
Example The delivery design has rank 3 and , so is the only solution. Now append a fourth column equal to twice the package count, with coefficient . The predictions depend on the two count coefficients only through , which must equal 3. The vectors and both give the fitted values 9, 15, 13 and 19, the new matrix is singular, and the rank is still 3. NumPy’s least-squares routine returns , the solution with the smallest Euclidean norm. That choice is a convention of the solver, not a finding about deliveries. For a new delivery whose fourth feature is again twice its count, all these solutions predict the same value.
Partial effect
Definition In a fitted multiple regression, the coefficient is the partial effect of in the model, the change in the predicted value when increases by one unit while every other feature in the model stays fixed. It is measured in target units per unit of . It describes the fitted model, and it reflects a comparison the data support only when they contain cases in which varies while the other features stay roughly the same. It is not a causal effect unless the study design or a separate causal argument justifies that reading. If another feature is computed from , such as , the other features cannot stay fixed and alone has no such reading.
In plain words Each coefficient says what the model adds for one more unit of its feature, with the other features in the model unchanged. When features move together in the data, that question concerns combinations the data rarely show, and the individual coefficients can become unstable even while the predictions stay sensible.
Example For , one more kilometer at a fixed package count adds 2 minutes to the prediction, and one more package at a fixed distance adds 3 minutes. Deliveries 1 and 4 differ by 2 kilometers and 2 packages, and their predictions 9 and 19 differ by minutes, a mix of both effects. In these four rows distance and package count are uncorrelated, so the slope of duration on distance alone is also 2 minutes per kilometer. When the features are correlated, the slope on one feature alone and its partial coefficient generally differ.
QR decomposition
Definition Let be a real matrix with full column rank. Its reduced QR decomposition is
where is with orthonormal columns, so that , and is , upper triangular and invertible. Requiring positive diagonal entries in makes the decomposition unique. Substituting it into the normal equations gives , and since is invertible the least-squares coefficients solve
a triangular system solved by back substitution, while the fitted vector is . This route never forms . For full column rank the condition number , the ratio of the largest to the smallest singular value, satisfies , so working through can lose up to about twice as many correct digits to rounding as working with directly. The singular value decomposition is another stable route. It also handles rank deficiency by treating singular values below a stated tolerance as zero, and it is the method behind NumPy’s lstsq. Neither method can create information that the columns do not contain.
In plain words QR rewrites the columns of as orthonormal directions together with a triangular recipe for rebuilding the original columns from them. Projecting onto orthonormal directions is easy and numerically gentle, and the triangular system is solved one coefficient at a time from the bottom up. The formula with an inverse is correct on paper, but it is not how careful software computes a fit.
Example For the delivery design, the Gram-Schmidt process applied to the three columns gives
and . The last row of gives , so . The middle row gives , so . The first row gives , so . Here and . In NumPy both routes return the same coefficients. Its QR routine may flip the signs of some columns of and rows of , which does not change the solution.
import numpy as np
X = np.array([[1, 1, 1], [1, 1, 3], [1, 3, 1], [1, 3, 3]], dtype=float)
y = np.array([10, 14, 12, 20], dtype=float)
Q, R = np.linalg.qr(X) # reduced QR, R is 3 by 3
theta_qr = np.linalg.solve(R, Q.T @ y) # R is upper triangular
theta, _, rank, sv = np.linalg.lstsq(X, y, rcond=None)
print(theta_qr, theta, rank) # [4. 2. 3.] [4. 2. 3.] 3Check yourself
- A fitted model for delivery time in minutes is , with distance in kilometers and package count . Write the design-matrix row of a delivery of 6 kilometers with 4 packages and find its prediction. Then find how the prediction changes when the distance becomes 8 kilometers and the count stays at 4.
Show the answer
The row is , where the leading 1 multiplies the intercept. The prediction is minutes.
At 8 kilometers it is minutes, an increase of 5 minutes. That is the distance coefficient times the 2-kilometer change, , with the package count held fixed. It describes the model, not what would happen if a particular route were made longer.
- For the four deliveries on this page, a design matrix has a column of ones, the distance in kilometers, the package count and the distance in meters. Is invertible? Give two different coefficient vectors with the same fitted values. Are the fitted values unique?
Show the answer
The meters column is 1000 times the kilometers column, so the four columns are linearly dependent. The rank is 3, less than the 4 columns, and is singular.
The predictions depend on the two distance coefficients only through , which must equal the original distance coefficient 2. So and both give the fitted values 9, 15, 13 and 19 minutes, and so does every vector .
The fitted values are unique, because they are the projection of onto the column space, which the extra column does not change. Only the split of the distance effect between the two columns is undetermined. Dropping one of the two distance columns restores full column rank.
- Four constructed deliveries have planned distance and driven distance , in kilometers, of , , and . In one sample the durations are 3.0, 4.8, 7.4 and 8.8 minutes, and least squares gives . In a second sample with the same features the durations are 3.4, 4.4, 7.0 and 9.2 minutes, each 0.4 minute away from the first, and least squares gives . Compare the two fits’ predictions for a delivery with and for one with and . Why do the coefficients change so much?
Show the answer
At the fits predict and minutes, the same value. At and they predict and minutes, which differ by 4 minutes.
In these data the driven distance is always within 0.1 kilometer of the planned one, so the two columns are nearly dependent, with correlation about 0.996. Writing
shows that the data pin down , which is 2 in both fits, but barely constrain how it is split. Raising by 4 and lowering by 4 changes each training prediction by only minute, so a change of 0.4 minute in the durations is enough to swing each coefficient by 4.
The matrix still has full column rank, so each fit has unique coefficients, yet they are poorly determined. The partial effect of planned distance at a fixed driven distance concerns deliveries like , , which the data never show, so neither fit should be trusted there. A fit on planned distance alone gives the same line for both samples.