Annotation of ext/extinction_risk_1.R, Revision 1.15
1.7 hako 1: # extinction_risk_1.R
1.15 ! hako 2: # $Id: extinction_risk_1.R,v 1.14 2015/08/08 03:50:17 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,
53: # the ML estimate of the probability of extinction within
54: # a specific time period t (P),
55: # a lower alpha % confidence limit of parameter *
56: # (lower.CL.*), and
57: # an upper alpha % confidence limit of parameter *
58: # (upper.CL.*).
1.10 hako 59: # exact.CL If set to FALSE, give a CI by the w-z method.
60: # If set to TRUE, give an asymptotically exact CI.
61: # n.sim The number of iteration for the asymptotically exact CI.
62: # alpha.sim Level of significance to decide the convergence of iteration.
1.13 hako 63: # qq.plot If set to TRUE, give a QQ plot for delta.log.n.
1.14 hako 64: # formatted If set to TRUE, give the result by formatted output.
1.8 hako 65: # If set to FALSE, give a list of estimates.
1.1 hako 66: # References:
1.8 hako 67: # R. Lande and S. H. Orzack. Extinction dynamics of age-structured
68: # populations in a fluctuating environment. Proceedings of the
69: # National Academy of Sciences, 85(19):7418-7421, 1988.
70: # B. Dennis, P. L. Munholland, and J. M. Scott. Estimation of
71: # growth and extinction parameters for endangered species.
72: # Ecological Monographs, 61:115-143, 1991.
73: # H. Hakoyama (in preparation).
1.1 hako 74: #
75:
1.13 hako 76: 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 77: yr <- ts(dat[, 1], start = c(dat[, 1][1])) # Year
78: ps <- ts(dat[, 2], start = c(dat[, 1][1])) # Population size
79: complete <- complete.cases(yr, ps)
80: yr <- yr[complete]
81: ps <- ps[complete]
82: tau <- diff(yr) # time intervals, \tau_i = t_i - t_{i-1}
1.8 hako 83: delta.log.n <- diff(log(ps))
1.1 hako 84: q <- length(yr) - 1 # yr = \{t_0, t_1, \dots, t_q\}
85: tq <- sum(tau)
1.8 hako 86: mu <- sum(delta.log.n) / tq # ML estimate of growth rate
87: s <- (1 / q) * sum((delta.log.n - mu * tau)^2 / tau) # ML estimate of variance
1.1 hako 88: us <- q * s / (q - 1) # an unbiased estimate of variance
89: xd <- log(ps[length(ps)] / ne)
1.8 hako 90:
1.13 hako 91: if (qq.plot == TRUE) {
92: qqnorm(delta.log.n)
93: qqline(delta.log.n)
94: }
95:
96: lower.CL.mu <- mu - qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.8 hako 97: upper.CL.mu <- mu + qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.13 hako 98: lower.CL.s <- q * s / qchisq(1 - alpha / 2, q - 1)
1.8 hako 99: upper.CL.s <- q * s / qchisq(alpha / 2, q - 1)
100:
101: w <- function(mu, xd, s, t) (mu * t + xd) / sqrt(s * t)
102: z <- function(mu, xd, s, t) (- mu * t + xd) / sqrt(s * t)
103: pr <- function(w, z) {
104: if(z < 35) {
105: pnorm(-w) + exp((z^2 - w^2) / 2) * pnorm(-z)
106: } else {
1.13 hako 107: pnorm(-w) + exp(- w^2 / 2) * (sqrt(2) / (2 * sqrt(pi))) *
108: (1 / z - 1 / z^3 + 3 / z^5 - 15 / z^7
109: + 105 / z^9 - 945 / z^11 + 10395 / z^13)
1.8 hako 110: }
1.1 hako 111: }
1.8 hako 112:
113: ww <- w(mu, xd, s, t)
114: zz <- z(mu, xd, s, t)
115: pp <- pr(ww, zz)
116:
117: c.limit <- function(tq, q, t, est, a, width = 10) {
118: t.obs <- sqrt((q - 1) * tq / (q * t)) * est
119: df <- q - 1
120: const <- sqrt(tq / t)
121: f <- function(x) {
122: pt(t.obs, df, const * x) - a
123: }
124: d.est <- est / width + 1
125: uniroot(f, c(- d.est + est, d.est + est), extendInt = "yes")$root
1.1 hako 126: }
1.8 hako 127:
128: confidence.interval <- function(mu, xd, s, t, tq, q, alpha) {
129: den1 <- sqrt(s * t)
130: w.est <- (mu * t + xd) / den1
131: z.est <- (- mu * t + xd) / den1
132: lower.CL.w <- c.limit(tq, q, t, w.est, 1 - alpha / 2)
133: upper.CL.w <- c.limit(tq, q, t, w.est, alpha / 2)
134: lower.CL.z <- c.limit(tq, q, t, z.est, 1 - alpha / 2)
135: upper.CL.z <- c.limit(tq, q, t, z.est, alpha / 2)
136: lower.CL.P <- pr(upper.CL.w, lower.CL.z)
137: upper.CL.P <- pr(lower.CL.w, upper.CL.z)
138: c(lower.CL.P, upper.CL.P)
1.1 hako 139: }
1.8 hako 140:
1.12 hako 141: CI.rand <- function(mu.obs, xd, s.obs, t, tq, q, alpha) {
1.10 hako 142: mu <- rnorm(1, mean = mu.obs, sd = sqrt(s.obs / q))
143: s <- rchisq(1, df = q - 1) * s.obs / q
144: confidence.interval(mu, xd, s, t, tq, q, alpha)
145: }
146:
1.12 hako 147: CI.sim <- function(n.sim, gam, alpha, mu.obs, xd, s.obs, t, tq, q) {
148: CI.dist <- replicate(n.sim, CI.rand(mu.obs, xd, s.obs, t, tq, q, alpha))
1.10 hako 149: w.obs <- w(mu.obs, xd, s.obs, t)
150: z.obs <- z(mu.obs, xd, s.obs, t)
151: P.obs <- pr(w.obs, z.obs)
152: ci <- confidence.interval(mu.obs, xd, s.obs, t, tq, q, alpha)
153: complete <- complete.cases(CI.dist[1, ], CI.dist[2, ])
154: lower <- CI.dist[1, ][complete]
155: upper <- CI.dist[2, ][complete]
156: n.estimables <- length(lower)
157: n.rejects <- (sum(P.obs < lower) + sum(P.obs > upper))
158: if (n.estimables > 0) {
159: binom <- binom.test(n.rejects, n.estimables, p = gam)
160: } else {
161: binom <- list(estimate = NaN, p.value = NaN)
162: }
163: c(binom$estimate[[1]], binom$p.value, ci)
164: }
165:
1.12 hako 166: asymptotically.exact.confidence.interval <- function(n.sim, alpha.sim, mu.obs, xd, s.obs, t, tq, q, alpha) {
167: res <- CI.sim(n.sim, alpha, alpha, mu.obs, xd, s.obs, t, tq, q)
1.10 hako 168: gg <- res[[1]]
169: pp <- res[[2]]
170: a <- alpha
171: if (pp > alpha.sim) {
172: return(confidence.interval(mu, xd, s, t, tq, q, alpha))
173: } else {
174: while (pp < alpha.sim) {
1.12 hako 175: res <- CI.sim(n.sim, alpha, a, mu.obs, xd, s.obs, t, tq, q)
1.10 hako 176: gg <- res[[1]]
177: pp <- res[[2]]
178: a <- a * alpha / gg
179: }
180: c(res[[3]], res[[4]])
181: }
182: }
183:
184: if (exact.CL == TRUE) {
1.13 hako 185: CL.P <- asymptotically.exact.confidence.interval(n.sim, alpha.sim, mu, xd, s, t, tq, q, alpha)
186: } else {
187: CL.P <- confidence.interval(mu, xd, s, t, tq, q, alpha)
188: }
1.8 hako 189: lower.CL.P <- CL.P[[1]]
190: upper.CL.P <- CL.P[[2]]
191:
1.1 hako 192: if (verbose == TRUE) {
193: results <- list(ne = ne, t = t, verbose = verbose,
1.8 hako 194: n = q + 1,
1.6 hako 195: xd = xd,
1.1 hako 196: Growth.rate = mu,
1.8 hako 197: lower.CL.mu = lower.CL.mu,
198: upper.CL.mu = upper.CL.mu,
1.1 hako 199: Variance = s,
1.8 hako 200: lower.CL.s = lower.CL.s,
201: upper.CL.s = upper.CL.s,
202: # Unbiased.variance = us,
203: Extinction.probability = pp,
204: lower.CL.P = lower.CL.P,
205: upper.CL.P = upper.CL.P)
1.4 hako 206: if (formatted == TRUE) {
207: class(results) <- "ext1"
208: }
1.1 hako 209: return(results)
210: } else {
211: results <- list(ne = ne, t = t, verbose = verbose,
1.8 hako 212: Extinction.probability = pp)
1.4 hako 213: if (formatted == TRUE) {
214: class(results) <- "ext1"
215: }
1.1 hako 216: return(results)
217: }
218: }
219:
220: print.ext1 <- function(obj, digits = 5) {
221: if (obj$verbose == TRUE) {
222: output.est <- data.frame(
223: c(formatC(obj$Growth.rate, digits = digits),
224: formatC(obj$Variance, digits = digits),
1.8 hako 225: formatC(obj$xd, digits = digits),
226: formatC(obj$n, digits = digits),
1.1 hako 227: formatC(obj$Extinction.probability, digits = digits)),
1.8 hako 228: c(paste("(",formatC(obj$lower.CL.mu, digits = digits),", ",
229: formatC(obj$upper.CL.mu, digits = digits),")", sep = ""),
230: paste("(",formatC(obj$lower.CL.s, digits = digits),", ",
231: formatC(obj$upper.CL.s, digits = digits),")", sep = ""),
232: "-",
233: "-",
234: paste("(",formatC(obj$lower.CL.P, digits = digits),", ",
235: formatC(obj$upper.CL.P, digits = digits),")", sep = "")))
1.1 hako 236: dimnames(output.est) <- list(
237: c("Growth rate:",
238: "Variance:",
1.8 hako 239: "Current population size, xd = log(N(q) / ne):",
240: "Sample size, n = q + 1:",
1.1 hako 241: paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
242: c("Estimate","95% confidence interval"))
243: print(output.est)
244: } else {
245: output.est <- data.frame(
246: c(formatC(obj$Extinction.probability, digits = digits)))
247: dimnames(output.est) <- list(
248: c(paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
249: c("Estimate"))
1.13 hako 250: print(output.est)
1.1 hako 251: }
252: }
253: #
254: # Examples
255: # Yellowstone grizzly bears (from Dennis et al., 1991)
1.13 hako 256: # 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),
257: # 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 258: # The probability of extinction (of decline to population size 1) within 100 years
1.8 hako 259: # ext1(dat, t = 100)
1.1 hako 260: # The probability of decline to 10 individuals within 100 years
1.8 hako 261: # ext1(dat, t = 100, ne = 10, verbose = TRUE)
1.13 hako 262: # with QQ-plot
263: # ext1(dat, t = 100, ne = 10, verbose = TRUE, qq.plot = T)
1.10 hako 264: # The probability of decline to 1 individuals within 100 years with an asymptotically exact confidence interval
265: # ext1(dat, t = 100, ne = 1, verbose = TRUE, exact.CL = TRUE)