chjackson / flexsurv

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

`predict.flexsurvreg()` without covariates #146

Closed ndunnewind closed 1 year ago

ndunnewind commented 1 year ago

In version 2.1, the quantile example in the predict.flexsurvreg() documentation works as expected when using a model without covariates.

fitg <- flexsurvreg(formula = Surv(futime, fustat) ~ 1, data = ovarian, dist = "gengamma")
predict(fitg, type = "quantile", p = 0.5)
#> # A tibble: 26 x 2
#>    .quantile .pred
#>        <dbl> <dbl>
#>  1       0.5  909.
#>  2       0.5  909.
#>  3       0.5  909.
#>  4       0.5  909.
#>  5       0.5  909.
#>  6       0.5  909.
#>  7       0.5  909.
#>  8       0.5  909.
#>  9       0.5  909.
#> 10       0.5  909.
#> # ... with 16 more rows

However, version 2.2 returns only a single row:

#> # A tibble: 1 x 2
#>   .quantile .pred_quantile
#>       <dbl>          <dbl>
#> 1       0.5           909.

and raises an error with multiple quantiles (e.g. default quantiles c(0.1, 0.9)):

predict(fitg, type = "quantile")
#> Error in `dplyr::mutate()`:
#> ! Problem while computing `.id = rep(1:orig_nrow, each = num_reps)`.
#> x `.id` must be size 2 or 1, not 52.

#> Backtrace:
#>      x
#>   1. +-stats::predict(fitg, type = "quantile")
#>   2. +-flexsurv:::predict.flexsurvreg(fitg, type = "quantile")
#>   3. | +-dplyr::mutate(res, .id = rep(1:orig_nrow, each = num_reps))
#>   4. | \-dplyr:::mutate.data.frame(res, .id = rep(1:orig_nrow, each = num_reps))
#>   5. |   \-dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
#>   6. |     +-base::withCallingHandlers(...)
#>   7. |     \-mask$eval_all_mutate(quo)
#>   8. +-dplyr:::dplyr_internal_error(...)
#>   9. | \-rlang::abort(class = c(class, "dplyr:::internal_error"), dplyr_error_data = data)
#>  10. |   \-rlang:::signal_abort(cnd, .file)
#>  11. |     \-base::signalCondition(cnd)
#>  12. \-dplyr (local) `<fn>`(`<dpl:::__>`)
#>  13.   \-rlang::abort(...)
chjackson commented 1 year ago

I think I've now fixed this in https://github.com/chjackson/flexsurv-dev/commit/5bb67e4512f3c423a184ce0e427bc8df07aedbc3 (though @mattwarkentin if you could check this still does what you expect, that'd be great.)

mattwarkentin commented 1 year ago

@chjackson I just wanted to let you know that I have seen this message and I will check things out shortly to see if I have any comments.

mattwarkentin commented 1 year ago

Looks good to me! Assuming all previous and new tests pass I think everything should be good.