Hi everyone,
I am new to stan and still trying to figure out the language. I followed instructions online for an exercise, but it seems that stan has updated and some of the language logic has changed a bit.
Please see the replicable code and the error below, and give me some advice. Thank you very much.
d=data.frame(
subjID=c(rep(1:10,each=100)),
ntrials=rep(1:100,10),
choice=sample(c(1,2),size=1000,replace = T),
outcome=sample(c(1,-1),size=1000,replace = T)
)
library(cmdstanr)
stan_file=write_stan_file('
data{
int<lower=1,upper=10>nSubjects;
int<lower=1,upper=100> nTrials;
array[nSubjects,nTrials] int<lower=1,upper=2> choice; array[nSubjects,nTrials] int<lower=-1,upper=1> outcome;
}
parameters{
real<lower=0,upper=1> alpha_mu; real<lower=0> alpha_sd;
real<lower=0,upper=20> tau_mu; real<lower=0> tau_sd;
array[nSubjects] real<lower=0,upper=1> alpha;
array[nSubjects] real<lower=0,upper=20> tau;
}
model{
alpha~normal(alpha_mu,alpha_sd); tau~normal(tau_mu,tau_sd);
alpha_sd~cauchy(0,1);tau_sd~cauchy(0,20);
for (s in 1:nSubjects){
vector[2] v=rep_vector(0,2);
for(t in 1:nTrials){
choice[s,t]~categorical_logit(tau[s]*v);
v[choice[s,t]]+=alpha[s]*(outcome[s,t]-v[choice[s,t]]);
}
}
}
'
)
stan_d=list(nSubjects=10,nTrials=100,choice=d$choice,outcome=d$outcome)
stan_m=cmdstan_model(stan_file)
fit=stan_m$sample(data=stan_d)
Running MCMC with 4 sequential chains...
Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=choice; dims declared=(10,100); dims found=(1000) (in 'C:/Users/HUAWEI/AppData/Local/Temp/Rtmp8c0HJt/model-5490189352e5.stan', line 6, column 0 to column 53)
Warning: Chain 1 finished unexpectedly!
Chain 2 Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=choice; dims declared=(10,100); dims found=(1000) (in 'C:/Users/HUAWEI/AppData/Local/Temp/Rtmp8c0HJt/model-5490189352e5.stan', line 6, column 0 to column 53)
Warning: Chain 2 finished unexpectedly!
Chain 3 Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=choice; dims declared=(10,100); dims found=(1000) (in 'C:/Users/HUAWEI/AppData/Local/Temp/Rtmp8c0HJt/model-5490189352e5.stan', line 6, column 0 to column 53)
Warning: Chain 3 finished unexpectedly!
Chain 4 Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=choice; dims declared=(10,100); dims found=(1000) (in 'C:/Users/HUAWEI/AppData/Local/Temp/Rtmp8c0HJt/model-5490189352e5.stan', line 6, column 0 to column 53)
Warning: Chain 4 finished unexpectedly!
Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
2: No chains finished successfully. Unable to retrieve the fit.
I also tried the hbayesDM way for this model, because the code is much simpler. The error I got looks like this:
> library(hBayesDM)
Loading required package: Rcpp
This is hBayesDM version 1.2.1
Warning message:
package ‘hBayesDM’ was built under R version 4.5.1
> fit=bandit2arm_delta(data=d,adapt_delta = 0.8)
Error in is.null(data) || is.na(data) :
'length = 4000' in coercion to 'logical(1)'