Hello STAN experts,
I’m running a hierarchical model for predicting categorically distributed data – subjective ratings made at different time points on a 0-10 scale, before and after receiving social feedback. In the model I aim to estimate the effect the social feedback had on changes in participants’ ratings.
In the model the probabilities for each possible categorical outcome (i.e. 11 possible outcomes and 10 thresholds) are computed in the transformed parameters block. The model was primarily based on the examples presented in chapter 23 in John Kruschke’s book (Doing Bayesian Data Analysis) on predicting ordinal variables. Following Kruschke, I used a normal_cdf function for estimating the probabilities of each outcome. The first and last thresholds are fixed, and the non-end thresholds are free parameters. However, Kruschke’s example is written in JAGS, so I had to make two main adaptations for STAN:
- In JAGS each categorical outcome is distributed normally as follows – pr[i,K] <- pnorm( thresh[1] , mu , 1/sigmaˆ2 ). In my model variance was transformed based on this formula:
- In the JAGS model only one vector is used for the thresholds. This vector consists of fixed thresholds at its ends, and NaNs are placed in the non-end positions. I had to split the threshold vector into two separate components: a data vector for the fixed values, and a parameter vector for the non-end thresholds which are estimated.
The code compiles successfully, but I’m constantly receiving the following error message:
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: categorical_lpmf: Probabilities parameter is not a valid simplex. sum(Probabilities parameter) = 3.111144369, but should be 1 (in ‘model1068159777f0_2f29cae781c6aa44e4c61751dc2681b3’ at line 139)
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: categorical_lpmf: Probabilities parameter is not a valid simplex. sum(Probabilities parameter) = 2.786957506, but should be 1 (in ‘model1068159777f0_2f29cae781c6aa44e4c61751dc2681b3’ at line 139)
[…]
(this repeats several times).
The model is very long because of the loop for the theta probabilities computation – but note that the same calculation repeats in this part. I also attached the R code and data for 2 participants:
data{
int <lower=1> Nsubj ; // number of participants
int <lower=1> Ntotal ; // number of trials
int <lower=1> nYlevels ; // number of responses on scale (0-10 scale, so 11 responses)
int <lower=1> s[Ntotal] ; // index of subject at a given trial
real <lower=1> fixedThresh[nYlevels-9] ; // lower and upper thresholds are fixed, not stochastic
int yPreA[Ntotal] ; // participants’ ratings before feedback
int yJud[Ntotal] ; // social feedback
int yPost[Ntotal] ; // participants’ ratings after feedback
}
parameters {
vector[nYlevels-3] thresh ; // non-fixed thresholds
vector[Nsubj] muMuPreA ;
real mu_muMuPreAGroup ;
real <lower=0> sigma2_muMuPreAGroup ;
vector[Nsubj] muMuJud ;
real mu_muMuJudGroup ;
real <lower=0> sigma2_muMuJudGroup ;
vector<lower=0>[Nsubj] sigma2MuPreA ;
real <lower=0> alphaSigmaMuPreAGroup ;
real <lower=0> betaSigmaMuPreAGroup ;
vector<lower=0>[Nsubj] sigma2MuJud ;
real<lower=0> alphaSigmaMuJudGroup ;
real<lower=0> betaSigmaMuJudGroup ;
vector<lower=0>[Nsubj] tau2ThetaPreA ;
real<lower=0> alphaTauThetaPreA ;
real<lower=0> betaTauThetaPreA ;
vector<lower=0>[Nsubj] tau2ThetaJud ;
real<lower=0> alphaTauThetaJud ;
real<lower=0> betaTauThetaJud ;
}
transformed parameters {
vector[Nsubj] precisPreA = inv(sigma2MuPreA+tau2ThetaPreA) ;
vector[Nsubj] precisJud = inv(sigma2MuJud+tau2ThetaJud) ;
vector[nYlevels] thetaPreA[Ntotal] ;
vector[nYlevels] thetaJud[Ntotal] ;
vector[nYlevels] thetaPost[Ntotal] ;
for (i in 1:Ntotal) {
thetaPreA[i,1] <- normal_cdf(fixedThresh[1], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1) ;
thetaJud[i,1] <- normal_cdf(fixedThresh[1], muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1) ;
thetaPost[i,1] <- normal_cdf(fixedThresh[1], (precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]]))) ;
thetaPreA[i,2] <- fmax(0, normal_cdf(thresh[1], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1) -
normal_cdf(fixedThresh[1], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1)) ;
thetaJud[i,2] <- fmax(0, normal_cdf(thresh[1],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1) -
normal_cdf(fixedThresh[1],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1)) ;
thetaPost[i,2] <- fmax(0, normal_cdf(thresh[1],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]]))) -
normal_cdf(fixedThresh[1],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]])))) ;
for (k in 3:nYlevels-2) {
thetaPreA[i,k] <- fmax(0, normal_cdf(thresh[k-1], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1) -
normal_cdf(thresh[k-2], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1)) ;
thetaJud[i,k] <- fmax(0, normal_cdf(thresh[k-1],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1) -
normal_cdf(thresh[k-2],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1)) ;
thetaPost[i,k] <- fmax(0, normal_cdf(thresh[k-1],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]]))) -
normal_cdf(thresh[k-2],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]])))) ;
}
thetaPreA[i,10] <- fmax(0, normal_cdf(fixedThresh[2], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1) -
normal_cdf(thresh[nYlevels-3], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1)) ;
thetaJud[i,10] <- fmax(0, normal_cdf(fixedThresh[2],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1) -
normal_cdf(thresh[nYlevels-3],muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1)) ;
thetaPost[i,10] <- fmax(0, normal_cdf(fixedThresh[2],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]]))) -
normal_cdf(thresh[nYlevels-3],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]])))) ;
thetaPreA[i,nYlevels] <- normal_cdf(fixedThresh[2], muMuPreA[s[i]], (sqrt(precisPreA[s[i]]))^-1) ;
thetaJud[i,nYlevels] <- normal_cdf(fixedThresh[2], muMuJud[s[i]], (sqrt(precisJud[s[i]]))^-1) ;
thetaPost[i,nYlevels]<-normal_cdf(fixedThresh[2],(precisPreA[s[i]]*muMuPreA[s[i]]+precisJud[s[i]]*muMuJud[s[i]])/
(precisPreA[s[i]]+precisJud[s[i]]), sqrt(1/(precisPreA[s[i]]+precisJud[s[i]]))) ;
}
}
model {
// priors:
// thresholds:
for (k in 1:nYlevels-3) {
thresh[k] ~ normal(k+1.5, 2) ;
}
// Sigma for Mu of Mu-Trials:
alphaSigmaMuPreAGroup ~ gamma(0.01, 0.01) ;
betaSigmaMuPreAGroup ~ gamma(0.01, 0.01) ;
sigma2MuPreA ~ inv_gamma(alphaSigmaMuPreAGroup, betaSigmaMuPreAGroup) ;
alphaSigmaMuJudGroup ~ gamma(0.01, 0.01) ;
betaSigmaMuJudGroup ~ gamma(0.01, 0.01) ;
sigma2MuJud ~ inv_gamma(alphaSigmaMuJudGroup, betaSigmaMuJudGroup) ;
// Mu for Mu of Mu-Trials:
sigma2_muMuPreAGroup ~ inv_gamma(1, 1) ;
mu_muMuPreAGroup ~ normal((1+nYlevels)/2 , nYlevels) ; //
muMuPreA ~ normal(mu_muMuPreAGroup, sqrt(sigma2_muMuPreAGroup)) ;
sigma2_muMuJudGroup ~ inv_gamma(1, 1) ;
mu_muMuJudGroup ~ normal((1+nYlevels)/2 , nYlevels) ; //
muMuJud ~ normal(mu_muMuJudGroup, sqrt(sigma2_muMuJudGroup)) ;
// tau:
betaTauThetaPreA ~ gamma(0.01, 0.01) ; //
alphaTauThetaPreA ~ gamma(0.01, 0.01) ;
tau2ThetaPreA ~ inv_gamma(alphaTauThetaPreA, betaTauThetaPreA) ;
betaTauThetaJud ~ gamma(0.01, 0.01) ;
alphaTauThetaJud ~ gamma(0.01, 0.01) ;
tau2ThetaJud ~ inv_gamma(alphaTauThetaJud, betaTauThetaJud) ;
for (i in 1:Ntotal) {
yPreA[i] ~ categorical(thetaPreA[i, 1:nYlevels]) ;
yJud[i] ~ categorical(thetaJud[i, 1:nYlevels]) ;
yPost[i] ~ categorical(thetaPost[i, 1:nYlevels]) ;
}
}
I would greatly appreciate your thoughts about the possible source of thisyPreAJudPost_forForum.R (7.1 KB) yPreAJudPost_forForum.stan (5.7 KB) yPreAPreFbJudPostPreBOrd_GROUP_1-11_testSubj.csv (1.1 KB) error.
Thanks,
Ofir Shany