Syntax error, message(s) from parser:

Hi, do you know why does this error message generate?
In my function block, first I defined integral function and then defined likelihood function as likelihood_log(){…} . I am trying to do some integrations within likelihood function and return log likelihood. Any help?

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model2e9c734b369_44cdcf1353fac8db7bb5ebd1e42635a8' at line 88, column 96
  -------------------------------------------------
    86:       }
    87:       
    88:       D[1]=beta_t[2]*integrate_1d(WQ_integral,t[1], t[2], { mu, sigma2, alpha, lambda }, {t[2]}, {}, 1e-8);
                                                                                                       ^
    89:       
  -------------------------------------------------

PARSER EXPECTED: <expression>

The parser has problems with empty arrays so you can’t write {}. A non-empty array like {0} should work. Alternatively you can declare a size zero array in transformed data and use that

transformed data {
  int x_int[0]; // x_int is an empty array
}

It says fifth argument is the problem. Where is t defined? It must be in data or transformed data block.
Or maybe you could put t[3] in the previous array that is allowed to have parameters.

integrate_1d(WQ_integral,t[1], t[2],{ mu, sigma2, alpha, lambda, t[3] }, {0.0}, {0}, 1e-8))

As I think, in my case, it is not possible to define t in data or transformed data block because vector t updates over a for loop as this,
functions{
.
.
.

real likelihood_log(){
.
.
.
for (i in 1:rows(DataTable)){

  t = rep_vector(0,K+2); 
  t[1]=0;
  t[2]=DataTable[i,1]; 
  
  for (j in 3:6) {    
    t[j]=t[j-1]+delta;
  }

  D[1]=beta_t[2]*(integrate_1d(WQ_integral,t[1], t[2], { mu, sigma2, alpha, lambda }, {t[2]}, {0}, 1e-8));

.
.
.
.

}

}

Then you have to put it in the same array as mu, sigma2, alpha, and lambda.

Thank you. Here, as t is a vector of six elements (say) and then I got an error again because I parse mu , sigma2 , alpha , and lambda through real[] theta. Any idea is highly appreciated.

real WQ_integral(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i){

       real mu = theta[1];
       real sigma2 = theta[2];
       real lambda = theta[3];
       real alpha = theta[4];
       vector t= theta[5];
      .
      .
      .
       return(WQ);
  }
integrate_1d(WQ_integral,t[1], t[2], { mu, sigma2, alpha, lambda, t }, {t[2]}, {0}, 1e-8)

In the earlier code you only had t[2] but now you’re passing the full vector (and also t[2] in the other array which should not be there).
I don’t quite follow what you are trying to do. Can you post the full model?

This will help you to grasp some idea.
real WQ_integral(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i){

       real mu = theta[1];
       real sigma2 = theta[2];
       real lambda = theta[3];
       real alpha = theta[4];
       //vector t= theta[5];
       real WQ;
       int t1;
       WQ = (0.3*exp(-((log(x)-mu)^2)/(2*sigma2))/(sqrt(2*pi()*sigma2)*x))*(exp(-(lambda*((t1-x)^alpha))));
       return(WQ);
  }

t = c(0,50,51,52,53,54);
integrate_1d(WQ_integral,t[1], t[2], { mu, sigma2, alpha, lambda, t}, {t[2]}, {0}, 1e-8)

Here, t1 should have one particular value from vector t.

Maybe something like this

real WQ_integral(real x,
      real xc,
      real[] theta,
      real[] x_r,
      int[] x_i){

  real mu = theta[1];
  real sigma2 = theta[2];
  real lambda = theta[3];
  real alpha = theta[4];
  real tn = theta[5];
  real WQ;
  int t1;
  WQ = (0.3*exp(-((log(x)-mu)^2)/(2*sigma2))/(sqrt(2*pi()*sigma2)*x))*(exp(-(lambda*((tn-x)^alpha))));
  return(WQ);
}

t = c(0,50,51,52,53,54);

integrate_1d(WQ_integral,t[1], t[2], { mu, sigma2, alpha, lambda, t[n]}, {0.0}, {0}, 1e-8)

where n is the particular element of t you want.

Thanks. Will try for that. :)

In the above mentioned integrate_1d() function, I should parse a real value for t[n], but my t is a vector of integers. Do I want to convert it as a real number of vectors? I mean t = c(50.0,51.0,52.,53.,)

Stan converts integers automatically to real number when needed. It’s the other direction (going from a real number to an integer) that’s difficult.

If so it is not the error in my code.

Do you have any idea about

file2c585b18381.cpp:375:1452: note: cannot convert ‘stan::math::array_builder::array() with T = stan::math::var’ (type ‘std::vectorstan::math::var’) to type ‘const std::vector&’

