# User specified multivariate model

I am trying to incorporate a user defined multivariate skewed t-distribution - It works in the univariate case, but I can’t get it to work in the multivariate case - I think it has to do with the definition of the random variabel vector and/or parameters:

functions {
//Multivariate skewed t-distribution (Sahu, Dey & Branco, 2003)
real MskT_lpdf(vector z, vector nu, vector xi){
int N = rows(nu);
vector[K] l;
for(n in 1:K){
real a = (tgamma((nu[n]-1)/2)sqrt(nu[n]-2))/(sqrt(pi())tgamma(nu[n]/2))(xi[n]-1/xi[n]); // To ensure zero mean
real b = sqrt(pow(xi[n],2)+1/pow(xi[n],2)-1-pow(a,2)); // To ensure unit variance
real k = (-a/b >= z[n] ? (bz[n]+a)xi[n] : (bz[n]+a)/xi[n]);
l[n] = log(2b/(xi[n]+1/xi[n])) + lgamma((nu[n]+1)/2)-(log(sqrt(pi()(nu[n]-2)))+lgamma(nu[n]/2))-((nu[n]+1)/2)*log(1+(pow(k,2)/(nu[n]-2)));
}
return sum(l);

Any suggestions?

It helps immensely if you return the error message you get. If you don’t, we have to load it up into Stan and fish it out ourselves.

``````functions {
//Multivariate skewed t-distribution (Sahu, Dey & Branco, 2003)
real MskT_lpdf(vector z, vector nu, vector xi){
int N = rows(nu);
vector[K] l;
for(n in 1:K){
real a = (tgamma((nu[n]-1)/2)sqrt(nu[n]-2))/(sqrt(pi())tgamma(nu[n]/2))(xi[n]-1/xi[n]); // To ensure zero mean
real b = sqrt(pow(xi[n],2)+1/pow(xi[n],2)-1-pow(a,2)); // To ensure unit variance
real k = (-a/b >= z[n] ? (bz[n]+a)xi[n] : (bz[n]+a)/xi[n]);
l[n] = log(2b/(xi[n]+1/xi[n])) + lgamma((nu[n]+1)/2)-(log(sqrt(pi()(nu[n]-2)))+lgamma(nu[n]/2))-((nu[n]+1)/2)*log(1+(pow(k,2)/(nu[n]-2)));
}
return sum(l);
}
}
``````

and then compiling in RStan gives you:

``````> stan_model("funs.stan")
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

variable "K" does not exist.
error in 'modelef10625be5_funs' at line 5, column 13
-------------------------------------------------
3:   real MskT_lpdf(vector z, vector nu, vector xi){
4:     int N = rows(nu);
5:     vector[K] l;
^
6:     for(n in 1:K){
-------------------------------------------------
``````

The key line is this one:

``````variable "K" does not exist.
``````

You are trying to use a variable in your function that isn’t one of the function arguments. You should either define

``````int K = rows(xi);
``````

or pass it in. You can’t access variables from the program, only things given as arguments to the function.

1 Like