Search code examples
rsurvival

survival::tmerge() giving as.Date.numeric(value) error


I'm trying to tmerge some data together to do some survival analysis, but I keep getting an error.

library(survival)
library(lubridate)

df1 <- data.frame(id = "62103",
                  hire.date = ymd("2016-05-16"),
                  end.date = ymd(Sys.Date())
)
job <- data.frame(id = c("62103", "62103"),
                  job1 = c("level 1 coder", "level 2 coder"),
                  start.date = c(ymd("2016-05-16"), ymd("2017-05-16")),
                  end.date = c(ymd("2017-05-16"), NA)
)

df2 <- tmerge(df1, df1, id = id,
              tstart = hire.date,
              tstop = end.date)
df3 <- tmerge(df2, job, id = id,
              job = tdc(start.date, job1)
)

This throws the error:

Error in as.Date.numeric(value) : 'origin' must be supplied

Here is some information about my session:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] lubridate_1.7.4   survival_2.44-1.1

loaded via a namespace (and not attached):
 [1] compiler_3.6.1  magrittr_1.5    Matrix_1.2-17   tools_3.6.1    
 [5] Rcpp_1.0.2      stringi_1.4.3   splines_3.6.1   grid_3.6.1     
 [9] knitr_1.24      stringr_1.4.0   xfun_0.9        lattice_0.20-38

Solution

  • It appears the issue with tdc is that some date math does not preform correctly. Converting (back and forth?) to numeric solves this.

    library(survival)
    library(lubridate)
    library(tidyverse)
    df1 <- data.frame(id = "62103",
                      hire.date = ymd("2016-05-16"),
                      end.date = ymd(Sys.Date())
    ) %>% 
      mutate_if(is.Date, as.numeric)
    job <- data.frame(id = c("62103", "62103"),
                      job1 = c("level 1 stacker", "level 2 stacker"),
                      start.date = c(ymd("2016-05-16"), ymd("2017-05-16")),
                      end.date = c(ymd("2017-05-16"), NA)
    )%>% 
      mutate_if(is.Date, as.numeric)
    
    df2 <- tmerge(df1, df1, id = id,
                  tstart = hire.date,
                  tstop = end.date)
    df3 <- tmerge(df2, job, id = id,
                  job = event(start.date, job1)
    ) %>%
      mutate_if(is.numeric, as.Date, origin = "1970-01-01")