# Ill-typed arguments to '~' statement. No distribution 'categorical' was found with the correct signature

Hello,
I’m a beginner doing with Stan and met some problems. In my model, there are 4 strategies for a decision task. Participants are assumed to be able to switch strategies 3 times, so the task can be divided into 4 stages. The strategies for each stage are represented by the variable ‘z’, where
z[i,1] \sim categorical(piPrime), piPrime \sim dirichlet([1,1,1,1])
Here, z[i,1] represents the strategy participant i used in stage 1.

Here’s a snippet of my code (maybe too lengthy, feel free to focus on the several initial lines):

``````data {
int<lower=1> nsubj;
int<lower=1> ntrials;
matrix[nsubj, ntrials] ttb_theta;
matrix[nsubj, ntrials] tally_theta;
matrix[nsubj, ntrials] guess_theta;
matrix[nsubj, ntrials] decision;
}
parameters {
real<lower=0.5, upper=1> mu;
real<lower=0, upper=0.5> sigma;
simplex[4] piPrime;
array[4] simplex[4] pi;
array[nsubj] vector[4] z;
vector<lower=0.5, upper=1>[nsubj] gamma;
array[nsubj] ordered[3] tau;
vector<lower=0, upper=0.5>[nsubj] epsilon;
}
model {
piPrime ~ dirichlet([1,1,1,1]);
pi[1] ~ dirichlet([0,1,1,1]);
pi[2] ~ dirichlet([1,0,1,1]);
pi[3] ~ dirichlet([1,1,0,1]);
pi[4] ~ dirichlet([1,1,1,0]);
for (i in 1:nsubj) {
gamma[i] ~ normal(mu, sigma);
tau[i] ~ uniform(1, gamma[i]*ntrials/(1-gamma[i]));
z[i,1] ~ categorical(piPrime);
z[i,2] ~ categorical(pi[z[i,1]]);
z[i,3] ~ categorical(pi[z[i,2]]);
z[i,4] ~ categorical(pi[z[i,3]]);
for (t in 1:ntrials) {
if (t < tau[i,1]) {
strategy = z[i,1];
}
else if (tau[i,1] <= t < tau[i,2]) {
strategy = z[i,2];
}
else if (tau[i,2] <= t < tau[i,3]) {
strategy = z[i,3];
}
else {
strategy = z[i,4];
}
if (strategy == 1) {
if (ttb_theta[i,j] == 1) {
decision[i,j] ~ bernoulli(1-epsilon[i]);
}
else if (ttb_theta[i,j] == 0) {
decision[i,j] ~ bernoulli(epsilon[i]);
}
else {
decision[i,j] ~ bernoulli(0.5);
}
}
if (strategy == 2) {
if (tally_theta[i,j] == 1) {
decision[i,j] ~ bernoulli(1-epsilon[i]);
}
else if (tally_theta[i,j] == 0) {
decision[i,j] ~ bernoulli(epsilon[i]);
}
else {
decision[i,j] ~ bernoulli(0.5);
}
}
if (strategy == 3) {
if (wadd_theta[i,j] == 1) {
decision[i,j] ~ bernoulli(1-epsilon[i]);
}
else if (wadd_theta[i,j] == 0) {
decision[i,j] ~ bernoulli(epsilon[i]);
}
else {
decision[i,j] ~ bernoulli(0.5);
}
}
else {
decision[i,j] ~ bernoulli(0.5);
}
}
}
}
``````

I’ve encountered the following error:
Semantic error in ‘string’, line 29, column 4 to column 34:
Ill-typed arguments to ‘~’ statement. No distribution ‘categorical’ was found with the correct signature.

Any help is appreciated!

Welcome to the Stan Discourse.

The direct answer is that `z` is an array of vectors (of unconstraiend continuous parameters) but the categorical distribution requires that the variable be discrete.

This can’t be solved simply by turning `z` into an array of integers because Stan doesn’t allow integer parameters. Instead, you’d either have to pass `z` as data or marginalize over the discrete parameters. You can find information about marginalization here and in the Stan docs.

Thank u so much! I found the method for marginalization in the manual. I’ll study it and hope it can help resolve the issue. Appreciate your response!