Simple and multiple regression簡單回歸與多元回歸

Regression turns a question such as “how much more do people with more schooling earn?” into a number. This week builds the linear regression model, derives the ordinary least squares (OLS) estimator from scratch, and then writes the whole method in one line of matrix algebra.

回歸分析把「多讀書的人收入高多少?」這類問題變成一個數字。本週建立線性回歸模型,從零推導普通最小二乘(OLS)估計量,最後用一行矩陣代數寫出整個方法。

Builds on 先修內容
Weeks 1–2
Reading time 閱讀時間
About 40 minutes
Data 數據
wage2, n = 935
Code 程式碼
R, Python

The simple linear regression model簡單線性回歸模型

Regression starts from a question about averages: if we know xx, what value of yy should we expect? Week 2 called this the conditional expectation function, E(y∣x)E(y \mid x). The simple linear regression model assumes that it is a straight line:

回歸分析從一個關於平均值的問題開始:如果知道 xx,我們應該預期 yy 是多少?第 2 週把這稱為條件期望函數 E(y∣x)E(y \mid x)。簡單線性回歸模型假設它是一條直線:

E(yi∣xi)=β0+β1xi,i=1,…,n.E(y_i \mid x_i) = \beta_0 + \beta_1 x_i, \qquad i = 1, \dots, n.

Raise xx by one unit and the expected value of yy rises by exactly β1\beta_1, whatever the starting point:

把 xx 提高一個單位,yy 的期望值便會上升 β1\beta_1,與起點無關:

E(yi∣xi+1)−E(yi∣xi)=β1.E(y_i \mid x_i + 1) - E(y_i \mid x_i) = \beta_1.

So β1\beta_1 is the average change in yy for a one-unit change in xx. The same model is usually written with an error term uiu_i:

所以 β1\beta_1 是 xx 每改變一個單位時 yy 的平均變化。同一個模型通常會加上誤差項 uiu_i 來表達:

yi=β0+β1xi+ui,E(ui∣xi)=0.y_i = \beta_0 + \beta_1 x_i + u_i, \qquad E(u_i \mid x_i) = 0.

The error ui=yi−E(yi∣xi)u_i = y_i - E(y_i \mid x_i) is how far an individual observation lies from its conditional mean. It collects every factor other than xix_i that affects yiy_i. Holding those factors fixed, β1\beta_1 is the marginal effect of xx on yy: dyi/dxi=β1dy_i / dx_i = \beta_1.

誤差項 ui=yi−E(yi∣xi)u_i = y_i - E(y_i \mid x_i) 是單個觀測值與其條件平均值的距離,它包含了 xix_i 以外所有影響 yiy_i 的因素。在這些因素保持不變的情況下,β1\beta_1 就是 xx 對 yy 的邊際效應:dyi/dxi=β1dy_i / dx_i = \beta_1。

Ordinary least squares普通最小二乘法

We never observe β0\beta_0 and β1\beta_1. We observe a sample of nn pairs (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) and must use it to estimate them. For any candidate line b0+b1xb_0 + b_1 x, the residual u^i=yi−b0−b1xi\uh_i = y_i - b_0 - b_1 x_i is the prediction error for observation ii. Ordinary least squares picks the line that makes the sum of squared residuals as small as possible:

我們永遠觀測不到 β0\beta_0 和 β1\beta_1,只能觀測到 nn 對數據 (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n),並用它們來估計。對任何一條候選直線 b0+b1xb_0 + b_1 x,殘差 u^i=yi−b0−b1xi\uh_i = y_i - b_0 - b_1 x_i 就是第 ii 個觀測值的預測誤差。普通最小二乘法選擇令殘差平方和最小的那條直線:

SSR(b0,b1)=∑i=1nu^i 2=∑i=1n(yi−b0−b1xi)2.\begin{aligned} \text{SSR}(b_0, b_1) &= \sumi \uh_i^{\,2} \\ &= \sumi \left(y_i - b_0 - b_1 x_i\right)^2. \end{aligned}

