I am running into a syntax issue where I am using my own function (p.filter) to calculate a likelihood that uses a parameter being estimated in the model. I want the estimate to update the likelihood before incorporating the likelihood into my model section, which is why I chose transformed parameters over generated quantiles.
The syntax error is "SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable “matrix” does not exist.’
For reference N and q are real values declared in the data. Any suggestions for how to reformat this or address this issue?
transformed parameters{
matrix[N,q] likel;
likel = p.filter(param, ...);
}
Can you post the full model?
Sure. I have tried the same method with a vector in the transformed parameter section and get the same syntax error for a vector, should I be formatting the transformed parameters differently?
data{
int<lower=1> N; //number of samples
int<lower=1> mesh; //number of size bins
vector[N] enc; //list of encounters
vector[N] hypox; //hypoxia
vector[4] prior_mu;
vector[4] prior_sd;
// p.filter variables
int nact[mesh, N];
matrix[mesh, N] Dat;
real<lower=0> Cv;
}
parameters{
real logit_det;
real hypox_p; //hypoxia slope
}
transformed parameters{
real<lower=0,upper=1> det; // detection
det = inv_logit(logit_det);
matrix[N, mesh] likel;
likel = p.filter(Dat, nact, hypox_p, Cv)$likelihood; // p.filter() produces a list of two matrix - this calls the likelihood matrix
}
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable “matrix” does not exist.
error in ‘model334c7790e305_occ5’ at line 29, column 18
27: real<lower=0,upper=1> det; // detection
28: det = inv_logit(logit_det);
29: matrix[N] likel;
^
Ah, I think this might be because you have an outdated version of rstan installed. It used to be that one had to put all the variable declarations together at the top of a block then have their values expressed below:
transformed parameters{
real<lower=0,upper=1> det; // detection
matrix[N, mesh] likel;
det = inv_logit(logit_det);
likel = p.filter(Dat, nact, hypox_p, Cv)$likelihood; // p.filter() produces a list of two matrix - this calls the likelihood matrix
}
Ah I see, thanks for bringing that to my attention. I am now running into an ERROR of “No matches for p.filter”.
I have previously been able to call my functions (R scripts) and use them in the transformed parameters block, but this is a more complicated function. Do I have to call it in the functions{} block of Rstan?
No, Stan is a wholly separate language from R and you can’t call arbitrary R code in a Stan program. See here for the full set of functions that are built into the Stan language. Users can define custom functions in an initial functions block but said user-defined functions must be composed from the built-in functions.