【发布时间】:2021-03-21 05:58:43
【问题描述】:
我正在尝试使用包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,但没有一个产生与 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 的结果吗?
【问题讨论】:
-
可能由于 Stata 在其内部模型中包含截距而 plm 没有,因此小样本调整略有不同(有限情况;在大样本中不相关)。在此处或 Stackoverflow 上的小样本调整中讨论了 Stata 与 plm 的差异,也许这会有所帮助。您还可以查看
plm::within_intercept的编码(或文档),以了解如何计算具有截距的 FE 模型(文档是参考)。我怀疑xtssc以某种方式使用了Stata 的xtreg。不知道xtssc是否使用了和xtreg一样的小样本调整。 -
感谢您的评论,实际数据的问题在于,虽然 R 系数很重要,但 Stata 系数不重要。调整为 0.03。还有你知道Stata中使用什么类型的标准错误吗?从 xtscc 的文档看不清楚..
-
xtscc是用户贡献的命令。如果其文档没有显示所采用的小样本调整,您可能需要阅读函数源代码或联系其作者。在 plm 中,type =sss模仿 Stata 的小样本调整(因此得名)。 -
请注意,您对
plm的调用中的index参数将被忽略,因为您的数据已经是pdata.frame。此外,索引参数包含一个不在数据中的变量 (iso3c)。
标签: r panel stata panel-data plm