# \dontshow{
## for R_DEFAULT_PACKAGES=NULL
library( stats, pos = "package:base", verbose = FALSE)
library(graphics, pos = "package:base", verbose = FALSE)
# }
if (requireNamespace("deSolve")) withAutoprint({
to <- 100; by <- 0.01; R0 <- c(0, 16); ell <- c(0.5, 1)
soln.peak <- sir.aoi(to = to, by = by, R0 = R0, ell = ell,
root = function (S, R0) sum(R0) * S - 1,
aggregate = TRUE)
str(soln.peak)
info.peak <- attr(soln.peak, "root.info")
to <- 4 * info.peak[["tau"]] # a more principled endpoint
soln <- sir.aoi(to = to, by = by, R0 = R0, ell = ell,
aggregate = TRUE, atol = 0, rtol = 1e-12)
## ^^^^ ^^^^
## 'atol', 'rtol', ... are passed to deSolve::lsoda
head(soln)
tail(soln)
plot(soln) # dispatching stats:::plot.ts
plot(soln, log = "y")
(lamb <- summary(soln)) # left and right tail exponents
xoff <- function (x, k) x - x[k]
k <- c(16L, nrow(soln)) # c(1L, nrow(soln)) worse due to transient
plot(soln[, "Y"], log = "y", ylab = "Y")
abline(v = info.peak[["tau"]], h = info.peak[["state"]][, "Y"],
lty = 2, lwd = 2, col = "red")
for (i in 1:2)
lines(soln[k[i], "Y"] * exp(lamb[i] * xoff(time(soln), k[i])),
lty = 2, lwd = 2, col = "red")
wrap <-
function (root, ...)
attr(sir.aoi(to = to, by = by, R0 = R0, ell = ell,
root = root, aggregate = TRUE, ...),
"root.info")[["tau"]]
Ymax <- info.peak[["state"]][, "Y"]
## NB: want *simple* roots, not *multiple* roots
L <- list(function (Y) (Y - Ymax * 0.5) ,
function (Y) (Y - Ymax * 0.5)^2,
function (Y) (Y - Ymax ) ,
function (Y) (Y - Ymax )^2)
lapply(L, wrap)
## NB: critical values can be attained more than once
L <- list(function (Y, dY) Y - Ymax * 0.5,
function (Y, dY) if (dY > 0) Y - Ymax * 0.5 else 1,
function (Y, dY) if (dY < 0) Y - Ymax * 0.5 else 1)
lapply(L, wrap, root.max = 2L)
})
Run the code above in your browser using DataLab