我意识到我在错误地思考这个问题。我无需尝试直接将no.cluster 与使用coeftest 生成的对象进行比较,而是只需将with.clusters 中的标准错误替换为coeftest 生成的稳健值。
这比听起来要复杂一些,因为我使用的是零膨胀回归。
零膨胀回归由两个模型组成,这两个模型都包含在coeftest 的输出中。我只关心以count_开头的值。
> with.clusters.final
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
count_(Intercept) 1.08748 0.11608 9.3686 < 0.00000000000000022 ***
count_child -0.82116 0.33572 -2.4460 0.0151678 *
count_camper 0.80562 0.15933 5.0563 0.000000850945 ***
count_factor(lake)lake2 0.18206 0.19467 0.9352 0.3506175
count_factor(lake)lake3 -0.59536 0.10789 -5.5184 0.000000088810 ***
zero_(Intercept) -21.32602 5.36229 -3.9770 0.000092516485 ***
zero_child 11.73360 2.42002 4.8486 0.000002241063 ***
zero_camper -3.07137 0.85151 -3.6070 0.0003772 ***
zero_factor(lake)lake2 10.87828 2.60947 4.1688 0.000042846049 ***
zero_factor(lake)lake3 1.35318 0.22321 6.0624 0.000000005192 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
我需要删除以zero_ 开头的行,然后从其余名称中删除“count_”前缀。
> clustered.se <- with.clusters.final[,2]
> clustered.se <- clustered.se[str_sub(names(clustered.se), 1, 5) == "count"]
> names(clustered.se) <- str_remove(names(clustered.se), "count_")
> clustered.se
(Intercept) child camper factor(lake)lake2 factor(lake)lake3
0.1160763 0.3357177 0.1593300 0.1946651 0.1078864
我们可以对检验统计量和 p 值做同样的事情。这是一个简化流程的函数。
clustered_stat <- function(coeftest_object, statistic){
statistic.row <- if(statistic == "se"){
2
} else if(statistic == "t") {
3
} else if(statistic == "p") {
4
}
# pull the appropraite row out of the coeftest object
new.values <- coeftest_object[,statistic.row]
# just keep the values from the count model, not the logit model
new.values <- new.values[str_sub(names(new.values), 1, 5) == "count"]
# remove the "count_" prefix from the names of the values
names(new.values) <- str_remove(names(new.values), "count_")
new.values
}
现在我可以将原始的集群 zeroinfl 对象提供给 stargazer,用 coeftest 创建的强大版本替换统计信息。
> stargazer::stargazer(no.clusters, with.clusters,
+ se = list(NULL, clustered_stat(with.clusters.final, "se")),
+ t = list(NULL, clustered_stat(with.clusters.final, "t")),
+ p = list(NULL, clustered_stat(with.clusters.final, "p")),
+ type = "text",
+ column.labels = c("no clusters", "with clusters"))
==============================================
Dependent variable:
----------------------------
count
no clusters with clusters
(1) (2)
----------------------------------------------
child -0.911*** -0.821**
(0.285) (0.336)
camper 0.798*** 0.806***
(0.305) (0.159)
factor(lake)lake2 0.182
(0.195)
factor(lake)lake3 -0.595***
(0.108)
Constant 1.052*** 1.087***
(0.270) (0.116)
----------------------------------------------
Observations 250 250
Log Likelihood -434.906 -430.784
==============================================
Note: *p<0.1; **p<0.05; ***p<0.01