Model with sample data runs fine; actual data of 1000 participants returns several errors

Hi all,

I’ve been working with the super computer; that sample of roughly 32 participants runs fine. No problem.

Then I move to all 1000 participants, 14 conditions (and four time points each of which is run separately). It takes about two days to run with each chain (4) using a separate core, and then I get the following error.

Error: cannot allocate vector of size 17.4 Gb
Error in FUN(X[[i]], ...) : 
  trying to get slot "mode" from an object (class "try-error") that is not an S4 object 
Calls: stan ... sampling -> sampling -> .local -> sapply -> lapply -> FUN
In addition: Warning message:
In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  1 function calls resulted in an error
Execution halted
Warning message:
system call failed: Cannot allocate memory 

Unfortunately, I don’t know all of the details on the rstan version, etc., since it’s running from the super computer, but I can email the folks running the system and can ask them.

It seems like these comprise multiple errors: one of which is solved by reinstalling Rcpp (found here: Error: cannot allocate vector of size 13813.6 Gb)

The other errors seem more evasive. For instance, I received the same 17.4gb warning after the first attempt, and then I changed the output so that I was only pulling out the threshold and width parameters,

fit.w <- summary(fit.norm_subj.cond.t1, pars = c("w"))
fit.w <- fit.w$summary
write.csv(fit.w, "fit.w.1")
fit.m <- summary(fit.norm_subj.cond.t1, pars = c("m"))
fit.m <- fit.m$summary
write.csv(fit.m, "fit.m.1")

and I still got the same warning 17.4gb warning.

As a second question, I’m sure there are ways that I could speed the process along (faster than 2 days). Let me know if there are any resources you’d recommend.

Thanks!

Here is the code

data{  // Everything that must be input by the user
  int<lower=4> n_total;                         // Total number of trials to analyze
  int<lower=2> n_subjects;                      // Number of unique observers
  int<lower=2> n_levels;                        // Number of levels of Factor 
  real intensity[n_total];                      // Intensity of the stimulus on each trial
  int<lower=0> subject[n_total];                // Observer on each trial
  int<lower=0> level[n_total];                  // Level of Factor on each trial
  int<lower=0,upper=1> correct[n_total];        // Whether the response was correct (1) on each trial
  real<lower=0,upper=1> chance_performance;     // Chance performance for experiment (e.g., 1/n for n-AFC)
}
transformed data{
  int df_levels_sA;          // Number of degrees of freedom in the interaction 
  int n_levels_sA;          // Number of levels in the interaction 
  
  df_levels_sA = (n_levels - 1) * (n_subjects - 1);
  n_levels_sA = n_levels * n_subjects;
}
parameters{  
  vector<lower=0,upper=1>[n_subjects] lapse;        // Observer's lapse rate
  real mum;
  real muw;
  vector[n_levels-1] fA;
  vector[n_subjects-1] sA;
  vector[n_subjects-1] sB;
  //matrix[n_subjects-1,n_levels-1] fsA;
  vector[(n_subjects - 1) * (n_levels-1)] fsA;
}
transformed parameters {
real<lower=0,upper=1> mean_beta;              // Mean of beta prior for individual lapse rate
real<lower=0> betaEta;                       // Precision parameter of beta prior for individual lapse rate
vector<lower=0,upper=1>[n_total] psi;
real<lower=0> lapse_alpha;
real<lower=0> lapse_beta;
real m[n_subjects,n_levels];
real<lower=0> w[n_subjects,n_levels];
real threshold[n_total];
real width[n_total];
vector[n_levels] factor_alpha = append_row(fA, -sum(fA));
vector[n_subjects] subject_alpha = append_row(sA, -sum(sA));
vector[n_subjects] subject_beta = append_row(sB, -sum(sB));
matrix[n_subjects,n_levels] interaction_alpha;


interaction_alpha = rep_matrix(0, n_subjects, n_levels);
for(sj in 1:(n_subjects - 1)) { 
for(l in 1:(n_levels - 1)) { 
interaction_alpha[sj, l] = fsA[(sj - 1) * (n_levels - 1) + l];
}
interaction_alpha[sj, n_levels] = -1 * sum(interaction_alpha[sj,]);
}
for (l in 1:n_levels) { 
interaction_alpha[n_subjects,l] = -1 * sum(interaction_alpha[,l]);
}


for (sj in 1:n_subjects) {
for (l in 1:n_levels) {
m[sj,l] = mum + factor_alpha[l] + subject_alpha[sj] + interaction_alpha[sj, l];
w[sj,l] = exp(muw + subject_beta[sj]);
}
}

for (tr in 1:n_total) {
threshold[tr] = m[subject[tr],level[tr]];
width[tr] = w[subject[tr],level[tr]];
psi[tr] = chance_performance 
              + (1 - lapse[subject[tr]] - chance_performance) 
              * inv_logit((intensity[tr]-threshold[tr])/width[tr]);
if (is_nan(psi[tr])) {
  print("lapse = ", lapse[subject[tr]]);
  print("width = ", width[tr]);
  print("threshold = ", threshold[tr]);
}
}

mean_beta  = 0.01;
betaEta = 100;
lapse_alpha = mean_beta * betaEta;
lapse_beta = (1-mean_beta) * betaEta ;
}
model {
//mean_beta ~ beta(1,60);
//betaEta ~ gamma(1,0.01);
fsA ~ normal(0, inv(sqrt(1 - inv(n_levels_sA)))); // Produces a standard normal on on interaction_alpha
fA ~ normal(0, inv(sqrt(1 - inv(n_levels)))); 
sA ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha
sB ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha

muw ~ gamma(2,.5);
lapse ~ beta(lapse_alpha,lapse_beta);
correct ~ bernoulli(psi);
}

