stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
383 stars 132 forks source link

ranef() missing for stan_surv model #440

Open e5c opened 4 years ago

e5c commented 4 years ago

Summary:

Hello, I was hoping to see if ranef() is available in any branch (or will be soon) for multilevel stan_surv objects? The documentation states that it is there but calling it errors out (I'm on rstanarm 2.19.1):

> ranef(multilevel_model)

Error: This method is for stan_glmer and stan_lmer models only.
Traceback:

1. ranef(multilevel_model)
2. ranef.stanreg(multilevel_model)
3. .glmer_check(object)
4. stop("This method is for stan_glmer and stan_lmer models only.", 
 .     call. = FALSE)

In case it's helpful, here's the model formula:

Surv(time_to_status_days_float, Expired) ~ treatment + Sex + age_ordinal + DNR + (1 | LocationName)

RStanARM Version:

2.19.1

R Version:

3.6.3

Operating System:

OS X 10.14.2

sambrilleman commented 4 years ago

Woops, I must have not implemented it and forgotten to write a test to catch that.

This is hopefully easy to fix for a future release. But in the meantime you should be able to get the same result from the parameter summary object? Or taking the median of the relevant parameters in the trace?

Something like rstanarm::summary(my_model) or rstan::summary(my_model$stanfit)? I can't remember whether my_model$stan_summary includes or excludes the random effects.

e5c commented 4 years ago

@sambrilleman Yes they're included in my_model$stan_summary and I can access them that way. Good to know re. a fix for future release, thanks!

In case it's ok to add onto this thread (I'll happily post elsewhere with this question, e.g. Discourse, if you can lmk if that's more appropriate): when using loo and posterior_survfit with the above multilevel_model(and all other multilevel models) I get this warning:

> loo_ml <- loo(multilevel_model, k_threshold = 0.7)

Warning message in model.frame.default(Terms, data, xlev = xlevs, drop.unused.levels = drop.unused.levels, :
“variable 'LocationName' is not a factor”

and

> mm_ps_treatment0 <- posterior_survfit(multilevel_model, ids = treatment0,  standardise = TRUE, times = 0)
# (here there are so many warnings that i just turn them off)

Could you explain what this warning indicates?

Thanks, Eliza

sambrilleman commented 4 years ago

It probably means that in the data / model frame used in the model estimation the grouping variable (LocationName) what a factor variable. But in the prediction data it isn't getting converted to a factor but must be a string or integer or something. It still works, but since the prediction variable isn't of the same class as variable used in the model fitting R issues a warning.

Where you say here there are so many warnings that i just turn them off... are they all the same warning as the previous one? (i.e. something like Warning message in model.frame.default(Terms, data, xlev = xlevs, drop.unused.levels = drop.unused.levels, “variable 'LocationName' is not a factor”).

It would help to have a reproducible example to help debug. But I realise it you might not be able to share the data. If not, can you create a small fake dataset that generates the same issue?

sambrilleman commented 4 years ago

Btw, if you are able to post a reprex to demonstrate those warnings, it would be handy if you are able to post it as a separate issue. It will be easier to track the issues that way (i.e. the fix for ranef can be separate from the fix for the warnings about the grouping factor). Cheers!

sambrilleman commented 4 years ago

Hopefully the missing ranef() method is resolved by this commit: https://github.com/stan-dev/rstanarm/commit/d8fea177ddc95671bfb8d8be7060b44c0de839cb

But it will have to be tested before closing this issue (I can't currently build rstanarm so can't test it yet).

jgabry commented 4 years ago

(I can't currently build rstanarm so can't test it yet).

Hmm you still can't build it even after the survival branch was updated from master? Not sure if @bgoodri is monitoring this conversation, but @sambrilleman you might try reaching out to him separately. I don't know why rstanarm won't build, but I think it should.

bgoodri commented 4 years ago

It might build from source on R for Windows 4.0 now that the LTO stuff is in. I haven't been able to do it on the Windows computer in my office for 32bit (64bit works if you pass --no-multiarch), although both 32bit and 64bit somehow work on CRAN.

On Mon, Aug 10, 2020 at 2:08 PM Jonah Gabry notifications@github.com wrote:

(I can't currently build rstanarm so can't test it yet).

Hmm you still can't build it even after the survival branch was updated from master? Not sure if @bgoodri https://github.com/bgoodri is monitoring this conversation, but @sambrilleman https://github.com/sambrilleman you might try reaching out to him separately. I don't know why rstanarm won't build, but I think it should.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstanarm/issues/440#issuecomment-671505627, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKUU3YBM5N557ZAHYWTSAAZRLANCNFSM4NSFXJ3Q .

sambrilleman commented 4 years ago

@jgabry @bgoodri ok thanks both - I will give this another crack tonight.