MSKCC-Epi-Bio / tidycmprsk

https://mskcc-epi-bio.github.io/tidycmprsk
GNU Affero General Public License v3.0
21 stars 6 forks source link

crr() function does not work properly #109

Closed t-vinn closed 4 months ago

t-vinn commented 4 months ago

Hi, I am conducting a competing risk analysis on a large registry database and recently have experienced some troubles using crr() function in tidycmprsk. Every time I run the crr() function, it works for the first time. Then, starting second time, it always end up in the following error message.

crr(Surv(time, status) ~ group, failcode = "PTLD", cencode = "censor", data = df_surv) 
#> Error in crr(Surv(time, status) ~ group, failcode = "PTLD", cencode = "censor", : could not find function "crr"

Created on 2024-06-18 with reprex v2.1.0 In tidycmprsk, status value in crr() function should be a factor, so I cannot change the status value to a logical or numeric one. crr() function from cmprsk works properly so I assume there is a conflict issue between tidycmprsk and other packages. Any ideas?

ddsjoberg commented 4 months ago

Dear @t-vinn , we can help you troubleshoot, but you'll first need to create a minimal reproducible example to illustrate the issue (aka a reprex). If you haven't created a reprex before, spend a couple of minutes reviewing these instructions, and you'll be an expert reprex creator. https://reprex.tidyverse.org/

t-vinn commented 4 months ago

In the end, it turned out I made a simple mistake. After using "read_csv," the status column was changed from a factor to a character type, causing the error. Using reprex helped me identify and resolve the issue. Thank you!

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(readr)
library(tidycmprsk)
data(Melanoma, package = "MASS")

Melanoma <- 
  Melanoma %>% 
  mutate(
    status = as.factor(recode(status, `2` = "censor", `1` = "death", `3` = "event")),
  )
crr(Surv(time, status) ~ age, data = Melanoma)
#> 
#> ── crr() ───────────────────────────────────────────────────────────────────────
#> • Call Surv(time, status) ~ age
#> • Failure type of interest "death"
#> 
#> Variable   Coef    SE      HR     95% CI       p-value    
#> age        0.015   0.009   1.02   1.00, 1.03   0.11

write_csv(Melanoma, "Melanoma.csv")
new_data <- read_csv("Melanoma.csv")
#> Rows: 205 Columns: 7
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): status
#> dbl (6): time, sex, age, year, thickness, ulcer
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
crr(Surv(time, status) ~ age , failcode = "event", cencode = "censor", data = new_data) # -> error
#> ✖ There was an error evaluating the LHS of the formula.
#> Error: Error in Surv(time, status): Invalid status value, must be logical or numeric

new_data <- read_csv("Melanoma.csv") %>% mutate(status = as.factor(status))
#> Rows: 205 Columns: 7
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): status
#> dbl (6): time, sex, age, year, thickness, ulcer
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
crr(Surv(time, status) ~ age , failcode = "event", cencode = "censor", data = new_data) # -> success
#> 
#> ── crr() ───────────────────────────────────────────────────────────────────────
#> • Call Surv(time, status) ~ age
#> • Failure type of interest "event"
#> 
#> Variable   Coef    SE      HR     95% CI       p-value    
#> age        0.059   0.014   1.06   1.03, 1.09   <0.001

Created on 2024-06-18 with reprex v2.1.0