问题描述
我正在尝试使用MASS
包的polr()
函数估计具有聚类标准误差的序数逻辑回归.没有内置的聚类功能,因此我正在寻找(a)软件包或(b)使用模型输出使用模型输出来计算聚类标准误差的手动方法.我打算使用margins
包来估计模型的边际效应.
I am trying to estimate an ordinal logistic regression with clustered standard errors using the MASS
package's polr()
function. There is no built-in clustering feature, so I am looking for (a) packages or (b) manual methods for calculating clustered standard errors using the model output. I plan to use margins
package to estimate marginal effects from the model.
这里是一个例子:
library(MASS)
set.seed(1)
obs <- 500
# Create data frame
dat <- data.frame(y = as.factor(round(rnorm(n = obs, mean = 5, sd = 1), 0)),
x = sample(x = 1:obs, size = obs, replace = T),
clust = rep(c(1,2), 250))
# Estimate and summarize model
m1 <- MASS::polr(y ~x, data = dat, Hess = TRUE)
summary(m1)
尽管关于堆栈溢出的许多问题询问如何针对普通最小二乘模型(以及在某些情况下针对),目前尚不清楚如何在有序逻辑回归(即比例赔率逻辑回归)中对误差进行聚类.此外,现有的SO问题集中在具有其他严重缺陷(例如,模型输出的类别与其他用于分析和表示结果的标准软件包不兼容的软件包)上,而不是使用与predict()
兼容的MASS::polr()
. /p>
While many questions on Stack Overflow ask about how to cluster standard errors in R for ordinary least squares models (and in some cases for logistic regression), it's unclear how to cluster errors in ordered logistic regression (i.e. proportional odds logistic regression). Additionally, the existing SO questions focus on packages that have other severe drawbacks (e.g. the classes of model outputs are not compatible with other standard packages for analysis and presentation of results) rather than using MASS::polr()
which is compatible with predict()
.
推荐答案
这基本上是在 Achim Zeleis在2016年针对rhelp提供的答案.
library(lmtest)
library("sandwich")
coeftest(m1, vcov=vcovCL(m1, factor(dat$clust) ))
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
x 0.00093547 0.00023777 3.9343 9.543e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
这篇关于R:在MASS :: polr()中聚类标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!