1

I am trying to calculate cumulative incidence in R. I used previously [published][1] code that worked perfectly on the R-related dataset but it didn't work on my dataset.

Here is my code

## Published code ##
library(tidycmprsk); library(ggsurvfit)
cuminc(Surv(ttdeath, death_cr) ~ trt, trial) %>%  tidy_cuminc(time=c(12,24))

## My modified code as I am dealing with a large dataset ##
################ I used tidycmprsk::cuminc ################
d<-subset(data, data$CKD =="No",  select=c(id, t2event_compCVD.MI.Hosp, compCVD.MI.Hosp, trt ))
str(d) ## My data structure ##
d<-as_tibble(d) ## I tried with and without this line and got the same error
cuminc(Surv(t2event_compCVD.MI.Hosp, compCVD.MI.Hosp) ~ trt, d) %>%  tidy_cuminc(time=c(12,24))

I got the following error;

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"formula"’ to a data.frame

Note about my data structure

str(d)
tibble [5,179 × 3] (S3: tbl_df/tbl/data.frame)
 $ t2event_compCVD.MI.Hosp:Class 'labelled' int [1:5179] 1845 1536 1464 806 1001 584 1975 40 46 1521 ...
   .. .. LABEL: Time (in days) to CV death 
 $ compCVD.MI.Hosp        :Class 'labelled' num [1:5179] 0 0 0 0 0 0 0 1 1 0 ...
   .. .. LABEL: Composite CV death, MI, and hospitalization (primary endpoint) 
 $ trt                    :Class 'labelled' chr [1:5179] "CON" "INV" "CON" "INV" ...

Additionally, I tried to attach data attach(d) but I got the same error.

Here is a sample of my dataset

