 # Writing functions for log-likelihoods in rstan

Hi all,
I’m writing the functions for the log-likelihood of my hierarchical model in RSTAN. But, I got the below error in stan. Here, the county is a vector with dim= num_elements(t) that shows the residence place of patients (N=34750, county = 1, … , 31). Can anyone help me to solve this error?
Stan Error:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Container index must be integer; found type=real
error in ‘model52ad62e48c9e_Onicesco_dependency_model_right_interval_weibull34744’ at line 21, column 20
19: spdf = a (p,kernel, exp_x);
20: for (i in 1:num_elements(t)){
21: log_S[i] = log(spdf[county[i]]) + weibull_lccdf(t[i]|shape,scale[i]);

^

## 22: }

PARSER EXPECTED: <one or more container indexes followed by ‘]’>
Error in stanc(filename, allow_undefined = TRUE) :

Thank you

My stan codes are as follows:

``````functions{
// defines the log new pdf
vector a (vector p,matrix kernel, vector exp_x){
vector[num_elements(p)] a;
vector[num_elements(p)] landa;
for (k in 1 : num_elements(p)) {
landa[k] = kernel[k] * exp_x * p[k];
}
a = landa / sum(landa); // assign `a`
return a;
}
// defines the log survival
vector log_S (vector t,vector p, matrix kernel, vector exp_x, vector county, real shape,vector scale){
vector[num_elements(t)] log_S;
vector[num_elements(p)] spdf;
spdf = a (p,kernel, exp_x);
for (i in 1:num_elements(t)){
log_S[i] = log(spdf[county[i]]) + weibull_lccdf(t[i]|shape,scale[i]); // county is a vector that shows the location of patients with dim = num_elements(t)
}
return log_S;
}
//defines the log pdf
vector log_h (vector t,vector p, matrix kernel, vector exp_x, vector county, real shape,vector scale){
vector[num_elements(t)] log_f ;
vector[num_elements(p)] spdf;
spdf = a (p,kernel, exp_x);
for (i in 1:num_elements(t)){
log_f[i] = log(spdf[county[i]]) + (weibull_lpdf(t[i]|shape,scale[i]));
}
return log_h;
}
//defines the log-likelihood for right censored data
real surv_weibull_lpdf(vector t,vector d,real shape,
vector scale){
vector[num_elements(t)] log_lik;
real prob;
log_lik = d .* log_h(t,shape,scale)+
log_S(t,shape,scale);
prob = sum(log_lik);
return prob;
}
}
.`````````

My guess is it’s this indexing statement: `spdf[county[i]])`. The Stan language reference manual section 5.4 says

Vectors and matrices cannot be typed to return integer values. They are restricted to `real` values

Maybe you could declare county as an integer array?