chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
53 stars 28 forks source link

summary 'quantile' fails for distribution without quantile function #186

Closed stephematician closed 3 months ago

stephematician commented 3 months ago

Incorrect construction of q from qgeneric leads to issues with summary(type='quantile') for certain custom distributions.

This is an example based on a test in testthat/test-custom.R;

  # in actual test, the argument rate has default rate=1, simply remove it
    hfoo <- function(x, rate){ rate }
    Hfoo <- function(x, rate){ rate*x }
    custom.foo <- list(name="foo", pars=c("rate"), location="rate", transforms=c(log), inv.transforms=c(exp), inits=function(t)1/median(t))
    fitf <- flexsurvreg(Surv(futime, fustat) ~ 1, data = ovarian, dist=custom.foo, dfns=list(h=hfoo, H=Hfoo))
  # ...
    summary(fit, type='quantile')
Error in H(q, ...) : argument "rate" is missing, with no default

The reason it fails is that that arguments are not forwarded to qgeneric when it is built.

Similarly, integrate.dh fails in the case of density-only even when the default argument is available:

    custom.baz <- list(name="baz", pars=c("rate"), location="rate", transforms=c(log), inv.transforms=c(exp), inits=function(t)1/mean(t))
    dbaz <- function(x, rate=1, log=FALSE){
        if (log) {log(rate) - x*rate} else rate*exp(-rate*x)
    }
    dbaz <- Vectorize(dbaz)
    fitf <- flexsurvreg(Surv(futime, fustat) ~ 1, data = ovarian, dist=custom.baz, dfns=list(d=dbaz))
  # ...
    summary(fit, type='quantile')
Error in args[[i]] : subscript out of bounds