FE 回归中的 Driscoll 和 Kraay 标准错误:在 R 中再现 Stata xtscc 输出
Driscoll and Kraay standard errors in FE regression: reproducing Stata xtscc output in R
我正在尝试使用包 plm
在 R 中复制 Stata 命令 xtscc
提供的结果,但我在查看相同的标准错误时遇到了一些麻烦
我也在 Stata 中使用 plm 包中的数据集进行复制。
# code to obtain dataset
library(lmtest)
library(car)
library(tidyverse)
data("Produc", package="plm")
write.dta(Produc,"test.dta")
我的目标是 运行 使用 Driscoll 和 Kraay 标准误差的双向固定效应面板模型估计。 Stata中的套路如下
use "test.dta", clear \ to import data
** i declare the panel
xtset state year
* create the dummies for the time fixed effects
quietly tab year, gen(yeardum)
* run a two way fixed effect regression model with Driscoll and Kraay standard errors
xi: xtscc gsp pcap emp unemp yeardum*,fe
* results are the following
Coef. Std. Err. t P>|t| [95% Conf. Interval]
pcap | -.1769881 .265713 -0.67 0.515 -.7402745 .3862983
emp | 40.61522 2.238392 18.14 0.000 35.87004 45.3604
unemp | 23.59849 85.10647 0.28 0.785 -156.8192 204.0161
在 R 中,我使用以下例程:
# I declare the panel
Produc <- pdata.frame(Produc, index = c("state","year"), drop.index = FALSE)
# run a two way fixed effect model
femodel <- plm(gsp~pcap+emp+unemp, data=Produc,effect = "twoway",
index = c("iso3c","year"), model="within")
# compute Driscoll and Kraay standard errors using vcovSCC
coeftest(femodel, vcovSCC(femodel))
pcap -0.17699 0.25476 -0.6947 0.4874
emp 40.61522 2.14610 18.9252 <2e-16 ***
unemp 23.59849 81.59730 0.2892 0.7725
虽然点估计与 Stata 中的相同,但标准误差不同。
为了检查我是否对标准误差使用了“错误的”小样本调整,我还尝试了 运行使用所有可用调整对 coeftest 进行调整,但 none 产生与 xtscc
.
library(purrr)
results <- map(c("HC0", "sss", "HC1", "HC2", "HC3", "HC4"),~coeftest(femodel, vcovSCC(femodel,type = .x)))
walk(results,print)
# none of the estimated standard errors is the same as xtscc
有谁知道如何在 R 中复制 Stata 的结果?
从plm
version 2.4 开始,它的函数within_intercept(., return.model = TRUE)
可以return 一个within 模型的完整模型,就像在Stata 中一样。有了这个,就可以准确地复制 Stata 的用户贡献命令 xtscc
.
的结果
xtscc
的工作方式似乎是将双向 FE 模型估计为单向 FE 模型 + 时间维度的虚拟变量。所以让我们用 plm
:
复制它
data("Produc", package="plm")
Produc <- pdata.frame(Produc, index = c("state","year"), drop.index = FALSE)
femodel <- plm(gsp ~ pcap + emp + unemp + factor(year), data = Produc, model="within")
femodelint <- within_intercept(femodel, return.model = TRUE)
lmtest::coeftest(femodelint, vcov. = function(x) vcovSCC(x, type = "sss"))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) -6547.68816 3427.47163 -1.9104 0.0564466 .
# pcap -0.17699 0.26571 -0.6661 0.5055481
# emp 40.61522 2.23839 18.1448 < 0.00000000000000022 ***
# unemp 23.59849 85.10647 0.2773 0.7816356
# [...]
我正在尝试使用包 plm
在 R 中复制 Stata 命令 xtscc
提供的结果,但我在查看相同的标准错误时遇到了一些麻烦
我也在 Stata 中使用 plm 包中的数据集进行复制。
# code to obtain dataset
library(lmtest)
library(car)
library(tidyverse)
data("Produc", package="plm")
write.dta(Produc,"test.dta")
我的目标是 运行 使用 Driscoll 和 Kraay 标准误差的双向固定效应面板模型估计。 Stata中的套路如下
use "test.dta", clear \ to import data
** i declare the panel
xtset state year
* create the dummies for the time fixed effects
quietly tab year, gen(yeardum)
* run a two way fixed effect regression model with Driscoll and Kraay standard errors
xi: xtscc gsp pcap emp unemp yeardum*,fe
* results are the following
Coef. Std. Err. t P>|t| [95% Conf. Interval]
pcap | -.1769881 .265713 -0.67 0.515 -.7402745 .3862983
emp | 40.61522 2.238392 18.14 0.000 35.87004 45.3604
unemp | 23.59849 85.10647 0.28 0.785 -156.8192 204.0161
在 R 中,我使用以下例程:
# I declare the panel
Produc <- pdata.frame(Produc, index = c("state","year"), drop.index = FALSE)
# run a two way fixed effect model
femodel <- plm(gsp~pcap+emp+unemp, data=Produc,effect = "twoway",
index = c("iso3c","year"), model="within")
# compute Driscoll and Kraay standard errors using vcovSCC
coeftest(femodel, vcovSCC(femodel))
pcap -0.17699 0.25476 -0.6947 0.4874
emp 40.61522 2.14610 18.9252 <2e-16 ***
unemp 23.59849 81.59730 0.2892 0.7725
虽然点估计与 Stata 中的相同,但标准误差不同。
为了检查我是否对标准误差使用了“错误的”小样本调整,我还尝试了 运行使用所有可用调整对 coeftest 进行调整,但 none 产生与 xtscc
.
library(purrr)
results <- map(c("HC0", "sss", "HC1", "HC2", "HC3", "HC4"),~coeftest(femodel, vcovSCC(femodel,type = .x)))
walk(results,print)
# none of the estimated standard errors is the same as xtscc
有谁知道如何在 R 中复制 Stata 的结果?
从plm
version 2.4 开始,它的函数within_intercept(., return.model = TRUE)
可以return 一个within 模型的完整模型,就像在Stata 中一样。有了这个,就可以准确地复制 Stata 的用户贡献命令 xtscc
.
xtscc
的工作方式似乎是将双向 FE 模型估计为单向 FE 模型 + 时间维度的虚拟变量。所以让我们用 plm
:
data("Produc", package="plm")
Produc <- pdata.frame(Produc, index = c("state","year"), drop.index = FALSE)
femodel <- plm(gsp ~ pcap + emp + unemp + factor(year), data = Produc, model="within")
femodelint <- within_intercept(femodel, return.model = TRUE)
lmtest::coeftest(femodelint, vcov. = function(x) vcovSCC(x, type = "sss"))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) -6547.68816 3427.47163 -1.9104 0.0564466 .
# pcap -0.17699 0.26571 -0.6661 0.5055481
# emp 40.61522 2.23839 18.1448 < 0.00000000000000022 ***
# unemp 23.59849 85.10647 0.2773 0.7816356
# [...]