How to compute the residual value in Stan

Hey, I just would like to compute the residuals in Stan and got this error:

functions{
 real EL_lpdf(matrix X,vector y, real[] theta){
    real N = rows(X);
    vector[N] resi = y - X *to_vector(theta);
  }
}

Expression denoting integer required; found type=real
error in ‘model915f15caf1e8_EL’ at line 15, column 11

13:  real EL_lpdf(matrix X,vector y, real[] theta){
14:     real N = rows(X);
15:     vector[N] resi = y - X *to_vector(theta);
              ^
16:     //matrix Xs;

PARSER EXPECTED:

if.I change the type of “resi” from vector to real and got this:

functions{
 real EL_lpdf(matrix X,vector y, real[] theta){
    real N = rows(X);
    real[N] resi = y - X *to_vector(theta);
  }
}

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in ‘model915f8bbe70b_EL’ at line 15, column 8

13:  real EL_lpdf(matrix X,vector y, real[] theta){
14:     real N = rows(X);
15:     real[N] resi = y - X *to_vector(theta);
           ^
16:     //matrix Xs;

PARSER EXPECTED:

Anyone knows what is going wrong??

The error is caused by the variable N being a real, and then being used to index a vector. The variable N needs to be an int:

functions{
 real EL_lpdf(matrix X,vector y, real[] theta){
    int N = rows(X);
    vector[N] resi = y - X *to_vector(theta);
  }
}
1 Like

@rok-cesnovar It might be worth having a more informative compiler error here (if possible)

1 Like

Thanks!!

How can I make such a foolish error :(

I think we luckily already have a better error message in stanc3:

   -------------------------------------------------
     2:   real EL_lpdf(matrix X,vector y, real[] theta){
     3:      real N = rows(X);
     4:      vector[N] resi = y - X *to_vector(theta);
                    ^
     5:    }
     6:  }
   -------------------------------------------------

Vector sizes must be of type int. Instead found type real.
2 Likes

Oh nice, that’s much clearer