[BACK]Return to extinction_risk_1.R CVS log [TXT][DIR] Up to [local] / ext

Annotation of ext/extinction_risk_1.R, Revision 1.16

1.7       hako        1: # extinction_risk_1.R
1.16    ! hako        2: # $Id: extinction_risk_1.R,v 1.15 2015/08/09 01:28:14 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.16    ! hako       90:   nn <- ps[-1]
        !            91:   nn2 <- ps[-length(ps)]
        !            92:   lnl <- - sum(log(nn * sqrt(2 * tau * pi))) - (q / 2) * log(s)
        !            93:          - 1 / (2 * s) * sum((1 / tau) * (log(nn / nn2) - mu * tau)^2)
        !            94:   AIC <- - 2 * lnl + 4 # 2 parameters, mu and s
        !            95:
1.13      hako       96:   if (qq.plot == TRUE) {
                     97:     qqnorm(delta.log.n)
                     98:     qqline(delta.log.n)
                     99:   }
                    100:
                    101:   lower.CL.mu <- mu - qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.8       hako      102:   upper.CL.mu <- mu + qt(1 - alpha / 2, q - 1) * sqrt(us / tq)
1.13      hako      103:   lower.CL.s <- q * s / qchisq(1 - alpha / 2, q - 1)
1.8       hako      104:   upper.CL.s <- q * s / qchisq(alpha / 2, q - 1)
                    105:
                    106:   w <- function(mu, xd, s, t) (mu * t + xd) / sqrt(s * t)
                    107:   z <- function(mu, xd, s, t) (- mu * t + xd) / sqrt(s * t)
                    108:   pr <- function(w, z) {
                    109:     if(z < 35) {
                    110:       pnorm(-w) + exp((z^2 - w^2) / 2) * pnorm(-z)
                    111:     } else {
1.13      hako      112:       pnorm(-w) + exp(- w^2 / 2) * (sqrt(2) / (2 * sqrt(pi))) *
                    113:       (1 / z - 1 / z^3 + 3 / z^5 - 15 / z^7
                    114:       + 105 / z^9 - 945 / z^11 + 10395 / z^13)
1.8       hako      115:     }
1.1       hako      116:   }
1.8       hako      117:
                    118:   ww <- w(mu, xd, s, t)
                    119:   zz <- z(mu, xd, s, t)
                    120:   pp <- pr(ww, zz)
                    121:
                    122:   c.limit <- function(tq, q, t, est, a, width = 10) {
                    123:     t.obs <- sqrt((q - 1) * tq / (q * t)) * est
                    124:     df <- q - 1
                    125:     const <- sqrt(tq / t)
                    126:     f <- function(x) {
                    127:       pt(t.obs, df, const * x) - a
                    128:     }
                    129:     d.est <- est / width + 1
                    130:     uniroot(f, c(- d.est + est, d.est + est), extendInt = "yes")$root
1.1       hako      131:   }
1.8       hako      132:
                    133:   confidence.interval <- function(mu, xd, s, t, tq, q, alpha) {
                    134:     den1 <- sqrt(s * t)
                    135:     w.est <- (mu * t + xd) / den1
                    136:     z.est <- (- mu * t + xd) / den1
                    137:     lower.CL.w <- c.limit(tq, q, t, w.est, 1 - alpha / 2)
                    138:     upper.CL.w <- c.limit(tq, q, t, w.est, alpha / 2)
                    139:     lower.CL.z <- c.limit(tq, q, t, z.est, 1 - alpha / 2)
                    140:     upper.CL.z <- c.limit(tq, q, t, z.est, alpha / 2)
                    141:     lower.CL.P <- pr(upper.CL.w, lower.CL.z)
                    142:     upper.CL.P <- pr(lower.CL.w, upper.CL.z)
                    143:     c(lower.CL.P, upper.CL.P)
1.1       hako      144:   }
1.8       hako      145:
1.12      hako      146:   CI.rand <- function(mu.obs, xd, s.obs, t, tq, q, alpha) {
1.10      hako      147:     mu <- rnorm(1, mean = mu.obs, sd = sqrt(s.obs / q))
                    148:     s <-  rchisq(1, df = q - 1) * s.obs / q
                    149:     confidence.interval(mu, xd, s, t, tq, q, alpha)
                    150:   }
                    151:
1.12      hako      152:   CI.sim <- function(n.sim, gam, alpha, mu.obs, xd, s.obs, t, tq, q) {
                    153:     CI.dist <- replicate(n.sim, CI.rand(mu.obs, xd, s.obs, t, tq, q, alpha))
1.10      hako      154:     w.obs <- w(mu.obs, xd, s.obs, t)
                    155:     z.obs <- z(mu.obs, xd, s.obs, t)
                    156:     P.obs <- pr(w.obs, z.obs)
                    157:     ci <- confidence.interval(mu.obs, xd, s.obs, t, tq, q, alpha)
                    158:     complete <- complete.cases(CI.dist[1, ], CI.dist[2, ])
                    159:     lower <- CI.dist[1, ][complete]
                    160:     upper <- CI.dist[2, ][complete]
                    161:     n.estimables <- length(lower)
                    162:     n.rejects <- (sum(P.obs < lower) + sum(P.obs > upper))
                    163:     if (n.estimables > 0) {
                    164:     binom <- binom.test(n.rejects, n.estimables, p = gam)
                    165:     } else {
                    166:     binom <- list(estimate = NaN, p.value = NaN)
                    167:     }
                    168:     c(binom$estimate[[1]], binom$p.value, ci)
                    169:   }
                    170:
