I am currently trying to reproduce the circular inference model sketched in this paper: https://www.nature.com/articles/ncomms14218 .
data{ int<lower=1> N; int<lower=1> N_Participant; int ChoiceRed[N]; int Participant[N]; real lSelf[N]; real lOthers[N];}parameters{ vector[N_Participant] a; real muA; vector[N_Participant] wOthers; real muO; vector[N_Participant] wSelf; real muS;}model{ vector[N] mu; muS ~ normal( 0.5 , 0.1 ); wSelf ~ normal( muS , 0.3 ); muO ~ normal( 0.5 , 0.1 ); wOthers ~ normal( muO , 0.3 ); muA ~ normal( 0.5 , 0.1 ); a ~ normal( muA , 0.1 ); for ( i in 1:N ) { mu[i] = a[Participant[i]] + log((wSelf[Participant[i]] * exp(lSelf[i]) + 1 - wSelf[Participant[i]])/((1 - wSelf[Participant[i]]) * exp(lSelf[i]) + wSelf[Participant[i]])) + log((wOthers[Participant[i]] * exp(lOthers[i]) + 1 - wOthers[Participant[i]])/((1 - wOthers[Participant[i]]) * exp(lOthers[i]) + wOthers[Participant[i]])); } ChoiceRed ~ binomial_logit( 1 , mu );}generated quantities{ vector[N] mu; real dev; dev = 0; for ( i in 1:N ) { mu[i] = a[Participant[i]] + log((wSelf[Participant[i]] * exp(lSelf[i]) + 1 - wSelf[Participant[i]])/((1 - wSelf[Participant[i]]) * exp(lSelf[i]) + wSelf[Participant[i]])) + log((wOthers[Participant[i]] * exp(lOthers[i]) + 1 - wOthers[Participant[i]])/((1 - wOthers[Participant[i]]) * exp(lOthers[i]) + wOthers[Participant[i]])); } dev = dev + (-2)*binomial_logit_lpmf( ChoiceRed | 1 , mu );}
data{ int<lower=1> N; int<lower=1> N_Participant; int ChoiceRed[N]; int Participant[N]; real lSelf[N]; real lOthers[N];}parameters{ vector[N_Participant] a; vector[N_Participant] aSelf; vector[N_Participant] aOthers; vector[N_Participant] wSelf; vector[N_Participant] wOthers; real muA; real muS; real muO; real<lower=0> scaleS; real<lower=0> scaleO;}model{ vector[N] mu; scaleO ~ normal( 0 , 3 ); scaleS ~ normal ( 0 , 3 ); muO ~ normal( 0 , 1 ); muS ~ normal( 0 , 1 ); muA ~ normal( 0 , 1 ); wOthers ~ normal( muO , 3 ); wSelf ~ normal( muS , 3 ); for ( j in 1:N_Participant ) aOthers[j] ~ cauchy( 0 , scaleO ); for ( j in 1:N_Participant ) aSelf[j] ~ cauchy( 0 , scaleS ); a ~ normal( muA , 3 ); for ( i in 1:N ) { mu[i] = a[Participant[i]]+ #F(lSelf + fOthers + fSelf, wSelf) + log((wSelf[Participant[i]]*exp(lSelf + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) )+ 1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(lSelf + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) )+ wSelf[Participant[i]])) + #F(lOthers + fSelf + fOthers,wOthers) log((wOthers[Participant[i]]*exp(lOthers + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) )+ 1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(lOthers + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) )+wOthers[Participant[i]])) } ChoiceRed ~ binomial_logit( 1 , mu );}generated quantities{ vector[N] mu; real dev; dev = 0; for ( i in 1:N ) { mu[i] = a[Participant[i]]+ #F(lSelf + fOthers + fSelf, wSelf) + log((wSelf[Participant[i]]*exp(lSelf + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) )+ 1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(lSelf + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) )+ wSelf[Participant[i]])) + #F(lOthers + fSelf + fOthers,wOthers) log((wOthers[Participant[i]]*exp(lOthers + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) )+ 1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(lOthers + log((wSelf[Participant[i]]*exp(aSelf[Participant[i]]*lSelf)+1-wSelf[Participant[i]])/((1-wSelf[Participant[i]])*exp(aSelf[Participant[i]]*lSelf)+wSelf[Participant[i]])) + log((wOthers[Participant[i]]*exp(aOthers[Participant[i]]*lOthers)+1-wOthers[Participant[i]])/((1-wOthers[Participant[i]])*exp(aOthers[Participant[i]]*lOthers)+wOthers[Participant[i]])) )+wOthers[Participant[i]])) } dev = dev + (-2)*binomial_logit_lpmf( ChoiceRed | 1 , mu );}functions { real F(real L, real w) { real eL = exp(L);
return log((w * eL + 1 - w)/((1 - w) * eL + w)); }}
data{ int<lower=1> N; int<lower=1> N_Participant; int ChoiceRed[N]; int Participant[N]; real lSelf[N]; real lOthers[N];}
parameters{ vector[N_Participant] a; real muA; vector[N_Participant] wOthers; real muO; vector[N_Participant] wSelf; real muS;}
model{ vector[N] mu; muS ~ normal( 0.5 , 0.1 ); wSelf ~ normal( muS , 0.3 ); muO ~ normal( 0.5 , 0.1 ); wOthers ~ normal( muO , 0.3 ); muA ~ normal( 0.5 , 0.1 ); a ~ normal( muA , 0.1 );
for ( i in 1:N ) { mu[i] = a[Participant[i]] + F(lSelf[i], wSelf[Participant[i]]) + F(lOthers[i], wOthers[Participant[i]]); }
ChoiceRed ~ binomial_logit( 1 , mu );}functions { real F(real L, real w) { real eL = exp(L);
return log((w * eL + 1 - w)/((1 - w) * eL + w)); }}
data{ int<lower=1> N; int<lower=0, upper=1> ChoiceRed[N]; real lSelf[N]; real lOthers[N]; int<lower=1> P; int<lower=1, upper=P> Participant[N];}
parameters{ vector[P] a; vector[P] wOthers; vector[P] wSelf;}
model{ vector[N] mu; wSelf ~ normal( 0.5 , 0.1 ); wOthers ~ normal( 0.5 , 0.1); a ~ normal( 0.5 , 0.1 );
for ( i in 1:N ) { mu[i] = a[Participant[i]] + F(lSelf[i], wSelf[Participant[i]]) + F(lOthers[i], wOthers[Participant[i]]); }
ChoiceRed ~ bernoulli_logit(mu);}
generated quantities{ vector[N] mu; real dev; dev = 0; for ( i in 1:N ) { mu[i] = a[Participant[i]] + F(lSelf[i], wSelf[Participant[i]]) + F(lOthers[i], wOthers[Participant[i]]); } dev = dev + (-2)*bernoulli_logit_lpmf( ChoiceRed | mu );}functions { real F(real L, real w) { real eL = exp(L); return log((w * eL + 1 - w)/((1 - w) * eL + w)); }}
data{ int<lower=1> N; int<lower=0, upper=1> ChoiceRed[N]; real lSelf[N]; real lOthers[N]; int<lower=1> P; int<lower=1, upper=P> Participant[N];}
parameters{ vector[P] a; vector[P] wOthers; vector[P] wSelf;}
model{ vector[N] mu; wSelf ~ normal( 0.5 , 0.3 ); wOthers ~ normal( 0.5 , 0.3 ); a ~ normal( 0.5, 0.1 );
for ( i in 1:N ) { mu[i] = a[Participant[i]] + F(lSelf[i] + F(aOthers[Participant[i]]*lOthers[i],wOthers[Participant[i]]) + F(aSelf[Participant[i]]*lSelf[i],wSelf[Participant[i]]), wSelf[Participant[i]]) + F(lOthers[i] + F(aSelf[Participant[i]]*lSelf[i],wSelf[Participant[i]]) + F(aOthers[Participant[i]]*lOthers[i],wOthers[Participant[i]]), wOthers[Participant[i]]) }
ChoiceRed ~ bernoulli_logit(mu);}
generated quantities{ vector[N] mu; real dev; dev = 0; for ( i in 1:N ) { mu[i] = a[Participant[i]] + F(lSelf[i] + F(aOthers[Participant[i]]*lOthers[i],wOthers[Participant[i]]) + F(aSelf[Participant[i]]*lSelf[i],wSelf[Participant[i]]), wSelf[Participant[i]]) + F(lOthers[i] + F(aSelf[Participant[i]]*lSelf[i],wSelf[Participant[i]]) + F(aOthers[Participant[i]]*lOthers[i],wOthers[Participant[i]]), wOthers[Participant[i]]) } dev = dev + (-2)*bernoulli_logit_lpmf( ChoiceRed | mu );}data <- list( Participant = as.integer(d1$Participant), ChoiceRed = d1$ChoiceRed, lSelf= d1$lSelf, lOthers= d1$lOthers, N=nrow(d1), P = length(unique(d1$Participant)))
fit3 <- stan( file = "CircularInference.stan", data = data, chains = 2, warmup = 3e3, iter = 1e4, cores = 2, refresh = 1000, init=0)