Description Usage Arguments Value See Also Examples
Roll Warn Calculate early warnings statistics over a rolling window
1 |
x |
numeric vector over which to calculate statistics; should be a regularly spaced time series |
stat |
name of statistic to use; see |
win |
window length |
numeric vector of a rolling window statistic
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 | # ==========================================
# = Set up simulation and analysis options =
# ==========================================
nYear <- 2E2
Year <- 1:nYear
tripVal <- c(0.36, 0.21)
nBurn <- 50
sdU <- 0.05
# set up qE to increase harvest until collapse
qE_range <- c(tripVal[1]-0.2, tripVal[1]+0.05)
# goal here is to have harvest rate precisely hit certain
# values of qE during an integer year,
# while retaining linear change throughout
qEPoints <- c(qE_range[1], tripVal[2], tripVal[1], qE_range[2])
qEPoints <- qEPoints[qEPoints>=min(qE_range) & qEPoints<=max(qE_range)]
seq_arg0 <- c(as.list(range(qEPoints)), list(length.out=nYear))
yrProbs <- (qEPoints-min(qEPoints))/diff(range(qEPoints))
yrPoints <- quantile(Year, yrProbs)
qE <- approx(x=yrPoints, y=qEPoints, xout=Year)$y
# ==============================
# = Claculate time series of B =
# ==============================
B0 <- 400
B0 <- Burn(B0, n=nBurn, qE=qE[1], sdU=0)
Bvec <- c(B0, rep(NA, nYear-1))
for(i in 2:nYear){
Bvec[i] <- Bstep(B=Bvec[i-1], qE=qE[i], sdU=sdU)
}
# ==================================
# = Calculate rolling window stats =
# ==================================
sdVec <- rollWarn(Bvec, stat='sd', win=min(50, nYear/5))
ac1Vec <- rollWarn(Bvec, stat='ac1', win=min(50, nYear/5))
redVec0 <- rollWarn(Bvec, stat="redShift", win=min(50, nYear/5)) # holds the full spectrum
redList <- attributes(redVec0)$rsl # this gets the full spectrum formatted for plotting
redVec <- sapply(redVec0, function(x)x[[1]]) # this gets just the spectral slope
# =======================
# = Plot B and Warnings =
# =======================
par(mfrow=c(2,2), mar=c(1.75,1.75,0.5,1.75), ps=8, cex=1, mgp=c(0.75,0.15,0), tcl=-0.15)
plot(Year, Bvec, type='l')
abline(v=yrPoints, lty='dashed')
text_y <- min(Bvec)+yrProbs*diff(range(Bvec))
text(yrPoints, y=text_y, label=paste0("qE=", qEPoints), pos=c(4,4,2,2))
plot(Year, sdVec, col="forestgreen", type="l", ylab="Standard Deviation")
abline(v=yrPoints, lty='dashed')
plot(Year,ac1Vec, col="blue", type='l', ylab="AR(1)")
abline(v=yrPoints, lty='dashed')
plot(redList, xaxs='r')
par(new=TRUE)
plot(Year, redVec, col=adjustcolor("white",0.5), lwd=3, type='l', ylab="", xaxt='n', yaxt='n')
lines(Year, redVec, col=adjustcolor("black",0.5), lwd=0.5)
axis(side=4)
mtext("redShift", side=4, line=0.75)
abline(v=yrPoints, lty='dashed')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.