Squaring stops positive and negative errors from cancelling, penalises large misses more than small ones, and, as the next steps show, gives an answer in closed form.

取平方可以避免正負誤差互相抵銷,對較大的誤差給予更重的懲罰,而且正如下面所示,可以得出一條明確的公式。

β^0\bh_0
–
β^1\bh_1
–
SSR\text{SSR}
–
R2R^2
–

Drag any point. The line is always the one that makes the total area of the shaded squares as small as possible.

拖動任何一點。圖中的直線永遠是令所有陰影正方形總面積最小的那一條。

At the minimum, both partial derivatives are zero. These are the first-order conditions, evaluated at the OLS estimates β^0\bh_0 and β^1\bh_1:

在最小值處,兩個偏導數都等於零。這就是一階條件,在 OLS 估計值 β^0\bh_0 和 β^1\bh_1 處成立:

∂ SSR∂b0=−2∑i=1n(yi−β^0−β^1xi)=0,∂ SSR∂b1=−2∑i=1nxi(yi−β^0−β^1xi)=0.\begin{aligned} \frac{\partial\, \text{SSR}}{\partial b_0} &= -2 \sumi \left(y_i - \bh_0 - \bh_1 x_i\right) = 0, \\ \frac{\partial\, \text{SSR}}{\partial b_1} &= -2 \sumi x_i \left(y_i - \bh_0 - \bh_1 x_i\right) = 0. \end{aligned}

Two equations, two unknowns. Solving them gives the OLS estimators:

兩條方程、兩個未知數,解出來便得到 OLS 估計量:

β^1=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2,β^0=yˉ−β^1xˉ.\begin{gathered} \bh_1 = \frac{\sumi (x_i - \xb)(y_i - \yb)}{\sumi (x_i - \xb)^2}, \\[6pt] \bh_0 = \yb - \bh_1 \xb. \end{gathered}
Show every step 顯示完整推導

Intercept. Divide the first condition by −2-2 and split the sum. Since ∑i=1nyi=nyˉ\sumi y_i = n\yb and ∑i=1nxi=nxˉ\sumi x_i = n\xb:

截距。把第一個條件除以 −2-2,再把總和拆開。由於 ∑i=1nyi=nyˉ\sumi y_i = n\yb、∑i=1nxi=nxˉ\sumi x_i = n\xb:

nyˉ−nβ^0−β^1nxˉ=0⟹β^0=yˉ−β^1xˉ.\begin{gathered} n\yb - n\bh_0 - \bh_1 n\xb = 0 \\ \Longrightarrow\quad \bh_0 = \yb - \bh_1 \xb. \end{gathered}

Slope. Substitute this into the second condition. Then yi−β^0−β^1xi=(yi−yˉ)−β^1(xi−xˉ)y_i - \bh_0 - \bh_1 x_i = (y_i - \yb) - \bh_1 (x_i - \xb), so

斜率。把上式代入第二個條件,得到 yi−β^0−β^1xi=(yi−yˉ)−β^1(xi−xˉ)y_i - \bh_0 - \bh_1 x_i = (y_i - \yb) - \bh_1 (x_i - \xb),所以

∑i=1nxi(yi−yˉ)=β^1∑i=1nxi(xi−xˉ).\sumi x_i (y_i - \yb) = \bh_1 \sumi x_i (x_i - \xb).

Centring. Deviations from a mean add up to zero: ∑i=1n(yi−yˉ)=0\sumi (y_i - \yb) = 0. So subtracting xˉ\xb from xix_i on either side changes nothing:

中心化。離均差的總和必定為零:∑i=1n(yi−yˉ)=0\sumi (y_i - \yb) = 0。因此在兩邊把 xix_i 換成 xi−xˉx_i - \xb 不會改變結果:

∑i=1nxi(yi−yˉ)=∑i=1n(xi−xˉ)(yi−yˉ),∑i=1nxi(xi−xˉ)=∑i=1n(xi−xˉ)2.\begin{aligned} \sumi x_i (y_i - \yb) &= \sumi (x_i - \xb)(y_i - \yb), \\ \sumi x_i (x_i - \xb) &= \sumi (x_i - \xb)^2. \end{aligned}

