forked from joshuacfowler/Endodemog_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendodemog_surv_matrix.stan
55 lines (41 loc) · 1.58 KB
/
endodemog_surv_matrix.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
data {
int<lower=0> N; // number of observations
int<lower=0> K; // number of predictors
int<lower=0> nyear; // number of years (used as index)
int<lower=0> year_t[N]; // year of observation
int<lower=0> nEndo; // number of endo treatments
int<lower=1, upper=2> endo_index[N]; // index for endophyte effect
int<lower=0, upper=1> surv_t1[N]; // plant survival at time t+1 and target variable (response)
matrix[N,K] Xs; // predictor matrix - surv_t1~logsize_t+endo+origin+logsize_t*endo
}
parameters {
vector[K] beta; // predictor parameters
matrix[nEndo, nyear] tau_year; // random year effect
real<lower=0> sigma_0[nEndo]; //year variance intercept
}
model {
vector[N] mu;
// Linear Predictor
for(n in 1:N){
mu[n] = Xs[n]*beta
+ tau_year[endo_index[n], year_t[n]];
}
// Priors
beta ~ normal(0,1e6); // prior for predictor intercepts
for(n in 1:nyear){
for(e in 1:nEndo){
tau_year[e,n] ~ normal(0,sigma_0[e]); // prior for year random effects
}}
// Likelihood
surv_t1 ~ bernoulli_logit(mu);
}
//generated quantities{
//int yrep[N];
//vector[N] mu;
// for posterior predictive check
//for(n in 1:N){
// mu[n] = Xs[n]*beta
// + tau_year[endo_index[n], year_t[n]];
// yrep[n] = bernoulli_logit_rng(mu[n]);
//}
// }