-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostfire_season.stan
93 lines (82 loc) · 2.73 KB
/
postfire_season.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
data {
int<lower=0> N; // # of pixels * time steps
int<lower=0> J; // # of pixels
int<lower=0> P; // # of environment vars
array[N] int<lower=1,upper=N> pid; // pixel count
matrix[J,P] x; // NxP environmental matrix
vector<lower=-1>[N] age; // age at observation N
vector<lower=1,upper=12>[N] firemonth; // month of previous fire {1,12}
vector<lower=-1,upper=1>[N] y_obs; // ndvi at observation N
// a switch to evaluate the likelihood following:
// https://khakieconomics.github.io/2017/-6/30/An-easy-way-to-simulate-fake-data-in-stan.html
int<lower = 0, upper = 1> fit; // fit the model? Or just run with the priors
int<lower = 0, upper = 1> predict; // predict NDVI for all pixels?
}
parameters {
vector<lower=0, upper = 1>[J] alpha;
vector<lower=0, upper = 1>[J] gamma;
vector<lower=0>[J] lambda;
vector<lower=0, upper = 1>[J] A;
real<lower=0,upper=1> alpha_mu;
vector[P] gamma_beta;
vector[P] lambda_beta;
vector[P] A_beta;
real phi;
real<lower=0> tau_sq;
real<lower=0> gamma_tau_sq;
real<lower=0> lambda_tau_sq;
real<lower=0> alpha_tau_sq;
real<lower=0> A_tau_sq;
}
transformed parameters {
vector[N] mu;
vector[J] gamma_mu;
vector[J] lambda_mu;
vector[J] A_mu;
real tau = sqrt(tau_sq);
real gamma_tau = sqrt(gamma_tau_sq);
real lambda_tau = sqrt(lambda_tau_sq);
real alpha_tau = sqrt(alpha_tau_sq);
real A_tau = sqrt(A_tau_sq);
// regressions
gamma_mu = x*gamma_beta;
lambda_mu = x*lambda_beta;
A_mu = x*A_beta;
if(fit==1){ // only run if fitting is desired
for (i in 1:N){
mu[i] = alpha[pid[i]]+gamma[pid[i]]-gamma[pid[i]]*exp(-(age[i]/lambda[pid[i]]))+
sin((phi+((firemonth[i]-1)*3.141593/6))+6.283185*age[i])*A[pid[i]];
// mu = exp(alpha[pid])+exp(gamma[pid])-exp(gamma[pid])*exp(-(age/exp(lambda[pid])));
}
}
}
model {
// hyperpriors
tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
gamma_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
lambda_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
alpha_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
A_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
// priors
alpha_mu ~ normal(0.2,.2);
gamma_beta ~ normal(0,3);
lambda_beta ~ normal(0,3);
A_mu ~ normal(0,3);
// month effects
phi ~ uniform(-3.141593,3.141593);
// recovery curve - @Glenn - why the switch to normal from lognormal?
alpha ~ lognormal(alpha_mu, alpha_tau);
gamma ~ lognormal(gamma_mu,gamma_tau);
lambda ~ lognormal(lambda_mu,lambda_tau);
A ~ lognormal(A_mu, A_tau);
// likelihood
// if(fit==1){ // only run if fitting is desired
y_obs ~ normal(mu, tau);
// }
}
generated quantities {
array[N] real y_pred;
//if(predict==1){ // only run if prediction is desired
y_pred = normal_rng(mu, tau);
}
//}