You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gtsummary的tbl_uvregression中结合coxph指定聚类效应?

Add Clustering to tbl_uvregression with coxph

Hey there! Let's fix your clustering issue with tbl_uvregression and coxph—I've been in this spot before, so I know exactly where to adjust.

First, Let's Clear Up the Missteps in Your Original Code

Your current approach has a few key issues that are preventing the clustering effect from being applied:

  • coxph doesn't recognize the lme4-style (1|grade) syntax for clustering. Instead, we use the cluster() function built into the survival package to adjust standard errors for clustered data.
  • You set y = response, but coxph requires a Surv() object (combining time and status) as the outcome for survival analysis.
  • The family = binomial argument is for generalized linear models, not Cox proportional hazards models—this shouldn't be included here.

Correct Code Implementation

Here's the revised code that properly applies clustering by grade in your single-variable Cox regression table:

library(coxph)
library(gtsummary)
library(survival)

# Load trial dataset
data(trial)

# Create the clustered single-variable Cox regression table
trial %>% 
  tbl_uvregression(
    method = coxph,
    # Define the survival outcome correctly with Surv()
    y = Surv(time, status),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2),
    # Add cluster(grade) to the formula to adjust for clustering
    formula = "{y} ~ {x} + cluster(grade)"
  )

Why This Works

  • The cluster(grade) term tells coxph to compute robust standard errors that account for within-cluster correlation (i.e., observations in the same grade group are not independent).
  • We're using the correct survival outcome (Surv(time, status)) which is required for Cox models.
  • Removed the unnecessary family = binomial argument since it's irrelevant for survival analysis.

Verify the Results

To confirm the clustering is working, you can compare the standard errors/confidence intervals from this table to a standalone coxph model with clustering:

# Example standalone clustered Cox model for one variable
coxph(Surv(time, status) ~ age + cluster(grade), data = trial) %>% 
  summary()

The standard errors and CIs from this should match what's in your gtsummary table—this confirms the clustering adjustment is applied correctly.

内容的提问来源于stack exchange,提问作者gideon1321

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:52:30