如何在gtsummary的tbl_uvregression中结合coxph指定聚类效应?
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:
coxphdoesn't recognize the lme4-style(1|grade)syntax for clustering. Instead, we use thecluster()function built into thesurvivalpackage to adjust standard errors for clustered data.- You set
y = response, butcoxphrequires aSurv()object (combining time and status) as the outcome for survival analysis. - The
family = binomialargument 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 tellscoxphto compute robust standard errors that account for within-cluster correlation (i.e., observations in the samegradegroup are not independent). - We're using the correct survival outcome (
Surv(time, status)) which is required for Cox models. - Removed the unnecessary
family = binomialargument 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

