首页 > 解决方案 > FE 回归中的 Driscoll 和 Kraay 标准误差:在 R 中再现 Stata xtscc 输出

问题描述

我正在尝试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 的结果?

标签: rpanelstatapanel-dataplm

解决方案


plm2.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    
# [...] 

推荐阅读