10.10 Plotting the data and model fits
We can plot the fits for our DFA model along with the data. The following function will return the fitted values ± (1-\(\alpha\))% confidence intervals.
<- function(MLEobj, dd = NULL, alpha = 0.05) {
get_DFA_fits ## empty list for results
<- list()
fits ## extra stuff for var() calcs
<- MARSS:::MARSShatyt(MLEobj)
Ey ## model params
<- coef(MLEobj, type = "matrix")$Z
ZZ ## number of obs ts
<- dim(Ey$ytT)[1]
nn ## number of time steps
<- dim(Ey$ytT)[2]
TT ## get the inverse of the rotation matrix
<- varimax(ZZ)$rotmat
H_inv ## check for covars
if (!is.null(dd)) {
<- coef(MLEobj, type = "matrix")$D
DD ## model expectation
$ex <- ZZ %*% H_inv %*% MLEobj$states + DD %*% dd
fitselse {
} ## model expectation
$ex <- ZZ %*% H_inv %*% MLEobj$states
fits
}## Var in model fits
<- MARSSkfss(MLEobj)$VtT
VtT <- NULL
VV for (tt in 1:TT) {
<- coef(MLEobj, type = "matrix")$R - ZZ %*% VtT[,
RZVZ %*% t(ZZ)
, tt] <- Ey$yxtT[, , tt] - Ey$ytT[, tt, drop = FALSE] %*%
SS t(MLEobj$states[, tt, drop = FALSE])
<- cbind(VV, diag(RZVZ + SS %*% t(ZZ) + ZZ %*% t(SS)))
VV
}<- sqrt(VV)
SE ## upper & lower (1-alpha)% CI
$up <- qnorm(1 - alpha/2) * SE + fits$ex
fits$lo <- qnorm(alpha/2) * SE + fits$ex
fitsreturn(fits)
}
Here are time series of the five phytoplankton groups (points) with the mean of the DFA fits (black line) and the 95% confidence intervals (gray lines).
## get model fits & CI's
<- get_DFA_fits(dfa_1)
mod_fit ## plot the fits
<- phytoplankton
ylbl 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) {
<- mod_fit$up[i, ]
up <- mod_fit$ex[i, ]
mn <- mod_fit$lo[i, ]
lo 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")
}