On 11/30/12 12:20 PM, Noah wrote:
> Hello,
>
> I have a hierarchical model in JAGS. Appears to work fine, but takes forever on my current machine (latest core, i7,
> 16GB RAM, etc.) So, I want to try things in stan.
>
> Two questions:
> 1) Will it run faster/better from the command line than from within R with the rstan package?
It runs the same compiled code. There's a bit of
overhead from using R, but that's only in getting
data in and out. It can take more size to store the
data in R, too.
> 2) Can someone help me with the format of the model file. I'm having trouble understanding how to convert this jags
> code to stan code. This is a hierarchical model with factors for both "group" and "program". Looking at counts of
> students based on their group membership. (Pre-defined cluster) and the program they might enroll
>
> Thanks!
>
> Model below
> ================================
You need to declare data and parameters. Look in
the manual to see how to do that, and make sure
you get the constraints on the deviation parameters sigma.
> model{
>
> for(i in 1:N){
> eta[i] <- ranef_group[group[i]] + ranef_prog[program[i]]+ b_var_1 * var_1[i] + b_var_2 * var_2[i] + b_var_3 *
> var_3[i]
> log(lambda[i]) <- eta[i]
No reason to make this separate, you just do
lambda[i] <- exp(eta[i]);
> count[i] ~ dpois(lambda[i])
You can just do this in Stan:
count[i] ~ poisson(exp(lambda[i]));
> }
>
> b_var_1 ~ dnorm(0, tau_var_1)
b_var_1 ~ normal(0,sigma_var_1);
> b_var_2 ~ dnorm(0, tau_var_2)
> b_var_3 ~ dnorm(0, tau_var_3)
>
> tau_var_1 <- pow(sigma_var_1, -2)
> tau_var_2 <- pow(sigma_var_2, -2)
> tau_var_3 <- pow(sigma_var_3, -2)
> tau_group <- pow(sigma_group, -2)
> tau_prog <- pow(sigma_prog, -2)
>
> sigma_var_1 ~ dunif(0,100)
> sigma_var_2 ~ dunif(0,100)
> sigma_var_3 ~ dunif(0,100)
> sigma_group ~ dunif(0,100)
> sigma_prog ~ dunif(0,100)
>
>
> for (i in 1:n_group){
> ranef_group[i] ~ dnorm(0, tau_group)
> }
>
> for (i in 1:n_prog){
> ranef_prog[i] ~ dnorm(0, tau_prog)
> }
> }
Also, things get executed in order, so that if
you did want to transform a variable for using it with <-
then you need to do it BEFORE using it.
- Bob