Annotation of ext/extinction_risk_1.R, Revision 1.19
1.7 hako 1: # extinction_risk_1.R
1.19 ! hako 2: # $Id: extinction_risk_1.R,v 1.18 2015/08/14 11:33:15 hako Exp $
1.1 hako 3: #
4: # Author: Hiroshi Hakoyama <hako@affrc.go.jp>
5: # Copyright (c) 2013-2015 Hiroshi Hakoyama <hako@affrc.go.jp>, All rights reserved.
6: #
7: # Redistribution and use in source and binary forms, with or without
8: # modification, are permitted provided that the following conditions
9: # are met:
10: # 1. Redistributions of source code must retain the above copyright
11: # notice, this list of conditions and the following disclaimer.
12: # 2. Redistributions in binary form must reproduce the above copyright
13: # notice, this list of conditions and the following disclaimer in the
14: # documentation and/or other materials provided with the distribution.
15: #
16: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17: # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18: # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19: # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20: # AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21: # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23: # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24: # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25: # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: #
29: # Description:
1.8 hako 30: # Estimates the demographic parameters and the probability of
31: # extinction within a specific time period, t, from a time series
32: # of population size based on the Wiener-drift process model.
33: # An estimator for confidence interval of extinction risk
34: # developed by Hakoyama (w-z method, in preparation) is implemented,
35: # which is better than the estimator from the ordinary Delta method.
1.1 hako 36: #
37: # Usage:
1.8 hako 38: # ext1(dat, t = 100, ne = 1, alpha = 0.05, verbose = FALSE,
1.15 hako 39: # exact.CL = FALSE, n.sim = 10000, alpha.sim = 0.05, qq.plot = FALSE, formatted = TRUE)
1.1 hako 40: #
41: # Arguments:
1.13 hako 42: # dat data.frame of 2 variables: Time and Population size
43: # t a time period of interest
44: # ne a lower (extinction) threshold of population size
1.8 hako 45: # alpha 1 - confidence level
1.13 hako 46: # verbose If set to FALSE, give the ML estimate of the probability
1.8 hako 47: # of extinction within a specific time period t. If set to
48: # TRUE, give a more verbose output:
49: # the ML estimate of the growth rate (mu),
50: # the ML estimate of variance (s),
51: # Current population size, xd = log(N(q) / ne),
52: # Sample size, n = q + 1,
1.19 ! hako 53: # AIC for the distribution of X = log(N),
1.8 hako 54: # the ML estimate of the probability of extinction within
55: # a specific time period t (P),
56: # a lower alpha % confidence limit of parameter *
57: # (lower.CL.*), and
58: # an upper alpha % confidence limit of parameter *
59: # (upper.CL.*).
1.10 hako 60: # exact.CL If set to FALSE, give a CI by the w-z method.
61: # If set to TRUE, give an asymptotically exact CI.
62: # n.sim The number of iteration for the asymptotically exact CI.
63: # alpha.sim Level of significance to decide the convergence of iteration.
1.13 hako 64: # qq.plot If set to TRUE, give a QQ plot for delta.log.n.
1.14 hako 65: # formatted If set to TRUE, give the result by formatted output.
1.8 hako 66: # If set to FALSE, give a list of estimates.
1.1 hako 67: # References:
1.8 hako 68: # R. Lande and S. H. Orzack. Extinction dynamics of age-structured
69: # populations in a fluctuating environment. Proceedings of the
70: # National Academy of Sciences, 85(19):7418-7421, 1988.
71: # B. Dennis, P. L. Munholland, and J. M. Scott. Estimation of
72: # growth and extinction parameters for endangered species.
73: # Ecological Monographs, 61:115-143, 1991.
74: # H. Hakoyama (in preparation).
1.1 hako 75: #
76:
1.13 hako 77: 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) {
1.1 hako 78: yr <- ts(dat[, 1], start = c(dat[, 1][1])) # Year
79: ps <- ts(dat[, 2], start = c(dat[, 1][1])) # Population size
80: complete <- complete.cases(yr, ps)
81: yr <- yr[complete]
82: ps <- ps[complete]
83: tau <- diff(yr) # time intervals, \tau_i = t_i - t_{i-1}
1.8 hako 84: delta.log.n <- diff(log(ps))
1.1 hako 85: q <- length(yr) - 1 # yr = \{t_0, t_1, \dots, t_q\}
86: tq <- sum(tau)
1.8 hako 87: mu <- sum(delta.log.n) / tq # ML estimate of growth rate
88: s <- (1 / q) * sum((delta.log.n - mu * tau)^2 / tau) # ML estimate of variance
1.1 hako 89: us <- q * s / (q - 1) # an unbiased estimate of variance
90: xd <- log(ps[length(ps)] / ne)
1.17 hako 91: l <- - (q / 2) * log(2 * pi * s) - sum((delta.log.n - mu)^2) / (2 * s) # ML for the distribution of x = log.n (not for n).
92: AIC <- - 2 * l + 4 # AIC for x
93:
1.13 hako 94: if (qq.plot == TRUE) {
95: qqnorm(delta.log.n)
96: qqline(delta.log.n)
97: }
98:
99: lower.CL.mu <- mu - qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.8 hako 100: upper.CL.mu <- mu + qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.13 hako 101: lower.CL.s <- q * s / qchisq(1 - alpha / 2, q - 1)
1.8 hako 102: upper.CL.s <- q * s / qchisq(alpha / 2, q - 1)
103:
104: w <- function(mu, xd, s, t) (mu * t + xd) / sqrt(s * t)
105: z <- function(mu, xd, s, t) (- mu * t + xd) / sqrt(s * t)
106: pr <- function(w, z) {
107: if(z < 35) {
108: pnorm(-w) + exp((z^2 - w^2) / 2) * pnorm(-z)
109: } else {
1.13 hako 110: pnorm(-w) + exp(- w^2 / 2) * (sqrt(2) / (2 * sqrt(pi))) *
111: (1 / z - 1 / z^3 + 3 / z^5 - 15 / z^7
112: + 105 / z^9 - 945 / z^11 + 10395 / z^13)
1.8 hako 113: }
1.1 hako 114: }
1.8 hako 115:
116: ww <- w(mu, xd, s, t)
117: zz <- z(mu, xd, s, t)
118: pp <- pr(ww, zz)
119:
120: c.limit <- function(tq, q, t, est, a, width = 10) {
121: t.obs <- sqrt((q - 1) * tq / (q * t)) * est
122: df <- q - 1
123: const <- sqrt(tq / t)
124: f <- function(x) {
125: pt(t.obs, df, const * x) - a
126: }
127: d.est <- est / width + 1
128: uniroot(f, c(- d.est + est, d.est + est), extendInt = "yes")$root
1.1 hako 129: }
1.8 hako 130:
131: confidence.interval <- function(mu, xd, s, t, tq, q, alpha) {
132: den1 <- sqrt(s * t)
133: w.est <- (mu * t + xd) / den1
134: z.est <- (- mu * t + xd) / den1
135: lower.CL.w <- c.limit(tq, q, t, w.est, 1 - alpha / 2)
136: upper.CL.w <- c.limit(tq, q, t, w.est, alpha / 2)
137: lower.CL.z <- c.limit(tq, q, t, z.est, 1 - alpha / 2)
138: upper.CL.z <- c.limit(tq, q, t, z.est, alpha / 2)
139: lower.CL.P <- pr(upper.CL.w, lower.CL.z)
140: upper.CL.P <- pr(lower.CL.w, upper.CL.z)
141: c(lower.CL.P, upper.CL.P)
1.1 hako 142: }
1.8 hako 143:
1.12 hako 144: CI.rand <- function(mu.obs, xd, s.obs, t, tq, q, alpha) {
1.10 hako 145: mu <- rnorm(1, mean = mu.obs, sd = sqrt(s.obs / q))
146: s <- rchisq(1, df = q - 1) * s.obs / q
147: confidence.interval(mu, xd, s, t, tq, q, alpha)
148: }
149:
1.12 hako 150: CI.sim <- function(n.sim, gam, alpha, mu.obs, xd, s.obs, t, tq, q) {
151: CI.dist <- replicate(n.sim, CI.rand(mu.obs, xd, s.obs, t, tq, q, alpha))
1.18 hako 152: P.obs <- pr(w(mu.obs, xd, s.obs, t), z(mu.obs, xd, s.obs, t))
1.10 hako 153: ci <- confidence.interval(mu.obs, xd, s.obs, t, tq, q, alpha)
154: complete <- complete.cases(CI.dist[1, ], CI.dist[2, ])
155: lower <- CI.dist[1, ][complete]
156: upper <- CI.dist[2, ][complete]
157: n.estimables <- length(lower)
158: n.rejects <- (sum(P.obs < lower) + sum(P.obs > upper))
159: if (n.estimables > 0) {
160: binom <- binom.test(n.rejects, n.estimables, p = gam)
161: } else {
162: binom <- list(estimate = NaN, p.value = NaN)
163: }
164: c(binom$estimate[[1]], binom$p.value, ci)
165: }
166:
1.12 hako 167: asymptotically.exact.confidence.interval <- function(n.sim, alpha.sim, mu.obs, xd, s.obs, t, tq, q, alpha) {
1.18 hako 168: P.obs <- pr(w(mu.obs, xd, s.obs, t), z(mu.obs, xd, s.obs, t))
169: if (P.obs == 0 || P.obs == 1) {
170: return(confidence.interval(mu.obs, xd, s.obs, t, tq, q, alpha))
171: }
1.12 hako 172: res <- CI.sim(n.sim, alpha, alpha, mu.obs, xd, s.obs, t, tq, q)
1.10 hako 173: gg <- res[[1]]
174: pp <- res[[2]]
175: a <- alpha
176: while (pp < alpha.sim) {
1.12 hako 177: res <- CI.sim(n.sim, alpha, a, mu.obs, xd, s.obs, t, tq, q)
1.10 hako 178: gg <- res[[1]]
179: pp <- res[[2]]
180: a <- a * alpha / gg
181: }
182: c(res[[3]], res[[4]])
183: }
184:
185: if (exact.CL == TRUE) {
1.13 hako 186: CL.P <- asymptotically.exact.confidence.interval(n.sim, alpha.sim, mu, xd, s, t, tq, q, alpha)
187: } else {
188: CL.P <- confidence.interval(mu, xd, s, t, tq, q, alpha)
189: }
1.8 hako 190: lower.CL.P <- CL.P[[1]]
191: upper.CL.P <- CL.P[[2]]
192:
1.1 hako 193: if (verbose == TRUE) {
194: results <- list(ne = ne, t = t, verbose = verbose,
1.16 hako 195: AIC = AIC,
1.8 hako 196: n = q + 1,
1.6 hako 197: xd = xd,
1.1 hako 198: Growth.rate = mu,
1.8 hako 199: lower.CL.mu = lower.CL.mu,
200: upper.CL.mu = upper.CL.mu,
1.1 hako 201: Variance = s,
1.8 hako 202: lower.CL.s = lower.CL.s,
203: upper.CL.s = upper.CL.s,
204: # Unbiased.variance = us,
205: Extinction.probability = pp,
206: lower.CL.P = lower.CL.P,
207: upper.CL.P = upper.CL.P)
1.4 hako 208: if (formatted == TRUE) {
209: class(results) <- "ext1"
210: }
1.1 hako 211: return(results)
212: } else {
213: results <- list(ne = ne, t = t, verbose = verbose,
1.8 hako 214: Extinction.probability = pp)
1.4 hako 215: if (formatted == TRUE) {
216: class(results) <- "ext1"
217: }
1.1 hako 218: return(results)
219: }
220: }
221:
222: print.ext1 <- function(obj, digits = 5) {
223: if (obj$verbose == TRUE) {
224: output.est <- data.frame(
225: c(formatC(obj$Growth.rate, digits = digits),
226: formatC(obj$Variance, digits = digits),
1.8 hako 227: formatC(obj$xd, digits = digits),
228: formatC(obj$n, digits = digits),
1.16 hako 229: formatC(obj$AIC, digits = digits),
1.1 hako 230: formatC(obj$Extinction.probability, digits = digits)),
1.8 hako 231: c(paste("(",formatC(obj$lower.CL.mu, digits = digits),", ",
232: formatC(obj$upper.CL.mu, digits = digits),")", sep = ""),
233: paste("(",formatC(obj$lower.CL.s, digits = digits),", ",
234: formatC(obj$upper.CL.s, digits = digits),")", sep = ""),
235: "-",
236: "-",
1.16 hako 237: "-",
1.8 hako 238: paste("(",formatC(obj$lower.CL.P, digits = digits),", ",
239: formatC(obj$upper.CL.P, digits = digits),")", sep = "")))
1.1 hako 240: dimnames(output.est) <- list(
241: c("Growth rate:",
242: "Variance:",
1.8 hako 243: "Current population size, xd = log(N(q) / ne):",
244: "Sample size, n = q + 1:",
1.19 ! hako 245: "AIC for the distribution of X = log(N):",
1.1 hako 246: paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
247: c("Estimate","95% confidence interval"))
248: print(output.est)
249: } else {
250: output.est <- data.frame(
251: c(formatC(obj$Extinction.probability, digits = digits)))
252: dimnames(output.est) <- list(
253: c(paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
254: c("Estimate"))
1.13 hako 255: print(output.est)
1.1 hako 256: }
257: }
258: #
259: # Examples
260: # Yellowstone grizzly bears (from Dennis et al., 1991)
1.18 hako 261: # 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),
262: # 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))
1.1 hako 263: # The probability of extinction (of decline to population size 1) within 100 years
1.8 hako 264: # ext1(dat, t = 100)
1.1 hako 265: # The probability of decline to 10 individuals within 100 years
1.8 hako 266: # ext1(dat, t = 100, ne = 10, verbose = TRUE)
1.13 hako 267: # with QQ-plot
268: # ext1(dat, t = 100, ne = 10, verbose = TRUE, qq.plot = T)
1.10 hako 269: # The probability of decline to 1 individuals within 100 years with an asymptotically exact confidence interval
270: # ext1(dat, t = 100, ne = 1, verbose = TRUE, exact.CL = TRUE)