My data is the data of reversal learning task consisting of 4 blocks. There are two choices(1 and 2) and the values of two choices reversed when a block is switched to the next block. I have 52 participants and in most cases, participants completed 120 trials. I am trying to fit a hierarchical reinforcement learning model to my data using stan. The problem is that when I use loo function to compute the looic value, most of Pareto k diagnostic values are bad or very bad as follows:
Computed from 12000 by 52 log-likelihood matrix
Estimate SE
elpd_loo -2282.4 155.6
p_loo 65.4 3.4
looic 4564.8 311.1
Monte Carlo SE of elpd_loo is NA.
Pareto k diagnostic values:
Count Pct. Min. n_eff
(-Inf, 0.5] (good) 0 0.0%
(0.5, 0.7] (ok) 8 15.4% 112
(0.7, 1] (bad) 36 69.2% 13
(1, Inf) (very bad) 8 15.4% 12
See help(‘pareto-k-diagnostic’) for details.
Does this mean that my model is misspecified? Is there any problem in my code? I attached my data and code.
Thank you for helping me.
RLdata.txt (171.7 KB)
data {
int<lower=1> N; // Number of subjectsint<lower=1> B; // Maximum number of blocks across subjects
int<lower=1> Bsubj[N]; // Number of blocks for each subjectint<lower=0> T; // Maximum number of trials across subjects
int<lower=0, upper=T> Tsubj[N, B]; // Number of trials/blocks for each subjectint<lower=-1, upper=2> choice[N, B, T]; // The choices subjects made
real outcome[N, B, T]; // The outcome
}transformed data {
// Default value for (re-)initializing parameter vectors
vector[2] initV;
initV = rep_vector(0.0, 2);
}// Declare all parameters as vectors for vectorizing
parameters {
// Hyper(group)-parameters
vector[2] mu_pr;
vector<lower=0>[2] sigma;// Subject-level raw parameters (for Matt trick)
vector[N] alpha_pr; // learning rate
vector[N] beta_pr; // inverse temperature
}transformed parameters {
// Transform subject-level raw parameters
vector<lower=0, upper=1>[N] alpha;
vector<lower=0, upper=10>[N] beta;for (i in 1:N) {
alpha[i] = Phi_approx(mu_pr[1] + sigma[1] * alpha_pr[i]);
beta[i] = Phi_approx(mu_pr[2] + sigma[2] * beta_pr[i]) * 10;
}
}model {
// Hyperparameters
mu_pr ~ normal(0, 1);
sigma ~ normal(0, 1);// individual parameters
alpha_pr ~ normal(0, 1);
beta_pr ~ normal(0, 1);for (i in 1:N) {
for (bIdx in 1:Bsubj[i]) { // new
// Define Values
vector[2] ev; // Expected value
real PE; // Prediction error// Initialize values ev = initV; // Initial ev values for (t in 1:Tsubj[i, bIdx]) { // Softmax choice choice[i, bIdx, t] ~ categorical_logit(ev * beta[i]); // Prediction Error PE = outcome[i, bIdx, t] - ev[choice[i, bIdx, t]]; // Update expected value of chosen stimulus ev[choice[i, bIdx, t]] += alpha[i] * PE; } }
}
}generated quantities {
// For group level parameters
real<lower=0, upper=1> mu_alpha;
real<lower=0, upper=10> mu_beta;// For log likelihood calculation
real log_lik[N];// For model regressors
real ev_c[N, B, T]; // Expected value of the chosen option
real ev_nc[N, B, T]; // Expected value of the non-chosen option
real pe[N, B, T]; // Prediction error// For posterior predictive check
real y_pred[N, B, T];// Initialize all the variables to avoid NULL values
for (i in 1:N) {
for (b in 1:B) {
for (t in 1:T) {
ev_c[i, b, t] = 0;
ev_nc[i, b, t] = 0;
pe[i, b, t] = 0;y_pred[i, b, t] = -1; } }
}
mu_alpha = Phi_approx(mu_pr[1]);
mu_beta = Phi_approx(mu_pr[2]) * 10;{ // local section, this saves time and space
for (i in 1:N) {log_lik[i] = 0; for (bIdx in 1:Bsubj[i]) { // new // Define values vector[2] ev; // Expected value real PE; // prediction error // Initialize values ev = initV; // initial ev values for (t in 1:Tsubj[i, bIdx]) { // Softmax choice log_lik[i] += categorical_logit_lpmf(choice[i, bIdx, t] | ev * beta[i]); // generate posterior prediction for current trial y_pred[i, bIdx, t] = categorical_rng(softmax(ev * beta[i])); // Prediction Error PE = outcome[i, bIdx, t] - ev[choice[i, bIdx, t]]; // Store values for model regressors ev_c[i, bIdx, t] = ev[choice[i, bIdx, t]]; ev_nc[i, bIdx, t] = ev[1 - choice[i, bIdx, t]]; pe[i, bIdx, t] = PE; // Update expected value of chosen stimulus ev[choice[i, bIdx, t]] += alpha[i] * PE; } } }
}
}