chjackson/flexsurv

Automatically Extracting Parameters for Prediction Interval

Closed this issue · 3 comments

I have a prediction model which I made using flexsurvmix (plus some extra math on the side; things were a bit weird since I'm ultimately predicting the number of cases that end in "death" in a certain time period), and now I'm trying to create prediction intervals to go with it. It seems to me that bootstrapping would be a sensible way to get prediction intervals for this, but I have a problem; I know how to automatically re-run flexsurvmix on, say, 500 different samples, but I don't know how automatically extract the model parameters that are estimated so I can feed them into more code.

The closest I've managed thus far is "modelname[2]" (where modelname is a stand-in for the name of the variable I've stored the flexsurvmix model in), which will give me the matrix of parameters without also giving me commentary on the AIC and whatnot. However, I haven't managed to figure out a way to subset that matrix to extract, say, the shape parameter for one of the Weibull distributions I fit to the data. Is there any way to do so that you're aware of?

modelname$res is preferred to modelname[2], as described in help(flexsurvmix) section "Value". This is a data frame, not a matrix. How to subset a data frame is a generic R question, not specifically about flexsurv. If you prefer base R this would be something like

res <- modelname$res
res$est[res$component==1 & res$terms=="shape"]

Or if you prefer tidyverse, this is something like

library(dplyr)
res %>% filter(component==1, terms=="shape") %>% pull(est)

Thank you, I didn't know that I could use modelname$res. I had tried subsetting the results of modelname[2] like a dataframe but that didn't work; modelname$res worked like a charm.

Oh I see your confusion now. There's a difference between the [ and [[ operators for subsetting lists in R. modelname[2] returns a list with one component, while modelname[[2]] returns the component itself. So you could also have subsetted modelname[[2]] in the same way as modelname$res.