Econometrics · Week 5
Dummy variables虛擬變數
Many things that shape an outcome are categories, not numbers: sex, industry, region, whether a house has a pool. Dummy variables bring them into a regression. This week shows how to include them, how to read their coefficients, and how to test whether categories really differ.
許多影響結果的因素是類別而不是數字:性別、行業、地區、房屋有沒有泳池。虛擬變數讓這些類別可以放進回歸。本週講解怎樣加入虛擬變數、怎樣解讀其係數,以及怎樣檢定不同類別之間是否真的有差異。
- Builds on 先修內容
- Week 4
- Reading time 閱讀時間
- About 40 minutes
- Data 數據
wage1,ceosal1- Code 程式碼
- R, Python
Qualitative information定性資料
So far every explanatory variable has had a numerical meaning: years of education, years of experience, an IQ score. But two people with the same schooling and experience can earn differently because of the job they hold, the industry they work in, or their sex. These are qualitative or categorical variables: they say which group an observation belongs to.
到目前為止,每個解釋變數都有數值上的意義:教育年數、工作經驗年數、智商分數。但兩個教育和經驗相同的人,可能因為職業、行業或性別不同而收入不同。這些是定性或類別變數:它們表示一個觀測值屬於哪一組。
Sometimes a number is deliberately turned into categories. Mothers may not recall how many cigarettes they smoked each week during pregnancy, but they reliably know whether they smoked at all. And for earnings, the highest qualification completed may matter more than the count of years.
有時候我們會刻意把數字變成類別。母親未必記得懷孕期間每週吸了多少支煙,但一定記得自己有沒有吸煙。對收入而言,最高學歷可能比讀書年數更重要。
Categories enter a regression through a dummy variable (also called a binary or indicator variable): it equals 1 if the observation is in a category and 0 otherwise.
類別透過虛擬變數(又稱二元變數或指示變數)進入回歸:觀測值屬於某個類別時取 1,否則取 0。
A dummy variable shifts the intercept虛擬變數改變截距
Add the dummy to a regression on education:
把虛擬變數加入對教育的回歸:
Set the dummy to 0 and then to 1 to see what it does. Men and women get their own regression lines, with the same slope and different intercepts:
把虛擬變數分別設為 0 和 1,便能看到它的作用:男性和女性各有一條回歸線,斜率相同,截距不同:
is the difference in expected wages between women and men with the same education. Men, the group with , are the base group: the coefficient measures how far the other group sits from them.
是教育程度相同的女性與男性之間預期工資的差距。男性(即 的一組)是基準組:係數量度的是另一組與基準組的距離。
Men 男性Women 女性
Each marker is the mean wage of everyone with that many years of education; larger markers stand for more people. The lines are the fitted regressions. With an intercept dummy the two lines are parallel; adding the interaction lets their slopes differ.
每個標記代表該教育年數所有人的平均工資,標記越大代表人數越多。直線是擬合的回歸線。只有截距虛擬變數時兩條線互相平行;加入交互項後,兩條線的斜率可以不同。
The wage1 dataset records hourly wages for 526 workers in the United States in 1976. Column (1) of the table below gives : with the same years of education, women earned $2.27 an hour less than men on average, when the average wage was $5.90.
wage1 數據集記錄了 1976 年美國 526 名勞工的時薪。下表第 (1) 欄顯示 :在教育年數相同的情況下,女性的時薪平均比男性低 2.27 美元,而當時的平均時薪是 5.90 美元。
| (1) Intercept dummy | (2) With interaction | |
|---|---|---|
| Intercept 截距 | 0.623(0.673) | 0.201(0.844) |
female 女性 | −2.273(0.279) | −1.199(1.325) |
educ 教育年數 | 0.506(0.050) | 0.540(0.064) |
female × educ 女性 × 教育 | — | −0.086(0.104) |
| 0.259 | 0.260 | |
| 5,307.2 | 5,300.2 |
Standard errors in parentheses.
括號內為標準誤。
Is the gap statistically significant? Test against the one-sided alternative that women earn less:
這個差距在統計上顯著嗎?以「女性收入較低」作為單邊備擇假設來檢定:
Hypotheses 假設
Test statistic 檢定統計量
Decision 決定
, the one-sided 5% critical value. Reject .
(單邊 5% 臨界值),拒絕 。
Conclusion 結論
Holding education fixed, women in this sample earned significantly less than men.
在教育程度相同的情況下,這個樣本中女性的收入顯著低於男性。
Without the education control, the raw gap is larger: men averaged $7.10 an hour and women $4.59, a difference of $2.51. A little of that gap comes from differences in schooling; most of it does not.
如果不控制教育,原始差距更大:男性平均時薪 7.10 美元,女性 4.59 美元,相差 2.51 美元。差距中只有一小部分來自教育程度的差異,大部分並不是。
With no other regressors, a dummy gives a difference in means 沒有其他解釋變數時,虛擬變數等於平均值之差
Regress on a constant and a single dummy . With observations in the group () and in the base group, the Week 3 formula gives
把 只對常數和一個虛擬變數 做回歸。設該組()有 個觀測值,基準組有 個。第 3 週的公式給出
where and are the two group means. The intercept is the base group’s mean and the dummy coefficient is the difference in means. In wage1, regressing wage on female alone gives 7.10 and −2.51.
其中 與 是兩組的平均值。截距等於基準組的平均值,虛擬變數的係數等於兩組平均值之差。在 wage1 中,只把工資對 female 回歸,得到 7.10 和 −2.51。
To see the middle step, note that and , while . Since , we have , and the ratio is .
中間一步:,,而 。由於 ,得 ,相除即為 。
Slope dummies: interactions斜率虛擬變數:交互項
An intercept dummy lets the level differ between groups. Perhaps the return to education differs too. Allow for it by adding the product of the dummy and education, an interaction term, to the regression:
截距虛擬變數讓兩組的水平不同。也許兩組的教育回報也不同。我們可以加入虛擬變數與教育的乘積,即交互項,放入回歸:
Again set the dummy to 0 and 1:
同樣把虛擬變數設為 0 和 1:
is the difference in the slope: how much more, or less, a year of education is worth to women than to men. Switch the chart above to “With interaction” to see the two lines tilt apart.
是斜率的差異:每多一年教育,女性比男性多(或少)得多少。把上面的圖切換到「加入交互項」,便能看到兩條線的斜率分開。
Column (2) of the table gives . The estimated return to a year of education is $0.540 an hour for men and for women. These are exactly the slopes you would get by running separate regressions for men and women: a model in which every coefficient has its own dummy interaction is the same as splitting the sample.
表中第 (2) 欄顯示 。每多一年教育,男性時薪估計多 0.540 美元,女性多 美元。這與分別對男性和女性做回歸得到的斜率完全相同:如果每個係數都配上虛擬變數交互項,就等於把樣本分開來估計。
Jointly significant, separately not聯合顯著,個別卻不顯著
In column (2), neither female (, ) nor female × educ (, ) is significant on its own. Does sex not matter after all? Test the joint hypothesis that it has no effect at all, on either the intercept or the slope:
在第 (2) 欄中,female(,)和 female × educ(,)各自都不顯著。那麼性別到底是否沒有影響?我們檢定性別對截距和斜率都沒有影響的聯合假設:
Hypotheses 假設
. : at least one is not zero.
。:至少一個不為零。
Models 模型
Unrestricted: column (2), . Restricted: wage on education only, .
無約束:第 (2) 欄,。約束:工資只對教育回歸,。
Test statistic 檢定統計量
Decision 決定
The 5% critical value of is 3.01 (3.07 from the row of a printed table). : reject .
的 5% 臨界值是 3.01(統計表 一行為 3.07)。:拒絕 。
Jointly, the two terms are overwhelmingly significant. The individual t tests failed because female and female × educ move almost together: their correlation in this sample is 0.96. The data say clearly that men’s and women’s wage lines differ, but cannot say whether the difference is in the intercept or the slope.
兩項聯合起來極為顯著。個別 t 檢定之所以不顯著,是因為 female 和 female × educ 幾乎同步變動:兩者在此樣本中的相關係數是 0.96。數據清楚顯示男性和女性的工資線不同,但無法分辨差異是在截距還是斜率。
More than two categories多於兩個類別
The ceosal1 dataset records the 1990 salaries of 209 chief executives, with their firm’s sales and return on equity (ROE). Each firm is in one of four sectors, each with its own dummy:
ceosal1 數據集記錄了 209 名行政總裁 1990 年的薪酬,以及所屬公司的銷售額和股本回報率(ROE)。每家公司屬於四個行業之一,每個行業各有一個虛擬變數:
| Sector | Dummy | Firms |
|---|---|---|
| Industrial 工業 | indus | 67 |
| Financial 金融 | finance | 46 |
| Consumer products 消費品 | consprod | 60 |
| Utilities 公用事業 | utility | 36 |
Every firm has exactly one of the four dummies equal to 1. So the four always add up to 1, for every firm, which is exactly the column of ones that carries the intercept. Including all four together with an intercept is the dummy variable trap:
每家公司的四個虛擬變數中剛好有一個等於 1。所以對每家公司而言,四者之和永遠是 1,恰好等於代表截距的那一列 1。同時放入四個虛擬變數和截距,就會掉進虛擬變數陷阱:
The columns of are then linearly dependent, cannot be inverted, and OLS has no unique solution (Week 3). The fix is to leave one category out. With categories, include dummies and an intercept.
這時 的各列線性相關, 不可逆,OLS 沒有唯一解(見第 3 週)。解決辦法是剔除一個類別。有 個類別時,放入 個虛擬變數和截距。
The omitted sector, industrial firms here, becomes the base group.
被剔除的行業,這裏是工業公司,便成為基準組。
Reading the coefficients and changing the base解讀係數與更換基準組
Each dummy coefficient compares its sector with the base, holding sales and ROE fixed:
每個虛擬變數的係數都是在銷售額和股本回報率不變的情況下,把該行業與基準組比較:
is the intercept for industrial firms.
是工業公司的截距。
is the difference in expected log salary between financial and industrial firms.
是金融公司與工業公司之間預期薪酬對數的差異。
compares consumer-product firms with financial firms. Two non-base sectors are compared through the difference of their coefficients.
比較消費品公司與金融公司。兩個非基準組之間的比較,要看它們係數之差。
| (1) Base: industrial | (2) Base: utilities | |
|---|---|---|
| Intercept 截距 | 4.588(0.295) | 4.305(0.282) |
indus 工業 | — | 0.283(0.099) |
finance 金融 | 0.158(0.089) | 0.441(0.104) |
consprod 消費品 | 0.181(0.085) | 0.464(0.109) |
utility 公用事業 | −0.283(0.099) | — |
log(sales) 銷售額對數 | 0.257(0.032) | 0.257(0.032) |
roe 股本回報率 | 0.0112(0.0043) | 0.0112(0.0043) |
| 0.357 | 0.357 |
Standard errors in parentheses.
括號內為標準誤。
The choice of base changes the labels, not the model. Column (2) drops utility instead and includes indus. The new intercept is the utilities intercept from column (1): . Every sector comparison is the same as before; for example, finance against utilities is . The slopes and do not change at all.
基準組的選擇只改變係數的含義,不改變模型本身。第 (2) 欄改為剔除 utility,加入 indus。新的截距就是第 (1) 欄中公用事業的截距:。所有行業之間的比較都與之前相同,例如金融相對公用事業為 。斜率和 完全不變。
Percentage effects in log models對數模型中的百分比效應
When the dependent variable is in logs, a coefficient times 100 is often read as a percentage change. For a dummy that reading is only approximate, and for large coefficients the error is noticeable. The exact predicted percentage difference is
被解釋變數取對數時,係數乘以 100 通常被解讀為百分比變化。對虛擬變數而言,這只是近似值;係數較大時誤差會很明顯。準確的預測百分比差異是
Show every step 顯示完整推導
Switching the dummy from 0 to 1, with everything else fixed, raises predicted by . Write the predicted change in as :
在其他因素不變時,把虛擬變數由 0 變為 1,預測的 增加 。把 的預測變化記作 :
For small , , which gives the usual approximation. It is good to within about half a percentage point when .
當 很小時,,這就是常用的近似。當 時,誤差大約在半個百分點以內。
| Approx. | Exact | ||
|---|---|---|---|
| Finance 金融 | 0.158 | 15.8% | 17.1% |
| Consumer products 消費品 | 0.181 | 18.1% | 19.8% |
| Utilities 公用事業 | −0.283 | −28.3% | −24.6% |
Holding sales and ROE fixed, chief executives of financial firms were predicted to earn 17.1% more than those of industrial firms, and utility chiefs 24.6% less. The simple approximation would have overstated the utility gap by almost four percentage points.
在銷售額和股本回報率相同的情況下,金融公司行政總裁的預測薪酬比工業公司高 17.1%,公用事業公司則低 24.6%。如果用簡單近似,公用事業的差距會被高估接近四個百分點。
Testing differences between categories檢定類別之間的差異
One comparison: a t test. Is pay different in utilities and industrial firms, once sales and ROE are held fixed?
單一比較:t 檢定。在銷售額和股本回報率相同時,公用事業與工業公司的薪酬是否不同?
Hypotheses 假設
Test statistic 檢定統計量
Decision 決定
, the two-sided 5% critical value. Reject .
(雙邊 5% 臨界值),拒絕 。
By contrast, consumer-product and financial firms are not significantly different: with a standard error of 0.097, so .
相比之下,消費品公司與金融公司之間沒有顯著差異:,標準誤 0.097,所以 。
All sectors at once: an F test. Does the sector matter at all? The restricted model drops all three dummies.
所有行業一起:F 檢定。行業到底有沒有影響?約束模型把三個虛擬變數全部去掉。
Hypotheses 假設
. : at least one is not zero.
。:至少一個不為零。
Models 模型
with the sector dummies; with only log sales and ROE. Both explain log salary, so the form applies.
有行業虛擬變數時 ;只有銷售額對數和股本回報率時 。兩者的被解釋變數都是薪酬對數,所以可以用 公式。
Test statistic 檢定統計量
Decision 決定
The 5% critical value of is 2.65 (2.68 from the row). : reject . The sector matters for CEO pay, even after sales and ROE are taken into account.
的 5% 臨界值是 2.65(統計表 一行為 2.68)。:拒絕 。即使考慮了銷售額和股本回報率,行業對行政總裁薪酬仍然有影響。
Reproduce the results自己重現結果
Both datasets come with the free wooldridge package.
兩個數據集都收錄在免費的 wooldridge 套件中。
library(wooldridge)
data("wage1"); data("ceosal1")
m1 <- lm(wage ~ female + educ, data = wage1)
m2 <- lm(wage ~ female * educ, data = wage1) # adds female:educ
anova(lm(wage ~ educ, data = wage1), m2) # F = 33.51
# Four sectors, industrial firms as the base
m3 <- lm(log(salary) ~ finance + consprod + utility + log(sales) + roe,
data = ceosal1)
summary(m3)
100 * (exp(coef(m3)[c("finance", "consprod", "utility")]) - 1) # 17.1, 19.8, -24.6
anova(lm(log(salary) ~ log(sales) + roe, data = ceosal1), m3) # F = 7.88
# The same model with utilities as the base
m4 <- lm(log(salary) ~ indus + finance + consprod + log(sales) + roe,
data = ceosal1)
# pip install wooldridge statsmodels
import numpy as np
import statsmodels.formula.api as smf
import wooldridge as wd
wage1 = wd.data("wage1")
m1 = smf.ols("wage ~ female + educ", data=wage1).fit()
m2 = smf.ols("wage ~ female * educ", data=wage1).fit() # adds female:educ
print(m2.f_test("female = 0, female:educ = 0")) # F = 33.51
# Four sectors, industrial firms as the base
ceo = wd.data("ceosal1")
m3 = smf.ols("np.log(salary) ~ finance + consprod + utility"
" + np.log(sales) + roe", data=ceo).fit()
print(m3.summary())
print(100 * (np.exp(m3.params[["finance", "consprod", "utility"]]) - 1)) # 17.1, 19.8, -24.6
print(m3.f_test("finance = 0, consprod = 0, utility = 0")) # F = 7.88
print(m3.t_test("consprod - finance = 0")) # t = 0.24
Check your understanding自我測驗
-
A dataset records which of Australia’s eight states and territories each person lives in. How many dummy variables should a regression with an intercept include?
一個數據集記錄了每個人居住在澳洲八個州和領地中的哪一個。含截距的回歸應該放入多少個虛擬變數?
Answer 答案
Seven. One state or territory is left out as the base group; including all eight with an intercept is the dummy variable trap.
七個。剔除一個州或領地作為基準組;八個全放再加截距就會掉進虛擬變數陷阱。
-
Using column (1) of the wage table, predict the hourly wage of a woman and of a man with 12 years of education.
根據工資表第 (1) 欄,預測受過 12 年教育的女性和男性的時薪。
Answer 答案
Woman: . Man: . The gap is , whatever the years of education.
女性:。男性:。無論教育年數多少,差距都等於 。
-
Someone replaces
femalewithmalein column (1). What happens to the coefficient on the dummy, the intercept, and ?如果把第 (1) 欄的
female換成male,虛擬變數的係數、截距和 會怎樣?Answer 答案
The dummy coefficient becomes +2.273 and the intercept becomes , the women’s intercept. The fitted values, residuals and are unchanged: only the base group has changed.
虛擬變數係數變為 +2.273,截距變為 ,即女性的截距。擬合值、殘差和 都不變,改變的只是基準組。
-
In column (2), what is the estimated return to a year of education for women, and is it significantly different from the return for men?
在第 (2) 欄中,女性每多一年教育的估計回報是多少?它與男性的回報是否顯著不同?
Answer 答案
dollars an hour. The difference is with , so it is not significant at any usual level.
每小時 美元。差異為 ,,在任何常用水平下都不顯著。
-
In a log wage regression, the coefficient on a dummy for union membership is 0.25. What is the predicted wage premium for union members?
在一個對數工資回歸中,工會會員虛擬變數的係數是 0.25。工會會員的預測工資溢價是多少?
Answer 答案
. The quick reading of 25% understates it by more than three points.
。直接讀作 25% 會低估超過三個百分點。
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, Chapter 7.
- Data: wage1 and ceosal1, distributed with the textbook and in the wooldridge R package. All estimates on this page were computed from them.
Any errors are our own.