stan-dev / rstanarm

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

stan_jm with offset + assoc causes invalid stanfit object (dimension mismatch) #516

Closed jgabry closed 3 years ago

jgabry commented 3 years ago

@sambrilleman @pamelanluna When fixing #497 I discovered the following:

This model with an offset works

d <- pbcLong
d$off <- 1
fit <- stan_jm(formulaLong = logBili ~ year + (year | id) + offset(off), 
               dataLong = d,
               formulaEvent = Surv(futimeYears, death) ~ sex + trt, 
               dataEvent = pbcSurv,
               time_var = "year")

but this one where I specify assoc causes an invalid stanfit object due to a dimension mismatch:

d <- pbcLong
d$off <- 1
fit <- stan_jm(formulaLong = logBili ~ year + (year | id) + offset(off), 
               dataLong = d,
               formulaEvent = Surv(futimeYears, death) ~ sex + trt, 
               dataEvent = pbcSurv,
               time_var = "year", 
               assoc = "muauc")
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=y1_offset_auc; position=0; dims declared=(629); dims found=(9435)  (in '/data/data_assoc.stan' at line 127; included from 'model_jm' at line 39)

failed to create the sampler; sampling not done
Error in check_stanfit(stanfit) : 
  Invalid stanfit object produced please report bug

This happens with other values of assoc also, e.g. assoc = c("etavalue", "etaauc").

Any idea what's going on here?

sambrilleman commented 3 years ago

I've not had time to look at this, but my first impression is that the dimensions are out by a factor of 15, which is the expansion of the 15 quadrature nodes.

The fact that the declared dimensions is smaller than the actual dimensions suggests that it might just be the declared dimensions that are actually wrong. But won't really know until I take a closer look, or if @pamelanluna has time to.

I can take a look at the codebase, but I really struggle to test changes to the code at the moment, since I don't have a developer environment set up for rstanarm anymore and the install from source takes so long. But will try check it out when I'm back at a computer this arvo.

sambrilleman commented 3 years ago

Sweet, think I've found it just looking at the code. It is that declared dimension issue.

The lines 127 through 129 here: https://github.com/stan-dev/rstanarm/blob/d13296df93afe5dd07d9d79429aa40d513754abe/src/stan_files/data/data_assoc.stan#L127

I think they should be using nrow_y_Xq_auc instead of nrow_y_Xq[1], nrow_y_Xq[2], and nrow_y_Xq[3].

As an aside, interestingly the _auc version is just a scalar and not a length 3 vector, but that must be intentional. I can't remember why, but perhaps because only one submodel can have an auc association structure at any one time or something.

sambrilleman commented 3 years ago

Would you be able to try that fix @jgabry if you have time? And see if it fixes the build/tests?

jgabry commented 3 years ago

Thanks Sam! I'll give that a try and let you know.

On Fri, May 7, 2021 at 9:04 PM Sam Brilleman @.***> wrote:

Would you be able to try that fix @jgabry https://github.com/jgabry if you have time? And see if it fixes the build/tests?

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

jgabry commented 3 years ago

That did the trick! I'll make a PR with the fix.

sambrilleman commented 3 years ago

Epic, thanks!!

On Sat, 8 May 2021, 13:54 Jonah Gabry, @.***> wrote:

That did the trick! I'll make a PR with the fix.

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