Econometrics · Week 6
Functional form and model selection函數形式與模型選擇
A straight line says every extra year of schooling adds the same number of dollars, and every extra year of experience adds as much at 40 as at 20. Neither is how pay works. This week bends the regression line with logs and squares, and then asks how to choose among the models that result.
一條直線意味著每多讀一年書都會增加同樣多的收入,40 歲時多一年經驗的價值也和 20 歲時一樣。現實中的薪酬並不是這樣。本週用對數和平方項讓回歸線彎曲,然後討論怎樣在由此得到的多個模型之間作出選擇。
- Builds on 先修內容
- Week 5
- Reading time 閱讀時間
- About 45 minutes
- Data 數據
wage1,wage2,ceosal2,bwght- Code 程式碼
- R, Python
What “linear” means「線性」的真正含義
The model of the past weeks is linear in three senses: in the dependent variable, in the regressors and in the coefficients.
過去幾週的模型在三個意義上是線性的:被解釋變數、解釋變數和係數都是線性的。
Only the last one matters for OLS. The and the ’s can be any functions of the observed data, such as or , as long as each coefficient enters as a plain multiplier. The model is still estimated by OLS, and every test from Weeks 4 and 5 still applies.
對 OLS 來說,只有最後一點才重要。 和各個 可以是觀測數據的任何函數,例如 或 ,只要每個係數都只是單純的乘數即可。模型仍然用 OLS 估計,第 4、5 週的所有檢定也依然適用。
Logs and percentage changes對數與百分比變化
Logs turn proportional changes into differences. If rises to ,
對數把按比例的變化轉成差。如果 上升到 ,
and the approximation is close when is between about −0.1 and 0.1. So is approximately the percentage change in . When an effect on is large, use the exact form from Week 5, .
當 大約在 −0.1 至 0.1 之間時,近似式 相當準確。所以 大約就是 的百分比變化。如果對 的效應較大,便應使用第 5 週的準確公式 。
Logging the dependent variable, a regressor, or both, gives four combinations:
對被解釋變數、解釋變數或兩者取對數,可以得到四種組合:
| Model | Equation | Reading of β₁ β₁ 的解讀 |
|---|---|---|
| Level-level | x up 1 unit → y up β₁ units x 增加 1 單位,y 增加 β₁ 單位 | |
| Log-level | x up 1 unit → y up about 100β₁ % x 增加 1 單位,y 約增加 100β₁ % | |
| Level-log | x up 1% → y up about β₁/100 units x 增加 1%,y 約增加 β₁/100 單位 | |
| Log-log | x up 1% → y up about β₁ % (elasticity) x 增加 1%,y 約增加 β₁ %(彈性) |
Log-level: a constant percentage effect對數-水平模型:固定百分比效應
Week 4 estimated . It says an extra year of schooling adds $42.06 a month whether you earn $500 or $3,000. It is more natural to think that schooling raises pay by a roughly constant percentage. Put the log of wages on the left:
第 4 週估計了 。這表示無論月收入是 500 還是 3,000 美元,多讀一年書都同樣增加 42.06 美元。更合理的想法是,教育令薪酬上升一個大致固定的百分比。把工資取對數放在左邊:
Holding experience and IQ fixed, a year of education raises predicted earnings by about 5.7%, or exactly . In dollars the gain now grows with the wage: 5.9% of $500 is $30, of $3,000 it is $176.
在經驗和智商不變時,多一年教育令預測收入上升約 5.7%,準確值為 。以金額計,增幅會隨工資上升:500 美元的 5.9% 是 30 美元,3,000 美元的 5.9% 則是 176 美元。
The log-level form suits a dependent variable that is always positive, when proportional changes are what matter: wages, prices, sales, house values.
當被解釋變數永遠為正、而且我們關心的是比例變化時,對數-水平模型最合適,例如工資、價格、銷售額、樓價。
Level-log: diminishing returns水平-對數模型:遞減效應
Now log a regressor but not . Does family income matter for a baby’s birth weight? In the bwght data on 1,387 births in the United States, with birth weight in ounces, family income in thousands of dollars, the mother’s years of education, and cigarettes smoked per day during pregnancy:
現在對解釋變數取對數,但 不取。家庭收入會影響嬰兒的出生體重嗎?bwght 數據記錄了美國 1,387 宗出生個案,出生體重以安士計,家庭收入以千美元計,另有母親的受教育年數和懷孕期間每日吸煙支數:
Here , and , so a 1% rise in changes by about units:
這裏 ,而 ,所以 上升 1% 時, 大約改變 個單位:
A 1% rise in family income goes with 0.018 ounces more birth weight; a 10% rise with about 0.18 ounces, holding education and smoking fixed. The coefficient is significant (), but the effect is small. Each extra cigarette a day goes with 0.47 ounces less.
在教育和吸煙不變時,家庭收入上升 1%,出生體重多 0.018 安士;上升 10%,約多 0.18 安士。係數在統計上顯著(),但效應很小。相比之下,每天多吸一支煙,出生體重便少 0.47 安士。
The level-log form builds in diminishing returns: going from $10,000 to $20,000 has the same effect as going from $30,000 to $60,000, because both double income.
水平-對數模型內含遞減效應:收入由 1 萬美元升到 2 萬美元,與由 3 萬升到 6 萬的效果相同,因為兩者都是收入翻倍。
Log-log: elasticities雙對數模型:彈性
Log both sides and the coefficient becomes an elasticity: the percentage change in for a 1% change in .
兩邊都取對數,係數便成為彈性: 改變 1% 時 改變的百分比。
For 177 chief executives in ceosal2, regress log salary on log sales, log market value and years as CEO (ceoten):
以 ceosal2 中 177 名行政總裁為例,把薪酬對數對銷售額對數、市值對數和擔任行政總裁的年數(ceoten)做回歸:
Holding market value and tenure fixed, 1% higher sales predicts 0.16% higher salary.
在市值和任期不變時,銷售額高 1%,預測薪酬高 0.16%。
Holding sales and tenure fixed, 1% higher market value predicts 0.11% higher salary.
在銷售額和任期不變時,市值高 1%,預測薪酬高 0.11%。
Tenure is in levels, so its coefficient has the log-level reading: each extra year as CEO predicts about 1.2% higher salary.
任期是水平值,所以它的係數按對數-水平模型解讀:每多擔任一年行政總裁,預測薪酬高約 1.2%。
Both elasticities are far below 1. A firm twice as large does not pay its chief executive twice as much; it pays about 12% more.
兩個彈性都遠低於 1。規模大一倍的公司並不會付雙倍薪酬給行政總裁,只會多付大約 12%。
Why the coefficient is an elasticity 為甚麼係數就是彈性
With , differentiate both sides with respect to , using :
對 兩邊關於 求導,並利用 :
The right-hand side is the ratio of two proportional changes, which is the definition of an elasticity. Unlike the level-log and log-level readings, it does not depend on where along the curve you are.
右邊是兩個比例變化之比,正是彈性的定義。和水平-對數、對數-水平模型的解讀不同,它不取決於位於曲線上的哪一點。
For a doubling of sales the exact effect is , the figure used above.
銷售額翻倍時的準確效應是 ,即上文所用的數字。
Levels or logs?用水平值還是對數?
Only positive variables can be logged. A variable that can be zero or negative, such as profit, cannot.
只有正數變數才能取對數。可能為零或負數的變數,例如利潤,就不能取對數。
Think about the mechanism. Does a unit change in x plausibly add a constant amount to y, or a constant percentage?
思考背後的機制。x 改變一個單位,y 增加的應該是固定數量,還是固定百分比?
Look at the data. With one regressor, a scatter plot shows whether the relationship curves.
觀察數據。只有一個解釋變數時,散點圖能顯示關係是否彎曲。
Years are not logged. Education, experience and age enter in levels.
年數不取對數。教育、經驗和年齡都以水平值放入模型。
Percentages are not logged. A one-unit change in an interest rate or tax rate is already a one-point change.
百分比不取對數。利率或稅率改變一個單位,本身已經是一個百分點的變化。
Logs tame skew. Income, wealth, sales and market value are heavily right-skewed; their logs are much closer to symmetric, so a few very large values carry less weight.
對數能減少偏態。收入、財富、銷售額和市值都明顯右偏;取對數後分佈接近對稱得多,少數極大值的影響也會減少。
Quadratics: effects that change sign二次項:會改變方向的效應
Logs bend a line but keep it moving in one direction. Some effects rise and then fall. In wage1, a straight line in experience explains almost nothing (). Add the square of experience:
對數能令直線彎曲,但方向保持不變。有些效應卻是先升後降。在 wage1 中,經驗的直線模型幾乎解釋不了甚麼()。加入經驗的平方項:
rises to 0.093. The marginal effect of in is the derivative
升至 0.093。在 中, 的邊際效應就是導數
so it changes with . Neither nor means much on its own. The first year of experience adds about 30 cents an hour; by the tenth year an extra year adds .
所以它隨 而改變。 和 單獨看都沒有太大意義。第一年經驗令時薪增加約 30 美仙;到第十年,多一年經驗只增加 美元。
Quadratic fit 二次擬合Straight line 直線擬合Slope at the chosen point 該點的斜率
- Predicted wage 預測時薪
- –
- One more year adds 多一年經驗
- –
Move the slider. The slope of the short line is the marginal effect, β̂₁ + 2β̂₂x: large early in a career, zero at 24.3 years, negative after.
拖動滑桿。短線的斜率就是邊際效應 β̂₁ + 2β̂₂x:職業初期很大,24.3 年時為零,之後轉為負數。
The marginal effect is zero at the turning point:
邊際效應在轉折點等於零:
With the curve is a hill and is a maximum; with it is a valley and is a minimum. Before reading too much into the fall after 24 years, check how many observations lie there. Here 28% of the sample has more than 24 years of experience, so the downturn is not driven by a handful of points, but other factors that change with age may play a part.
當 時,曲線呈山形, 是最大值;當 時呈谷形, 是最小值。在解讀 24 年之後的下降之前,應先看看那個範圍有多少觀測值。這裏有 28% 的樣本經驗超過 24 年,所以下降並非由少數幾點造成,但其他隨年齡變化的因素也可能有影響。
Is the curve needed at all? Test : the coefficient on has a standard error of 0.0009 and , so the quadratic term is clearly significant.
是否真的需要曲線?檢定 : 係數的標準誤是 0.0009,,所以二次項明顯顯著。
When to try a quadratic: when theory suggests the effect of x grows or shrinks with x, when there is a natural peak (wages and age, house prices and distance to the station), when a scatter plot curves, or simply add the squared term and check its t statistic and the adjusted R².
甚麼時候應該嘗試二次項:理論上 x 的效應會隨 x 增大或減小;存在自然的峰值(例如工資與年齡、樓價與距離車站的遠近);散點圖呈彎曲;或者直接加入平方項,看看它的 t 統計量和調整後 R²。
Choosing between models模型選擇
A good model explains the dependent variable well, with as few coefficients as possible. More regressors always fit the sample better, but each one adds estimation noise and makes forecasts less reliable. Model selection criteria trade fit against size.
好的模型要能好好解釋被解釋變數,同時係數越少越好。解釋變數越多,樣本擬合一定越好,但每多一個都會增加估計誤差,令預測更不可靠。模型選擇準則就是在擬合程度與模型大小之間取捨。
Why not R²? Adding a regressor can never raise SSR (OLS could set its coefficient to zero), so never falls. It always prefers the biggest model. The adjusted R² charges for each coefficient by dividing SSR by its degrees of freedom:
為甚麼不能用 R²?加入解釋變數永遠不會令 SSR 上升(OLS 大可把它的係數設為零),所以 永遠不會下降,總是偏好最大的模型。調整後 R² 把 SSR 除以其自由度,從而為每個係數收取「費用」:
Adding a regressor lowers SSR but also lowers , so can go either way. Choose the model with the largest .
加入解釋變數會令 SSR 下降,但也會令 下降,所以 可升可降。選擇 最大的模型。
Information criteria take the same idea further. Each adds a penalty that grows with the number of regressors to :
資訊準則把同一個想法推進一步。每個準則都在 上加一個隨解釋變數數目增加的懲罰項:
The constant differs between software packages but is the same for every model fitted to the same data, so it never changes a ranking. Choose the model with the smallest value. For , , so the penalties rank BIC > HQ > AIC > : BIC picks the smallest models and the largest.
常數 在不同軟件中各有不同,但對同一組數據擬合的所有模型都一樣,所以不會改變排序。選擇數值最小的模型。當 時,,所以懲罰力度依次為 BIC > HQ > AIC > :BIC 選出的模型最小, 最大。
Example. Explain log earnings in wage2. Education is always included; experience, tenure with the current employer, and age are candidates. That gives eight models:
例子。用 wage2 解釋收入的對數。教育一定放入;經驗、在現職公司的年資和年齡是候選變數。這樣共有八個模型:
| Regressors | AIC | HQ | BIC | ||
|---|---|---|---|---|---|
educ | 0.0974 | 0.0964 | 1.0090 | 1.0130 | 1.0194 |
educ exper | 0.1309 | 0.1290 | 0.9734 | 0.9793 | 0.9889 |
educ tenure | 0.1363 | 0.1345 | 0.9671 | 0.9730 | 0.9826 |
educ age | 0.1249 | 0.1230 | 0.9803 | 0.9862 | 0.9958 |
educ exper tenure | 0.1551 | 0.1524 | 0.9473 | 0.9551 | 0.9680 |
educ exper age | 0.1369 | 0.1341 | 0.9685 | 0.9764 | 0.9893 |
educ tenure age | 0.1499 | 0.1472 | 0.9533 | 0.9612 | 0.9741 |
educ exper tenure age | 0.1580 | 0.1544 | 0.9460 | 0.9559 | 0.9719 |
Bold marks each criterion’s choice: the largest adjusted R², or the smallest information criterion.
粗體表示各準則所選的模型:調整後 R² 最大,或資訊準則最小。
and AIC choose all four regressors; HQ and BIC drop age. The criteria disagree, and that is common. Age adds little once education and experience are in, since the three move together: its t statistic in the largest model is 1.78.
和 AIC 選擇全部四個解釋變數;HQ 和 BIC 則剔除年齡。不同準則意見不一,這很常見。教育和經驗已經放入之後,年齡的作用不大,因為三者會一起變動:在最大的模型中,年齡的 t 統計量是 1.78。
Each criterion is a hurdle for the t statistic 每個準則都是 t 統計量的一道門檻
Compare a model with one extra regressor against the same model without it. From Week 4, the F statistic for dropping that regressor equals , with degrees of freedom in the larger model:
把多一個解釋變數的模型與沒有它的同一模型比較。由第 4 週可知,剔除這個解釋變數的 F 統計量等於 ,大模型的自由度是 :
Adjusted R². rises when . Substituting the ratio above, this holds exactly when .
調整後 R²。當 時, 上升。代入上面的比率,這恰好等價於 。
Information criteria. The larger model wins when the fall in beats the extra penalty. With and , this is roughly
資訊準則。當 的下降超過新增的懲罰時,大模型勝出。利用 及 ,大約等於
With , a variable stays in if exceeds about 1 for , 1.41 for AIC, 1.96 for HQ and 2.62 for BIC. Age, with , clears the first two hurdles but not the last two, which is exactly the pattern in the table.
當 時,變數要留在模型中, 大約要超過: 為 1,AIC 為 1.41,HQ 為 1.96,BIC 為 2.62。年齡的 ,過得了前兩關但過不了後兩關,這正是表中的結果。
Reproduce the results自己重現結果
All datasets come with the free wooldridge package.
所有數據集都收錄在免費的 wooldridge 套件中。
library(wooldridge)
m1 <- lm(log(wage) ~ educ + exper + IQ, data = wage2)
100 * (exp(coef(m1)["educ"]) - 1) # 5.9% per year of education
m2 <- lm(log(salary) ~ log(sales) + log(mktval) + ceoten, data = ceosal2)
coef(m2) # elasticities 0.163 and 0.109
m3 <- lm(wage ~ exper + I(exper^2), data = wage1)
b <- coef(m3)
-b["exper"] / (2 * b["I(exper^2)"]) # turning point: 24.3 years
# Information criteria per observation, on the scale most packages report
ic <- function(m) {
n <- nobs(m); K <- length(coef(m))
s <- log(sum(resid(m)^2) / n) + 1 + log(2 * pi)
c(AIC = s + 2 * K / n, HQ = s + 2 * K * log(log(n)) / n, BIC = s + K * log(n) / n)
}
small <- lm(lwage ~ educ + exper + tenure, data = wage2)
large <- lm(lwage ~ educ + exper + tenure + age, data = wage2)
rbind(small = ic(small), large = ic(large))
c(summary(small)$adj.r.squared, summary(large)$adj.r.squared)
# pip install wooldridge statsmodels
import numpy as np
import statsmodels.formula.api as smf
import wooldridge as wd
wage2 = wd.data("wage2")
m1 = smf.ols("lwage ~ educ + exper + IQ", data=wage2).fit()
print(100 * (np.exp(m1.params["educ"]) - 1)) # 5.9% per year of education
ceo = wd.data("ceosal2")
m2 = smf.ols("np.log(salary) ~ np.log(sales) + np.log(mktval) + ceoten", data=ceo).fit()
print(m2.params) # elasticities 0.163 and 0.109
wage1 = wd.data("wage1")
m3 = smf.ols("wage ~ exper + I(exper**2)", data=wage1).fit()
b = m3.params
print(-b["exper"] / (2 * b["I(exper ** 2)"])) # turning point: 24.3 years
# Information criteria per observation, on the scale most packages report
def ic(m):
n, K = m.nobs, len(m.params)
s = np.log(m.ssr / n) + 1 + np.log(2 * np.pi)
return {"AIC": s + 2 * K / n, "HQ": s + 2 * K * np.log(np.log(n)) / n, "BIC": s + K * np.log(n) / n}
for f in ["lwage ~ educ + exper + tenure", "lwage ~ educ + exper + tenure + age"]:
m = smf.ols(f, data=wage2).fit()
print(f, round(m.rsquared_adj, 4), {k: round(v, 4) for k, v in ic(m).items()})
Check your understanding自我測驗
-
Which of these can be estimated by OLS? (a) ; (b) ; (c) .
下列哪些可以用 OLS 估計?(a) ;(b) ;(c) 。
Answer 答案
(a) and (c): each is linear in the coefficients, with a transformed regressor. (b) is not linear in . OLS would estimate the coefficient on , which is , but not the sign of .
(a) 和 (c):兩者對係數都是線性的,只是解釋變數經過轉換。(b) 對 不是線性的。OLS 可以估計 的係數,即 ,但無法確定 的正負號。
-
In a log-log model of house prices, the coefficient on is 0.8. Interpret it.
在一個樓價的雙對數模型中, 的係數是 0.8。請解讀。
Answer 答案
Holding the other variables fixed, a home with 1% more floor area is predicted to cost about 0.8% more. Price rises less than in proportion to size.
在其他變數不變時,面積大 1% 的物業,預測價格高約 0.8%。價格的升幅低於面積的升幅。
-
With the CEO equation in Section 5, what is the predicted salary difference between two otherwise identical CEOs, one at a firm with sales 50% higher?
用第 5 節的行政總裁方程,兩名其他條件相同的行政總裁,其中一人所在公司的銷售額高 50%,預測薪酬相差多少?
Answer 答案
Exactly . The quick reading, , overstates it because 50% is not a small change.
準確值為 。簡單讀法 會高估,因為 50% 並不是小幅變化。
-
A regression gives . At what x is predicted y largest, and what is the marginal effect at x = 2?
一個回歸得到 。x 等於多少時預測的 y 最大?x = 2 時的邊際效應是多少?
Answer 答案
. At the marginal effect is .
。在 時,邊際效應是 。
-
Adding a variable raises from 0.300 to 0.302 and lowers . What does that tell you about its t statistic?
加入一個變數後, 由 0.300 升至 0.302, 卻下降。這說明它的 t 統計量是怎樣的?
Answer 答案
falls exactly when . The variable is nowhere near significant, and the rise in is what any variable, even pure noise, would produce.
下降恰好等價於 。這個變數遠未達到顯著, 的上升是任何變數(即使是純粹的雜訊)都會帶來的。
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.4 and 6.
- Data: wage1, wage2, bwght, ceosal2 and sleep75, distributed with the textbook and in the wooldridge R package. All estimates on this page were computed from them.
Any errors are our own.