Dividing gives the formula for β^1\bh_1. It exists only if ∑i=1n(xi−xˉ)2>0\sumi (x_i - \xb)^2 > 0, that is, if the xix_i are not all equal. SSR is a sum of squares and is convex in (b0,b1)(b_0, b_1), so this stationary point is the minimum.

兩邊相除便得到 β^1\bh_1 的公式。公式成立的前提是 ∑i=1n(xi−xˉ)2>0\sumi (x_i - \xb)^2 > 0,即 xix_i 不能全部相等。SSR 是平方和,對 (b0,b1)(b_0, b_1) 是凸函數,所以這個駐點就是最小值。

Divide the top and bottom of β^1\bh_1 by n−1n - 1 and a familiar pair appears: the sample covariance of xx and yy over the sample variance of xx.

把 β^1\bh_1 的分子和分母同時除以 n−1n - 1,就會出現兩個熟悉的量:xx 與 yy 的樣本協方差,除以 xx 的樣本方差。

β^1=Cov⁡^(x,y)Var⁡^(x)=σ^xyσ^x2\bh_1 = \frac{\widehat{\operatorname{Cov}}(x, y)}{\widehat{\operatorname{Var}}(x)} = \frac{\hat\sigma_{xy}}{\hat\sigma_x^{2}}

Fitted values, residuals and interpretation擬合值、殘差與係數解讀

Once the estimates are in hand, every observation gets a fitted value and a residual:

有了估計值之後,每個觀測值都有一個擬合值和一個殘差:

y^i=β^0+β^1xi,u^i=yi−y^i.\yh_i = \bh_0 + \bh_1 x_i, \qquad \uh_i = y_i - \yh_i.
  • β^1\bh_1 is the predicted change in yy when xx rises by one unit.

    β^1\bh_1 是 xx 增加一個單位時 yy 的預測變化。

  • β^0\bh_0 is the predicted value of yy when x=0x = 0. Treat it with care when x=0x = 0 lies far outside the data.

    β^0\bh_0 是 x=0x = 0 時 yy 的預測值。如果 x=0x = 0 遠在數據範圍之外,解讀時要特別小心。

Three properties hold in every sample estimated by OLS with an intercept. They are not assumptions; they follow directly from the first-order conditions.

凡是包含截距的 OLS 估計,以下三個性質在任何樣本中都成立。它們不是假設,而是一階條件的直接結果。

∑i=1nu^i=0,∑i=1nxiu^i=0,y^‾=yˉ.\sumi \uh_i = 0, \qquad \sumi x_i \uh_i = 0, \qquad \overline{\yh} = \yb.

The first is the first-order condition for b0b_0; the second is the one for b1b_1. The third follows from the first, since yi=y^i+u^iy_i = \yh_i + \uh_i. And because yˉ=β^0+β^1xˉ\yb = \bh_0 + \bh_1 \xb, the fitted line always passes through the point of means (xˉ,yˉ)(\xb, \yb).

第一個性質就是 b0b_0 的一階條件,第二個是 b1b_1 的一階條件。第三個由第一個推出,因為 yi=y^i+u^iy_i = \yh_i + \uh_i。又因為 yˉ=β^0+β^1xˉ\yb = \bh_0 + \bh_1 \xb,擬合直線必定經過平均值點 (xˉ,yˉ)(\xb, \yb)。

Goodness of fit: R-squared擬合優度:判定係數

How much of the variation in yy does the line account for? Split the total variation into an explained part and a residual part:

這條直線解釋了 yy 多少的變化?我們可以把總變異拆成已解釋部分和殘差部分:

∑i=1n(yi−yˉ)2⏟SST=∑i=1n(y^i−yˉ)2⏟SSE+∑i=1nu^i 2⏟SSR\underbrace{\sumi (y_i - \yb)^2}_{\text{SST}} = \underbrace{\sumi (\yh_i - \yb)^2}_{\text{SSE}} + \underbrace{\sumi \uh_i^{\,2}}_{\text{SSR}}
Show every step 顯示完整推導

