adibender / pammtools

Piece-wise exponential Additive Mixed Modeling tools
https://adibender.github.io/pammtools/
Other
47 stars 11 forks source link

`as_ped`: status variable can not be of type factor #220

Open jonas-schropp opened 2 years ago

jonas-schropp commented 2 years ago

When I try to use a factor variable for the status in as_ped, the code errors out. I tested this only with competing risk data where using factor or character variables for the status can make the data easier to understand.

While I think this makes some sense (eg to avoid considering the wrong factor level as censoring), the error message is not especially clear and it is somewhat unexpected because the survival package allows for factors (and I guess this is the package most users are familiar with).

Below is an example:

library(pammtools)

dat <- data.frame(
  status_fct = factor(c(0, 0, 0, 0, 1, 1, 1, 2, 2, 2)),
  time = seq(from = 10, to = 100, by = 10)
)

ped_dat <- as_ped(
  data = dat,
  formula = Surv(time, status_fct) ~ group
)

and this is the result:

Error in if (!any(1L * (unique(data[[outcome_vars[length(outcome_vars)]]])) ==  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(1L, (unique(data[[outcome_vars[length(outcome_vars)]]]))) :
  ‘*’ not meaningful for factors

I think a more helpful error message would already improve the situation, but maybe adding the functionality would make it more intuitive to use.