This is a part of error what I got earlier.

A C++ error? That’s an error in the Stan compiler. Can you share the model that caused it?

Yes it is the compilation error in Stan, As the code is so long I can share some parts.
Within my likelihood function I am doing some integrations as mentioned earlier comments,

real likelihood_log(matrix RT, real[] param, int K, int L){
.
.
vector[4] t= c(50,55,60,65) ;
integrate_1d(integral_func,t[1], t[2],{ mu, sigma2, alpha, lambda, t[2]}, {0.0}, {0}, (1e-8));
.
.
return loglikelihood;
}

model{
//prior distributions
mu ~ uniform(4.0,4.5);
sigma2 ~ uniform(0.01,0.05);
lambda ~ uniform(0.01,0.5);
alpha ~ uniform(1.5,4.0);
DT ~ likelihood({mu,sigma2,lambda,alpha}, 4, 1);
}

Once defining integral function in the function block, is it possible to parse this integration with the likelihood function which is defined in the function block?
If someone can give me some idea on this?

**[edit: escaped and indented code]

Hello there, I am fresh to research and totally new to Bayes and hence stand scripts. I have been working in a group but currently, the support team is out and alone here trying to figure out something. I have a stan script that is giving somewhat a similar challenge like this but I am lost in the details provided.

here is my script, any help will be appreciated.

data {
  int<lower=1> Nobs;         // number of observations 
  int<lower=1> N1;         // number of students 
  int<lower=1> N2;         // number of course units
  int<lower=1> J;         // number of fixed effects  
  int y[Nobs];              // resits
  matrix[Nobs,J] X; //the model matrix
  int male[Nobs]; // sex of student 
  int bbs[Nobs]; // BBS student 
  int bps[Nobs]; // BPS student 
  int bqe[Nobs]; // BQE student 
  int sas[Nobs]; // SAS student 
  int<lower=1, upper=N1> id[Nobs]; //  student 
  int<lower=1, upper=N2> course[Nobs]; // course unit

}
    
    
    
parameters {
  vector[J] delta;  
  vector<lower=0>[J] tau;  
  vector<lower=0, upper=1>[N2] gamma;  // geometric paratemeter
  vector[N2] theta1;      // scale parameter
  real<lower=0> sigma1;      // scale parameter
  real<lower=0> sigma2;
  vector[J] beta[N2];          // fixed effects parameters
  real b[N1];          // student random effects
}

transformed parameters {
  vector<lower=0>[N2] mu ;      // scale parameter
  vector<lower=0>[N2] theta ;      // scale parameter
  vector<lower=0>[Nobs] mu1[N2];
  vector<lower=0>[N2] betamu;      // mean parameter
  vector<lower=0>[N2] betasigm;      // variance parameter


  for (k in 1:N2){
    for (i in 1:Nobs){
      mu1[k,i] = inv_logit(X[i]*beta[course[i]] + b[id[i]]);
    }
  }
  
  for (n in 1:N2){
    mu[n] = mean(mu1[n]);
    theta[n] = exp(theta1[n]);
    betamu[n] =(1-theta[n])/(mu[n]-theta[n]);
    betasigm[n] = (mu[n]*(1-mu[n])*(1-theta[n]))/pow((mu[n] -theta[n]),2)*(mu[n] -(2*theta[n]));
  }
}

model  {
  target += lognormal_lpdf(theta1 | 0, 1);   // hyperprior half-normal log-density
  target += cauchy_lpdf(sigma1 |0.0, 2.5);       // hyperprior log-density
  target += cauchy_lpdf(sigma2 |0.0, 2.5);       // hyperprior log-density
  target += normal_lpdf(beta |0,1);       // hyperprior log-density
  target += normal_lpdf(b |0.0, sigma1);       // hyperprior log-density
  target += normal_lpdf(g |0.0, sigma2);       // hyperprior log-density
  target += beta_lpdf(gamma | mu/theta, (1 - mu)/theta);       // prior log-density
  target += neg_binomial_lpmf(x | 1, gamma/(1-gamma)); // log-likelihood

}
          
generated quantities {
  int y_rep[Nobs];
  vector[Nobs] log_lik;
  
  for (i in 1:Nobs) {
    y_rep[i] = neg_binomial_rng(1, gamma[course[i]]/(1-gamma[course[i]]));
    log_lik[i] = neg_binomial_lpmf(y[i] |1, gamma[course[i]]/(1-gamma[course[i]]));
     
  }
    
}

when I run that in r, I get the following error message

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 

  normal_lpdf(vector[ ], int, int)

