hputter / mstate

https://hputter.github.io/mstate/
7 stars 5 forks source link

crprep: wrong calculation and no warning if data is a tibble #18

Closed PStaus closed 2 years ago

PStaus commented 2 years ago

Thank you for the great package.

Problem: When the data argument for crprep is a tibble the calculation is done wrongly. But there is no warning.

It's OK that tibbles are not supported, but maybe a warning would be nice. I only found out by accident.

reproducible example:

library(tibble)
library(survival)
library(mstate)

# creating test data

testdata <- data.frame(exit = rexp(200), 
                       to = sample(1:3,200,replace=TRUE), 
                       entry = 0, 
                       from = 0,  
                       id = 1:200)
cov <- matrix(runif(600), nrow=200)
covariates <- c('x1','x2','x3')
dimnames(cov)[[2]] <- covariates
testdata<-cbind(testdata, cov)
# Test with data.frame
fg_data <- crprep(Tstop = "exit", status = "to", 
                  data = testdata[testdata$from == 0, ], 
                  trans = c("1", "2","3"), cens = "cens",
                  Tstart = "entry", id = "id",
                  keep = covariates
)

str(fg_data)

testtibble<- as_tibble(testdata)
fg_tibble <- crprep(Tstop = "exit", status = "to", 
                    data = testtibble[testtibble$from == 0, ], 
                    trans = c("1", "2","3"), cens = "cens",
                    Tstart = "entry", id = "id",
                    keep = covariates
)
str(fg_tibble)
edbonneville commented 2 years ago

Sorry for delay in responding, and thank you for reporting @PStaus ! It should be fixed in the development version - any different data formats (tibble, data.table etc.) should be coerced to simple data.frames inside crprep()