Syntax errors with bernoulli_logit

Hello,

I’m trying to put together a regression with a binary outcome variable where there are two paths to one outcome and one path to the other, and each of these paths are informed by different factors about the experimental items. However, I’m getting syntax errors I don’t understand. Any help would be very much appreciated with this!


data { 
  int<lower = 1> N_trials;
int<lower = 0,upper = 1> shift_or_not[N_trials];
real frequency[N_trials]; 
real weight[N_trials];
real know_remote[N_trials];


}
parameters {

real lexicon_intercept;
real grammar_intercept;
real b_weight;
real b_freq;
real dep;
} 
transformed parameters {
for (n in 1:N_trials){

real lexicon = (lexicon_intercept + b_freq*frequency[n]);
real grammar = (grammar_intercept + b_weight*weight[n]);

real dep = inv_logit((know_remote[n] * lexicon)+(know_remote[n] * (1-lexicon) * grammar)+((1-know_remote[n]) * grammar));
}


}
model {


target += normal_lpdf(lexicon_intercept | 0, 1);
target += normal_lpdf(grammar_intercept | 0, 1);  
target += normal_lpdf(b_weight | 0, 1);  
target += normal_lpdf(b_freq | 0, 1);  

for(n in 1:N_trials){
shift_or_not[n] ~ bernoulli_logit(dep);
}
}


I apologize if the error is trivial, I’m pretty new to hand-coding custom models in Stan.

The main issue is that you’re declaring dep as a parameter twice, once in the parameters block and again in the transformed parameters block. You’re also not really using the transformed parameters block properly. That block is really only there to keep a record of any intermediate variables, variables that are determined by other parameters, for use later in either the model or generated quantities block. Your current loop over N_trials in that block serves to overwrite dep each time, so you get a single value at the end that is used for all loops over N_trials down in the model block. Here’s a version that omits use of transformed parameters block entirely:

data { 
  int<lower = 1> N_trials;
  int<lower = 0,upper = 1> shift_or_not[N_trials];
  real frequency[N_trials]; 
  real weight[N_trials];
  real know_remote[N_trials];
}
parameters {
  real lexicon_intercept;
  real grammar_intercept;
  real b_weight;
  real b_freq;
}
model {
  real lexicon ;
  real grammar;
  real dep;
  target += normal_lpdf(lexicon_intercept | 0, 1);
  target += normal_lpdf(grammar_intercept | 0, 1);  
  target += normal_lpdf(b_weight | 0, 1);  
  target += normal_lpdf(b_freq | 0, 1);  
  for(n in 1:N_trials){
    lexicon = (lexicon_intercept + b_freq*frequency[n]);
    grammar = (grammar_intercept + b_weight*weight[n]);
    dep = inv_logit((know_remote[n] * lexicon)+(know_remote[n] * (1-lexicon) * grammar)+((1-know_remote[n]) * grammar));
    shift_or_not[n] ~ bernoulli_logit(dep);
  }
}

And here’s a version that uses the transformed parameters block properly:

data { 
  int<lower = 1> N_trials;
  int<lower = 0,upper = 1> shift_or_not[N_trials];
  real frequency[N_trials]; 
  real weight[N_trials];
  real know_remote[N_trials];
}
parameters {
  real lexicon_intercept;
  real grammar_intercept;
  real b_weight;
  real b_freq;
}
transformed parameters{
  vector[N_trials] lexicon ;
  vector[N_trials] grammar;
  vector[N_trials] dep;
  for(n in 1:N_trials){
    lexicon[n] = (lexicon_intercept + b_freq*frequency[n]);
    grammar[n] = (grammar_intercept + b_weight*weight[n]);
    dep[n] = inv_logit((know_remote[n] * lexicon[n])+(know_remote[n] * (1-lexicon[n]) * grammar[n])+((1-know_remote[n]) * grammar[n]));
  }
}
model {
  target += normal_lpdf(lexicon_intercept | 0, 1);
  target += normal_lpdf(grammar_intercept | 0, 1);  
  target += normal_lpdf(b_weight | 0, 1);  
  target += normal_lpdf(b_freq | 0, 1);  
  for(n in 1:N_trials){
    shift_or_not[n] ~ bernoulli_logit(dep[n]);
  }
}

Ah, I see where I went wrong. Thank you very much for your help!

1 Like