-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLM_RE_NegativeBinomial_1.stan
56 lines (49 loc) · 1.64 KB
/
GLM_RE_NegativeBinomial_1.stan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
data {
int n;
int<lower=0> y[n];
int<lower=0> p;
int<lower=0> nyrs;
int<lower=0> ngrid;
int<lower=1,upper=ngrid> grid_id[n];
int<lower=1,upper=nyrs> year_id[n];
int<lower=0, upper=1> PRIOR_ONLY;
vector[n] salinity;
vector[n] doxy;
vector[n] season;
vector[n] wtemp;
vector[n] wdepth;
}
parameters {
real beta[p];
vector[nyrs] gamma;
vector[ngrid] delta;
real<lower=0> sigma_gamma;
real<lower=0> sigma_delta;
real<lower=0> phi;
}
model {
target += normal_lpdf(beta[1]|0,0.5);
target += normal_lpdf(beta[2]|0,0.5);
target += normal_lpdf(beta[3]|0,0.5);
target += normal_lpdf(beta[4]|0,0.5);
target += normal_lpdf(beta[5]|0,0.5);
target += normal_lpdf(beta[6]|0,0.5);
target += normal_lpdf(gamma|0,sigma_gamma);
target += cauchy_lpdf(sigma_gamma|0,3.5);
target += normal_lpdf(delta|0,sigma_delta);
target += cauchy_lpdf(sigma_delta|0,3.5);
target += cauchy_lpdf(phi|0,3);
if(PRIOR_ONLY ==0){
for(i in 1:n){
target += neg_binomial_2_log_lpmf(y[i]|beta[1] + gamma[year_id[n]] + delta[grid_id[n]] + beta[2]*salinity[i] + beta[3]*doxy[i] + beta[4]*season[i] + beta[5]*wtemp[i] + beta[6]*wdepth[i], phi);
}
}
}
generated quantities{
real ypred[n];
real log_lik[n];
for(i in 1:n){
ypred[i] = neg_binomial_2_log_rng(beta[1] + gamma[year_id[n]] + delta[grid_id[n]] + beta[2]*salinity[i] + beta[3]*doxy[i] + beta[4]*season[i] + beta[5]*wtemp[i] + beta[6]*wdepth[i], phi);
log_lik[i] = neg_binomial_2_log_lpmf(y[i]|beta[1] + gamma[year_id[n]] + delta[grid_id[n]] + beta[2]*salinity[i] + beta[3]*doxy[i] + beta[4]*season[i] + beta[5]*wtemp[i] + beta[6]*wdepth[i], phi);
}
}