R語言工具變量與兩階段最小二乘法
原文鏈接:http://tecdat.cn/?p=5374
?
?
我們要估計的模型是
Y = A + BX + CD + EY = A + BX + CD + E,
其中是解釋變量,,和是我們想要估計的系數(shù)。?
生成數(shù)據(jù)
首先,讓我們生成數(shù)據(jù)。
假設的工具變量和之間的相關矩陣如下:?
## ? ? ? x ? ? d ? ? z ? ? e
## x 1.000 0.001 0.002 0.001
## d 0.001 1.000 0.700 0.300
## z 0.002 0.700 1.000 0.001
## e 0.001 0.300 0.001 1.000
具體而言,相關性表明
cor(d,e)= 0.3,這意味著是內(nèi)生的; dd
cor(d,z)= 0.7,這意味著是的強大工具變量; zzdd
cor(z,e)= 0.001,這意味著工具變量滿足排除限制,因為它只影響到.zzyydd
現(xiàn)在,讓我們使用指定的相關性為,,和生成數(shù)據(jù).xxddzzee
nvars = dim(U) 1
numobs = 1000
random.normal = matrix(rnorm(nvars*numobs, 0 , nrow=nvars, ncol=numobs);
X = U %*% random.normal
newX = t(X)
data = as.data.frame(newX)
數(shù)據(jù)看起來像這樣:
## ? ? ? ? ? ? x ? ? ? ? ?d ? ? ? ? ?z ? ? ? ? ?e
## 1 -0.62645381 ?0.1830168 -0.4694601 ?1.7474361
## 2 ?0.32950777 -0.8201385 -0.2255741 ?0.2818908
## 3 ?0.57578135 -0.3048125 ?0.8670061 -0.1795257
## 4 -0.62124058 -2.2153200 -0.7481687 -1.0350488
## 5 -0.01619026 ?0.9438195 ?1.2471197 ?0.5820200
## 6 ?0.91897737 ?0.7830549 ?0.6025820 -1.5924689
?
?
以及數(shù)據(jù)之間的相關性
## ? ? ? ? ? ? x ? ? ? ? ?d ? ? ? ? ? ?z ? ? ? ? ? e
## x ?1.00000000 0.00668391 -0.012319595 0.016239235
## d ?0.00668391 1.00000000 ?0.680741763 0.312192680
## z -0.01231960 0.68074176 ?1.000000000 0.006322354
## e ?0.01623923 0.31219268 ?0.006322354 1.000000000
正如我們之前指定的那樣。
現(xiàn)在讓我們指定真正的數(shù)據(jù)生成過程并生成解釋變量YY
如果我們假裝我們不知道真正的關系并使用和來解釋,我們對和正確系數(shù)應該接近到。??
OLS
如果我們只使用OLS來估計系數(shù):
?
##
## Call:
## lm(formula = y ~ x + d)
##
## Residuals:
## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max
## -3.2395 -0.5952 -0.0308 ?0.6617 ?2.7592
##
## Coefficients:
## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) ?9.99495 ? ?0.03105 ?321.89 ? <2e-16 ***
## x ? ? ? ? ? ?1.01408 ? ?0.02992 ? 33.89 ? <2e-16 ***
## d ? ? ? ? ? ?1.31356 ? ?0.03023 ? 43.46 ? <2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9817 on 997 degrees of freedom
## Multiple R-squared: ?0.7541, Adjusted R-squared: ?0.7536
## F-statistic: ?1528 on 2 and 997 DF, ?p-value: < 2.2e-16
?
b的估計系數(shù)是1.31 instread of 1. ## 2SLS ##現(xiàn)在我們使用2SLS來估計這種關系。我們使用z作為d的工具變量
第1階段:在和上回歸,并將d的擬合值保存為d.ddxxzz
?
?
##
## Call:
## lm(formula = d ~ x + z)
##
## Residuals:
## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max
## -2.59344 -0.52572 ?0.04978 ?0.53115 ?2.01555
##
## Coefficients:
## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01048 ? ?0.02383 ? -0.44 ? ?0.660
## x ? ? ? ? ? ?0.01492 ? ?0.02296 ? ?0.65 ? ?0.516
## z ? ? ? ? ? ?0.68594 ? ?0.02337 ? 29.36 ? <2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7534 on 997 degrees of freedom
## Multiple R-squared: ?0.4636, Adjusted R-squared: ?0.4626
## F-statistic: 430.9 on 2 and 997 DF, ?p-value: < 2.2e-16
第2階段:在和上回歸yyxxd.hatd.hat
?
?
##
## Call:
## lm(formula = y ~ x + d.hat)
##
## Residuals:
## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max
## -4.4531 -1.0333 ?0.0228 ?1.0657 ?4.0104
##
## Coefficients:
## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) ?9.99507 ? ?0.04786 ?208.85 ? <2e-16 ***
## x ? ? ? ? ? ?1.01609 ? ?0.04612 ? 22.03 ? <2e-16 ***
## d.hat ? ? ? ?1.00963 ? ?0.06842 ? 14.76 ? <2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.513 on 997 degrees of freedom
## Multiple R-squared: ?0.4158, Adjusted R-squared: ?0.4146
## F-statistic: 354.8 on 2 and 997 DF, ?p-value: < 2.2e-16
結果
b的真值:1 OLS estiamte of b:.00963 2SLS estiamte of b:1.31356
如果治療變量是內(nèi)生的,我們使用2SLS。
?
?
非常感謝您閱讀本文,有任何問題請在下面留言!
參考文獻
1.R語言多元Logistic邏輯回歸 應用案例
2.面板平滑轉移回歸(PSTR)分析案例實現(xiàn)
3.matlab中的偏最小二乘回歸(PLSR)和主成分回歸(PCR)
4.R語言泊松Poisson回歸模型分析案例
5.R語言回歸中的Hosmer-Lemeshow擬合優(yōu)度檢驗
6.r語言中對LASSO回歸,Ridge嶺回歸和Elastic Net模型實現(xiàn)
7.在R語言中實現(xiàn)Logistic邏輯回歸
8.python用線性回歸預測股票價格
9.R語言如何在生存分析與Cox回歸中計算IDI,NRI指標