Rstanarm survival - random effects

The survival branch of rstanarm is excellent and the Brilleman et al paper has really accelerated my ability and understanding of survival models, so thanks to all who worked so hard on this branch.

My survival model is estimating a right censored response based on several fixed effects and a single random effect. According to the Brilleman paper, the ranef() function should return the random effects from the model object, but when I try to do this I receive: Error: This method is for stan_glmer and stan_lmer models only.

One other question I have pertains to using posterior_survfit() with newdata that contains a random effect level that wasn’t present in the training set. I know in lme4 the predict() function has which if set to TRUE can handle this case. Is there an analogous argument that can be passed, or another way to handle? Thanks!

  • Operating System: ubuntu 18.04
  • rstanarm Version: feature/survival
1 Like

Yeah I think this was a minor bug that should hopefully have been fixed on a recent PR.

But we would need to rebuild the hosted binary for you to be able to easily install the version with that fix.

@rok_cesnovar @jonah … do you know if it is easy for us to rebuild and host an updated binary for the feature/survival branch? Or is it a manual task?

I don’t think you need to specify an additional argument in posterior_survfit. Rather, I think you just need to make sure the prediction data (i.e. newdata) uses group id values that weren’t present in the original estimation data. For example you could call your new IDs / groups something like new_1, new_2 etc in the newdata. Then the predictions for each new group should be marginalised over the dist of random effects. That behaviour should be similar to what is described for stan_jm’s posterior_survfit method in the ?posterior_survfit docs I think.

Thanks for the info on the first issue.

On the second issue, I initially tried to pass data with a new level (1248 in agent_id) but the function threw this error. The warning is also confusing because I explicitly convert agent_id to a factor in the training and prediction sets:

> posterior_survfit(fit, newdata=pred_data)
Error in model.frame.default(Terms, data, xlev = xlevs, drop.unused.levels = drop.unused.levels,  : 
  factor agent_id has new level 1248
In addition: Warning messages:
1: In model.frame.default(Terms, data, xlev = xlevs, drop.unused.levels = drop.unused.levels,  :
  variable 'agent_id' is not a factor```

Should be doable. Will give it a try this week.

Yeah this sounds buggy. Also, sorry that I’m only getting to reply now!

It seems that there are some issues around handling the grouping variable when it was a factor data type in the training fitting. I have got tests here that check posterior_survfit but they only test it with numeric data types for the grouping variable, and not factors.

After a bit of digging I’ve actually found a related Github issue, that discussed the same problem for character variables. I’ve updated it to also include factor variables in the example.

Unfortunately it is from August 2020 and still hasn’t been fixed!! :-(

The quick solution seems to be to use numeric data type for your agent_id variable. Sorry that I wasn’t able to reply with that quick fix earlier for you! (hopefully you managed to find it)

Longer term, I hope we can fix up the handling of factors/characters. But not sure when that work will get done.

1 Like

Thanks Sam, converting to numeric worked. Appreciate the help!