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

pooling flexsurv models trained on imputations with mice - no glance method #52

Closed Geoff-Holmes closed 6 years ago

Geoff-Holmes commented 6 years ago

I am trying to combine models trained using flexsurvspline() on different imputations of a dataset with missing data.

I can pass a list of models to the pool() function from the mice package but get the error: Error: No glance method for objects of class flexsurvreg

Is this something that can be easily overcome? Thanks.

chjackson commented 6 years ago

I'm not familiar with the mice package. It's plausible that flexsurvreg provides the ingredients for someone who wants to implement this method though. A quick search suggests that glance() is from "broom", a tidyverse package for getting the results of fitted models into tidy format. I think that would be a good thing to add for flexsurv. Do you fancy having a go? Otherwise I'll put it on my long term todo list.

Geoff-Holmes commented 6 years ago

Thanks. I briefly created tidy() and glance() functions locally on my machine, following the basic ideas in broom. The pool() then ran successfully. I haven't attempted to contribute to someone else's repo before. Do I create a fork or something?

I hoped that having pooled the separate models I would have a usable flexsuvreg model. However, I just get a mipo object containing the coefficients and standard errors. So I am back at the point where I contacted you by email some time ago, wondering how I can create a pooled flexsurvreg model. My previous idea (that you rightly cautioned against) was to overwrite the mod$res (and maybe mod$res.t) components. If would be nice to create the model, not by estimating it from data, but by specifying the coefficients and standard errors directly - could that be possible?

chjackson commented 6 years ago

If you send me your tidy() and glance() functions I'll have a look, as a way to teach myself about broom, which I've been meaning to learn.

If you know what you're doing you could overwrite $res and $res.t, but you should also look through the list of other components in the fitted model object which contain parameter estimates and their SEs/covariance. For example there's also $opt$par and $cov, which are used by normboot.flexsurvreg.

Geoff-Holmes commented 6 years ago

Perhaps not surprisingly, there is not too much to the output of these functions. They could be augmented further but I just did enough for now to get what I needed working, so I'm afraid you won't learn a lot from them!

glance.flexsurvreg<-function(x, ...) { tibble( logLik=x$loglik, AIC=x$AIC, df.residual=x$npars ) }

tidy.flexsurvreg<-function(x, ...) { tibble( term=names(x$coefficients), estimate=x$coefficients, std.error=x$res[,4] ) }