-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultivariateHMM.R
168 lines (123 loc) · 4.58 KB
/
MultivariateHMM.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
sink("Bayesian/Diving.jags")
cat("
model{
pi <- 3.141592653589
#for each if 6 argos class observation error
for(x in 1:6){
##argos observation error##
argos_prec[x,1:2,1:2] <- argos_cov[x,,]
#Constructing the covariance matrix
argos_cov[x,1,1] <- argos_sigma[x]
argos_cov[x,1,2] <- 0
argos_cov[x,2,1] <- 0
argos_cov[x,2,2] <- argos_alpha[x]
}
for(i in 1:ind){
for(g in 1:tracks[i]){
## Priors for first true location
#for lat long
y[i,g,1,1:2] ~ dmnorm(argos[i,g,1,1,1:2],argos_prec[1,1:2,1:2])
#First movement - random walk.
y[i,g,2,1:2] ~ dmnorm(y[i,g,1,1:2],iSigma)
###First Behavioral State###
state[i,g,1] ~ dcat(lambda[]) ## assign state for first obs
#Process Model for movement
for(t in 2:(steps[i,g]-1)){
#Behavioral State at time T
logit(phi[i,g,t,1]) <- alpha[state[i,g,t-1]] + beta_dive * dive[i,g,t]
phi[i,g,t,2] <- 1-phi[i,g,t,1]
state[i,g,t] ~ dcat(phi[i,g,t,])
#Turning covariate
#Transition Matrix for turning angles
T[i,g,t,1,1] <- cos(theta[state[i,g,t]])
T[i,g,t,1,2] <- (-sin(theta[state[i,g,t]]))
T[i,g,t,2,1] <- sin(theta[state[i,g,t]])
T[i,g,t,2,2] <- cos(theta[state[i,g,t]])
#Correlation in movement change
d[i,g,t,1:2] <- y[i,g,t,] + gamma[state[i,g,t]] * T[i,g,t,,] %*% (y[i,g,t,1:2] - y[i,g,t-1,1:2])
#Gaussian Displacement in location
y[i,g,t+1,1:2] ~ dmnorm(d[i,g,t,1:2],iSigma)
}
#Final behavior state
phi[i,g,steps[i,g],1] <- alpha[state[i,g,steps[i,g]-1]] + beta_dive * dive[i,g,t]
phi[i,g,steps[i,g],2] <- 1-phi[i,g,steps[i,g],1]
state[i,g,steps[i,g]] ~ dcat(phi[i,g,steps[i,g],])
## Measurement equation - irregular observations
# loops over regular time intervals (t)
for(t in 2:steps[i,g]){
# loops over observed locations within interval t
for(u in 1:idx[i,g,t]){
zhat[i,g,t,u,1:2] <- (1-j[i,g,t,u]) * y[i,g,t-1,1:2] + j[i,g,t,u] * y[i,g,t,1:2]
#for each lat and long
#argos error
argos[i,g,t,u,1:2] ~ dmnorm(zhat[i,g,t,u,1:2],argos_prec[argos_class[i,g,t,u],1:2,1:2])
#Assess Model Fit
#Fit dive discrepancy statistics
eval[i,g,t,u] ~ dnorm(depth_mu[state[i,g,t]],depth_tau[state[i,g,t]])T(0.01,)
E[i,g,t,u]<-pow((dive[i,g,t,u]-eval[i,g,t,u]),2)/(eval[i,g,t,u])
Enew[i,g,t,u]<-pow((dive_new[i,g,t,u]-eval[i,g,t,u]),2)/(eval[i,g,t,u])
}
}
}
}
###Priors###
#Process Variance
iSigma ~ dwish(R,2)
Sigma <- inverse(iSigma)
##Mean Angle
tmp[1] ~ dbeta(10, 10)
tmp[2] ~ dbeta(10, 10)
# prior for theta in 'traveling state'
theta[1] <- (2 * tmp[1] - 1) * pi
# prior for theta in 'foraging state'
theta[2] <- (tmp[2] * pi * 2)
##Move persistance
# prior for gamma (autocorrelation parameter)
#from jonsen 2016
##Behavioral States
gamma[1] ~ dbeta(3,2) ## gamma for state 1
dev ~ dbeta(1,1) ## a random deviate to ensure that gamma[1] > gamma[2]
gamma[2] <- gamma[1] * dev
#Intercepts
alpha[1] ~ dbeta(1,1)
alpha[2] ~ dbeta(1,1)
#Probability of behavior switching
lambda[1] ~ dbeta(1,1)
lambda[2] <- 1 - lambda[1]
#Dive Priors
#average max depth
depth_mu[1] ~ dnorm(0,0.001)
#we know that foraging dives are probably 0.1km deeper,
#but are not more than 0.5 km deeper
forage ~ dunif(0.01,0.5)
depth_mu[2] <- depth_mu[1] + forage
#speed = depth/duration priors
duration_mu[1] ~ dunif(0,3600)
duration_mu[2] ~ dunif(0,3600)
duration_tau[1] ~ dgamma(0.0001,0.0001)
duration_tau[2] ~ dgamma(0.0001,0.0001)
#Dive counts, there can't be more than about 20 dives in a 6 hour period.
lambda_count[1] ~ dunif(0,20)
lambda_count[2] ~ dunif(0,20)
#depth variance
depth_tau[1] ~ dgamma(0.0001,0.0001)
depth_tau[2] ~ dgamma(0.0001,0.0001)
##Argos priors##
#longitudinal argos precision, from Jonsen 2005, 2016, represented as precision not sd
#by argos class
argos_sigma[1] <- 11.9016
argos_sigma[2] <- 10.2775
argos_sigma[3] <- 1.228984
argos_sigma[4] <- 2.162593
argos_sigma[5] <- 3.885832
argos_sigma[6] <- 0.0565539
#latitidunal argos precision, from Jonsen 2005, 2016
argos_alpha[1] <- 67.12537
argos_alpha[2] <- 14.73474
argos_alpha[3] <- 4.718973
argos_alpha[4] <- 0.3872023
argos_alpha[5] <- 3.836444
argos_alpha[6] <- 0.1081156
}"
,fill=TRUE)
sink()