# extinction_risk_1.R
# $Id: extinction_risk_1.R,v 1.14 2015/08/08 03:50:17 hako Exp $
#
# Author: Hiroshi Hakoyama <hako@affrc.go.jp>
# Copyright (c) 2013-2015 Hiroshi Hakoyama <hako@affrc.go.jp>, All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Description:
# Estimates the demographic parameters and the probability of
# extinction within a specific time period, t, from a time series
# of population size based on the Wiener-drift process model.
# An estimator for confidence interval of extinction risk
# developed by Hakoyama (w-z method, in preparation) is implemented,
# which is better than the estimator from the ordinary Delta method.
#
# Usage:
# ext1(dat, t = 100, ne = 1, alpha = 0.05, verbose = FALSE,
# exact.CL = FALSE, n.sim = 10000, alpha.sim = 0.05, formatted = TRUE)
#
# Arguments:
# dat data.frame of 2 variables: Time and Population size
# t a time period of interest
# ne a lower (extinction) threshold of population size
# alpha 1 - confidence level
# verbose If set to FALSE, give the ML estimate of the probability
# of extinction within a specific time period t. If set to
# TRUE, give a more verbose output:
# the ML estimate of the growth rate (mu),
# the ML estimate of variance (s),
# Current population size, xd = log(N(q) / ne),
# Sample size, n = q + 1,
# the ML estimate of the probability of extinction within
# a specific time period t (P),
# a lower alpha % confidence limit of parameter *
# (lower.CL.*), and
# an upper alpha % confidence limit of parameter *
# (upper.CL.*).
# exact.CL If set to FALSE, give a CI by the w-z method.
# If set to TRUE, give an asymptotically exact CI.
# n.sim The number of iteration for the asymptotically exact CI.
# alpha.sim Level of significance to decide the convergence of iteration.
# qq.plot If set to TRUE, give a QQ plot for delta.log.n.
# formatted If set to TRUE, give the result by formatted output.
# If set to FALSE, give a list of estimates.
# References:
# R. Lande and S. H. Orzack. Extinction dynamics of age-structured
# populations in a fluctuating environment. Proceedings of the
# National Academy of Sciences, 85(19):7418-7421, 1988.
# B. Dennis, P. L. Munholland, and J. M. Scott. Estimation of
# growth and extinction parameters for endangered species.
# Ecological Monographs, 61:115-143, 1991.
# H. Hakoyama (in preparation).
#
ext1 <- function(dat, t = 100, ne = 1, alpha = 0.05, verbose = FALSE, exact.CL = FALSE, n.sim = 10000, alpha.sim = 0.05, qq.plot = FALSE, formatted = TRUE) {
yr <- ts(dat[, 1], start = c(dat[, 1][1])) # Year
ps <- ts(dat[, 2], start = c(dat[, 1][1])) # Population size
complete <- complete.cases(yr, ps)
yr <- yr[complete]
ps <- ps[complete]
tau <- diff(yr) # time intervals, \tau_i = t_i - t_{i-1}
delta.log.n <- diff(log(ps))
q <- length(yr) - 1 # yr = \{t_0, t_1, \dots, t_q\}
tq <- sum(tau)
mu <- sum(delta.log.n) / tq # ML estimate of growth rate
s <- (1 / q) * sum((delta.log.n - mu * tau)^2 / tau) # ML estimate of variance
us <- q * s / (q - 1) # an unbiased estimate of variance
xd <- log(ps[length(ps)] / ne)
if (qq.plot == TRUE) {
qqnorm(delta.log.n)
qqline(delta.log.n)
}
lower.CL.mu <- mu - qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
upper.CL.mu <- mu + qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
lower.CL.s <- q * s / qchisq(1 - alpha / 2, q - 1)
upper.CL.s <- q * s / qchisq(alpha / 2, q - 1)
w <- function(mu, xd, s, t) (mu * t + xd) / sqrt(s * t)
z <- function(mu, xd, s, t) (- mu * t + xd) / sqrt(s * t)
pr <- function(w, z) {
if(z < 35) {
pnorm(-w) + exp((z^2 - w^2) / 2) * pnorm(-z)
} else {
pnorm(-w) + exp(- w^2 / 2) * (sqrt(2) / (2 * sqrt(pi))) *
(1 / z - 1 / z^3 + 3 / z^5 - 15 / z^7
+ 105 / z^9 - 945 / z^11 + 10395 / z^13)
}
}
ww <- w(mu, xd, s, t)
zz <- z(mu, xd, s, t)
pp <- pr(ww, zz)
c.limit <- function(tq, q, t, est, a, width = 10) {
t.obs <- sqrt((q - 1) * tq / (q * t)) * est
df <- q - 1
const <- sqrt(tq / t)
f <- function(x) {
pt(t.obs, df, const * x) - a
}
d.est <- est / width + 1
uniroot(f, c(- d.est + est, d.est + est), extendInt = "yes")$root
}
confidence.interval <- function(mu, xd, s, t, tq, q, alpha) {
den1 <- sqrt(s * t)
w.est <- (mu * t + xd) / den1
z.est <- (- mu * t + xd) / den1
lower.CL.w <- c.limit(tq, q, t, w.est, 1 - alpha / 2)
upper.CL.w <- c.limit(tq, q, t, w.est, alpha / 2)
lower.CL.z <- c.limit(tq, q, t, z.est, 1 - alpha / 2)
upper.CL.z <- c.limit(tq, q, t, z.est, alpha / 2)
lower.CL.P <- pr(upper.CL.w, lower.CL.z)
upper.CL.P <- pr(lower.CL.w, upper.CL.z)
c(lower.CL.P, upper.CL.P)
}
CI.rand <- function(mu.obs, xd, s.obs, t, tq, q, alpha) {
mu <- rnorm(1, mean = mu.obs, sd = sqrt(s.obs / q))
s <- rchisq(1, df = q - 1) * s.obs / q
confidence.interval(mu, xd, s, t, tq, q, alpha)
}
CI.sim <- function(n.sim, gam, alpha, mu.obs, xd, s.obs, t, tq, q) {
CI.dist <- replicate(n.sim, CI.rand(mu.obs, xd, s.obs, t, tq, q, alpha))
w.obs <- w(mu.obs, xd, s.obs, t)
z.obs <- z(mu.obs, xd, s.obs, t)
P.obs <- pr(w.obs, z.obs)
ci <- confidence.interval(mu.obs, xd, s.obs, t, tq, q, alpha)
complete <- complete.cases(CI.dist[1, ], CI.dist[2, ])
lower <- CI.dist[1, ][complete]
upper <- CI.dist[2, ][complete]
n.estimables <- length(lower)
n.rejects <- (sum(P.obs < lower) + sum(P.obs > upper))
if (n.estimables > 0) {
binom <- binom.test(n.rejects, n.estimables, p = gam)
} else {
binom <- list(estimate = NaN, p.value = NaN)
}
c(binom$estimate[[1]], binom$p.value, ci)
}
asymptotically.exact.confidence.interval <- function(n.sim, alpha.sim, mu.obs, xd, s.obs, t, tq, q, alpha) {
res <- CI.sim(n.sim, alpha, alpha, mu.obs, xd, s.obs, t, tq, q)
gg <- res[[1]]
pp <- res[[2]]
a <- alpha
if (pp > alpha.sim) {
return(confidence.interval(mu, xd, s, t, tq, q, alpha))
} else {
while (pp < alpha.sim) {
res <- CI.sim(n.sim, alpha, a, mu.obs, xd, s.obs, t, tq, q)
gg <- res[[1]]
pp <- res[[2]]
a <- a * alpha / gg
}
c(res[[3]], res[[4]])
}
}
if (exact.CL == TRUE) {
CL.P <- asymptotically.exact.confidence.interval(n.sim, alpha.sim, mu, xd, s, t, tq, q, alpha)
} else {
CL.P <- confidence.interval(mu, xd, s, t, tq, q, alpha)
}
lower.CL.P <- CL.P[[1]]
upper.CL.P <- CL.P[[2]]
if (verbose == TRUE) {
results <- list(ne = ne, t = t, verbose = verbose,
n = q + 1,
xd = xd,
Growth.rate = mu,
lower.CL.mu = lower.CL.mu,
upper.CL.mu = upper.CL.mu,
Variance = s,
lower.CL.s = lower.CL.s,
upper.CL.s = upper.CL.s,
# Unbiased.variance = us,
Extinction.probability = pp,
lower.CL.P = lower.CL.P,
upper.CL.P = upper.CL.P)
if (formatted == TRUE) {
class(results) <- "ext1"
}
return(results)
} else {
results <- list(ne = ne, t = t, verbose = verbose,
Extinction.probability = pp)
if (formatted == TRUE) {
class(results) <- "ext1"
}
return(results)
}
}
print.ext1 <- function(obj, digits = 5) {
if (obj$verbose == TRUE) {
output.est <- data.frame(
c(formatC(obj$Growth.rate, digits = digits),
formatC(obj$Variance, digits = digits),
formatC(obj$xd, digits = digits),
formatC(obj$n, digits = digits),
formatC(obj$Extinction.probability, digits = digits)),
c(paste("(",formatC(obj$lower.CL.mu, digits = digits),", ",
formatC(obj$upper.CL.mu, digits = digits),")", sep = ""),
paste("(",formatC(obj$lower.CL.s, digits = digits),", ",
formatC(obj$upper.CL.s, digits = digits),")", sep = ""),
"-",
"-",
paste("(",formatC(obj$lower.CL.P, digits = digits),", ",
formatC(obj$upper.CL.P, digits = digits),")", sep = "")))
dimnames(output.est) <- list(
c("Growth rate:",
"Variance:",
"Current population size, xd = log(N(q) / ne):",
"Sample size, n = q + 1:",
paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
c("Estimate","95% confidence interval"))
print(output.est)
} else {
output.est <- data.frame(
c(formatC(obj$Extinction.probability, digits = digits)))
dimnames(output.est) <- list(
c(paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
c("Estimate"))
print(output.est)
}
}
#
# Examples
# Yellowstone grizzly bears (from Dennis et al., 1991)
# dat <- data.frame(Year = c(1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987),
# Population = c(44, 47, 46, 44, 46, 45, 46, 40, 39, 39, 42, 44, 41, 40, 33, 36, 34, 39, 35, 34, 38, 36, 37, 41, 39, 51, 47, 57, 47))
# The probability of extinction (of decline to population size 1) within 100 years
# ext1(dat, t = 100)
# The probability of decline to 10 individuals within 100 years
# ext1(dat, t = 100, ne = 10, verbose = TRUE)
# with QQ-plot
# ext1(dat, t = 100, ne = 10, verbose = TRUE, qq.plot = T)
# The probability of decline to 1 individuals within 100 years with an asymptotically exact confidence interval
# ext1(dat, t = 100, ne = 1, verbose = TRUE, exact.CL = TRUE)