我正在尝试用包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
在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的结果吗?
发布于 2020-12-12 21:54:32
由于plm
版本2.4,它的函数within_intercept(., return.model = TRUE)
可以返回内部模型的完整模型,截取就像Stata中那样。这样,就可以准确地复制Stata的用户贡献命令xtscc
的结果。
xtscc
的工作方式是将双维有限元模型估计为单向有限元模型+时间维虚拟模型。所以让我们用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
# [...]
https://stackoverflow.com/questions/65223905
复制相似问题