A couple questions here. What are the dimensions of the input data? (i.e., what are n_total, n_subjects, n_levels here?). How many iterations are you running this model for?

Hi, thanks,

So dimensions of n_total would vary depending on time point, but at time point 1, it is 365,655. The n_subjects also varies by time point (given that some subjects aren’t present at different time points, but again, at time point 1, n_subject is 1,013. And n_levels will be the same for all time points; it is the number of conditions, which is 14.

Given that the models work until you add more participants, I would say that you’re legitimately running out of RAM. To see why, you can count the number of parameters and transformed parameters that need to be estimated/stored (I’ll focus on the vector/matrix parameters here).

parameters:

  • vector<lower=0,upper=1>[n_subjects] lapse;
  • vector[n_levels-1] fA;
  • vector[n_subjects-1] sA;
  • vector[n_subjects-1] sB;
  • vector[(n_subjects - 1) * (n_levels-1)] fsA;

For a total of n_subjects + n_levels-1 + n_subjects-1 + n_subjects-1 + (n_subjects - 1) * (n_levels-1) = 16206 parameters

transformed parameters:

  • vector<lower=0,upper=1>[n_total] psi;
  • real m[n_subjects,n_levels];
  • real<lower=0> w[n_subjects,n_levels];
  • real threshold[n_total];
  • real width[n_total];
  • vector[n_levels] factor_alpha;
  • vector[n_subjects] subject_alpha;
  • vector[n_subjects] subject_beta;
  • matrix[n_subjects,n_levels] interaction_alpha;

For a total of n_total + (n_subjects * n_levels) + (n_subjects * n_levels) + n_total + n_total + n_levels + n_subjects + n_subjects + (n_subjects * n_levels) = 1,141,551 parameters

So overall, your model needs to store 1,157,757 parameters per iteration. Assuming the Stan defaults of 2000 draws and four chains, that makes a total of 1,157,757 * 2000 * 4 = 9,262,056,000 estimates to be stored.

Thanks, this is helpful. Hmmm…so if the problem was RAM, wouldn’t the super computer fail to finish the iterations?

Given that the iterations finished, I suspected that the main problem was that there wasn’t enough memory to output all parameters (as you show that is a lot!!!), which is why I wrote the extra code to only pull out only the m and w parameters (m = n_total * n_levels; and w is just n_total). Is there something else that I should be doing? All I really need in the output in the mean values for m and w and the Rhats and neffs.

Did all the iterations finish? I thought this output:

Error in FUN(X[[i]], ...) : 
  trying to get slot "mode" from an object (class "try-error") that is not an S4 object 
Calls: stan ... sampling -> sampling -> .local -> sapply -> lapply -> FUN
In addition: Warning message:
In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  1 function calls resulted in an error

Meant that the sampling failed?

If the sampling is fine, but loading the results up is an issue, you can try making it so that only the m and w parameters are saved in the transformed parameters block. To do this, you just wrap the declaration and computation of the unwanted parameters in braces ({}), like so:

transformed parameters {
  real m[n_subjects,n_levels];
  real<lower=0> w[n_subjects,n_levels];
  real<lower=0> lapse_alpha;
  real<lower=0> lapse_beta;
  {
    real<lower=0,upper=1> mean_beta;              // Mean of beta prior for individual lapse rate
    real<lower=0> betaEta;                       // Precision parameter of beta prior for individual lapse rate
    vector<lower=0,upper=1>[n_total] psi;
    real threshold[n_total];
    real width[n_total];
    vector[n_levels] factor_alpha = append_row(fA, -sum(fA));
    vector[n_subjects] subject_alpha = append_row(sA, -sum(sA));
    vector[n_subjects] subject_beta = append_row(sB, -sum(sB));
    matrix[n_subjects,n_levels] interaction_alpha;


    interaction_alpha = rep_matrix(0, n_subjects, n_levels);
    for(sj in 1:(n_subjects - 1)) { 
    for(l in 1:(n_levels - 1)) { 
    interaction_alpha[sj, l] = fsA[(sj - 1) * (n_levels - 1) + l];
    }
    interaction_alpha[sj, n_levels] = -1 * sum(interaction_alpha[sj,]);
    }
    for (l in 1:n_levels) { 
    interaction_alpha[n_subjects,l] = -1 * sum(interaction_alpha[,l]);
    }


    for (sj in 1:n_subjects) {
    for (l in 1:n_levels) {
    m[sj,l] = mum + factor_alpha[l] + subject_alpha[sj] + interaction_alpha[sj, l];
    w[sj,l] = exp(muw + subject_beta[sj]);
    }
    }

    for (tr in 1:n_total) {
    threshold[tr] = m[subject[tr],level[tr]];
    width[tr] = w[subject[tr],level[tr]];
    psi[tr] = chance_performance 
                  + (1 - lapse[subject[tr]] - chance_performance) 
                  * inv_logit((intensity[tr]-threshold[tr])/width[tr]);
    if (is_nan(psi[tr])) {
      print("lapse = ", lapse[subject[tr]]);
      print("width = ", width[tr]);
      print("threshold = ", threshold[tr]);
    }
    }

    mean_beta  = 0.01;
    betaEta = 100;
    lapse_alpha = mean_beta * betaEta;
    lapse_beta = (1-mean_beta) * betaEta ;
  }
}

Any parameters declared outside the braces (e.g., m,w,lapse_alpha) will be saved (and can also be used in the model or generated quantities blocks). Any parameters declared inside the braces will be treated as ‘temporary’ and not saved or available elsewhere in the model

Sorry, perhaps, I could’ve been clearer: the warning comes after this

Chain 1: 
Chain 1:  Elapsed Time: 26198.5 seconds (Warm-up)
Chain 1:                30306.3 seconds (Sampling)
Chain 1:                56504.8 seconds (Total)
Chain 1: 
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 28145.9 seconds (Warm-up)
Chain 4:                28811.8 seconds (Sampling)
Chain 4:                56957.7 seconds (Total)
Chain 4: 
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 28610.4 seconds (Warm-up)
Chain 2:                30330.8 seconds (Sampling)
Chain 2:                58941.1 seconds (Total)
Chain 2: 
Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 43462.9 seconds (Warm-up)
Chain 3:                30355.5 seconds (Sampling)
Chain 3:                73818.4 seconds (Total)
Chain 3: 

So I am assuming that all iterations finish, and it is the loading of the results.

Great! I will re-run the code with only the m and w parameters in the transformed parameters block. Thanks for the example…super helpful! I’ll have that back in ~36 hours and will let you know how it goes!

Would there be any way of expediting the process given that I have access to multiple nodes and cores? What would be my best way moving forward with that?

Thanks much for your help…this has been months of trial and error!

Just a quick follow-up, Andrew.

I submitted the job with the brackets in the transformed parameters as you put them and was receiving an error. I “debugged” it on my computer and it appears that unless the first bracket comes after the model part of the code, it will return the following error

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  c++ exception (unknown reason)

Not sure if you have any insight as to why that might be?

I’ve posted below the code within transformed parameters that at least begins to compile. Thanks!

transformed parameters {
real<lower=0,upper=1> mean_beta;              // Mean of beta prior for individual lapse rate
real<lower=0> betaEta;                       // Precision parameter of beta prior for individual lapse rate
vector<lower=0,upper=1>[n_total] psi;
real<lower=0> lapse_alpha;
real<lower=0> lapse_beta;
real m[n_subjects,n_levels];
real<lower=0> w[n_subjects,n_levels];
real threshold[n_total];
real width[n_total];
vector[n_levels] factor_alpha = append_row(fA, -sum(fA));
vector[n_subjects] subject_alpha = append_row(sA, -sum(sA));
vector[n_subjects] subject_beta = append_row(sB, -sum(sB));
matrix[n_subjects,n_levels] interaction_alpha;


interaction_alpha = rep_matrix(0, n_subjects, n_levels);
for(sj in 1:(n_subjects - 1)) { 
for(l in 1:(n_levels - 1)) { 
interaction_alpha[sj, l] = fsA[(sj - 1) * (n_levels - 1) + l];
}
interaction_alpha[sj, n_levels] = -1 * sum(interaction_alpha[sj,]);
}
for (l in 1:n_levels) { 
interaction_alpha[n_subjects,l] = -1 * sum(interaction_alpha[,l]);
}


for (sj in 1:n_subjects) {
for (l in 1:n_levels) {
m[sj,l] = mum + factor_alpha[l] + subject_alpha[sj] + interaction_alpha[sj, l];
w[sj,l] = exp(muw + subject_beta[sj]);
}
}

for (tr in 1:n_total) {
threshold[tr] = m[subject[tr],level[tr]];
width[tr] = w[subject[tr],level[tr]];
psi[tr] = chance_performance 
              + (1 - lapse[subject[tr]] - chance_performance) 
              * inv_logit((intensity[tr]-threshold[tr])/width[tr]);
if (is_nan(psi[tr])) {
  print("lapse = ", lapse[subject[tr]]);
  print("width = ", width[tr]);
  print("threshold = ", threshold[tr]);
}
}

mean_beta  = 0.01;
betaEta = 100;
lapse_alpha = mean_beta * betaEta;
lapse_beta = (1-mean_beta) * betaEta ;
}
model {
//mean_beta ~ beta(1,60);
//betaEta ~ gamma(1,0.01);
{
fsA ~ normal(0, inv(sqrt(1 - inv(n_levels_sA)))); // Produces a standard normal on on interaction_alpha
fA ~ normal(0, inv(sqrt(1 - inv(n_levels)))); 
sA ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha
sB ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha

muw ~ gamma(2,.5);
lapse ~ beta(lapse_alpha,lapse_beta);
correct ~ bernoulli(psi);
}
}

Can you post the code that causes the error on your computer? The code above will have the same issues as the original, as the braces need to be around the parameter declarations, not in the model block.

Yes, here it is:

data{  // Everything that must be input by the user
  int<lower=4> n_total;                         // Total number of trials to analyze
  int<lower=2> n_subjects;                      // Number of unique observers
  int<lower=2> n_levels;                        // Number of levels of Factor 
  real intensity[n_total];                      // Intensity of the stimulus on each trial
  int<lower=0> subject[n_total];                // Observer on each trial
  int<lower=0> level[n_total];                  // Level of Factor on each trial
  int<lower=0,upper=1> correct[n_total];        // Whether the response was correct (1) on each trial
  real<lower=0,upper=1> chance_performance;     // Chance performance for experiment (e.g., 1/n for n-AFC)
}
transformed data{
  int df_levels_sA;          // Number of degrees of freedom in the interaction 
  int n_levels_sA;          // Number of levels in the interaction 
  
  df_levels_sA = (n_levels - 1) * (n_subjects - 1);
  n_levels_sA = n_levels * n_subjects;
}
parameters{  
  vector<lower=0,upper=1>[n_subjects] lapse;        // Observer's lapse rate
  real mum;
  real muw;
  vector[n_levels-1] fA;
  vector[n_subjects-1] sA;
  vector[n_subjects-1] sB;
  //matrix[n_subjects-1,n_levels-1] fsA;
  vector[(n_subjects - 1) * (n_levels-1)] fsA;
}

transformed parameters {
  real m[n_subjects,n_levels];
  real<lower=0> w[n_subjects,n_levels];
  real<lower=0> lapse_alpha;
  real<lower=0> lapse_beta;
  {
    real<lower=0,upper=1> mean_beta;              // Mean of beta prior for individual lapse rate
    real<lower=0> betaEta;                       // Precision parameter of beta prior for individual lapse rate
    vector<lower=0,upper=1>[n_total] psi;
    real threshold[n_total];
    real width[n_total];
    vector[n_levels] factor_alpha = append_row(fA, -sum(fA));
    vector[n_subjects] subject_alpha = append_row(sA, -sum(sA));
    vector[n_subjects] subject_beta = append_row(sB, -sum(sB));
    matrix[n_subjects,n_levels] interaction_alpha;


    interaction_alpha = rep_matrix(0, n_subjects, n_levels);
    for(sj in 1:(n_subjects - 1)) { 
    for(l in 1:(n_levels - 1)) { 
    interaction_alpha[sj, l] = fsA[(sj - 1) * (n_levels - 1) + l];
    }
    interaction_alpha[sj, n_levels] = -1 * sum(interaction_alpha[sj,]);
    }
    for (l in 1:n_levels) { 
    interaction_alpha[n_subjects,l] = -1 * sum(interaction_alpha[,l]);
    }


    for (sj in 1:n_subjects) {
    for (l in 1:n_levels) {
    m[sj,l] = mum + factor_alpha[l] + subject_alpha[sj] + interaction_alpha[sj, l];
    w[sj,l] = exp(muw + subject_beta[sj]);
    }
    }

    for (tr in 1:n_total) {
    threshold[tr] = m[subject[tr],level[tr]];
    width[tr] = w[subject[tr],level[tr]];
    psi[tr] = chance_performance 
                  + (1 - lapse[subject[tr]] - chance_performance) 
                  * inv_logit((intensity[tr]-threshold[tr])/width[tr]);
    if (is_nan(psi[tr])) {
      print("lapse = ", lapse[subject[tr]]);
      print("width = ", width[tr]);
      print("threshold = ", threshold[tr]);
    }
    }

    mean_beta  = 0.01;
    betaEta = 100;
    lapse_alpha = mean_beta * betaEta;
    lapse_beta = (1-mean_beta) * betaEta ;
  }
}

Ah, I see my mistake! When using local variables (i.e., variables declared within braces), you can’t have constraints/bounds, so you need to remove the lower/upper bounds from mean_beta, betaEta, and psi

Ah, got it. Thanks for catching that! I’ll run it this eve.

Given that it does take several days to run (and is only using four cores for the four chains), and that I do have access to multiple cores and an absurd amount of nodes, are there places you could point me for coding things to make use of that extra computer power?

There are two types of parallelism currently available in Stan (more in the pipeline though!).

The first is map_rect: https://mc-stan.org/docs/2_24/stan-users-guide/map-rect.html

The second is reduce_sum: https://mc-stan.org/docs/2_24/stan-users-guide/reduce-sum.html

Note that reduce_sum is only available in Stan versions 2.23 and newer, so it won’t be available through RStan. An alternative for R is cmdstanR which uses R to run models through cmdstan

1 Like

Thanks for the parallelism info!

So after waiting for the super comp to return and finding exceedingly high Rhats, I realized that I forgot to paste in the model. When I looked at this on my computer, I realized that I received the same error I was receiving before, even after removing the lower/upper bounds from the parameters within the brackets after transformed parameters.

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  c++ exception (unknown reason)

Here is the entire code for which I am receiving the error (sorry for that mishap on my part). Can you see what might be causing the error after adding the model into the code?

data{  // Everything that must be input by the user
  int<lower=4> n_total;                         // Total number of trials to analyze
  int<lower=2> n_subjects;                      // Number of unique observers
  int<lower=2> n_levels;                        // Number of levels of Factor 
  real intensity[n_total];                      // Intensity of the stimulus on each trial
  int<lower=0> subject[n_total];                // Observer on each trial
  int<lower=0> level[n_total];                  // Level of Factor on each trial
  int<lower=0,upper=1> correct[n_total];        // Whether the response was correct (1) on each trial
  real<lower=0,upper=1> chance_performance;     // Chance performance for experiment (e.g., 1/n for n-AFC)
}
transformed data{
  int df_levels_sA;          // Number of degrees of freedom in the interaction 
  int n_levels_sA;          // Number of levels in the interaction 
  
  df_levels_sA = (n_levels - 1) * (n_subjects - 1);
  n_levels_sA = n_levels * n_subjects;
}
parameters{  
  vector<lower=0,upper=1>[n_subjects] lapse;        // Observer's lapse rate
  real mum;
  real muw;
  vector[n_levels-1] fA;
  vector[n_subjects-1] sA;
  vector[n_subjects-1] sB;
  //matrix[n_subjects-1,n_levels-1] fsA;
  vector[(n_subjects - 1) * (n_levels-1)] fsA;
}

transformed parameters {
  real m[n_subjects,n_levels];
  real<lower=0> w[n_subjects,n_levels];
  real<lower=0> lapse_alpha;
  real<lower=0> lapse_beta;
  {
    real mean_beta;              // Mean of beta prior for individual lapse rate
    real betaEta;                       // Precision parameter of beta prior for individual lapse rate
    vector [n_total] psi;
    real threshold[n_total];
    real width[n_total];
    vector[n_levels] factor_alpha = append_row(fA, -sum(fA));
    vector[n_subjects] subject_alpha = append_row(sA, -sum(sA));
    vector[n_subjects] subject_beta = append_row(sB, -sum(sB));
    matrix[n_subjects,n_levels] interaction_alpha;

    interaction_alpha = rep_matrix(0, n_subjects, n_levels);
    for(sj in 1:(n_subjects - 1)) { 
    for(l in 1:(n_levels - 1)) { 
    interaction_alpha[sj, l] = fsA[(sj - 1) * (n_levels - 1) + l];
    }
    interaction_alpha[sj, n_levels] = -1 * sum(interaction_alpha[sj,]);
    }
    for (l in 1:n_levels) { 
    interaction_alpha[n_subjects,l] = -1 * sum(interaction_alpha[,l]);
    }


    for (sj in 1:n_subjects) {
    for (l in 1:n_levels) {
    m[sj,l] = mum + factor_alpha[l] + subject_alpha[sj] + interaction_alpha[sj, l];
    w[sj,l] = exp(muw + subject_beta[sj]);
    }
    }

    for (tr in 1:n_total) {
    threshold[tr] = m[subject[tr],level[tr]];
    width[tr] = w[subject[tr],level[tr]];
    psi[tr] = chance_performance 
                  + (1 - lapse[subject[tr]] - chance_performance) 
                  * inv_logit((intensity[tr]-threshold[tr])/width[tr]);
    if (is_nan(psi[tr])) {
      print("lapse = ", lapse[subject[tr]]);
      print("width = ", width[tr]);
      print("threshold = ", threshold[tr]);
    }
    }

    mean_beta  = 0.01;
    betaEta = 100;
    lapse_alpha = mean_beta * betaEta;
    lapse_beta = (1-mean_beta) * betaEta ;
  }
}

model {
//mean_beta ~ beta(1,60);
//betaEta ~ gamma(1,0.01);
fsA ~ normal(0, inv(sqrt(1 - inv(n_levels_sA)))); // Produces a standard normal on on interaction_alpha
fA ~ normal(0, inv(sqrt(1 - inv(n_levels)))); 
sA ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha
sB ~ normal(0, inv(sqrt(1 - inv(n_subjects)))); // Produces a standard normal on on interaction_alpha

muw ~ gamma(2,.5);
lapse ~ beta(lapse_alpha,lapse_beta);
correct ~ bernoulli(psi);
}

When I try to compile the model I get:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable "psi" does not exist.
 error in 'model15de6eb7c3c1_t' at line 94, column 23
  -------------------------------------------------
    92: muw ~ gamma(2,.5);
    93: lapse ~ beta(lapse_alpha,lapse_beta);
    94: correct ~ bernoulli(psi);
                              ^
    95: }
  -------------------------------------------------

Which is because psi is declared within the braces in the transformed parameters, which tells Stan that it is temporary and shouldn’t be available anywhere else in the model. The model compiles when moving the parameter declaration outside of the braces

Awesome! Everything finished!

In order to check the psychometric threshold fits and look at posterior predictive simulation, which I read about here: (http://www.stat.columbia.edu/~gelman/book/software.pdf), I believe I would need to extract this from the supercomputer and write it to an object/dataset in the r script (similar to what I did with m and w parameters)

Would the following be correct to extract the posterior predictive?

ex.model <- extract(model) 
n_sims <- length(ex.model$lp__) 
y_rep <- array(NA, c(n_sims, n_total)) 
for (s in 1:n_sims) 
	y_rep[s,] <- rnorm(n_total, ex.model$psi[s,]) 

Also, given that I exponentiated the width parameter at line 67, in order for the model to compile, would I need to account for this at some point later in the model?

Thanks much!!!