forked from EcoForecast/PhenologyForecast
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RunJAGS.R
172 lines (143 loc) · 4.54 KB
/
RunJAGS.R
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
### define function that runs JAGS model
RunJAGS <- function(data,n.iter,n.chains){
require(rjags)
source("global_input_parameters.R") # For burn-in
model = global_input_parameters$model
##JAGS code
LogisticGrowth = "
model{
#### Data Model: NDVI
for(i in 1:n){
y[i] ~ dnorm(x[i],tau_ndvi)
}
#### Data Model: GCC
for(i in 1:n){
z[i] ~ dnorm(x[i],tau_gcc)
}
#### Process Model
#### Color is the expected new phenology stage given the previous stage and logistic
#### subtraction instead of addition in the discrete logistic eqn makes r negative (so logistic goes down).
for(i in 2:n){
color[i] <- max(0, min(1, x[i-1] - r * x[i-1] * (1-x[i-1]) ) )
x[i]~dnorm(color[i],tau_add)
}
#### Priors
x[1] ~ dnorm(x_ic,tau_ic)
tau_ndvi ~ dgamma(a_ndvi,r_ndvi)
tau_gcc ~ dgamma(a_gcc,r_gcc)
tau_add ~ dgamma(a_add,r_add)
r ~ dexp(0.148) # Exp is the maximum entropy distribution for constraints of positive with givn mean
# 0.148 is from Richardson et al. 2006.
}"
Threshold_Day_Logistic= "
model{
for(yr in 1:ny){
#### Data Model: NDVI
for(i in 1:n){
y[yr,i] ~ dnorm(x[yr,i],tau_ndvi)
}
#### Data Model: GCC
for(i in 1:n){
z[yr,i] ~ dnorm(x[yr,i],tau_gcc)
}
#### Process Model
for(i in 2:n){
color[yr,i] <- ifelse(i > k,x[yr,i-1] - r * x[yr,i-1] * (1-x[yr,i-1]),1 )
lcolor[yr,i] ~ dnorm(color[yr,i],tau_add)
x[yr,i] <- min(1,max(0,lcolor[yr,i]))
}
x[yr,1] ~ dnorm(x_ic,tau_ic)
} ## end loop over years
#### Priors
tau_ndvi ~ dgamma(a_ndvi,r_ndvi)
tau_gcc ~ dgamma(a_gcc,r_gcc)
tau_add ~ dgamma(a_add,r_add)
r ~ dexp(0.148) # Exp is the maximum entropy distribution for constraints of positive with givn mean
# 0.148 is from Richardson et al. 2006.
k ~ dunif(1,180)
}"
LogitRandomWalk= "
model{
for(yr in 1:ny){
#### Data Model: NDVI
for(i in 1:n){
y[yr,i] ~ dnorm(x[yr,i],tau_ndvi)
}
#### Data Model: GCC
for(i in 1:n){
z[yr,i] ~ dnorm(x[yr,i],tau_gcc)
}
#### Process Model
for(i in 2:n){
# lcolor[yr,i] <- logit(x[yr,i-1])
# color[yr,i]~dnorm(lcolor[yr,i],tau_add)
# logit(x[i]) <- color[i]
lcolor[yr,i] ~ dnorm(x[yr,i-1],tau_add)
x[yr,i] <- min(1,max(0,lcolor[yr,i]))
}
x[yr,1] ~ dnorm(x_ic,tau_ic)
} ## end loop over years
#### Priors
tau_ndvi ~ dgamma(a_ndvi,r_ndvi)
tau_gcc ~ dgamma(a_gcc,r_gcc)
tau_add ~ dgamma(a_add,r_add)
}"
RandomWalk= "
model{
for(yr in 1:ny){
#### Data Model: NDVI
for(i in 1:n){
y[yr,i] ~ dnorm(x[yr,i],tau_ndvi)
}
#### Data Model: GCC
for(i in 1:n){
z[yr,i] ~ dnorm(x[yr,i],tau_gcc)
}
#### Process Model
for(i in 2:n){
# lcolor[yr,i] <- logit(x[yr,i-1])
# color[yr,i]~dnorm(lcolor[yr,i],tau_add)
# logit(x[i]) <- color[i]
lcolor[yr,i] ~ dnorm(x[yr,i-1],tau_add)
x[yr,i] <- min(1,max(0,lcolor[yr,i]))
}
x[yr,1] ~ dnorm(x_ic,tau_ic)
} ## end loop over years
#### Priors
tau_ndvi ~ dgamma(a_ndvi,r_ndvi)
tau_gcc ~ dgamma(a_gcc,r_gcc)
tau_add ~ dgamma(a_add,r_add)
}"
ModisGCCModel = switch(model,
LogisticGrowth = LogisticGrowth,
LogitRandomWalk = LogitRandomWalk,
Threshold_Day_Logistic = Threshold_Day_Logistic)
## for some models, data needs to be a vector
if(model %in% "RandomWalk"){
data$y = as.vector(t(data$y))
data$z = as.vector(t(data$z))
}
## JAGS initial conditions
init <- list()
for(i in 1:n.chains){
#y.samp = sample(data$y,length(data$y),replace=TRUE)
########## what are the values for tau_ndvi and tau_gcc based on? is this reasonable?
init[[i]] <- list(#x = rep(1,length(data$y)),
tau_add = runif(1,0.5,2),#runif(1,0,1)/var(diff(y.samp),na.rm=TRUE),
tau_ndvi = 10,tau_gcc=10)
}
## compile JAGS model
j.model <- jags.model (file = textConnection(ModisGCCModel),
data = data,
inits = init,
n.chains = n.chains)
## burn-in
jags.out <- coda.samples (model = j.model,
variable.names = c("tau_add","tau_ndvi","tau_gcc","r"),
n.iter = min(n.iter,global_input_parameters$burn.in.iterations))
## run MCMC
jags.out <- coda.samples (model = j.model,
variable.names = c("x","tau_add","tau_ndvi","tau_gcc","r","k"),
n.iter = n.iter)
return(jags.out)
}