Stan model 'anon_model' does not contain samples

Hi there! I got an error message: Stan model ‘anon_model’ does not contain samples.

Basically, the purpose of this model is to combine two reinforcement learning process (Learning from the validity of advice and learning from subject’s own experience )

Here is my model:

data {
  int<lower=1> nSubjects;
  int<lower=1> nTrials[nSubjects];
  int<lower=1,upper=2> choice_green[nSubjects,max(nTrials)];
  int<lower=1,upper=2> advice_follow[nSubjects,max(nTrials)];
  int<lower=-1,upper=1> advice_acc[nSubjects,max(nTrials)];
  int<lower=-1,upper=1> choice_acc[nSubjects,max(nTrials)];
}

transformed data {
  vector[2] initv1;
  initv1 = rep_vector(0.0,2);
  vector[2] initv2;
  initv2 = rep_vector(0.0,2);
}

parameters {
  // group-Level parameters
 // own experience
  real lr1_mu_raw; 
  real tau1_mu_raw;
  real<lower=0> lr1_sd_raw;
  real<lower=0> tau1_sd_raw;
  // advice following experience
  real lr2_mu_raw;
  real tau2_mu_raw;
  real<lower=0> lr2_sd_raw;
  real<lower=0> tau2_sd_raw;

  // The weight of following advice
  real omega_mu_raw;
  real omega_sd_raw;
  
  // subject-Level parameters
  // own experience
  vector[nSubjects] lr1_raw;
  vector[nSubjects] tau1_raw;
  // advice following experience
  vector[nSubjects] lr2_raw;
  vector[nSubjects] tau2_raw;
  
  // The weight of following advice
  vector[nSubjects] omega_raw;
}

transformed parameters{
  vector<lower=0,upper=1>[nSubjects] lr1;
  vector<lower=0,upper=3>[nSubjects] tau1;
  vector<lower=0,upper=1>[nSubjects] lr2;
  vector<lower=0,upper=3>[nSubjects] tau2;
  
  vector<lower=0,upper=1>[nSubjects] omega;
  
  for (s in 1:nSubjects){
    lr1[s] = Phi_approx( lr1_mu_raw + lr1_sd_raw * lr1_raw[s] );
    tau1[s] = Phi_approx( tau1_mu_raw + tau1_sd_raw * tau1_raw[s] )*3;
    lr2[s] = Phi_approx( lr2_mu_raw + lr2_sd_raw * lr2_raw[s] );
    tau2[s] = Phi_approx( tau2_mu_raw + tau2_sd_raw * tau2_raw[s] )*3;
    
    omega[s] = Phi_approx( omega_mu_raw + omega_sd_raw * omega_raw[s] );
  }
}

model {
  lr1_mu_raw ~ normal(0,1);
  tau1_mu_raw ~ normal(0,1);
  lr1_sd_raw ~ cauchy(0,3);
  tau1_sd_raw ~ cauchy(0,3);
  
  
  lr1_raw ~ normal(0,1);
  tau1_raw ~ normal(0,1);
  
  lr2_mu_raw ~ normal(0,1);
  tau2_mu_raw ~ normal(0,1);
  lr2_sd_raw ~ cauchy(0,3);
  tau2_sd_raw ~ cauchy(0,3);
  
  lr2_raw ~ normal(0,1);
  tau2_raw ~ normal(0,1);
  
  omega_mu_raw ~ normal(0,1);
  omega_sd_raw ~ cauchy(0,3);
  omega_raw ~ normal(0,1);
  
  for (s in 1:nSubjects){
    vector[2] v1; //  expectation of value for green card and blue card
    real pe1;  // prediction error
    v1 = initv1;
    
    vector[2] v2; // expectation of value for following advice and not following advice
    real pe2;  // prediction error
    v2 = initv2;
    
    for (t in 1:nTrials[s]){
      // combine own experience and advice-following experience
      choice_green[s,t] ~ categorical(omega[s]*softmax(tau2[s]*v2) + (1-omega[s])*softmax(tau1[s]*v1));

     //  own experience
     pe1 = choice_acc[s,t] - v1[2-choice_green[s,t]];
     v1[2-choice_green[s,t]] = v1[2-choice_green[s,t]] + lr1[s] * pe1;
     //  advice following experience
     pe2 = advice_acc[s,t] - v2[2-advice_follow[s,t]];
     v2[2-advice_follow[s,t]] = v2[2-advice_follow[s,t]]+ lr2[s] * pe2;
    }
  }
}

here is my data:

dataList <- list(nSubjects=nSubjects,
                 nTrials=nTrials,
                 choice_green=choice_green_matrix,
                 advice_follow=advice_follow_matrix,
                 cond=cond_matrix,
                 choice_acc=choice_acc_matrix,
                 advice_acc=advice_acc_matrix
                 )

and my fit:

fit_rl <- stan(modelFile1,
                data   = dataList,
                chains = nChains,
                iter   = nIter,
                warmup = nWarmup,
                thin   = nThin,
                init   = 'random',
                seed   = 145015634)

