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 ...
$ 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 ...