-
Notifications
You must be signed in to change notification settings - Fork 10
/
intro-to-dfa.R
307 lines (258 loc) · 10.9 KB
/
intro-to-dfa.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
## ----dfa-read-data--------------------------------------------------------------------------------------------
## load MARSS
library(MARSS)
## load the data (there are 3 datasets contained here)
data(lakeWAplankton, package = "MARSS")
## we want lakeWAplanktonTrans, which has been transformed
## so the 0s are replaced with NAs and the data z-scored
all_dat <- lakeWAplanktonTrans
## use only the 10 years from 1980-1989
yr_frst <- 1980
yr_last <- 1989
plank_dat <- all_dat[all_dat[, "Year"] >= yr_frst &
all_dat[, "Year"] <= yr_last,]
## create vector of phytoplankton group names
phytoplankton <- c("Cryptomonas", "Diatoms", "Greens",
"Unicells", "Other.algae")
## get only the phytoplankton
dat_1980 <- plank_dat[, phytoplankton]
## ----dfa-trans-data-------------------------------------------------------------------------------------------
## transpose data so time goes across columns
dat_1980 <- t(dat_1980)
## get number of time series
N_ts <- dim(dat_1980)[1]
## get length of time series
TT <- dim(dat_1980)[2]
## ----dfa-demean-data------------------------------------------------------------------------------------------
y_bar <- apply(dat_1980, 1, mean, na.rm = TRUE)
dat <- dat_1980 - y_bar
rownames(dat) <- rownames(dat_1980)
## ----dfa-plot-phytos, fig.height=9, fig.width=8, fig.cap='Demeaned time series of Lake Washington phytoplankton.'----
spp <- rownames(dat_1980)
clr <- c("brown","blue","darkgreen","darkred","purple")
cnt <- 1
par(mfrow = c(N_ts,1), mai = c(0.5,0.7,0.1,0.1), omi = c(0,0,0,0))
for(i in spp){
plot(dat[i,],xlab = "",ylab="Abundance index", bty = "L", xaxt = "n", pch=16, col=clr[cnt], type="b")
axis(1,12*(0:dim(dat_1980)[2])+1,yr_frst+0:dim(dat_1980)[2])
title(i)
cnt <- cnt + 1
}
## ----dfa-dfa-obs-eqn------------------------------------------------------------------------------------------
## 'ZZ' is loadings matrix
Z_vals <- list("z11", 0 , 0 ,
"z21","z22", 0 ,
"z31","z32","z33",
"z41","z42","z43",
"z51","z52","z53")
ZZ <- matrix(Z_vals, nrow = N_ts, ncol = 3, byrow = TRUE)
ZZ
## 'aa' is the offset/scaling
aa <- "zero"
## 'DD' and 'd' are for covariates
DD <- "zero" # matrix(0,mm,1)
dd <- "zero" # matrix(0,1,wk_last)
## 'RR' is var-cov matrix for obs errors
RR <- "diagonal and unequal"
## ----dfa-dfa-proc-eqn-----------------------------------------------------------------------------------------
## number of processes
mm <- 3
## 'BB' is identity: 1's along the diagonal & 0's elsewhere
BB <- "identity" # diag(mm)
## 'uu' is a column vector of 0's
uu <- "zero" # matrix(0, mm, 1)
## 'CC' and 'cc' are for covariates
CC <- "zero" # matrix(0, mm, 1)
cc <- "zero" # matrix(0, 1, wk_last)
## 'QQ' is identity
QQ <- "identity" # diag(mm)
## ----dfa-create-model-lists-----------------------------------------------------------------------------------
## list with specifications for model vectors/matrices
mod_list <- list(Z = ZZ, A = aa, D = DD, d = dd, R = RR,
B = BB, U = uu, C = CC, c = cc, Q = QQ)
## list with model inits
init_list <- list(x0 = matrix(rep(0, mm), mm, 1))
## list with model control parameters
con_list <- list(maxit = 3000, allow.degen = TRUE)
## ----dfa-fit-dfa-1, cache=TRUE--------------------------------------------------------------------------------
## fit MARSS
dfa_1 <- MARSS(y = dat, model = mod_list, inits = init_list, control = con_list)
## ----dfa-get-H-inv--------------------------------------------------------------------------------------------
## get the estimated ZZ
Z_est <- coef(dfa_1, type = "matrix")$Z
## get the inverse of the rotation matrix
H_inv <- varimax(Z_est)$rotmat
## ----dfa-rotate-Z-x-------------------------------------------------------------------------------------------
## rotate factor loadings
Z_rot = Z_est %*% H_inv
## rotate processes
proc_rot = solve(H_inv) %*% dfa_1$states
## ----dfa-plot-dfa1, fig.height=9, fig.width=8, eval=TRUE, fig.cap='Estimated states from the DFA model.'------
ylbl <- phytoplankton
w_ts <- seq(dim(dat)[2])
layout(matrix(c(1,2,3,4,5,6), mm, 2), widths = c(2,1))
## par(mfcol=c(mm,2), mai = c(0.5,0.5,0.5,0.1), omi = c(0,0,0,0))
par(mai = c(0.5,0.5,0.5,0.1), omi = c(0,0,0,0))
## plot the processes
for(i in 1:mm) {
ylm <- c(-1,1)*max(abs(proc_rot[i,]))
## set up plot area
plot(w_ts,proc_rot[i,], type = "n", bty = "L",
ylim = ylm, xlab = "", ylab = "", xaxt = "n")
## draw zero-line
abline(h=0, col="gray")
## plot trend line
lines(w_ts, proc_rot[i,], lwd = 2)
lines(w_ts, proc_rot[i,], lwd = 2)
## add panel labels
mtext(paste("State",i), side = 3, line = 0.5)
axis(1,12*(0:dim(dat_1980)[2])+1,yr_frst+0:dim(dat_1980)[2])
}
## plot the loadings
minZ <- 0
ylm <- c(-1,1)*max(abs(Z_rot))
for(i in 1:mm) {
plot(c(1:N_ts)[abs(Z_rot[,i])>minZ], as.vector(Z_rot[abs(Z_rot[,i])>minZ,i]), type="h",
lwd = 2, xlab = "", ylab = "", xaxt = "n", ylim = ylm, xlim=c(0.5,N_ts+0.5), col=clr)
for(j in 1:N_ts) {
if(Z_rot[j,i] > minZ) {text(j, -0.03, ylbl[j], srt=90, adj=1, cex=1.2, col=clr[j])}
if(Z_rot[j,i] < -minZ) {text(j, 0.03, ylbl[j], srt=90, adj=0, cex=1.2, col=clr[j])}
abline(h=0, lwd=1.5, col="gray")
}
mtext(paste("Factor loadings on state",i),side=3,line=0.5)
}
## ----dfa-xy-states12, height=4, width=5, fig.cap='Cross-correlation plot of the two rotations.'---------------
par(mai = c(0.9,0.9,0.1,0.1))
ccf(proc_rot[1,],proc_rot[2,], lag.max = 12, main="")
## ----dfa-defn-get-DFA-fits------------------------------------------------------------------------------------
get_DFA_fits <- function(MLEobj, dd = NULL, alpha = 0.05) {
## empty list for results
fits <- list()
## extra stuff for var() calcs
Ey <- MARSS:::MARSShatyt(MLEobj)
## model params
ZZ <- coef(MLEobj, type="matrix")$Z
## number of obs ts
nn <- dim(Ey$ytT)[1]
## number of time steps
TT <- dim(Ey$ytT)[2]
## get the inverse of the rotation matrix
H_inv <- varimax(ZZ)$rotmat
## check for covars
if(!is.null(dd)) {
DD <- coef(MLEobj, type = "matrix")$D
## model expectation
fits$ex <- ZZ %*% H_inv %*% MLEobj$states + DD %*% dd
} else {
## model expectation
fits$ex <- ZZ %*% H_inv %*% MLEobj$states
}
## Var in model fits
VtT <- MARSSkfss(MLEobj)$VtT
VV <- NULL
for(tt in 1:TT) {
RZVZ <- coef(MLEobj, type = "matrix")$R - ZZ %*% VtT[,,tt] %*% t(ZZ)
SS <- Ey$yxtT[,,tt] - Ey$ytT[,tt,drop = FALSE] %*% t(MLEobj$states[,tt,drop = FALSE])
VV <- cbind(VV, diag(RZVZ + SS %*% t(ZZ) + ZZ %*% t(SS)))
}
SE <- sqrt(VV)
## upper & lower (1-alpha)% CI
fits$up <- qnorm(1-alpha/2)*SE + fits$ex
fits$lo <- qnorm(alpha/2)*SE + fits$ex
return(fits)
}
## ----dfa-plot-dfa-fits, fig.height=9, fig.width=8, fig.cap='Data and fits from the DFA model.'----------------
## get model fits & CI's
mod_fit <- get_DFA_fits(dfa_1)
## plot the fits
ylbl <- phytoplankton
par(mfrow = c(N_ts,1), mai = c(0.5,0.7,0.1,0.1), omi = c(0,0,0,0))
for(i in 1:N_ts) {
up <- mod_fit$up[i,]
mn <- mod_fit$ex[i,]
lo <- mod_fit$lo[i,]
plot(w_ts,mn,xlab = "",ylab=ylbl[i],xaxt = "n",type = "n", cex.lab = 1.2,
ylim=c(min(lo),max(up)))
axis(1,12*(0:dim(dat_1980)[2])+1,yr_frst+0:dim(dat_1980)[2])
points(w_ts,dat[i,], pch=16, col=clr[i])
lines(w_ts, up, col="darkgray")
lines(w_ts, mn, col="black", lwd = 2)
lines(w_ts, lo, col="darkgray")
}
## ----dfa-get-covars-------------------------------------------------------------------------------------------
temp <- t(plank_dat[,"Temp",drop = FALSE])
TP <- t(plank_dat[,"TP",drop = FALSE])
## ----dfa-fit-DFA-covars, cache=TRUE, results='hide'-----------------------------------------------------------
mod_list = list(m = 3, R = "diagonal and unequal")
dfa_temp <- MARSS(dat, model = mod_list, form = "dfa", z.score = FALSE,
control = con_list, covariates=temp)
dfa_TP <- MARSS(dat, model = mod_list, form = "dfa", z.score = FALSE,
control = con_list, covariates=TP)
dfa_both <- MARSS(dat, model = mod_list, form = "dfa", z.score = FALSE,
control = con_list, covariates=rbind(temp,TP))
## ----dfa-model-selection--------------------------------------------------------------------------------------
print(cbind(model = c("no covars", "Temp", "TP", "Temp & TP"),
AICc = round(c(dfa_1$AICc, dfa_temp$AICc, dfa_TP$AICc, dfa_both$AICc))),
quote = FALSE)
## ----dfa-fit-dfa-dummy, cache=TRUE----------------------------------------------------------------------------
cos_t <- cos(2 * pi * seq(TT) / 12)
sin_t <- sin(2 * pi * seq(TT) / 12)
dd <- rbind(cos_t,sin_t)
dfa_seas <- MARSS(dat, model = mod_list, form = "dfa", z.score = FALSE,
control = con_list, covariates = dd)
dfa_seas$AICc
## ----dfa-plot-dfa-temp-fits, fig.height=9, fig.width=8, fig.cap="Data and model fits for the DFA with covariates."----
## get model fits & CI's
mod_fit <- get_DFA_fits(dfa_seas,dd=dd)
## plot the fits
ylbl <- phytoplankton
par(mfrow = c(N_ts,1), mai = c(0.5,0.7,0.1,0.1), omi = c(0,0,0,0))
for(i in 1:N_ts) {
up <- mod_fit$up[i,]
mn <- mod_fit$ex[i,]
lo <- mod_fit$lo[i,]
plot(w_ts,mn,xlab = "",ylab=ylbl[i],xaxt = "n",type = "n", cex.lab = 1.2,
ylim=c(min(lo),max(up)))
axis(1,12*(0:dim(dat_1980)[2])+1,yr_frst+0:dim(dat_1980)[2])
points(w_ts,dat[i,], pch=16, col=clr[i])
lines(w_ts, up, col="darkgray")
lines(w_ts, mn, col="black", lwd = 2)
lines(w_ts, lo, col="darkgray")
}
## ----lwa_zoops, echo = TRUE-----------------------------------------------------------------------------------
## zooplankton species
zooplankton <- c("Cyclops", "Daphnia", "Diaptomus",
"Non.daphnid.cladocerans", "Epischura")
## zooplankton data
dat_zoop <- plank_dat[, zooplankton]
## rename Non.daphnid.cladocerans to NDC
colnames(dat_zoop)[4] <- "NDC"
## transpose data so time goes across columns
dat_zoop <- t(dat_zoop)
## de-mean the data
y_bar <- apply(dat_zoop, 1, mean, na.rm = TRUE)
dat_zoop <- dat_zoop - y_bar
## ----task_1, eval = FALSE-------------------------------------------------------------------------------------
## ## fill in this list as appropriate
## mod_list = list(m = ?, R = ?)
##
## ## fit the model
## dfa_task_1 <- MARSS(dat_zoop, model = mod_list, form = "dfa", z.score = FALSE,
## control = con_list)
## ----task_2, eval = FALSE-------------------------------------------------------------------------------------
## ## fill in this list as appropriate
## mod_list = list(m = ?, R = ?)
##
## ## fit the model
## dfa_task_2 <- MARSS(dat_zoop, model = mod_list, form = "dfa", z.score = FALSE,
## control = con_list)
## ----task_3, eval = FALSE-------------------------------------------------------------------------------------
## ## fill in this list as appropriate
## mod_list = list(m = ?, R = ?)
##
## ## assign the covariates
## dd <- ?
##
## ## fit the model
## dfa_task_3 <- MARSS(dat_zoop, model = mod_list, form = "dfa", z.score = FALSE,
## control = con_list, covariates = dd)