RobinDenz1 / adjustedCurves

An R-Package to estimate and plot confounder-adjusted survival curves (single event survival data) and confounder-adjusted cumulative incidence functions (data with competing risks) using various methods.
https://robindenz1.github.io/adjustedCurves/
GNU General Public License v3.0
35 stars 4 forks source link

Error in coxModelFrame.coxph(model.event) : invalid object set x=TRUE in the call to coxph #33

Closed YutingHan317 closed 2 months ago

YutingHan317 commented 2 months ago

Hi, I get an error when using direct method. Error is

"Error in coxModelFrame.coxph(model.event) : invalid object set x=TRUE in the call to coxph" What should I do to revise the code? Best wishes Yuting

To reproduce,

# Load packages
library(survival)  
library(adjustedCurves)  

# Generate database  
set.seed(123)  
n <- 200  
df_cox <- data.frame(  
  id = 1:n,  
  highest_education = sample(c("Low", "Medium", "High"), n, replace = TRUE),   
  age_strata = sample(c("Young", "Middle", "Old"), n, replace = TRUE),  
  region_code = sample(c("North", "South", "East", "West"), n, replace = TRUE),  
  alc_wek_7g = sample(c("None", "Light", "Moderate", "Heavy"), n, replace = TRUE),  
  age_at_study_start = round(rnorm(n, 50, 10)),  
  age_at_event_or_censoring = round(rnorm(n, 60, 10)),  
  event_occurred = sample(0:1, n, replace = TRUE),  
  is_female = sample(0:1, n, replace = TRUE)  
)  

# transform
df_cox$highest_education <- as.factor(df_cox$highest_education)  
df_cox$age_strata <- as.factor(df_cox$age_strata)  
df_cox$region_code <- as.factor(df_cox$region_code)  
df_cox$alc_wek_7g <- as.factor(df_cox$alc_wek_7g)  

# person-years
df_cox$ev_time <- df_cox$age_at_event_or_censoring - df_cox$age_at_study_start  

# keep py > 0 
df_cox <- df_cox[df_cox$ev_time >= 0, ]  

# coxph
fit_plot <- coxph(Surv(ev_time, event_occurred) ~ highest_education + age_strata + region_code + alc_wek_7g, data = df_cox)  

# adjusted KM 
adj <- adjustedsurv(data = df_cox,   
                    variable = "alc_wek_7g",  
                    ev_time = "ev_time",  
                    event = "event_occurred",  
                    method = "direct",  
                    outcome_model = fit_plot,  
                    conf_int = TRUE)  

plot(adj)

Session info

R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_China.utf8 [2] LC_CTYPE=Chinese (Simplified)_China.utf8 [3] LC_MONETARY=Chinese (Simplified)_China.utf8 [4] LC_NUMERIC=C [5] LC_TIME=Chinese (Simplified)_China.utf8

time zone: Asia/Shanghai tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] adjustedCurves_0.11.2 dplyr_1.1.4 survival_3.6-4

loaded via a namespace (and not attached): [1] gtable_0.3.5 xfun_0.45 [3] ggplot2_3.5.1 htmlwidgets_1.6.4 [5] lattice_0.22-6 numDeriv_2016.8-1.1 [7] vctrs_0.6.5 tools_4.4.1 [9] generics_0.1.3 sandwich_3.1-0 [11] parallel_4.4.1 tibble_3.2.1 [13] fansi_1.0.6 cluster_2.1.6 [15] pkgconfig_2.0.3 R.oo_1.26.0 [17] Matrix_1.7-0 data.table_1.15.4 [19] checkmate_2.3.1 riskRegression_2023.12.21 [21] rngtools_1.5.2 lifecycle_1.0.4 [23] compiler_4.4.1 stringr_1.5.1 [25] MatrixModels_0.5-3 munsell_0.5.1 [27] codetools_0.2-20 SparseM_1.84 [29] quantreg_5.98 htmltools_0.5.8.1 [31] htmlTable_2.4.2 prodlim_2024.06.25 [33] Formula_1.2-5 pillar_1.9.0 [35] MASS_7.3-60.2 R.utils_2.12.3 [37] cmprsk_2.2-12 rms_6.8-1 [39] Hmisc_5.1-3 doRNG_1.8.6 [41] iterators_1.0.14 multcomp_1.4-25 [43] rpart_4.1.23 foreach_1.5.2 [45] nlme_3.1-164 parallelly_1.38.0 [47] lava_1.8.0 timereg_2.0.5 [49] tidyselect_1.2.1 digest_0.6.36 [51] polspline_1.1.25 mvtnorm_1.2-5 [53] stringi_1.8.4 future_1.34.0 [55] listenv_0.9.1 splines_4.4.1 [57] fastmap_1.2.0 grid_4.4.1 [59] colorspace_2.1-0 cli_3.6.3 [61] magrittr_2.0.3 base64enc_0.1-3 [63] utf8_1.2.4 TH.data_1.1-2 [65] future.apply_1.11.2 foreign_0.8-86 [67] withr_3.0.0 mets_1.3.4 [69] scales_1.3.0 backports_1.5.0 [71] rmarkdown_2.27 globals_0.16.3 [73] nnet_7.3-19 gridExtra_2.3 [75] zoo_1.8-12 R.methodsS3_1.8.2 [77] evaluate_0.24.0 knitr_1.48 [79] rlang_1.1.4 Rcpp_1.0.13 [81] glue_1.7.0 rstudioapi_0.16.0 [83] jsonlite_1.8.8 R6_2.5.1

RobinDenz1 commented 2 months ago

The error message tells you all you need to know here: "set x=TRUE in the call to coxph". So if you change:

fit_plot <- coxph(Surv(ev_time, event_occurred) ~ highest_education + age_strata + region_code + alc_wek_7g, data = df_cox)

to

fit_plot <- coxph(Surv(ev_time, event_occurred) ~ highest_education + age_strata + region_code + alc_wek_7g, data = df_cox,
x=TRUE)

it should work.

YutingHan317 commented 2 months ago

Thank you very much for the prompt reply. It works!!

RobinDenz1 commented 2 months ago

No problem!