Is it possible to specify multiple models in a single STAN model file? If so, how?

502 views
Skip to first unread message

Yaohui Zeng

unread,
Jun 9, 2016, 4:04:49 PM6/9/16
to Stan users mailing list
Hi all,

I am modeling some count data, and wish to try logistic, Poisson, and negative binomial (NB) regression using Bayesian approach. For negative binomial model, I am using gamma-Poisson re-parameterization (like here). My goal is to combine the three different model specification into one .stan file, and switch to different model based on a user-defined option, named "model_form" for example. "model_form = 1, 2, 3"  corresponds to logistic, Poisson and NB, respectively. I just use if-else clause in model block to switch to different models.

So far I can combine logistic and Poisson because the difference is just the link function and I can use if-else clause in model block and keep all other blocks the same. However, for NB model, there is an extra parameter. So the parameter block would be different. But the issue is that I cannot declare different parameters based on if-else conditions.

Would anyone encounter this kind of situation? Just wondering what's the best way to approach this. Any comments are highly appreciated! Thank you very much!

Ben Goodrich

unread,
Jun 9, 2016, 4:25:55 PM6/9/16
to Stan users mailing list
On Thursday, June 9, 2016 at 4:04:49 PM UTC-4, Yaohui Zeng wrote:
So far I can combine logistic and Poisson because the difference is just the link function and I can use if-else clause in model block and keep all other blocks the same. However, for NB model, there is an extra parameter. So the parameter block would be different. But the issue is that I cannot declare different parameters based on if-else conditions.

We have this problem a lot when writing models for rstanarm. A solution is to do something like

data {
 
int<lower=0,upper=1> is_nb;
 
...
}
parameters
{
  real
<lower=0> overdispersion[is_nb];
 
...
}
model
{
 
if (is_nb) {
    y
~ neg_binomial_2_log(eta, overdispersion[1]);
 
}
 
...
}  

See https://github.com/stan-dev/rstanarm/blob/master/exec/count.stan

Ben


Yaohui Zeng

unread,
Jun 10, 2016, 10:20:42 AM6/10/16
to Stan users mailing list
Thank you very much, Ben! That trick is good to know. I will try that out and update you!

Yaohui Zeng

unread,
Jun 10, 2016, 1:03:21 PM6/10/16
to Stan users mailing list
Hi Ben,

A follow-up question. In this case, how do I initialize the parameter "overdispersion"? I just did the usual way as"init$alpha1 <- 1" in R, but got errors as follows. "alpha1" is "overdispersion" in my code.


SAMPLING FOR MODEL 'SAPKDLTEFF_STAN_Model_full' NOW (CHAIN 1).
[1] "Error : Initialization partially from source failed."                                                                                               
[2] "mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=alpha1; dims declared=(1); dims found=()"


Thanks for your help.
Yaohui


On Thursday, June 9, 2016 at 4:25:55 PM UTC-4, Ben Goodrich wrote:

Bob Carpenter

unread,
Jun 10, 2016, 1:04:56 PM6/10/16
to stan-...@googlegroups.com
It's impossible to diagnose problems without seeing the code
that causes them.

It looks like your code is expecting alpha1 to be an array, but
you're only giving it a single value.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jonah Gabry

unread,
Jun 10, 2016, 1:10:42 PM6/10/16
to Stan users mailing list
Yeah, maybe try array(1) as the value in R.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Ben Goodrich

unread,
Jun 10, 2016, 1:35:43 PM6/10/16
to Stan users mailing list
On Friday, June 10, 2016 at 1:10:42 PM UTC-4, Jonah Gabry wrote:
Yeah, maybe try array(1) as the value in R.

And even if you are estimating a Poisson model, you still have do do

array(double(0)

in R to "initialize" a zero-length array. So, it is best not to use initialization unless it is necessary, which it shouldn't be for a GLM.

Ben

Christopher Peterson

unread,
Jun 10, 2016, 4:05:13 PM6/10/16
to Stan users mailing list
Is there a section of the manual that describes the behavior of using zero-length arrays?  This seems like a cool trick, but I'm not sure what the expected behavior here is.


On Thursday, June 9, 2016 at 4:25:55 PM UTC-4, Ben Goodrich wrote:

Bob Carpenter

unread,
Jun 10, 2016, 9:44:13 PM6/10/16
to stan-...@googlegroups.com
The behavior should be the obvious one --- a vector of size 0
takes no memory and isn't included in I/O. I'm submitting a pull
request with the patch right now. It works for arrays as is, but
I needed to do some fiddling with the C++ types to get it to work
with vectors, row vectors, and matrices.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages