In my model, I have 10 options (from 1-10) for each subject and each trial to choose (expectancy
). I calculated the value for each option based on the rule in the graph below, so the value for each option updated based on the difference between shock
and v
in every trial (multiply alpha
). Then, I used softmax rule to transform v
for each option to a certain probability with the same function in this threat: JAGS errors: "Resolving undeclared variables" and "Invalid vector argument to exp".
I guess the problem here is I can't make jags update the value for the same choice.
data: expectancy
= number from 1-10 in each trial. shock
=number either 1 or 0 in each trial. (I provided example data below)
The second plot is how this be done in stan with 2 choices/1 subject situation.
RW_model <- function(){
# data
for(i in 1:nsubjects) # for each person
{
# initial value for v
v [i,1,expectancy[i,1]] <- 0
for (j in 2:ntrials) # for each trial
{
# expectancy chosen
expectancy[i,j] ~ dcat(mu[i,j,1:10])
predk[i,j] ~ dcat(mu[i,j,1:10])
# softmax rule to calculate values of each expectancy for each subject
# tau is the value sensitivity parameter
mu[i,j,1:10] <- exp_v[i,j,1:10] / sum(exp_v[i,j,1:10])
exp_v[i,j,expectancy[i,j-1]] <- exp(v[i,j,expectancy[i,j-1]]/tau[i])
# prediction error: difference between feedback and learned values of the chosen expectancy
pe [i,j-1] <- shock [i,j-1] - v [i,j-1,expectancy[i,j-1]]
# value updating process for expectancy
v [i,j,expectancy[i,j-1]] <- v [i,j-1,expectancy[i,j-1]] + alpha [i] * pe [i,j-1]
}
}
# priors
for (i in 1:nsubjects){
tau [i] ~ dunif (0,3)
alpha [i] ~ dunif (0,1)
}
}
# example data/ initial value/ parameters
nsubjects <- 42
ntrials <- 14
shock <- matrix(c(0,0,1,1,0,0,1,1,0,0,1,0,1,0),nrow=42,ncol = 14,byrow = T)
expectancy <- matrix(c(1,2,3,4,5,6,7,7,8,8,7,10,10,00),nrow=42,ncol = 14,byrow = T)
data <- list('shock','nsubjects','ntrials','expectancy')
myinits <- list(list(tau = runif (42,0,3),
alpha = runif (42,0,1)))
parameters <- c("tau",'alpha','v','predk')
# jags sampling
samples <- jags(data, inits=myinits, parameters,
model.file = RW_model,
n.chains=1, n.iter=1000, n.burnin=500, n.thin=1, DIC=T)