我使用
glm
函数估计了概率模型。
在包 lmtest
中,使用 coeftest
函数可以非常轻松地计算集群标准误差。
要计算平均边际效应,我输入:
AME=margins(model1, variables = c("x", "y"))
。
如何计算
AME
的聚类误差?
margins
软件包现在处于“维护模式”,这意味着它没有被积极开发。我建议您使用 marginaleffects
包,它被设计为后继者(免责声明:我是作者):https://marginaleffects.com
您可以通过在
vcov
参数中指定单边公式来使用聚集标准误差:
library(marginaleffects)
mod <- glm(vs ~ mpg + hp + factor(cyl), data = mtcars, family = binomial)
avg_slopes(mod,
variables = c("mpg", "hp"),
vcov = ~cyl)
#
# Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
# hp -0.00282 0.000476 -5.937 <0.001 28.4 -0.00376 -0.00189
# mpg -0.01242 0.015213 -0.817 0.414 1.3 -0.04224 0.01739
#
# Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
与经典 IID 标准误差比较:
mod <- glm(vs ~ mpg + hp + factor(cyl), data = mtcars, family = binomial)
avg_slopes(mod, variables = c("mpg", "hp"))
#
# Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
# hp -0.00282 0.00228 -1.236 0.216 2.2 -0.0073 0.00165
# mpg -0.01242 0.01681 -0.739 0.460 1.1 -0.0454 0.02053
#
# Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
详情请参阅
vcov
中?avg_slopes
的文档。