Dear Stan Community,
I am trying to use the horseshoe model from Piironen and Vehtari (2016). The paper was made in STAN and it provides a code in the end. I am attaching a copy of the code below (I only did a minor modification replacing all the <- for =, since <- since it’s deprecated).
When I paste this code in my Rstudio console I get the following error next to the prior distribution of z:
"Illegal statement beginning with non-void expression parsed as
Not a legal assignment, sampling, or function statement. Note that
PARSER EXPECTED: “}” "
It seems the problem is in the function chunk and not in the z prior distribution (which is straightforward). When I erase the function the error disappears, but obviously, the code claims that the function is missing.
If I try to run the code (for example in data_example.csv (7.2 KB) ) to some simulated data I get:
“Error in stanc(file = file, model_code = model_code, model_name = model_name, : failed to parse Stan model ‘stan_code’ due to the above error.”
After reading about functions, I don’t find what is wrong with that function or the code … specially since it seem it worked when the authors wrote the paper. Could not find guidance in other posts with similar problems.
Any ideas of what could be happening?
Thank you for your help.
Best wishes,
Nico.
Piironen and Vehtari code
functions {
vector sqrt_vec ( vector x) {
/* * element - wise square root */
vector [ dims ( x )[1]] res ;
for (m in 1: dims (x )[1]) res [m] = sqrt (x[m ]);
return res ;
}
}
data {
int < lower =0 > n ; // number of observations
int < lower =0 > d ; // number of predictors
vector [ n] y; // outputs
matrix [n ,d] x; // inputs
real < lower =0 > scale_icept ; // prior std for the intercept
real < lower =0 > scale_global ; // scale for the half - t prior for tau
// ( tau0 = scale_global * sigma )
real < lower =1 > nu_global ; // degrees of freedom for the half - t prior for tau
real < lower =1 > nu_local ; // degrees of freedom for the half - t priors for lambdas
// ( nu_local = 1 corresponds to the horseshoe )
}
parameters {
real w0 ; // intercept
real logsigma ; // log of noise std
// auxiliary variables that define the global and local parameters
vector [ d] z;
real < lower =0 > r1_global ;
real < lower =0 > r2_global ;
vector < lower =0 >[ d] r1_local ;
vector < lower =0 >[ d] r2_local ;
}
transformed parameters {
real < lower =0 > tau ; // global shrinkage parameter
vector < lower =0 >[ d] lambda ; // local shrinkage parameters
vector [ d] w; // regression coefficients
vector [ n] f; // latent values
real sigma ; // noise std
sigma = exp ( logsigma );
lambda = r1_local .* sqrt_vec ( r2_local );
tau = r1_global * sqrt ( r2_global );
w = z .* lambda * tau ;
f = w0 + x *w;
}
model {
// half - t priors for lambdas
z ∼ normal (0 , 1);
r1_local ∼ normal (0.0 , 1.0);
r2_local ∼ inv_gamma (0.5* nu_local , 0.5* nu_local );
// half - t prior for tau
r1_global ∼ normal (0.0 , scale_global * sigma );
r2_global ∼ inv_gamma (0.5* nu_global , 0.5* nu_global );
// gaussian prior for the intercept
w0 ∼ normal (0 , scale_icept );
// observation model
y ∼ normal (f , sigma );
}