User defined function syntax error: No matches for


This is my first time using Stan in R, so please bear with me.

My full Stan code for my model is shown below, which requires a user defined probability function :

functions {
  real smvn_lpdf(vector as, vector mus, matrix covmats, vector omega, real delta, 
                 matrix all_Xs, matrix all_X2, vector all_P) { 
    // Define additional local variables
    vector[256] IMP; 
    real IV; 
    real logK; 
    real logW;  
    real lprob;  
    IMP = 1/(2*delta)*all_X2*omega - 1/delta*all_P;
    IV = log_sum_exp(1/delta*all_Xs*as + IMP);
    logK = log_sum_exp(IMP + 1/delta*all_Xs*mus 
                       + 1/(2*delta^2)*diagonal(all_Xs*covmats*all_Xs));
    logW = IV - logK;
    // calculate the log likelihood value
    lprob = logW + multi_normal_lpdf(as | mus, covmats);
    // return the log likelihood value
    return lprob;

data {
  int<lower=1> J; 
  int<lower=1> R; 
  int<lower=1> S[R]; 
  int<lower=3> C; 
  int<lower=1> W; 
  int<lower=1> N; 
  vector<lower=1,upper=C>[N] Y;  
  matrix[N*3, J-1] Xs;  
  matrix[N*3, W] X2; 
  vector[N*3] P;  
  matrix[256, J-1] all_Xs; 
  matrix[256, W] all_X2; 
  vector[256] all_P; 

parameters {
  vector[J-1] mus;
  vector<lower=0>[J-1] sigmas;
  corr_matrix[J-1] Thetas;
  vector[W-1] omegas;
  real<lower = 0> delta;

transformed parameters {
  vector[J] omega = append_row(omegas, -sum(omegas));
  cov_matrix[J-1] covmats = quad_form_diag(Thetas, sigmas);  

model {
  int yr;
  int xr;
  yr = 1;
  xr = 1;
  // hyperpriors
  mus ~ normal(0, 1000);
  sigmas ~ cauchy(0, 2.5); 
  Thetas ~ lkj_corr(2);
  // priors
  omegas ~ normal(0, 1000);
  delta ~ gamma(0.001, 1000);
  // likelihood
  for (r in 1:R) { // for each individual
    int ypos;
    int xpos;
    vector[J-1] as;
    vector[J-1] alphas;

    ypos = yr;
    xpos = xr;
    as ~ multi_normal(mus, covmats);
    alphas ~ smvn(as, mus, covmats, omega, delta, all_Xs, all_X2, all_P);	
    for (s in 1:S[r]) { // for each choice task
    Y[ypos] ~ categorical_logit(block(Xs, xpos, 1, 2, 6)*alphas+block(X2, xpos, 1, 2, 27)*omega-1/delta*segment(P, xpos, C); 
    ypos = ypos + 1;
    xpos = xpos + C;
    yr = yr + S[r];
    xr = xr + S[r]*C;

I got the error message when tried to run the code:

No matches for: 

  vector ~ smvn(vector, vector, matrix, vector, real, matrix, matrix, vector)

Available argument signatures for smvn:

  vector ~ smvn(vector, matrix, vector, real, matrix, matrix, vector)

Real return type required for probability function.
 error in 'model31dc4445db9_conjoint' at line 90, column 73
    88:     xpos = xr;
    89:     as ~ multi_normal(mus, covmats);
    90:     alphas ~ smvn(as, mus, covmats, omega, delta, all_Xs, all_X2, all_P);	

I checked my model block, and am sure that I have all the data needed for the user defined function “smvn”. I do not understand why the available argument signatures is missing “vector” at the beginning. I also do not understand why the error is at column 73, which is “;”.

Any suggestion is greatly appreciated!

The problem is that

alphas ~ smvn(as, mus, covmats, omega, delta, all_Xs, all_X2, all_P);

is equivalent to

target += smvn_lpdf(alphas | as, mus, covmats, omega, delta, all_Xs, all_X2, all_P);

but the function smvn_lpdf that you have written accepts only 8 arguments, not 9. You’ve misunderstood how to use functions ending in “_lpdf” with the “~” operator.

If you can specify your model in this forum in mathematical notation, then we can probably figure out what your Stan statements should look like.


Thank you for your explanation, James! I now understand what I did wrong and am able to fix it. Really appreciate your help!