Write each deviation as yi−yˉ=(y^i−yˉ)+u^iy_i - \yb = (\yh_i - \yb) + \uh_i, square it and add up:

把每個離差寫成 yi−yˉ=(y^i−yˉ)+u^iy_i - \yb = (\yh_i - \yb) + \uh_i,平方後加總:

SST=SSE+SSR+2∑i=1n(y^i−yˉ) u^i.\text{SST} = \text{SSE} + \text{SSR} + 2 \sumi (\yh_i - \yb)\, \uh_i.

The cross term vanishes. Substituting y^i=β^0+β^1xi\yh_i = \bh_0 + \bh_1 x_i and using the first two properties from Section 3:

交叉項等於零。代入 y^i=β^0+β^1xi\yh_i = \bh_0 + \bh_1 x_i,並利用第 3 節的前兩個性質:

∑i=1n(y^i−yˉ) u^i=(β^0−yˉ)∑i=1nu^i+β^1∑i=1nxiu^i=0.\begin{aligned} &\sumi (\yh_i - \yb)\, \uh_i \\ &\quad = (\bh_0 - \yb) \sumi \uh_i + \bh_1 \sumi x_i \uh_i = 0. \end{aligned}

The coefficient of determination is the explained share:

判定係數就是已解釋部分所佔的比例:

R2=SSESST=1−SSRSST.R^2 = \frac{\text{SSE}}{\text{SST}} = 1 - \frac{\text{SSR}}{\text{SST}}.

With an intercept in the regression, 0≤R2≤10 \le R^2 \le 1. The closer it is to 1, the larger the share of the sample variation in yy explained by xx.

只要回歸包含截距,便有 0≤R2≤10 \le R^2 \le 1。數值越接近 1,xx 所解釋的 yy 樣本變異比例就越大。

Example: education and earnings例子:教育與收入

Does schooling raise earnings, and by how much? The wage2 dataset records monthly earnings, years of education and an IQ score for 935 men in the United States in 1980. Start with the simple regression

教育能否提高收入?提高多少?wage2 數據集記錄了 1980 年美國 935 名男性的月收入、受教育年數和智商分數。先做簡單回歸:

wagei=β0+β1 educi+ui.\text{wage}_i = \beta_0 + \beta_1\, \text{educ}_i + u_i.
OLS estimates, dependent variable: monthly earnings (US$) 普通最小二乘估計,被解釋變數:月收入(美元)
(1) Simple(2) With IQ
Intercept 截距 146.95(77.72)−128.89(92.18)
educ 教育年數 60.21(5.70)42.06(6.55)
IQ 智商 —5.14(0.96)
R2R^20.1070.134
Observations 樣本數935935

Standard errors in parentheses. They measure how much an estimate would vary from sample to sample, and are used for hypothesis tests in later weeks.

括號內為標準誤,反映估計值在不同樣本之間會有多大變化,會在之後幾週的假設檢定中使用。

In column (1), each extra year of education is associated with about $60 more in monthly earnings, on average. The intercept, $147, is the prediction for someone with no schooling at all. Nobody in the sample has fewer than 9 years, so it has no useful meaning here. R2=0.107R^2 = 0.107: education accounts for about a tenth of the variation in earnings.

第 (1) 欄顯示,平均而言每多一年教育,月收入約高 60 美元。截距 147 美元是完全沒有受過教育的人的預測收入,但樣本中沒有人少於 9 年教育,所以這裏的截距沒有實際意義。R2=0.107R^2 = 0.107,即教育解釋了收入變異的大約十分之一。

But education and earnings could both be driven by ability. Adding IQ as a proxy for ability gives column (2):

然而,教育和收入可能同樣受能力影響。加入智商作為能力的代理變數,得到第 (2) 欄:

wage^=−128.89+42.06 educ+5.14 IQ.\widehat{\text{wage}} = -128.89 + 42.06\, \text{educ} + 5.14\, \text{IQ}.

