Saving intermediate transformed parameters in generated quantities block - variable has undefined values error

Hello,

Quick question: I have a hierarchical stan model that contains two regressions which are then used in a mixture-like way to model a binary outcome. I have the model set up as below, and it runs great (thanks almost entirely to the generosity and kindness of members of this forum :) ). I can get posterior draws out of the ultimate binary outcome, but I’d like to be able to keep the intermediate values of the two regressions that generated it. I tried using the code below (the only additions are tagged, without it it runs great), but I get a ton of “this variable has undefined values” errors at the end. Any advice on how to go about this?

Thanks,
Canaan

data { 
  int<lower = 1> N_trials;
  int<lower = 1> N_subjects;
  int<lower = 1> N_items;
  int<lower = 1, upper = N_items> item[N_trials];

  int<lower = 1, upper = N_subjects> subject[N_trials];
  int<lower = 0,upper = 1> shift_or_not[N_trials];
   vector[N_trials] HB_continuous; 

  vector[N_trials] frequency; 
  vector[N_trials] weight;
  vector[N_trials] know_remote;
  vector[N_trials] cosine_sim;
}
parameters {
  vector[N_subjects] lexicon_intercept_subject_adjustor;
  vector[N_items] lexicon_intercept_item_adjustor;

  real lexicon_intercept;
  vector[N_subjects] grammar_intercept_subject_adjustor;
    vector[N_items] grammar_intercept_item_adjustor;

  real grammar_intercept;
  real b_weight;
    vector[N_subjects] weight_subject_adjustor;

  real b_cosine;
    vector[N_subjects] cosine_sim_subject_adjustor;

  real b_freq;
    vector[N_subjects] frequency_subject_adjustor;

  vector[N_subjects] HB_continuous_subject_adjustor;
  real b_HB_continuous;
}

transformed parameters {
  vector[N_trials] dep;

  {
  vector[N_trials] lexicon;
  vector[N_trials] grammar;
  lexicon = inv_logit(lexicon_intercept+ lexicon_intercept_subject_adjustor[subject]+lexicon_intercept_item_adjustor[item]
              +(( b_freq + frequency_subject_adjustor[subject]) .* frequency) + ((b_cosine+ cosine_sim_subject_adjustor[subject]) .* cosine_sim) + ((b_HB_continuous*HB_continuous_subject_adjustor[subject]) .* HB_continuous));
  grammar = inv_logit(grammar_intercept + grammar_intercept_subject_adjustor[subject]+grammar_intercept_item_adjustor[item] 
  + ((b_weight+weight_subject_adjustor[subject]) .* weight));
  dep = (know_remote .* lexicon) + (know_remote .* (1-lexicon) .* grammar)
                    +((1-know_remote) .* grammar);
  }
}

model {
  lexicon_intercept_subject_adjustor ~ std_normal();
    lexicon_intercept_item_adjustor ~ std_normal();
  HB_continuous_subject_adjustor ~ std_normal();
  weight_subject_adjustor ~ std_normal();
  frequency_subject_adjustor ~ std_normal();
  cosine_sim_subject_adjustor~ std_normal();
  grammar_intercept_subject_adjustor ~ std_normal();
    grammar_intercept_item_adjustor ~ std_normal();

  lexicon_intercept ~ std_normal();
  grammar_intercept ~ std_normal(); 
  b_weight ~ std_normal(); 
  b_cosine ~ std_normal();  
  b_freq ~ std_normal(); 
  b_HB_continuous ~ std_normal();

  shift_or_not ~ bernoulli_logit(dep);
}
generated quantities{
  int yrep[N_trials];
  vector[N_trials] lexicon; // <- intermediate variable of interest
  vector[N_trials] grammar; // <- intermediate variable of interest
 
  
  yrep = bernoulli_logit_rng(dep);
}


I would take lexicon and grammar out of the generated quantities block and then in the transformed parameters block, eliminate the local block so that lexicon and grammar get stored.

Thanks very much - two questions; what do you mean my local block, and also will thede values of lexicon and grammar be the ones that correspond to the posterior predictive yrep draws, or just to the ones in the sample drawn? I assume both but wanted to make sure.

I mean without the { ... } inside the transformed parameters block.

transformed parameters {
  vector[N_trials] lexicon = inv_logit(lexicon_intercept+ lexicon_intercept_subject_adjustor[subject]+lexicon_intercept_item_adjustor[item]
              +(( b_freq + frequency_subject_adjustor[subject]) .* frequency) + ((b_cosine+ cosine_sim_subject_adjustor[subject]) .* cosine_sim) + ((b_HB_continuous*HB_continuous_subject_adjustor[subject]) .* HB_continuous));

  vector[N_trials] grammar = inv_logit(grammar_intercept + grammar_intercept_subject_adjustor[subject]+grammar_intercept_item_adjustor[item] 
  + ((b_weight+weight_subject_adjustor[subject]) .* weight));

  vector[N_trials] dep = (know_remote .* lexicon) + (know_remote .* (1-lexicon) .* grammar)
                    +((1-know_remote) .* grammar);
}

yrep is still posterior predictive.

Awesome, thanks very much, i really appreciate it!