我正试图对两个子样本进行系数检验的比较.要做到这一点,我做了以下工作:

full_model <- lm(y ~ v1*subsample_dummy + fixed_effects, data=df)
reduced_model <- lm(y ~ v1 + subsample_dummy + fixed_effects, data=df)

test <- anova(full_model, reduced_model)

以上内容给出了我的结果.

然而,我不确定如何在cluster个模型中做同样的事情,比方说,year个变量.

我可以使用以下代码对lm模型进行集群:

library(sandwich)
# cluster by year
clustered_se <- vcovCL(full_model, ~ year) 
clustered_se1 <- vcovCL(reduced_model, ~ year)

# generate summaries with clustered standard errors 
a <- coeftest(full_model, vcov. = clustered_se) 
b <- coeftest(reduced_model, vcov. = clustered_se1) 

然而,问题仍然存在,我仍然无法做到:

anova(a, b)

当模型需要标准误差聚类时,如何实现子样本间的系数检验比较?

推荐答案

我们可以使用sandwich::vcovCL来获得与lfe::felm基本相同的标准误差.让我们估计一些模型.

> est1 <- lfe::felm(y ~ x1 + x2 | id + firm | 0 | firm, data=d)  ## id + firm FE, clustered by firm
> est2 <- lfe::felm(y ~ x1 | id + firm | 0 | firm, data=d)  ## same, restricted model
> est3 <- lm(y ~ x1 + x2 + as.factor(id) + as.factor(firm), data=d)  ## as est1 w/o clustering
> est4 <- lm(y ~ x1 + as.factor(id) + as.factor(firm), data=d)  ## as restricted est3

比较est3est1的标准误差,

> lmtest::coeftest(est3, vcov.=\(x) 
+                  sandwich::vcovCL(x, cluster=d$firm, type='HC0'))[1:3, ]
             Estimate Std. Error  t value      Pr(>|t|)
(Intercept) 3.7416642 0.10554929 35.44945 5.454680e-177
x1          1.0432612 0.02965723 35.17730 3.631800e-175
x2          0.4904104 0.03186679 15.38939  5.822822e-48
> coef(summary(est1))
    Estimate Cluster s.e.  t value     Pr(>|t|)
x1 1.0432612   0.02968696 35.14207 1.799173e-13
x2 0.4904104   0.03189874 15.37398 2.931632e-09

yield 率基本相同.

因此,根据Cross validated [1]上的一篇帖子,我们可以使用lmtest::waldtest来比较est1est2型号(还有一种lfe::waldtest,它的工作方式不同).

> lmtest::waldtest(est3, est4, vcov=\(x) 
+                  sandwich::vcovCL(x, cluster=d$firm, type='HC0'))
Wald test

Model 1: y ~ x1 + x2 + as.factor(id) + as.factor(firm)
Model 2: y ~ x1 + as.factor(id) + as.factor(firm)
  Res.Df Df      F    Pr(>F)    
1    966                        
2    967 -1 236.83 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

希望这能让你走得更远.

顺便说一句:你绝对可以把这件事作为作者GitHub分的一个问题来提交.


Data:

> set.seed(42)
> n <- 1e3
> d <- data.frame(x1=rnorm(n), x2=rnorm(n), id=factor(sample(20, n, replace=TRUE)),
+                 firm=factor(sample(13, n, replace=TRUE)), u=rnorm(n))
> id.eff <- rnorm(nlevels(d$id))
> firm.eff <- rnorm(nlevels(d$firm))
> d$y <- d$x1 + 0.5 * d$x2 + id.eff[d$id] + firm.eff[d$firm] + d$u

R相关问答推荐

如何在弹性表中为类别值的背景上色

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

R中具有gggplot 2的Likert图,具有不同的排名水平和显示百分比

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

如何自定义Shapviz图?

使用across,starts_with和ifelse语句变更多个变量

在ggplot2中更改小提琴情节的顺序

LOF中的插图短文字幕

用R ggplot2求上、下三角形中两个变量的矩阵热图

Data.table';S GForce-将多个函数应用于多列(带可选参数)

计算直线上点到参考点的总距离

Geom_arcbar()中出错:找不到函数";geom_arcbar";

SHILINY中DT列的条件着色

当由base::限定时,`[.factor`引发NextMethod错误

为什么不能使用lApply在包装函数中调用子集

根据排名的顶点属性调整曲线图布局(&Q)

使用卡环从R中的列中删除单位(&C)

如何根据顺序/序列从数据框中排除值

如何在分组蜂群小区中正确定位标签

Ggplot2水平线和垂直线的图例图标不匹配