![](/img/trans.png)
[英]Can I use modelsummary with vcov="robust" for cox model in R?
[英]R modelsummary output: robust standard errors from coxph model
我在 R 中运行一系列coxph
模型,并使用modelsummary
package 和命令将 output 编译成 latex 表。 coxph
提供 SE 和稳健的 se 作为输出,p 值基于稳健的 se。 这是一个简单的例子来说明 output:
test_data <- list(time=c(4,3,1,1,2,2,3)
, status=c(1,1,1,0,1,1,0)
, x=c(0,2,1,1,1,0,0)
, sex=c(0,0,0,0,1,1,1))
model <- coxph(Surv(time, status) ~ x + cluster(sex), test_data)
Call:
coxph(formula = Surv(time, status) ~ x, data = test1, cluster = sex)
coef exp(coef) se(coef) robust se z p
x 0.460778 1.585306 0.562800 0.001052 437.9 <2e-16
Likelihood ratio test=0.66 on 1 df, p=0.4176
n= 7, number of events= 5
接下来,我尝试从这些模型中创建 latex 个表,显示稳健的 se。
modelsummary(model, output = "markdown", fmt = 3, estimate = "{estimate}{stars}", statistic = "std.error")
| | Model 1 |
|:---------------------|:----------:|
|x | 0.461*** |
| | (0.563) |
|Num.Obs. | 7 |
|R2 | 0.090 |
|AIC | 13.4 |
如我们所见,仅显示未调整的 se。 我找不到适合这个statistic = "std.error"
参数的任何替代方法,而且vcov="robust"
类的参数也不起作用。
如何使用 modelsummary 为 coxph 模型显示任何类型的稳健标准误差?
感谢阅读,我们将不胜感激。
下一个版本的modelsummary
(现在可在 Github 上获得)将在这些情况下生成信息更丰富的错误消息:
library(survival)
library(modelsummary)
test_data <- list(time=c(4,3,1,1,2,2,3)
, status=c(1,1,1,0,1,1,0)
, x=c(0,2,1,1,1,0,0)
, sex=c(0,0,0,0,1,1,1))
model <- coxph(Surv(time, status) ~ x + cluster(sex), test_data)
modelsummary(model, vcov = "robust")
# Error: Unable to extract a variance-covariance matrix for model object of class
# `coxph`. Different values of the `vcov` argument trigger calls to the `sandwich`
# or `clubSandwich` packages in order to extract the matrix (see
# `?insight::get_varcov`). Your model or the requested estimation type may not be
# supported by one or both of those packages, or you were missing one or more
# required arguments in `vcov_args` (like `cluster`).
如您所见,问题在于modelsummary
本身并不计算稳健的标准误差。 相反,它将此任务委托给sandwich
或clubSandwich
包。 不幸的是,这些包似乎不支持这个coxph
model:
sandwich::vcovHC(model)
#> Error in apply(abs(ef) < .Machine$double.eps, 1L, all): dim(X) must have a positive length
sandwich
是R
生态系统中的主要 package,用于计算稳健的标准误差。 AFAICT,所有其他可用的制表包(例如, stargazer
, texreg
)也使用sandwich
,因此您不太可能通过查看这些来获得成功。 如果您发现另一个 package 可以计算 Cox 模型的稳健标准误差,请在模型modelsummary
存储库上提交报告。 我将调查看看是否可以添加支持。
如果您想要的信息在摘要 object 中可用,您可以按照此处的说明添加此信息:
tidy_custom.coxph <- function(x, ...) {
s <- summary(x)$coefficients
data.frame(
term = row.names(s),
robust.se = s[, "robust se", drop = FALSE])
}
modelsummary(model, statistic = "robust.se")
Model 1 | |
---|---|
X | 0.461 |
(0.001) | |
Num.Obs。 | 7 |
工商总局 | 13.4 |
BIC | 13.4 |
均方根误差 | 0.61 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.