|
a |
|
b/inst/stan/hs.stan |
|
|
1 |
// |
|
|
2 |
// Hierarchical shrinkage prior on regression coeffs (linear regression) |
|
|
3 |
// |
|
|
4 |
// This implements the regularized horseshoe prior according to the simple |
|
|
5 |
// parametrization presented in: |
|
|
6 |
// |
|
|
7 |
// Piironen, Vehtari (2017), Sparsity information and regularization in the |
|
|
8 |
// horseshoe and other shrinkage priors, Electronic Journal of Statistics |
|
|
9 |
|
|
|
10 |
functions { |
|
|
11 |
#include /chunks/hs.fun |
|
|
12 |
} |
|
|
13 |
|
|
|
14 |
data { |
|
|
15 |
|
|
|
16 |
// number of columns in model matrix |
|
|
17 |
int P; |
|
|
18 |
|
|
|
19 |
// number of unpenalized columns in model matrix |
|
|
20 |
int U; |
|
|
21 |
|
|
|
22 |
// number of observations |
|
|
23 |
int N; |
|
|
24 |
|
|
|
25 |
// design matrix |
|
|
26 |
matrix[N, P] X; |
|
|
27 |
|
|
|
28 |
// continuous response variable |
|
|
29 |
vector[N] y; |
|
|
30 |
|
|
|
31 |
// prior standard deviation for the unpenalised variables |
|
|
32 |
real<lower=0> scale_u; |
|
|
33 |
|
|
|
34 |
// whether the regularized horseshoe should be used |
|
|
35 |
int<lower=0, upper=1> regularized; |
|
|
36 |
|
|
|
37 |
// degrees of freedom for the half-t priors on lambda |
|
|
38 |
real<lower=1> nu; |
|
|
39 |
|
|
|
40 |
// scale for the half-t prior on tau |
|
|
41 |
real<lower=0> global_scale; |
|
|
42 |
|
|
|
43 |
// degrees of freedom for the half-t prior on tau |
|
|
44 |
real<lower=1> global_df; |
|
|
45 |
|
|
|
46 |
// slab scale for the regularized horseshoe |
|
|
47 |
real<lower=0> slab_scale; |
|
|
48 |
|
|
|
49 |
// slab degrees of freedom for the regularized horseshoe |
|
|
50 |
real<lower=0> slab_df; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
parameters { |
|
|
54 |
|
|
|
55 |
// unpenalized regression parameters |
|
|
56 |
vector[U] beta_u; |
|
|
57 |
|
|
|
58 |
// residual standard deviation |
|
|
59 |
real <lower=0> sigma; |
|
|
60 |
|
|
|
61 |
// global shrinkage parameter |
|
|
62 |
real<lower=0> tau; |
|
|
63 |
|
|
|
64 |
// local shrinkage parameter |
|
|
65 |
vector<lower=0>[P-U] lambda; |
|
|
66 |
|
|
|
67 |
// auxiliary variables |
|
|
68 |
vector[P-U] z; |
|
|
69 |
real<lower=0> c2; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
transformed parameters { |
|
|
73 |
|
|
|
74 |
// penalized regression parameters |
|
|
75 |
vector[P-U] beta_p; |
|
|
76 |
|
|
|
77 |
if (regularized) |
|
|
78 |
beta_p = reg_hs(z, lambda, tau, slab_scale^2 * c2); |
|
|
79 |
else |
|
|
80 |
beta_p = hs(z, lambda, tau); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
model { |
|
|
84 |
|
|
|
85 |
// regression coefficients |
|
|
86 |
vector[P] beta = append_row(beta_u, beta_p); |
|
|
87 |
|
|
|
88 |
// half t-priors for lambdas and tau |
|
|
89 |
z ~ std_normal(); |
|
|
90 |
lambda ~ student_t(nu, 0, 1); |
|
|
91 |
tau ~ student_t(global_df, 0, global_scale * sigma); |
|
|
92 |
|
|
|
93 |
// inverse-gamma prior for c^2 |
|
|
94 |
c2 ~ inv_gamma(0.5 * slab_df, 0.5 * slab_df); |
|
|
95 |
|
|
|
96 |
// unpenalized coefficients including intercept |
|
|
97 |
beta_u ~ normal(0, scale_u); |
|
|
98 |
|
|
|
99 |
// noninformative gamma priors on scale parameter are not advised |
|
|
100 |
sigma ~ inv_gamma(1, 1); |
|
|
101 |
|
|
|
102 |
// likelihood |
|
|
103 |
y ~ normal_id_glm(X, 0, beta, sigma); |
|
|
104 |
} |