1.12      hako      171:   asymptotically.exact.confidence.interval <- function(n.sim, alpha.sim, mu.obs, xd, s.obs, t, tq, q, alpha) {
                    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:     if (pp > alpha.sim) {
                    177:       return(confidence.interval(mu, xd, s, t, tq, q, alpha))
                    178:     } else {
                    179:     while (pp < alpha.sim) {
1.12      hako      180:       res <- CI.sim(n.sim, alpha, a, mu.obs, xd, s.obs, t, tq, q)
1.10      hako      181:       gg <- res[[1]]
                    182:       pp <- res[[2]]
                    183:       a <- a * alpha / gg
                    184:     }
                    185:     c(res[[3]], res[[4]])
                    186:     }
                    187:   }
                    188:
                    189:   if (exact.CL == TRUE) {
1.13      hako      190:     CL.P <- asymptotically.exact.confidence.interval(n.sim, alpha.sim, mu, xd, s, t, tq, q, alpha)
                    191:     } else {
                    192:     CL.P <- confidence.interval(mu, xd, s, t, tq, q, alpha)
                    193:     }
1.8       hako      194:   lower.CL.P <- CL.P[[1]]
                    195:   upper.CL.P <- CL.P[[2]]
                    196:
1.1       hako      197:   if (verbose == TRUE) {
                    198:     results <- list(ne = ne, t = t, verbose = verbose,
1.16    ! hako      199:       AIC = AIC,
1.8       hako      200:       n = q + 1,
1.6       hako      201:       xd = xd,
1.1       hako      202:       Growth.rate = mu,
1.8       hako      203:       lower.CL.mu = lower.CL.mu,
                    204:       upper.CL.mu = upper.CL.mu,
1.1       hako      205:       Variance = s,
1.8       hako      206:       lower.CL.s = lower.CL.s,
                    207:       upper.CL.s = upper.CL.s,
                    208: #      Unbiased.variance = us,
                    209:       Extinction.probability = pp,
                    210:       lower.CL.P = lower.CL.P,
                    211:       upper.CL.P = upper.CL.P)
1.4       hako      212:     if (formatted == TRUE) {
                    213:       class(results) <- "ext1"
                    214:     }
1.1       hako      215:     return(results)
                    216:   } else {
                    217:     results <- list(ne = ne, t = t, verbose = verbose,
1.8       hako      218:       Extinction.probability = pp)
1.4       hako      219:     if (formatted == TRUE) {
                    220:       class(results) <- "ext1"
                    221:     }
1.1       hako      222:     return(results)
                    223:   }
                    224: }
                    225:
                    226: print.ext1 <- function(obj, digits = 5) {
                    227:   if (obj$verbose == TRUE) {
                    228:     output.est <- data.frame(
                    229:     c(formatC(obj$Growth.rate, digits = digits),
                    230:       formatC(obj$Variance, digits = digits),
1.8       hako      231:       formatC(obj$xd, digits = digits),
                    232:       formatC(obj$n, digits = digits),
1.16    ! hako      233:       formatC(obj$AIC, digits = digits),
1.1       hako      234:       formatC(obj$Extinction.probability, digits = digits)),
1.8       hako      235:     c(paste("(",formatC(obj$lower.CL.mu, digits = digits),", ",
                    236:       formatC(obj$upper.CL.mu, digits = digits),")", sep = ""),
                    237:       paste("(",formatC(obj$lower.CL.s, digits = digits),", ",
                    238:       formatC(obj$upper.CL.s, digits = digits),")", sep = ""),
                    239:       "-",
                    240:       "-",
1.16    ! hako      241:       "-",
1.8       hako      242:       paste("(",formatC(obj$lower.CL.P, digits = digits),", ",
                    243:       formatC(obj$upper.CL.P, digits = digits),")", sep = "")))
1.1       hako      244:     dimnames(output.est) <- list(
                    245:       c("Growth rate:",
                    246:         "Variance:",
1.8       hako      247:         "Current population size, xd = log(N(q) / ne):",
                    248:         "Sample size, n = q + 1:",
1.16    ! hako      249:         "AIC:",
1.1       hako      250:         paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
                    251:       c("Estimate","95% confidence interval"))
                    252:     print(output.est)
                    253:   } else {
                    254:     output.est <- data.frame(
                    255:       c(formatC(obj$Extinction.probability, digits = digits)))
                    256:     dimnames(output.est) <- list(
                    257:       c(paste("Probability of decline to", obj$ne, "within", obj$t, "years:")),
                    258:       c("Estimate"))
1.13      hako      259:     print(output.est)
1.1       hako      260:   }
                    261: }
                    262: #
                    263: # Examples
                    264: # Yellowstone grizzly bears (from Dennis et al., 1991)
1.13      hako      265:   # 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),
                    266:  # 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      267: # The probability of extinction (of decline to population size 1) within 100 years
1.8       hako      268: # ext1(dat, t = 100)
1.1       hako      269: # The probability of decline to 10 individuals within 100 years
1.8       hako      270: # ext1(dat, t = 100, ne = 10, verbose = TRUE)
1.13      hako      271: # with QQ-plot
                    272: # ext1(dat, t = 100, ne = 10, verbose = TRUE, qq.plot = T)
1.10      hako      273: # The probability of decline to 1 individuals within 100 years with an asymptotically exact confidence interval
                    274: # ext1(dat, t = 100, ne = 1, verbose = TRUE, exact.CL = TRUE)