The model in matrix form
Lesson 5.10.1 explained with one regressor . Most questions involve several: , each measured on every observation. Write one equation per observation and stack them, as lesson 3.5.3 stacked the equations of a line.
Rule: Linear regression
holds the responses and the errors, each with mean 0 and variance . The design matrix is : a column of ones, then one column for each of the regressors, so . holds the coefficients, in the order of the columns.
Simple regression is the case : has the columns and , as in lesson 3.5.3. Here is an example with two regressors. The second, , is a dummy variable: 1 for the four points of a second group and 0 for the first.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 3 | 4 | 5 | 6 | |
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | |
| 3.5 | 6 | 5.5 | 7 | 1.5 | 1.5 | 2.5 | 4.5 |
Row of is : the first row is and the last . So is and .
The normal equations
Least squares chooses to make as small as possible over all , the sum of squared residuals. That is lesson 3.5.3's problem with , so its answer carries over.
Rule: Least-squares estimate
The inverse exists when the columns of are independent (lesson 3.5.1). The residuals are orthogonal to every column: . Compute with np.linalg.lstsq, not by inverting (lesson 3.5.3).
Each entry of is a dot product of two columns: the count of points, the regressors' sums, and their sums of squares and products. holds the sum of the and the sums of each regressor times . For the eight points:
The 8 is the number of points, 28 the sum of and 4 the number in the second group. The 18 is the sum of over that group, and the last 4 is the sum of , again the group's size.
Solving gives . The fitted values are for the first group and for the second. The residuals are , and their sum of squares is 2.5.
With , the two normal equations are those of lesson 3.5.3's line, and their solution is lesson 5.10.1's and .
Try it
The cell builds from a column of ones and the two regressors, prints and , and solves the normal equations.
Then lstsq gives the same and the rank, 3. The last line prints the residuals, (rounded, as it is rounding noise) and the sum of squares.
What a coefficient means
With several regressors, each coefficient is the change in the mean of per unit of its own regressor, with the others held fixed.
Here : within either group, one more unit of goes with one more unit of . And : at the same , the second group sits 5 lower. The fit is two parallel lines, for the first group and for the second.
Inside each group rises one for one with . The second group has larger and lower , so the dashed line, fitted to alone, mixes the two effects and slopes down.
Leaving a regressor out
Fit on alone and the slope is , with . On its own, seems to do nothing, or even to lower . With in the model, is 0.918.
The reason is that and move together: their correlation is 0.6667, as the second group's values average 4.5 against 2.5. The slope of on is ( is delta). Leaving out credits with part of 's effect.
Rule: Leaving a regressor out
and come from the regression on both, and is the slope of on . It holds exactly for least-squares fits. The slope alone equals only when (the regressors are uncorrelated) or .
Here . The gap is called omitted-variable bias. It is why a coefficient means nothing until you know which other regressors are in the model.
Try it
The first lines fit on alone and print its intercept and slope, then alone and with both regressors, and TSS.
Then the slope of on , the correlation of the two regressors, the mean of in each group, and the rule's check.
Linear in the coefficients
"Linear" regression means that is linear in , not that it is a straight line in . A column may hold , or a dummy, and least squares works the same.
A curve. The points , , , and bend upwards. A straight line fits them badly: . Add the column and the fit is , with .
np.polyfit(x, y, 2) gives the same coefficients, highest power first.
Groups. A dummy shifts the line for its group, as did. With groups and a column of ones, use dummies. A dummy for every group adds up to the column of ones, so the columns are dependent (lesson 3.5.1).
For the eight points, the columns , and have rank 2. lstsq returns one of infinitely many , as lesson 3.5.3 found for dependent columns. Its fitted values, the two group means 5.5 and 2.5, are still unique.
Try it
The first block fits the five points with and without the column and compares with polyfit.
The second puts a dummy for each group beside the column of ones. lstsq reports rank 2 of 3 columns and returns its shortest answer.
The hat matrix
The fitted values are . The matrix in front of is the projection matrix of lesson 3.5.2 onto the column space of . Regression calls it the hat matrix , because it puts the hat on .
Rule: The hat matrix
is . The fitted values are the projection of onto the column space, and the residuals are what is left, orthogonal to it. The trace, the sum of the diagonal entries, equals the number of columns .
For lesson 3.5.3's five points, to , with :
, lesson 3.5.3's fitted values. Each fitted value is a weighted sum of all the , with row of as the weights. The middle row gives every point the weight 0.2: the fitted value at is the mean .
Leverage
The diagonal entry is the leverage of point : the weight of in its own fitted value. Each lies between 0 and 1, and they add up to , so they average .
For the five points the leverages are , adding up to 2. In simple regression : points far from pull hardest.
Move the last point from to and its leverage becomes 0.9571. The line then has to pass close to it, whatever its . So check the leverages when one sits far from the rest: a large error there moves the whole fit.
Try it
The cell builds for the five points, printed as , and checks , and the trace.
Then , , the leverages against the formula, and the leverages with the last point moved to 12.
R² with several regressors
as in lesson 5.10.1, with including the column of ones. For the eight points, and , so .
Adding a column can never raise RSS. The column space only grows, so the nearest point in it can only come closer to (lesson 3.5.2). So never falls, even when the new column is pure noise.
Lesson 3.5.3's 20 points have . Add a column of 20 random numbers, unrelated to anything, and rises to 0.853. The adjusted charges for each column, and here it falls, from 0.844 to 0.8358.
Rule: Adjusted R squared
Each sum is divided by its degrees of freedom: for the residuals and for around its mean. Lesson 5.10.1's used , the case . A column that adds little lowers ; lesson 5.10.3 tests whether regressors help.
Try it
The cell rebuilds lesson 3.5.3's points, then adds a column of noise drawn with a fixed seed. For each fit it prints and .
The noise column's coefficient is small, and in this run still goes up.
Lesson 5.10.3 adds the statistics of : its standard errors, and tests of each coefficient and of all of them together. Case study 5.10.5 fits a stock's returns on two factors at once.
Common mistakes
- Keep the column of ones. Without it the fit is forced through the origin, and as defined here no longer makes sense.
- A coefficient depends on the other columns. 's slope is 1 with in the model and without it.
- Use one dummy fewer than the number of groups. A dummy for every group, plus the ones, makes the columns dependent.
- The order of follows the columns of .
np.polyfitlists the highest power first. - Do not invert . Use
lstsqand read its rank (lesson 3.5.3). - A higher is not proof that a new regressor matters. Even noise raises it; compare or test it (lesson 5.10.3).
- Look for high leverage. A point far from the other values can move the whole line.
Regression in matrix form
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- : is , a column of ones and one column per regressor, and follows the order of the columns.
- Least squares: , so when the columns are independent; compute it with
lstsq. The residuals are orthogonal to every column. - Each coefficient is the effect of its regressor with the others held fixed. Leaving out changes 's slope to .
- Columns may be or dummies: the model is linear in . Use dummies for groups.
- with , the projection onto the column space: , . Its diagonal holds the leverages.
- never falls when a column is added; charges for each one.