Holding IQ fixed, an extra year of education is now associated with $42, not $60. People with more schooling tend to have higher IQ scores (the correlation in this sample is 0.52), so in column (1) the education coefficient was also picking up part of what IQ explains.

在智商不變的情況下,多一年教育現在只對應 42 美元,而不是 60 美元。受教育較多的人往往智商分數也較高(此樣本中兩者的相關係數為 0.52),所以第 (1) 欄的教育係數其實也包含了部分本應由智商解釋的效果。

Column (2) is more persuasive as an estimate of the causal effect of education, provided IQ is a good proxy for ability. It is still not proof: other unobserved factors may remain in the error term.

如果智商是能力的良好代理,第 (2) 欄作為教育因果效應的估計會更有說服力。但這仍然不是證明:誤差項中可能還有其他未被觀測的因素。

Multiple regression多元回歸

The multiple linear regression model lets the conditional mean of yy depend on kk explanatory variables:

多元線性回歸模型讓 yy 的條件平均值取決於 kk 個解釋變數:

yi=β0+β1xi1+⋯+βkxik+ui.y_i = \beta_0 + \beta_1 x_{i1} + \dots + \beta_k x_{ik} + u_i.

Each coefficient now has a “holding the others fixed” reading. For β1\beta_1:

每個係數現在都要以「其他變數保持不變」來解讀。以 β1\beta_1 為例:

E(yi∣xi1+1,xi2,…,xik)−E(yi∣xi1,xi2,…,xik)=β1.\begin{aligned} &E(y_i \mid x_{i1} + 1, x_{i2}, \dots, x_{ik}) \\ &\quad - E(y_i \mid x_{i1}, x_{i2}, \dots, x_{ik}) = \beta_1. \end{aligned}

β1\beta_1 is the average change in yy for a one-unit change in x1x_1, with x2,…,xkx_2, \dots, x_k held at the same values. Regression holds those variables fixed statistically, without needing an experiment that holds them fixed in practice.

β1\beta_1 是在 x2,…,xkx_2, \dots, x_k 保持不變時,x1x_1 每改變一個單位 yy 的平均變化。回歸是在統計上把這些變數固定,而不需要真的做一個把它們固定下來的實驗。

OLS in matrix form矩陣形式的普通最小二乘法

Writing out the model for all nn observations gives nn equations. Stack them:

把模型對全部 nn 個觀測值寫出來,便得到 nn 條方程。把它們疊起來:

[y1y2⋮yn]=[1x11⋯x1k1x21⋯x2k⋮⋮⋱⋮1xn1⋯xnk][β0β1⋮βk]+[u1u2⋮un]\begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{bmatrix} = \begin{bmatrix} 1 & x_{11} & \cdots & x_{1k} \\ 1 & x_{21} & \cdots & x_{2k} \\ \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & \cdots & x_{nk} \end{bmatrix} \begin{bmatrix} \beta_0 \\ \beta_1 \\ \vdots \\ \beta_k \end{bmatrix} + \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{bmatrix}

The whole system becomes one line:

整個方程組變成一行:

yn×1=Xn×(k+1)  β(k+1)×1+un×1.\underset{n \times 1}{\mathbf{y}} = \underset{n \times (k+1)}{\mathbf{X}}\; \underset{(k+1) \times 1}{\boldsymbol{\beta}} + \underset{n \times 1}{\mathbf{u}}.

Each row of X\mathbf{X} is one observation; the first column of ones carries the intercept. For a candidate vector b\mathbf{b}, the residuals are y−Xb\mathbf{y} - \mathbf{X}\mathbf{b}, and the sum of squared residuals is their inner product with themselves:

X\mathbf{X} 的每一行是一個觀測值,第一列全是 1,對應截距。對任何候選向量 b\mathbf{b},殘差是 y−Xb\mathbf{y} - \mathbf{X}\mathbf{b},殘差平方和就是這個向量與自身的內積:

