r - Issues with add_n and add_nevent with tbl_regression and weighted cox models - Stack Overflow

admin2025-04-08  0

I'm encountering some strange behavior when using add_n and add_nevent functions with tbl_regression from the gtsummary package. Whenever I use weighted cox models the output table shows different N and N events then what exists in the actual dataset. Here we can see the table from the pre analysis dataset: 2x2 table

Here is code for one of my models the regression table, fit_dichot_mice_adjusted is a mimira object consisting of multiply imputed datasets (using mice) for which I calculated IPTW using the weightThem package:

weights_mice <- weightthem( formula =  as.formula(paste("intervention_status ~ ", paste(names(covs), collapse="+"), paste("+ moh_treat_prev_year"))), 
                  datasets = test_mice_2,
                  approach = "within",
                  method = "glm",
                  estimand = "ATT")

fit_dichot_mice_adjusted <- with(weights_mice,coxph(Surv(survival_time, event_status) ~  intervention_status,
                                           robust = T,
                                           cluster = ID,
                                           ties = "efron"))


tbl_regression(fit_dichot_mice_adjusted, 
      exponentiate = T,
      pvalue_fun = label_style_pvalue(digits = 2),
      show_single_row = "intervention_status",
      label = list(intervention_status = "5 < treatments")) %>% 
      add_n() %>% 
      add_nevent()

The tbl_regression output shows a different N (4092) and event N (46) than the actual data. weighted model regression table

This occurs whether or not the regression model object is a mimira or a simple coxph object. I've dug into the model objects and the N and Nevent variables are correct.mimira levels The only constant seems to be that the error occurs when the inputted model is weighted. If not - the numbers are correct:

fit_dichot_mice_unadjusted <-  coxph(Surv(survival_time, event_status) ~  intervention_status,
                           robust = T,
                           cluster = ID,
                           ties = "efron",
                           data = iptw_test)

tbl_regression(fit_dichot_mice_unadjusted,
                           exponentiate = T,
                           pvalue_fun = label_style_pvalue(digits = 2),
                           show_single_row = "intervention_status",
                           label = list(intervention_status = "5 < treatments")) %>% 
                           add_n() %>% 
                           add_nevent()

unweighted model regression table

Any help would be greatly appreciated.

I'm encountering some strange behavior when using add_n and add_nevent functions with tbl_regression from the gtsummary package. Whenever I use weighted cox models the output table shows different N and N events then what exists in the actual dataset. Here we can see the table from the pre analysis dataset: 2x2 table

Here is code for one of my models the regression table, fit_dichot_mice_adjusted is a mimira object consisting of multiply imputed datasets (using mice) for which I calculated IPTW using the weightThem package:

weights_mice <- weightthem( formula =  as.formula(paste("intervention_status ~ ", paste(names(covs), collapse="+"), paste("+ moh_treat_prev_year"))), 
                  datasets = test_mice_2,
                  approach = "within",
                  method = "glm",
                  estimand = "ATT")

fit_dichot_mice_adjusted <- with(weights_mice,coxph(Surv(survival_time, event_status) ~  intervention_status,
                                           robust = T,
                                           cluster = ID,
                                           ties = "efron"))


tbl_regression(fit_dichot_mice_adjusted, 
      exponentiate = T,
      pvalue_fun = label_style_pvalue(digits = 2),
      show_single_row = "intervention_status",
      label = list(intervention_status = "5 < treatments")) %>% 
      add_n() %>% 
      add_nevent()

The tbl_regression output shows a different N (4092) and event N (46) than the actual data. weighted model regression table

This occurs whether or not the regression model object is a mimira or a simple coxph object. I've dug into the model objects and the N and Nevent variables are correct.mimira levels The only constant seems to be that the error occurs when the inputted model is weighted. If not - the numbers are correct:

fit_dichot_mice_unadjusted <-  coxph(Surv(survival_time, event_status) ~  intervention_status,
                           robust = T,
                           cluster = ID,
                           ties = "efron",
                           data = iptw_test)

tbl_regression(fit_dichot_mice_unadjusted,
                           exponentiate = T,
                           pvalue_fun = label_style_pvalue(digits = 2),
                           show_single_row = "intervention_status",
                           label = list(intervention_status = "5 < treatments")) %>% 
                           add_n() %>% 
                           add_nevent()

unweighted model regression table

Any help would be greatly appreciated.

Share Improve this question asked Mar 26 at 21:54 Y.SilvermanY.Silverman 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you have weighted and/or clustered data, the definition of what `"N"` means is often different depending on the setting. You can read about weights and N and how they are calculated here: https://larmarange.github.io/broom.helpers/reference/tidy_add_n.html#details

You can use modify_table_body() to add the N and N Event that applies to your exact situation to your regression model summary table.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744124116a232383.html

最新回复(0)