chjackson / flexsurv

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

No 'nobs' method for flexsurvreg #70

Closed Chris-Angell closed 4 years ago

Chris-Angell commented 4 years ago

I am doing some survival modelling and I decided to use flexsurv because unlike survival, it treats the ancillary parameters like linear models with an intercept and various coefficients instead of fitting a separate value to each stratum. One of the benefits I thought this would have is that it would make the models amenable to model averaging via the MuMIn package.

However, the MuMIn functions 'dredge', 'model.sel', and 'model.avg' throw an error when I try to run them using flexsurvreg models: Error in nobs.default(global.model) : no 'nobs' method is available. Presumably the value it's looking for is the one given by model$N. Is it possible to include a 'nobs' method for flexsurvreg models? These functions work just fine on 'survreg' models.

Also is there a workaround? Could I define my own 'nobs' method??

Thanks, Chris

Chris-Angell commented 4 years ago

Haha, moments later I solved my own problem:

nobs.flexsurvreg <- function(object, ...) {object$N}

Perhaps this (or something like it) could be included by default!

chjackson commented 4 years ago

Thanks for the suggestion - this is now included in https://github.com/chjackson/flexsurv-dev/commit/229215b8970ba1346484e2b978a3535156fdafeb

Chris-Angell commented 4 years ago

BTW, adding a method for the MuMIn function 'coefTable' is also necessary for full compatibility with its model selection & model averaging functions. Here's what I wrote that got them to work as expected:

coefTable.flexsurvreg <- function(model, ...) { x <- as.data.frame(model$res[,c("est","se")]) colnames(x) <- c("Estimate", "Std. Error") x }

Of the other internal model utility functions in MuMIn, 'coeffs' and 'getAllTerms' automatically work with flexsurvreg objects (that is, they appear to work the same as with survreg models, which are officially supported by MuMIn). The following method for 'get.response' appears to bring this function in line with its behavior for survreg, though I'm not sure where this function is used:

get.response.flexsurvreg <- function(x, ...) {x$data$m[,1]}

I hope that's useful to you!