d<-read.table(text = "id,t2event_compCVD.MI.Hosp,compCVD.MI.Hosp,trt
1,1845,0,CON
2,1536,0,INV
3,1464,0,CON
4,806,0,INV
5,1001,0,CON
6,584,0,CON
7,1975,0,CON
8,40,1,INV
9,46,1,INV
10,1521,0,CON
11,515,0,CON
12,1140,0,INV
13,1269,0,INV
14,1792,0,INV
15,1364,0,CON
16,773,0,CON
17,205,1,CON
18,1479,0,INV", sep=",", header=T)

Update

Here is my session inf:

> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: Africa/Cairo
tzcode source: internal

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

other attached packages:
[1] ggsurvfit_0.3.0   tidycmprsk_0.2.0  etm_1.1.1         mosaicData_0.20.3 ggformula_0.10.4  dplyr_1.1.2      
[7] Matrix_1.5-4.1    ggplot2_3.4.2     lattice_0.21-8   

loaded via a namespace (and not attached):
  [1] splines_4.3.1        later_1.3.0          tibble_3.2.1         cellranger_1.1.0     polyclip_1.10-4     
  [6] hardhat_1.3.0        lifecycle_1.0.3      gtsummary_1.7.1      rstatix_0.7.2        globals_0.16.2      
 [11] processx_3.8.1       MASS_7.3-60          Publish_2023.01.17   backports_1.4.1      magrittr_2.0.3      
 [16] vcd_1.4-11           metafor_4.2-0        plotly_4.10.2        sass_0.4.5           rmarkdown_2.21      
 [21] jquerylib_0.1.4      remotes_2.4.2        httpuv_1.6.9         sp_1.6-0             sessioninfo_1.2.2   
 [26] pkgbuild_1.4.1       minqa_1.2.5          lubridate_1.9.2      abind_1.4-5          pkgload_1.3.2       
 [31] purrr_1.0.1          nnet_7.3-19          tweenr_2.0.2         labelled_2.11.0      lava_1.7.2.1        
 [36] listenv_0.9.0        parallelly_1.35.0    codetools_0.2-19     xml2_1.3.4           ggforce_0.4.1       
 [41] tidyselect_1.2.0     farver_2.1.1         lme4_1.1-33          base64enc_0.1-3      mathjaxr_1.6-0      
 [46] broom.helpers_1.13.0 jsonlite_1.8.4       e1071_1.7-13         ellipsis_0.3.2       ggridges_0.5.4      
 [51] survival_3.5-5       tools_4.3.1          cmprsk_2.2-11        Rcpp_1.0.10          glue_1.6.2          
 [56] mnormt_2.1.1         prodlim_2023.03.31   laeken_0.5.2         xfun_0.39            ranger_0.15.1       
 [61] usethis_2.2.0        withr_2.5.0          numDeriv_2016.8-1.1  fastmap_1.1.1        boot_1.3-28.1       
 [66] fansi_1.0.4          VIM_6.2.2            callr_3.7.3          digest_0.6.31        timechange_0.2.0    
 [71] R6_2.5.1             mime_0.12            colorspace_2.1-0     markdown_1.6         utf8_1.2.3          
 [76] tidyr_1.3.0          generics_0.1.3       Amelia_1.8.1         data.table_1.14.8    radiant.data_1.5.6  
 [81] robustbase_0.95-1    class_7.3-22         prettyunits_1.1.1    httr_1.4.5           htmlwidgets_1.6.2   
 [86] pkgconfig_2.0.3      gtable_0.3.3         lmtest_0.9-40        htmltools_0.5.5      carData_3.0-5       
 [91] profvis_0.3.8        scales_1.2.1         png_0.1-8            knitr_1.42           rstudioapi_0.14     
 [96] tzdb_0.3.0           nlme_3.1-162         mosaic_1.8.4.2       curl_5.0.0           nloptr_2.0.3        
[101] shinyAce_0.4.2       proxy_0.4-27         cachem_1.0.7         zoo_1.8-12           stringr_1.5.0       
[106] parallel_4.3.1       miniUI_0.1.1.1       metadat_1.2-0        foreign_0.8-84       pillar_1.9.0        
[111] grid_4.3.1           vctrs_0.6.2          urlchecker_1.0.1     promises_1.2.0.1     ggpubr_0.6.0        
[116] car_3.1-2            shinyFiles_0.9.3     xtable_1.8-4         evaluate_0.20        readr_2.1.4         
[121] meta_6.2-1           cli_3.6.1            compiler_4.3.1       rlang_1.1.1          crayon_1.5.2        
[126] future.apply_1.10.0  ggsignif_0.6.4       randomizr_0.24.0     ps_1.7.5             forcats_1.0.0       
[131] fs_1.6.2             writexl_1.4.2        stringi_1.7.12       psych_2.3.3          viridisLite_0.4.2   
[136] munsell_0.5.0        lazyeval_0.2.2       devtools_2.4.5       CompQuadForm_1.4.3   mosaicCore_0.9.2.1  
[141] hms_1.1.3            patchwork_1.1.2      future_1.32.0        shiny_1.7.4          haven_2.5.2         
[146] import_1.3.0         gt_0.9.0             broom_1.0.4          memoise_2.0.1        bslib_0.4.2         
[151] DEoptimR_1.0-13      readxl_1.4.2         ggstance_0.3.6  ```

**Any advice will be greatly appreciated**

  [1]: https://rdrr.io/cran/ggsurvfit/man/tidy_cuminc.html
Mohamed Rahouma
  • 1,084
  • 9
  • 20
  • It doesn’t appear to me that you are reading the help page for `cuminc` correctly. – IRTFM Jul 30 '23 at 05:28
  • @IRTFM Thx for your reply. I read it but I didn't get the solution yet. I put the event variable named `compCVD.MI.Hosp` as a factor with `0` as censored, as they mentioned, but this didn't work as well. Any advice is greatly appreciated. – Mohamed Rahouma Jul 30 '23 at 06:31
  • 1
    Neither the first nor the second argument to `cuminc` is documented to be a formula. Additionally you are using an object named "data" but I nowhere see where it is defined, so no coding solution can be attempted. – IRTFM Jul 30 '23 at 16:07
  • 1
    Multiple versions of `cumic` with completely different args. I saw the docs for `cmprsk::cumic`. Maybe your code was using that, too, despite the `library` calls to `tidycmprsk`. Without looking at the output of `sessionInfo()` and perhaps also examining the search path to see whether `tidycmprsk::cumic` was masked by `cmprsk::cumic`, I'm unable to explain the error. After `d<-subset(data, data$CKD =="No", select=c(id, t2event_compCVD.MI.Hosp, compCVD.MI.Hosp, trt ))` I now get `Error in eval(substitute(select), nl, parent.frame()) : object 't2event_compCVD.MI.Hosp' not found` – IRTFM Jul 30 '23 at 19:23
  • @IRTFM Thanks for your efforts. I edited my question to include `session.info()` and mentione dthat I used `tidycmprsk::cumic`. Upvoted. Appreciate. – Mohamed Rahouma Jul 31 '23 at 14:47
  • I still do not see where you are showing us anything about the `data` object. In a blank R session, `data` is a base function. – IRTFM Jul 31 '23 at 14:53
  • @IRTFM Thanks. `data` is the name of my dataframe and it has over 150 variables, so I can't show it here. I just added a sample of my dataset. Appreciate. ``` class(data) [1] "rowwise_df" "tbl_df" "tbl" "data.frame"``` – Mohamed Rahouma Aug 02 '23 at 04:54

0 Answers0