STAN novice with a parser error

834 views
Skip to first unread message

Jeremy

unread,
Feb 14, 2014, 10:30:27 AM2/14/14
to stan-...@googlegroups.com
I've just started using STAN, as accessed with Richard McElreath's glmer2stan package. Initially, there was some trickiness related to the contiguity of the integers that are used as the grouping factors, but I (thought that I) had that under control.

Now I'm getting an error message that I don't know how to interpret:

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'y ~ sex + age.center + age.squared + age.cubed + (1 | informant.id) +     (1 | species.id) [binomial]' with error message:
EXPECTATION FAILURE LOCATION: file=input; line=43, column=38

        vary[i] <- vary_informant_id[informant_id[i]]
                                     ^-- here

DIAGNOSTIC(S) FROM PARSER:
expression denoting integer required; found type=real
Parser expecting: <list><expression (top level, precedence 15, binary ||>","

As background, I'm running a recent version of STAN on a Mac, OS 10.7.5. Here is the model code:

data{
    int N;
    int y[N];
    real informant_sex[N];
    real age_center[N];
    real age_squared[N];
    real age_cubed[N];
    int informant_id[N];
    int species_id[N];
    int bin_total[N];
    int N_informant_id;
    int N_species_id;
}

parameters{
    real Intercept;
    real beta_informant_sex;
    real beta_age_center;
    real beta_age_squared;
    real beta_age_cubed;
    real vary_informant_id[N_informant_id];
    real<lower=0> sigma_informant_id;
    real vary_species_id[N_species_id];
    real<lower=0> sigma_species_id;
}

model{
    real vary[N];
    real glm[N];
    // Priors
    Intercept ~ normal( 0 , 100 );
    beta_informant_sex ~ normal( 0 , 100 );
    beta_age_center ~ normal( 0 , 100 );
    beta_age_squared ~ normal( 0 , 100 );
    beta_age_cubed ~ normal( 0 , 100 );
    sigma_informant_id ~ uniform( 0 , 100 );
    sigma_species_id ~ uniform( 0 , 100 );
    // Varying effects
    for ( j in 1:N_informant_id ) vary_informant_id[j] ~ normal( 0 , sigma_informant_id );
    for ( j in 1:N_species_id ) vary_species_id[j] ~ normal( 0 , sigma_species_id );
    // Fixed effects
    for ( i in 1:N ) {
        vary[i] <- vary_informant_id[informant_id[i]]
                + vary_species_id[species_id[i]];
        glm[i] <- vary[i] + Intercept
                + beta_informant_sex * informant_sex[i]
                + beta_age_center * age_center[i]
                + beta_age_squared * age_squared[i]
                + beta_age_cubed * age_cubed[i];
        glm[i] <- inv_logit( glm[i] );
    }
    y ~ binomial( bin_total , glm );
}

generated quantities{
    real dev;
    real vary[N];
    real glm[N];
    dev <- 0;
    for ( i in 1:N ) {
        vary[i] <- vary_informant_id[informant_id[i]]
                + vary_species_id[species_id[i]];
        glm[i] <- vary[i] + Intercept
                + beta_informant_sex * informant_sex[i]
                + beta_age_center * age_center[i]
                + beta_age_squared * age_squared[i]
                + beta_age_cubed * age_cubed[i];
        dev <- dev + (-2) * binomial_log( y[i] , bin_total[i] , inv_logit(glm[i]) );
    }
}

If it helps, here is the str() of the dataset. You can see that I've tried renaming (and reclassifying) variables, which has worked for past snags. But now I'm at a loss:

> str(pcc)
'data.frame': 5832 obs. of  19 variables:
 $ atomic.unit  : int  1 2 3 4 5 6 7 8 9 10 ...
 $ ID           : Factor w/ 5778 levels "Abel-anghangh",..: 4219 4229 4213 4237 4238 4224 4230 4225 4227 4231 ...
 $ row          : int  1 1 1 1 1 1 1 1 1 1 ...
 $ informant    : Factor w/ 214 levels "Abel","Abet",..: 157 157 157 157 157 157 157 157 157 157 ...
 $ col          : int  1 2 3 4 5 6 7 8 9 10 ...
 $ species      : Factor w/ 27 levels "anghangh","bahya",..: 7 17 1 25 26 12 18 13 15 19 ...
 $ y            : int  1 1 0 1 1 1 1 0 1 0 ...
 $ informant.age: int  34 34 34 34 34 34 34 34 34 34 ...
 $ informant.sex: int  0 0 0 0 0 0 0 0 0 0 ...
 $ age.center   : num  4 4 4 4 4 4 4 4 4 4 ...
 $ age.squared  : num  16 16 16 16 16 16 16 16 16 16 ...
 $ age.cubed    : num  64 64 64 64 64 64 64 64 64 64 ...
 $ informant.int: int  157 157 157 157 157 157 157 157 157 157 ...
 $ species.int  : int  7 17 1 25 26 12 18 13 15 19 ...
 $ informant.id : int  1 1 1 1 1 1 1 1 1 1 ...
 $ species.id   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ informantid  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ speciesid    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ sex          : int  0 0 0 0 0 0 0 0 0 0 ...

Bob Carpenter

unread,
Feb 14, 2014, 10:45:24 AM2/14/14
to stan-...@googlegroups.com
Now wonder you don't know how to interpret the error ---
your model's fine and the error message is just wrong.

There was a bug in the parser with integer arrays being used as
indexes. That bug's been fixed as of Stan 2.2, and RStan 2.2 should
be out any day now --- we just released CmdStan 2.2.

- 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.
> For more options, visit https://groups.google.com/groups/opt_out.

Jeremy

unread,
Feb 14, 2014, 2:07:40 PM2/14/14
to stan-...@googlegroups.com
That's reassuring! I'll be checking regularly for the updated version.

Is there a workaround in the short-term? Thanks!

Jeremy

unread,
Feb 14, 2014, 2:12:19 PM2/14/14
to stan-...@googlegroups.com
Never mind. Scratch that. I found this list of bug fixes in the email above and realized that I needed to rename "informant.sex" and "informant.id." The model is running now.

Bug Fixes
-------------
* increment_log_prob is now vectorized and compiles with vector arguments
* multinomial random number generator used the wrong size for the return value
* fixed memory leaks in auto-diff implementation
* variables can start with the prefix 'inf'
* fixed parameter output order for arrays when using optimization

Bob Carpenter

unread,
Feb 14, 2014, 3:30:00 PM2/14/14
to stan-...@googlegroups.com
I should've noticed that. The "inf" prefix issue is also fixed in 2.2.

- Bob

On 2/14/14, 8:12 PM, Jeremy wrote:
> Never mind. Scratch that. I found this list of bug fixes in the email above and realized that I needed to rename
> "informant.sex" and "informant.id." The model is running now.
>
> Bug Fixes
> -------------
> * increment_log_prob is now vectorized and compiles with vector arguments
> * multinomial random number generator used the wrong size for the return value
> * fixed memory leaks in auto-diff implementation
> ** variables can start with the prefix 'inf'*
> * fixed parameter output order for arrays when using optimization
>
>
> On Friday, February 14, 2014 2:07:40 PM UTC-5, Jeremy wrote:
>
> That's reassuring! I'll be checking regularly for the updated version.
>
> Is there a workaround in the short-term? Thanks!
>

Richard McElreath

unread,
Feb 15, 2014, 5:35:02 PM2/15/14
to stan-...@googlegroups.com
Thanks for helping out with this, Bob. I was conferencing last week, so some pent up glmer2stan support developed, as my email piled up. Usually I keep up with it.
Reply all
Reply to author
Forward
0 new messages