In my function, below I want to store a value from an array. However, it seems my variable declarations are not correct. How can i assign a value from an array to a real number?.

functions{
real tenismodel_lpdf(matrix dat, vector[] theta, vector[] alpha){
vector[rows(dat)] prob;
real t;
real x;
int s;
int re;
real out;
real a;
real f;
real ws;
real rs;
real ms;
real wr;
real rr;
real mr;
for (i in 1:rows(dat)){
t <- dat[i, 1];
x <- dat[i, 2];
s <- dat[i, 3];
re <- dat[i, 4];
a <- theta[s, 1];
f <- theta[s, 2];
ws <- alpha[s, 1];
rs <- alpha[s, 2];
ms <- alpha[s, 3];
wr <- alpha[re, 1];
rr <- alpha[re, 2];
mr <- alpha[re, 3];
if(x == -1){
prob[i] <- f;
}else if(t == 1){
prob[i] <- a^x * f^(1 - x);
}else if(fmod(t, 2) == 0){
prob[i] <- ((1- a - f) * (rr)^(t/2.0 - 1.0) * (rs)^(t/2.0 - 1.0) * (mr)^x * (wr)^(1-x));
}else {
prob[i] <- ((1- a - f) * (rr)^(t/2.0 - 0.5) * (rs)^(t/2.0 - 1.5) * (ms)^(1 - x) * (ws)^x);
}
}
out <- sum(log(prob));
return out;
}
}

I am getting an error “Ill-typed arguments supplied to assignment operator <-: lhs has type int and rhs has type real”

I can understand this is because, my s and re variables are integers (they should be integers because I am using again to extract values from an array) but dat[i, 3] and dat[i, 4] are real. How I can overcome this problem?.

I was working with this and I changed the functions by adding new arguments to my tenismodel_lpdf function. These two arguments are integer arrays, so that I can assign them to indexing. However, now my question is how I assign these two arguments in my model block?.

// Create user define function
functions{
real tenismodel_lpdf(matrix dat, vector[] theta, vector[] alpha, int[] ServerID, int[] ReceiverID){
vector[rows(dat)] prob;
real t;
real x;
int s;
int re;
real out;
real a;
real f;
real ws;
real rs;
real ms;
real wr;
real rr;
real mr;
for (i in 1:rows(dat)){
t <- dat[i, 1];
x <- dat[i, 2];
s <- ServerID[i];
re <- ReceiverID[i];
a <- theta[s, 1];
f <- theta[s, 2];
ws <- alpha[s, 1];
rs <- alpha[s, 2];
ms <- alpha[s, 3];
wr <- alpha[re, 1];
rr <- alpha[re, 2];
mr <- alpha[re, 3];
if(x == -1){
prob[i] <- f;
}else if(t == 1){
prob[i] <- a^x * f^(1 - x);
}else if(fmod(t, 2) == 0){
prob[i] <- ((1- a - f) * (rr)^(t/2.0 - 1.0) * (rs)^(t/2.0 - 1.0) * (mr)^x * (wr)^(1-x));
}else {
prob[i] <- ((1- a - f) * (rr)^(t/2.0 - 0.5) * (rs)^(t/2.0 - 1.5) * (ms)^(1 - x) * (ws)^x);
}
}
out <- sum(log(prob));
return out;
}
}
// The input data is a vector 'y' of length 'N'.
data {
int N; // number of observations (rally lengths)
int M; // number of players
matrix [N, 4] dat; // touches, indicator
int ServerID [N];
int ReceiverID [N];
int <lower = 2> m;
row_vector [3] beta1;
row_vector [3] beta2;
}
parameters {
simplex [m] theta_1 [M];
simplex [m] theta_2 [M];
}
// The model to be estimated.
model {
// Define priors
theta_1 ~ dirichlet(beta1);
theta_2 ~ dirichlet(beta2);
dat ~ tenismodel(theta_1, theta_2, ServerID, ReceiverID);
}

I am getting an error; “Ill-typed arguments to ‘~’ statement. No distribution ‘tenismodel’ was found with the correct signature.”

Are you using RStan? The reason I ask is because you’re using pretty old Stan syntax (some of it is deprecated) and RStan is still a few versions behind the rest of the Stan interfaces. If you’re using RStan I would suggest switching to CmdStanR:

# install cmdstanr R package
remotes::install_github("stan-dev/cmdstanr")
# install CmdStan (cmdstanr runs the command line interface CmdStan for you)
cmdstanr::install_cmdstan()

Then you can use this more modern version of your Stan code (using the newer array syntax as well as = instead of <-):

functions{
real tenismodel_lpdf(matrix dat, array[] vector theta, array[] vector alpha, array[] int ServerID, array[] int ReceiverID){
vector[rows(dat)] prob;
real t;
real x;
int s;
int re;
real out;
real a;
real f;
real ws;
real rs;
real ms;
real wr;
real rr;
real mr;
for (i in 1:rows(dat)){
t = dat[i, 1];
x = dat[i, 2];
s = ServerID[i];
re = ReceiverID[i];
a = theta[s, 1];
f = theta[s, 2];
ws = alpha[s, 1];
rs = alpha[s, 2];
ms = alpha[s, 3];
wr = alpha[re, 1];
rr = alpha[re, 2];
mr = alpha[re, 3];
if(x == -1){
prob[i] = f;
}else if(t == 1){
prob[i] = a^x * f^(1 - x);
}else if(fmod(t, 2) == 0){
prob[i] = ((1- a - f) * (rr)^(t/2.0 - 1.0) * (rs)^(t/2.0 - 1.0) * (mr)^x * (wr)^(1-x));
}else {
prob[i] = ((1- a - f) * (rr)^(t/2.0 - 0.5) * (rs)^(t/2.0 - 1.5) * (ms)^(1 - x) * (ws)^x);
}
}
out = sum(log(prob));
return out;
}
}
// The input data is a vector 'y' of length 'N'.
data {
int N; // number of observations (rally lengths)
int M; // number of players
matrix [N, 4] dat; // touches, indicator
array[N] int ServerID;
array[N] int ReceiverID;
int<lower = 2> m;
row_vector[3] beta1;
row_vector[3] beta2;
}
parameters {
array[M] simplex[m] theta_1;
array[M] simplex[m] theta_2;
}
// The model to be estimated.
model {
// Define priors
theta_1 ~ dirichlet(beta1);
theta_2 ~ dirichlet(beta2);
dat ~ tenismodel(theta_1, theta_2, ServerID, ReceiverID);
}

I haven’t examined the model too closely (just trying to debug the error), but when I try compiling this with cmdstanr I actually don’t get any errors (I haven’t tried running it since I don’t have the data, so there could be other issues at that point). Check out this vignette for how to compile models with cmdstanr: Getting started with CmdStanR • cmdstanr