# Real return type required error for binomial

Hi everybody.
I a new in using Stan. I searched in the FAQ but did not find a solution for my problem.
My model is not very complicated, but I have an error I do not understand. In my model block, when I specify the likelihood, it seems that the binomial function requires a real. The delta parameter is for me a vector of real numbers, so I do not understand the problem.

data {
int<lower=1> N;
vector[N] n0;
vector[N] n;
}

// The parameters of the model.
parameters {
vector<lower=0>[N] lambda;
}
// Transformed parameters. Here theta and delta calculated from lambda
transformed parameters {
vector[N] theta;
vector[N] delta;
theta = cumulative_sum(lambda);
delta = exp(-theta);
}
// The model to be estimated.
model {
vector[N] logitlambda;
real temp;
logitlambda = logit(lambda);
// prior
for (i in 1:61){
logitlambda[i] ~ normal(0, 1000);
}
temp ~ normal(0, 1000);
for (i in 62:N){
logitlambda[i] = temp;
}
// likelihood
for (i in 1:N){
n0[i] ~ binomial(n[i], delta[i]);  // here the error, with the index i of delta underlined with a tilde
}
}

Should I use an array instead of a vector for delta? But I think it would be less efficient.
I also tried to replace the loop of the likelihood by

n0 ~ binomial(n,delta);
``
But I still have the same error with delta underlined.

Any help would be appreciated.

The issue is not with delta but with n0 and n. They need to be integer types.

You can change your data block to contain

array[N] int n0;
array[N] int n;

or the old syntax for arrays (only recommended if you are using an older Stan version such as RStan on CRAN)

int n0[N];
int n[N];

After doing this you can also replace the for loop with the vectorized version you mention in the post

Thanks a lot! Changing the data block as suggested and replacing the loop with the vectorized version seems to be OK, as nothing is underlined in red tildes now in my model.
However, I have an error when trying to fit the model:
PARSER FAILED TO PARSE INPUT COMPLETELY
STOPPED AT LINE 1:
I am sure it should be something silly I am missing because I am new in Stan, but I really don’t see what can be the problem with line 1…

I exactly paste my model here, with all the lines and comments:

//
// The input data:
// 'n0' an array of integers,
// 'n' an array if integers,
// 'n0' and 'n'  of length 'N'.
data {
int<lower=1> N;
int<lower=0> n0[N];
int<lower=0> n[N];
}

// The parameters of the model.
parameters {
vector<lower=0>[N] lambda;
}
// Transformed parameters. Here theta and delta calculated from lambda
transformed parameters {
vector[N] theta;
vector[N] delta;
//  real delta[N];
theta = cumulative_sum(lambda);
delta = exp(-theta);
}
// The model to be estimated.
model {
vector[N] logitlambda;
real temp;
logitlambda = logit(lambda);
// prior
for (i in 1:61){
logitlambda[i] ~ normal(0, 1000);
}
temp ~ normal(0, 1000);
for (i in 62:N){
logitlambda[i] = temp;
}
n0 ~ binomial(n,delta);
}

I specify that I have an error when using the fit function:

fit = stan(
model_code = "Model2.stan",
data   = dat,
iter   = 200,
warmup = 100,
chains = 4,
seed=4938483
)

If I used stan_model(file=“Model2.stan”), I don’t have an error.

I believe your model code is fine, but its possible your data is invalid (either the wrong shape or possibly you have real numbers in n or n0)

Thanks for your answer. I tried different ways to specify my input data, but I always have the same error when fitting the model: PARSER FAILED TO PARSE INPUT COMPLETELY
STOPPED AT LINE 1: Model2.stan
Here is my input data, specifying that n0 and n are arrays of integers as expected by the Model2.stan

n0 <- array(as.integer(c(6,4,5,6,6,1,6,4,8,1,6,9,5,8,10,7,10,7,22,11,22,52,34,22,23,22,30,31,30,32,34,34,39,17,27,24,26,40,16,22,22,30,25,21,20,22,24,22,22,21,26,27,10,22,14,17,16,11,23,5,25,33,9,16,7,13,8,6,3,9,11,13,4,5,4,8,1,5,4,0,3,6,1,0,2,1,0,0,1,0,0,1,0,0,1)))
n <- array(as.integer(c(6,4,5,6,6,1,6,4,8,1,6,9,5,8,1,7,10,7,22,11,22,52,34,22,23,22,30,31,30,32,34,34,39,17,27,24,26,40,16,22,22,30,25,21,20,22,24,22,22,21,26,27,10,22,14,17,16,11,23,5,25,33,9,16,7,13,8,6,3,9,11,13,4,5,4,8,1,5,4,0,3,6,1,0,2,1,0,0,1,0,0,1,0,0,1)))

dat = list(
N = length(n0),
n0 = n0,
n = n
)

And the error is when running:

fit = stan(
model_code = "Model2.stan",
data   = dat,
iter   = 200,
warmup = 100,
chains = 4,
seed=4938483
)