And I have tried running with chains = 1, but the error message is the same.

Sorry the initial model is too complex. I have simplified it as following, but the error message is still the same.

data {
  int<lower=1> nSubjects;
  int<lower=1> nTrials;
  int<lower=1,upper=2> choice_green[nSubjects,nTrials];
  int<lower=-1,upper=1> choice_acc[nSubjects,nTrials];
}

transformed data {
  vector[2] initv;
  initv = rep_vector(0.0,2);
}

parameters {
  real<lower=0,upper=1> lr_mu;
  real<lower=0,upper=3> tau_mu;

  real<lower=0> lr_sd;
  real<lower=0> tau_sd;

  real<lower=0,upper=1> lr[nSubjects];
  real<lower=0,upper=3> tau[nSubjects];  
}

model {
  lr_sd  ~ cauchy(0,1);
  tau_sd ~ cauchy(0,3);
  
  lr ~ normal(lr_mu, lr_sd);
  tau ~ normal(tau_mu, tau_sd);
  
  for (s in 1:nSubjects){
    vector[2] v; 
    real pe;
    v = initv;
    
    for (t in 1:nTrials){
     choice_green[s,t] ~ categorical_logit(tau[s]*v);
     
     pe = choice_acc[s,t] - v[choice_green[s,t]];
     v[choice_green[s,t]] = v[choice_green[s,t]] + lr[s] * pe;
    }
  }
}

Hi @Nico, welcome to the Stan forum. Would you be able to attach some data (real or fake) or some code to simulate fake data that can be used to run your model? It will help someone figure out the problem if they are able to test your code, but that requires your dataList object (or a simulated/fake version of it).

Many thanks for your reply!! I uploaded my fake data.
And here is my dataList:

dataList <- list(nSubjects=nSubjects,
                 nTrials=100, 
                 choice_green=rl_mp[,,1], 
                 choice_acc=rl_mp[,,2])

And here is my fitting code:

fit_rl <- stan(modelFile, 
                data    = dataList, 
                chains  = 2,
                iter    = 2000,
                warmup  = 2000/2,
                thin    = 1,
                init    = "random",
                seed    = 1450154626
)
print(fit_rl)

rl_mp.RData (697 Bytes)

Thanks. I was actually able to run this without getting any error about it not containing samples. Does this happen for you only for this model or when you run any model? For example, here’s one of the examples from the documentation of the stan function, are you able to run it without any error messages?

library(rstan)
scode <- "
parameters {
  array[2] real y;
}
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
}
"
fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE)
print(fit1)

Also which version of rstan do you have installed (you can check with packageVersion("rstan"))?

I just ran this stan function, and there’s no error messages.
My rstan version is 2.32.5

Ok so your rstan seems to be working ok if you can run that code. Using the exact data that you shared in your previous message can you run the following code?

model_code <- "
data {
  int<lower=1> nSubjects;
  int<lower=1> nTrials;
  int<lower=1,upper=2> choice_green[nSubjects,nTrials];
  int<lower=-1,upper=1> choice_acc[nSubjects,nTrials];
}

transformed data {
  vector[2] initv;
  initv = rep_vector(0.0,2);
}

parameters {
  real<lower=0,upper=1> lr_mu;
  real<lower=0,upper=3> tau_mu;

  real<lower=0> lr_sd;
  real<lower=0> tau_sd;

  real<lower=0,upper=1> lr[nSubjects];
  real<lower=0,upper=3> tau[nSubjects];  
}

model {
  lr_sd  ~ cauchy(0,1);
  tau_sd ~ cauchy(0,3);
  
  lr ~ normal(lr_mu, lr_sd);
  tau ~ normal(tau_mu, tau_sd);
  
  for (s in 1:nSubjects){
    vector[2] v; 
    real pe;
    v = initv;
    
    for (t in 1:nTrials){
     choice_green[s,t] ~ categorical_logit(tau[s]*v);
     
     pe = choice_acc[s,t] - v[choice_green[s,t]];
     v[choice_green[s,t]] = v[choice_green[s,t]] + lr[s] * pe;
    }
  }
}
"

dataList <- list(nSubjects=10,
                 nTrials=100, 
                 choice_green=rl_mp[,,1], 
                 choice_acc=rl_mp[,,2])

# run in two steps, first compile then fit 
mod <- stan_model(model_code = model_code)
fit <- sampling(mod, data = dataList, chains = 1)
print(fit)

Does that still give you the same error as before? Or can you print the fitted model object successfully?

(As an aside, your model is also using some outdated syntax for arrays, but I don’t think that’s the source of the issue here. You’ll just want to update that in order to use future versions of Stan that will require it but it should still currently work using rstan. For example, real<lower=0,upper=1> lr[nSubjects] will become array[nSubjects] real<lower=0,upper=1> lr. But you can worry about that after we figure out the error.)