SSR(b)=(y−Xb)′(y−Xb)=y′y−2b′X′y+b′X′Xb.\begin{aligned} \text{SSR}(\mathbf{b}) &= (\mathbf{y} - \mathbf{X}\mathbf{b})\T (\mathbf{y} - \mathbf{X}\mathbf{b}) \\ &= \mathbf{y}\T\mathbf{y} - 2\mathbf{b}\T\mathbf{X}\T\mathbf{y} + \mathbf{b}\T\mathbf{X}\T\mathbf{X}\mathbf{b}. \end{aligned}

Setting the derivative to zero and solving gives the most famous formula in econometrics:

令導數等於零並求解,便得到計量經濟學中最著名的公式:

  β^=(X′X)−1X′y  \boxed{\;\bhv = (\mathbf{X}\T\mathbf{X})^{-1}\mathbf{X}\T\mathbf{y}\;}
Show every step 顯示完整推導

Expanding. Multiply out: y′y−b′X′y−y′Xb+b′X′Xb\mathbf{y}\T\mathbf{y} - \mathbf{b}\T\mathbf{X}\T\mathbf{y} - \mathbf{y}\T\mathbf{X}\mathbf{b} + \mathbf{b}\T\mathbf{X}\T\mathbf{X}\mathbf{b}. The two middle terms are the same number: y′Xb\mathbf{y}\T\mathbf{X}\mathbf{b} is 1×11 \times 1, and a scalar equals its own transpose, (y′Xb)′=b′X′y(\mathbf{y}\T\mathbf{X}\mathbf{b})\T = \mathbf{b}\T\mathbf{X}\T\mathbf{y}. That gives the −2b′X′y-2\mathbf{b}\T\mathbf{X}\T\mathbf{y} above.

展開。乘出來得到 y′y−b′X′y−y′Xb+b′X′Xb\mathbf{y}\T\mathbf{y} - \mathbf{b}\T\mathbf{X}\T\mathbf{y} - \mathbf{y}\T\mathbf{X}\mathbf{b} + \mathbf{b}\T\mathbf{X}\T\mathbf{X}\mathbf{b}。中間兩項其實是同一個數:y′Xb\mathbf{y}\T\mathbf{X}\mathbf{b} 是 1×11 \times 1 的純量,而純量等於自身的轉置,(y′Xb)′=b′X′y(\mathbf{y}\T\mathbf{X}\mathbf{b})\T = \mathbf{b}\T\mathbf{X}\T\mathbf{y},所以合併成上面的 −2b′X′y-2\mathbf{b}\T\mathbf{X}\T\mathbf{y}。

Differentiating. Two rules from Week 1: ∂(b′a)/∂b=a\partial(\mathbf{b}\T\mathbf{a})/\partial\mathbf{b} = \mathbf{a}, and ∂(b′Ab)/∂b=2Ab\partial(\mathbf{b}\T\mathbf{A}\mathbf{b})/\partial\mathbf{b} = 2\mathbf{A}\mathbf{b} when A\mathbf{A} is symmetric. X′X\mathbf{X}\T\mathbf{X} is symmetric, so the first-order condition is

求導。用第 1 週的兩條規則:∂(b′a)/∂b=a\partial(\mathbf{b}\T\mathbf{a})/\partial\mathbf{b} = \mathbf{a};當 A\mathbf{A} 對稱時,∂(b′Ab)/∂b=2Ab\partial(\mathbf{b}\T\mathbf{A}\mathbf{b})/\partial\mathbf{b} = 2\mathbf{A}\mathbf{b}。X′X\mathbf{X}\T\mathbf{X} 是對稱矩陣,所以一階條件是

∂ SSR∂b∣β^=−2X′y+2X′Xβ^=−2X′(y−Xβ^)=0.\begin{aligned} \frac{\partial\, \text{SSR}}{\partial \mathbf{b}}\bigg|_{\bhv} &= -2\mathbf{X}\T\mathbf{y} + 2\mathbf{X}\T\mathbf{X}\bhv \\ &= -2\mathbf{X}\T(\mathbf{y} - \mathbf{X}\bhv) = \mathbf{0}. \end{aligned}

