所上學校的空氣汙染水平 可能需要包含在模型中的控制變量我們想要估計 no2_class 對於 htime 的影響係數及相應的置信區間。問題是向量 中有 252 個控制變量,但我們只有 1084 個觀測值,如果直接進行回歸便會出現高維數據的過擬合問題。如果直接採用 Lasso 方法進行估計,因無法得到標準誤,則無法獲得估計係數的置信區間,即無法進行統計推斷。
實際上,除了我們關心的變量 no2_class 以外,其餘變量的統計推斷問題甚至係數估計都是次要的。只要我們能找到控制變量 的一個子集 ,使得能夠估計出可靠的 即可。換言之,如果我們知道 ,我們便可以使用如下的模型進行估計:
但問題是我們並不知道 中究竟哪些變量是屬於 的,因此要估計 ,首先要進行變量篩選的工作。
對於變量篩選,可參考上篇推文 Stata:拉索開心讀懂-Lasso 入門 中提到的一種方法 postselection 法。具體步驟分兩步,第一,採用 Lasso 方法進行估計,篩選出核心的控制變量;第二,用被解釋變量對自變量和篩選出來的控制變量進行回歸。
可是,直接採用 postselection 法是無法進行可靠的統計推斷的。Leeb and Pötscher (2008) 指出,postselection 法產生的估計量是不具備大樣本情形下正態分布性質的,並且在有限樣本的情形下採用一般的大樣本理論也是無法進行可靠的統計推斷的。
鑑於以上分析的問題,Belloni et al. (2012)、 Belloni, Chernozhukov, and Hansen (2014)、 Belloni, Chernozhukov, and Wei (2016) 以及 Chernozhukov et al. (2018) 推導了三種能夠提供對於 可靠統計推斷的方法。這三種方法分別為偏回歸法(partialing-out,以下簡稱 PO 法)、雙重選擇回歸法(double-selection,以下簡稱 DS 法)和交叉擬合偏回歸法(cross-fit partialing-out,以下簡稱 XPO 法)。下表是 Stata16 中三種方法在不同模型中的命令。
模型PO 法命令DS 法命令XPO 法命令linearporegressdsregressxporegresslogitpologitdslogitxpologitPoissonpopoissondspoissonxpopoissonlinear IVpoivregress/xpoivregress關於命令的實操參見下一小節的分析。
2. Stata 命令及操作本小節將延續上一節所討論的空氣汙染對小學生反應時間的例子,截取 Sunyer et al. (2017) 所用數據的部分變量,採用 PO、DS 和 XPO 三種方法進行估計和統計推斷,並給出方法背後的一些直覺。
2.1 PO 法Step 1: 調用數據並將連續變量和因子變量及其交互項存入暫元
webuse breathe, clear //調用數據 ***********將連續控制變量存入暫元`ccontrols'中******************** local ccontrols "sev_home sev_school age precip age0 siblings_old " local ccontrols "`ccontrols' siblings_young no2_home green_home noise_school " ***********將因子控制變量存入暫元`fcontrols'中******************** local fcontrols "grade sex lbweight breastfeed msmoke " local fcontrols "`fcontrols' feducation meducation overweight " ********將連續型、因子型控制變量及其交乘項存入暫元`ctrls'中********* local ctrls "i.(`fcontrols') c.(`ccontrols') " local ctrls "`ctrls' i.(`fcontrols')#c.(`ccontrols') "接著對變量進行一下簡單的描述性分析,代碼為
. describe react no2_class `fcontrols' `ccontrols' variable name type format label variable label --- react double %10.0g * Reaction time (ms) no2_class float %9.0g Classroom NO2 levels (ug/m3) grade byte %9.0g grade Grade in school sex byte %9.0g sex Sex lbweight byte %18.0g lowbw * Low birthweight breastfeed byte %19.0f bfeed Duration of breastfeeding msmoke byte %10.0f smoke * Mother smoked during pregnancy feducation byte %17.0g edu Father's education level meducation byte %17.0g edu Mother's education level overweight byte %32.0g overwt * Overweight by WHO/CDC definition sev_home float %9.0g Home socio-economic vulnerability index sev_school float %9.0g School socio-economic vulnerability index age float %9.0g Age (years) precip double %10.0g Daily total precipitation age0 double %4.1f Age started school siblings_old byte %1.0f Number of older siblings in house siblings_young byte %1.0f Number of younger siblings in house no2_home float %9.0g Home NO2 levels (ug/m3) green_home double %10.0g Home greenness (NDVI), 300m buffer noise_school float %9.0g School noise levels (dB)Step 2: 進行 PO 回歸
採用 poregress 命令進行回歸,controls() 設定潛在的控制變量,本例中加入了 Step1 當中設定的連續型、因子型變量及其交乘項作為控制變量。
poregress react no2_class, controls(`ctrls') //PO 法結果如下:
Partialing-out linear model Number of obs = 1,084 Number of controls = 0 Number of selected controls = 0 Wald chi2(1) = 11.39 Prob > chi2 = 0.0007 --- | Robust react | Coef. Std. Err. z P>|z| [95% Conf. Interval] ---+---- no2_class | 1.472821 .4363526 3.38 0.001 .617586 2.328057 --- Note: Chi-squared test is a Wald test of the coefficients of the variables of interest jointly equal to zero. Lassos select controls for model estimation. Type lassoinfo to see number of selected variables in each lasso.將結果存儲為 poplug
estimates store poplug以上結果表明,每立方米再增加一微克 NO2 ,平均反應時間增加 1.47 毫秒。可以注意到,結果中只有核心解釋變量 no2_class 的估計結果,其餘控制變量並沒有結果,這也是採用此類模型的特點。
PO 法採用的是多次 Lasso 方法來篩選控制變量。為說明 PO 方法的機理,我們考慮一個簡單的線性模型:
其中 為被解釋變量, 為核心自變量, 為控制變量,要採用 PO 法估計 ,一共有 5 步:
1.採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;3.採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;關於以上 5 步若想進一步了解,可參考 Chernozhukov, Hansen, and Spindler (2015a, b) 更為詳細的分析及推導。
注 : poregress 命令默認採用的是基於 plugin 方法的 Lasso 回歸,也可以通過在選項中添加 selection() 來使用其他檢驗方法,這部分也可以可參考上篇推文 Stata:拉索開心讀懂-Lasso 入門。
Step 3: 進一步分析
我們可以採用 lassocoef 命令看下每個 Lasso 回歸哪些變量被選擇。
lassocoef ( ., for(react)) ( ., for(no2_class))//變量篩選結果對比結果為:
--- | react no2_class -+- age | x | grade#c.green_home | 4th | x | grade#c.noise_school | 2nd | x | sex#c.age | 0 | x | feducation#c.age | 4 | x | sev_school | x precip | x no2_home | x green_home | x noise_school | x | grade#c.sev_school | 2nd | x | _cons | x x --- Legend: b - base level e - empty cell o - omitted x - estimated從上面的結果可以看出,對於 react 的回歸中, age 和四個交乘項被選擇。對於對於 no2_class 的回歸中, sev_school 、precip 、no2_home 、green_home 、noise_school 和一個交乘項被選擇。可見,兩次 Lasso 回歸中除了交乘項中的部分變量是相同的,其餘被選擇的控制變量差別是很大的。
當然,以上變量篩選的結果也可以用 lassoknots 命令,具體代碼為:
lassoknots , for(react)//變量 react 的變量篩選 lassoknots , for(no2_class)//變量 no2_class 的變量篩選結果為:
lassoknots , for(react) //變量 react 的變量篩選 | No. of | | nonzero In-sample | Variables (A)dded, (R)emoved, ID | lambda coef. R-squared | or left (U)nchanged --+-+ (*) 1 | .1375306 5 0.1619 | A age 0.sex#c.age 3.grade#c.green_home | | 1.grade#c.noise_school | | 4.feducation#c.age * lambda selected by plugin assuming heteroskedastic. . lassoknots , for(no2_class) //變量 no2_class 的變量篩選 | No. of | | nonzero In-sample | Variables (A)dded, (R)emoved, ID | lambda coef. R-squared | or left (U)nchanged --+-+ (*) 1 | .1375306 6 0.3411 | A precip no2_home sev_school | | green_home noise_school 1.grade#c.sev_school * lambda selected by plugin assuming heteroskedastic. 2.2 DS 法DS 法是 PO 法的拓展。簡單來講, DS 法是通過引入額外的控制變量使得估計結果更穩健。和 PO 法的語法結構類似,採用命令 dsregress 即可實現,最後將估計結構存儲為 dsplug。
. dsregress react no2_class, controls(`ctrls') //DS 法 . estimates store dsplug Double-selection linear model Number of obs = 1,084 Number of controls = 0 Number of selected controls = 0 Wald chi2(1) = 11.39 Prob > chi2 = 0.0007 -- | Robust react | Coef. Std. Err. z P>|z| [95% Conf. Interval] +- no2_class | 1.472821 .4363526 3.38 0.001 .617586 2.328057 -- Note: Chi-squared test is a Wald test of the coefficients of the variables of interest jointly equal to zero. Lassos select controls for model estimation. Type lassoinfo to see number of selected variables in each lasso.DS 法估計的結果和 PO 法相似,結果的解讀也是相似的,不再此處贅述。
對於 DS 法的機理,一共有 4 步,具體為:
採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;令 為 和 的合集;用 對 和 回歸,得到 的估計值和標準誤。Belloni, Chernozhukov, and Wei (2016) 指出,儘管在大樣本情況下 DS 法和 PO 法擁有同樣的性質,但在他們的模擬中,前者表現更好。在有限樣本情形下,DS 法表現更好可能是因為這種方法引入的控制變量是在一次回歸當中而非兩次單獨的回歸。
同理, DS 法也可以採用 lassocoef 和 lassoknots 命令進一步分析,因這部分分析和 PO 方法類似,故不再贅述。
2.3 XPO 法XPO 法也被稱為雙重機器學習法 (DML), Chernozhukov et al. (2018) 經過推導發現,這種方法相比於 PO 法而言在理論上具有更好的性質,並且在有限樣本情形下也表現更好。其中,二者最為重要的差別是 XPO 法要求的稀疏條件更弱。在實際操作中,這意味著 XPO 法可以提供更可靠的統計推斷,因為這種方法採用了樣本拆分技術(split-sample techniques),從而可以引入更多的控制變量。
具體實操和 PO 法也是類似的。採用命令 xporegress 即可實現,最後將估計結果存儲為 xpoplug。
xporegress react no2_class, controls(`ctrls') //XPO 法 estimates store xpoplug結果為:
Cross-fit partialing-out Number of obs = 1,084 linear model Number of controls = 0 Number of selected controls = 0 Number of folds in cross-fit = 10 Number of resamples = 1 Wald chi2(1) = 11.39 Prob > chi2 = 0.0007 - | Robust react | Coef. Std. Err. z P>|z| [95% Conf. Interval] + no2_class | 1.472821 .4363526 3.38 0.001 .617586 2.328057 - Note: Chi-squared test is a Wald test of the coefficients of the variables of interest jointly equal to zero. Lassos select controls for model estimation. Type lassoinfo to see number of selected variables in each lasso.估計和結果及相應的解釋與 PO 法也是類似的,故也不再贅述。
關於 XPO 法的機理,首先是將樣本進行拆分,拆分的子樣份數叫做折(folds),以 2 折為例,一共包含 6 步:
採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;用 A 子樣中估計係數,計算 B 子樣中的 和 的殘差,具體公式分別為:將上述操作 2-3 反過來再做一遍,即先使用子樣本 B 進行變量篩選:採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;採用 Lasso 方法,用 對 進行回歸,選出能夠最佳預測 的控制變量 ;用 B 子樣中估計係數,計算 A 子樣中的 和 的殘差,具體公式分別為:以上是將樣本拆分為 2 折的算法,10 折甚至 k 折的算法其實都是類似的。
3. 結論綜上,對比三種方法,最為推薦使用的是 XPO 法,因為這種方法在大樣本和有限樣本情形下都具有更好的性質,其缺點是計算會比較耗時。
4. 參考文獻溫馨提示: 文中連結在微信中無法生效。請點擊底部「閱讀原文」。
Belloni, A., D. Chen, V. Chernozhukov, and C. Hansen. 2012. Sparse models and methods for optimal instruments with an application to eminent domain. Econometrica 80: 2369–2429.-PDF-Belloni, A., V. Chernozhukov, and C. Hansen. 2014. Inference on treatment effects after selection among high-dimensional controls. Review of Economic Studies 81: 608–650.-PDF-Belloni, A., V. Chernozhukov, and Y. Wei. 2016. Post-selection inference for generalized linear models with many controls. Journal of Business & Economic Statistics 34: 606–619.-PDF-Chernozhukov, V., D. Chetverikov, M. Demirer, E. Duflo, C. Hansen, W. Newey, and J. Robins. 2018. Double/debiased machine learning for treatment and structural parameters. Econometrics Journal 21: C1–C68.-PDF-Chernozhukov, V., C. Hansen, and M. Spindler. 2015a. Post-selection and post-regularization inference in linear models with many controls and instruments. American Economic Review 105: 486–90.-PDF-——. 2015b. Valid post-selection and post-regularization inference: An elementary, general approach. Annual Review of Economics 7: 649–688.-PDF-Leeb, H., and B. M. Pötscher. 2008. Sparse estimators and the oracle property, or the return of Hodges estimator. Journal of Econometrics 142: 201–211.-PDF-Sunyer, J., et al. 2017. "Traffic-related Air Pollution and Attention in Primary School Children." 28(2): 181-189.-PDF-Wooldridge, J. M. 2020. Introductory Econometrics: A Modern Approach. 7th ed. Boston, MA: Cengage-Learning.楊繼超, 連享會推文 Stata:拉索開心讀懂-Lasso 入門
5. 相關推文Note:產生如下推文列表的命令為: lianxh lasso 高維 隨機推斷 安裝最新版 lianxh 命令: ssc install lianxh, replace
溫馨提示: 文中連結在微信中無法生效。請點擊底部「閱讀原文」。
Stata新命令-pdslasso:眾多控制變量和工具變量如何挑選?Stata:拉索回歸和嶺回歸-(Ridge,-Lasso)-簡介Stata Blogs - An introduction to the lasso in Stata (拉索回歸簡介)Stata:ritest-隨機推斷(Randomization Inference)
👉 課程主頁 :https://gitee.com/arlionn/PX
👉 課程主頁 :https://gitee.com/arlionn/PX
關於我們🍎 連享會 ( 主頁:lianxh.cn ) 由中山大學連玉君老師團隊創辦,定期分享實證分析經驗。👉 直達連享會: 【百度一下: 連享會】即可直達連享會主頁。亦可進一步添加 主頁,知乎,面板數據,研究設計 等關鍵詞細化搜索。New! lianxh 命令發布了: 在 Stata 命令窗口中輸入 ssc install lianxh 即可安裝,隨時搜索連享會推文、Stata 資源,詳情:help lianxh。連享會主頁 lianxh.cn
New! lianxh 命令發布了: GIF 動圖介紹 隨時搜索連享會推文、Stata 資源,安裝命令如下: . ssc install lianxh 使用詳情參見幫助文件 (有驚喜): . help lianxh