Available argument signatures for normal_lpdf:

  normal_lpdf(real, real, real)
  normal_lpdf(real, real, real[ ])
  normal_lpdf(real, real, vector)
  normal_lpdf(real, real, row_vector)
  normal_lpdf(real, real[ ], real)
  normal_lpdf(real, real[ ], real[ ])
  normal_lpdf(real, real[ ], vector)
  normal_lpdf(real, real[ ], row_vector)
  normal_lpdf(real, vector, real)
  normal_lpdf(real, vector, real[ ])
  normal_lpdf(real, vector, vector)
  normal_lpdf(real, vector, row_vector)
  normal_lpdf(real, row_vector, real)
  normal_lpdf(real, row_vector, real[ ])
  normal_lpdf(real, row_vector, vector)
  normal_lpdf(real, row_vector, row_vector)
  normal_lpdf(real[ ], real, real)
  normal_lpdf(real[ ], real, real[ ])
  normal_lpdf(real[ ], real, vector)
  normal_lpdf(real[ ], real, row_vector)
  normal_lpdf(real[ ], real[ ], real)
  normal_lpdf(real[ ], real[ ], real[ ])
  normal_lpdf(real[ ], real[ ], vector)
  normal_lpdf(real[ ], real[ ], row_vector)
  normal_lpdf(real[ ], vector, real)
  normal_lpdf(real[ ], vector, real[ ])
  normal_lpdf(real[ ], vector, vector)
  normal_lpdf(real[ ], vector, row_vector)
  normal_lpdf(real[ ], row_vector, real)
  normal_lpdf(real[ ], row_vector, real[ ])
  normal_lpdf(real[ ], row_vector, vector)
  normal_lpdf(real[ ], row_vector, row_vector)
  normal_lpdf(vector, real, real)
  normal_lpdf(vector, real, real[ ])
  normal_lpdf(vector, real, vector)
  normal_lpdf(vector, real, row_vector)
  normal_lpdf(vector, real[ ], real)
  normal_lpdf(vector, real[ ], real[ ])
  normal_lpdf(vector, real[ ], vector)
  normal_lpdf(vector, real[ ], row_vector)
  normal_lpdf(vector, vector, real)
  normal_lpdf(vector, vector, real[ ])
  normal_lpdf(vector, vector, vector)
  normal_lpdf(vector, vector, row_vector)
  normal_lpdf(vector, row_vector, real)
  normal_lpdf(vector, row_vector, real[ ])
  normal_lpdf(vector, row_vector, vector)
  normal_lpdf(vector, row_vector, row_vector)
  normal_lpdf(row_vector, real, real)
  normal_lpdf(row_vector, real, real[ ])
  normal_lpdf(row_vector, real, vector)
  normal_lpdf(row_vector, real, row_vector)
  normal_lpdf(row_vector, real[ ], real)
  normal_lpdf(row_vector, real[ ], real[ ])
  normal_lpdf(row_vector, real[ ], vector)
  normal_lpdf(row_vector, real[ ], row_vector)
  normal_lpdf(row_vector, vector, real)
  normal_lpdf(row_vector, vector, real[ ])
  normal_lpdf(row_vector, vector, vector)
  normal_lpdf(row_vector, vector, row_vector)
  normal_lpdf(row_vector, row_vector, real)
  normal_lpdf(row_vector, row_vector, real[ ])
  normal_lpdf(row_vector, row_vector, vector)
  normal_lpdf(row_vector, row_vector, row_vector)

 error in 'model5c9714bd0cb_Modified_courseunits_model_12_08_2022' at line 57, column 39
  -------------------------------------------------
    55:       target += cauchy_lpdf(sigma1 |0.0, 2.5);       // hyperprior log-density
    56:      target += cauchy_lpdf(sigma2 |0.0, 2.5);       // hyperprior log-density
    57:        target += normal_lpdf(beta |0,1);       // hyperprior log-density
                                              ^
    58:       target += normal_lpdf(b |0.0, sigma1);       // hyperprior log-density
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'Modified_courseunits-model-12-08-2022' due to the above error.

how do i solve this

the line causing the error is this one:

and the error message starts with

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 
  normal_lpdf(vector[ ], int, int)

The problem here is that we don’t support arrays of vectors as the first argument here. You need a loop.

for (n in 1:N2)
  target += normal_lpdf(beta[n] | 0, 1);

P.S. It’s probably going to be more efficient to use lupdf form—the difference is that the u stands for unnormalized and it drops constants that we don’t need for sampling. You can also write the equivalent of the lupdf form using sampling statements:

for (n in1:N2)
  beta[n] ~ normal_lpdf(0, 1);