Solving. Rearranging gives the normal equations (X′X)β^=X′y(\mathbf{X}\T\mathbf{X})\bhv = \mathbf{X}\T\mathbf{y}. Multiply both sides on the left by (X′X)−1(\mathbf{X}\T\mathbf{X})^{-1}, and the identity matrix I\mathbf{I} leaves the boxed formula:

求解。整理後得到正規方程 (X′X)β^=X′y(\mathbf{X}\T\mathbf{X})\bhv = \mathbf{X}\T\mathbf{y}。兩邊左乘 (X′X)−1(\mathbf{X}\T\mathbf{X})^{-1},左邊只剩單位矩陣 I\mathbf{I},便得到上面方框中的公式:

(X′X)−1(X′X)β^=(X′X)−1X′yIβ^=(X′X)−1X′y.\begin{aligned} (\mathbf{X}\T\mathbf{X})^{-1}(\mathbf{X}\T\mathbf{X})\bhv &= (\mathbf{X}\T\mathbf{X})^{-1}\mathbf{X}\T\mathbf{y} \\ \mathbf{I}\bhv &= (\mathbf{X}\T\mathbf{X})^{-1}\mathbf{X}\T\mathbf{y}. \end{aligned}

Checking with one regressor. When k=1k = 1, the normal equations are

用單一解釋變數驗證。當 k=1k = 1,正規方程是

[n∑xi∑xi∑xi2][β^0β^1]=[∑yi∑xiyi],\begin{bmatrix} n & \sum x_i \\ \sum x_i & \sum x_i^2 \end{bmatrix} \begin{bmatrix} \bh_0 \\ \bh_1 \end{bmatrix} = \begin{bmatrix} \sum y_i \\ \sum x_i y_i \end{bmatrix},

which are the two first-order conditions of Section 2, rearranged. The matrix formula is the same method, for any number of regressors.

這正是第 2 節的兩個一階條件,只是重新排列。矩陣公式是同一個方法,只不過適用於任意數量的解釋變數。

The normal equations can also be written X′u^=0\mathbf{X}\T\hat{\mathbf{u}} = \mathbf{0}: the residual vector is orthogonal to every column of X\mathbf{X}. This is the matrix version of ∑u^i=0\sum \uh_i = 0 and ∑xiu^i=0\sum x_i \uh_i = 0, and it means the residuals are uncorrelated in the sample with every regressor.

正規方程也可以寫成 X′u^=0\mathbf{X}\T\hat{\mathbf{u}} = \mathbf{0}:殘差向量與 X\mathbf{X} 的每一列都正交。這就是 ∑u^i=0\sum \uh_i = 0 和 ∑xiu^i=0\sum x_i \uh_i = 0 的矩陣版本,表示殘差在樣本中與每個解釋變數都不相關。

The ability to write very different questions as y=Xβ+u\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \mathbf{u} is the point. Returns to education, what drives a share price, the value of a house, tomorrow’s electricity demand: each becomes the same problem of estimating β\boldsymbol{\beta} well from a sample.

能把截然不同的問題都寫成 y=Xβ+u\mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \mathbf{u},正是這套方法的力量。教育回報、股價的驅動因素、房屋的價值、明天的電力需求,全都變成同一個問題:如何從樣本中準確估計 β\boldsymbol{\beta}。

Reproduce the results自己重現結果

The data come with the free wooldridge package for R and Python. Both snippets fit column (2) and then compute the same estimates directly from the matrix formula.

數據收錄在 R 和 Python 的免費套件 wooldridge 中。以下兩段程式碼都會估計第 (2) 欄,再直接用矩陣公式計算出相同的估計值。

R
# install.packages("wooldridge")   # run once
library(wooldridge)
data("wage2")

m1 <- lm(wage ~ educ, data = wage2)
m2 <- lm(wage ~ educ + IQ, data = wage2)
summary(m1)
summary(m2)

# The matrix formula, by hand
X <- cbind(1, wage2$educ, wage2$IQ)
y <- wage2$wage
solve(t(X) %*% X, t(X) %*% y)   # -128.89, 42.06, 5.14
Python
# pip install wooldridge numpy
import numpy as np
import wooldridge as wd

