-
Notifications
You must be signed in to change notification settings - Fork 4
/
example4.stan
36 lines (32 loc) · 941 Bytes
/
example4.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
//
// Zero-inflated Poisson model
//
data {
int<lower = 0> N; // Number of sites
int<lower = 0> Y[N]; // Number of new seedlings
vector<lower = 0>[N] X; // Explanatory variable
}
parameters {
real<lower = 0, upper = 1> p; // Probability of presence
real beta[2]; // Intercept and coefficient
}
transformed parameters {
// Log of Poisson mean
vector[N] log_lambda = beta[1] + beta[2] * X;
}
model {
// Improper uniform priors are implicitly defined on p and beta.
for (i in 1:N) {
if (Y[i] > 0) {
// Bernoulli(1|p) * Poisson(Y|λ)
1 ~ bernoulli(p);
Y[i] ~ poisson_log(log_lambda[i]);
} else {
// if Y[i] == 0
// Bernoulli(0|p) + Bernoulli(1|p) * Poisson(0|λ)
target += log_sum_exp(bernoulli_lpmf(0 | p),
bernoulli_lpmf(1 | p)
+ poisson_log_lpmf(0 | log_lambda[i]));
}
}
}