我正在尝试xtscc
使用软件包在R中复制Stata命令提供的结果,plm
但是我在查看相同的标准错误时遇到了一些麻烦,我也在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
在RI中,使用以下例程:
# 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,但没有一个产生与相同的值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
版本2.4开始,其功能within_intercept(., return.model = TRUE)
可以返回内部模型的完整模型,如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
# [...]