df = wd.data("wage2")
X = np.column_stack([np.ones(len(df)), df["educ"], df["IQ"]])
y = df["wage"].to_numpy()

# Solve (X'X) b = X'y rather than inverting X'X
beta_hat = np.linalg.solve(X.T @ X, X.T @ y)
print(beta_hat.round(2))   # [-128.89   42.06    5.14]

Check your understanding自我測驗

  1. Every person in a sample has exactly 12 years of education. Can OLS estimate the return to education?

    樣本中每個人都恰好受過 12 年教育。OLS 能估計教育回報嗎?

    Answer 答案

    No. ∑(xi−xˉ)2=0\sum (x_i - \xb)^2 = 0, so the formula for β^1\bh_1 divides by zero. In matrix terms, the education column is 12 times the column of ones, so the columns of X\mathbf{X} are not linearly independent and X′X\mathbf{X}\T\mathbf{X} cannot be inverted.

    不能。∑(xi−xˉ)2=0\sum (x_i - \xb)^2 = 0,β^1\bh_1 的公式分母為零。以矩陣來說,教育一列等於常數列的 12 倍,X\mathbf{X} 的各列並非線性獨立,X′X\mathbf{X}\T\mathbf{X} 不可逆。

  2. Using column (1), what monthly earnings are predicted for someone with 16 years of education?

    根據第 (1) 欄,受過 16 年教育的人預測月收入是多少?

    Answer 答案

    146.95+60.21×16≈1,110146.95 + 60.21 \times 16 \approx 1{,}110, so about $1,110 a month.

    146.95+60.21×16≈1,110146.95 + 60.21 \times 16 \approx 1{,}110,即每月約 1,110 美元。

  3. You run OLS with an intercept and find that the residuals add up to 5. What has happened?

    你用含截距的 OLS 做回歸,發現殘差總和是 5。發生了甚麼事?

    Answer 答案

    Something is wrong. With an intercept, ∑u^i=0\sum \uh_i = 0 in every sample, because it is the first-order condition for b0b_0. Check the calculation, or whether the intercept was left out.

    一定是哪裏出錯了。只要有截距,∑u^i=0\sum \uh_i = 0 在任何樣本中都成立,因為這正是 b0b_0 的一階條件。請檢查計算,或者確認是否漏了截距。

  4. Adding IQ raised R2R^2 from 0.107 to 0.134. Does that show the second model is the right one?

    加入智商後,R2R^2 由 0.107 升至 0.134。這是否證明第二個模型才是正確的?

    Answer 答案

    No. Adding a regressor can never lower R2R^2: OLS could always set its coefficient to zero and keep the old fit, so SSR cannot rise. A higher R2R^2 on its own says little about whether a model is right.

    不是。加入解釋變數永遠不會令 R2R^2 下降:OLS 大可把新係數設為零而保留原來的擬合,所以 SSR 不會上升。單憑 R2R^2 上升,並不能說明模型是否正確。

  5. Why does the education coefficient fall from 60.21 to 42.06 when IQ is added?

    為甚麼加入智商後,教育係數由 60.21 降至 42.06?

    Answer 答案

    IQ raises earnings and is positively related to education. Leaving it out lets education take credit for part of IQ’s effect: 42.06+5.14×3.53=60.2142.06 + 5.14 \times 3.53 = 60.21.

    智商會提高收入,而且與教育正相關。不把它放進模型,教育便會「搶走」部分屬於智商的效果:42.06+5.14×3.53=60.2142.06 + 5.14 \times 3.53 = 60.21。

Sources and acknowledgement 資料來源與鳴謝

  • Lecture materials for Introductory Econometrics (ETC2410) by Associate Professor Didier Nibbering, Department of Econometrics and Business Statistics, Monash Business School.
  • Jeffrey M. Wooldridge, Introductory Econometrics: A Modern Approach, Chapters 2 and 3, and Appendix E.
  • Data: wage2, distributed with the textbook and in the wooldridge R package. All estimates on this page were computed from it.

Any errors are our own.