index.ci.func <- function(index.matrix, conf = 0.95) { # index.ci.func: # Given a matrix of bootstrapped index values of the form # index.matrix = sp.bootind.Nrep.df, this function extracts # the lower and upper (100*conf)% confidence limits for the abundance # indices. # # conf should be chosen so that (Nrep+1)*(1-conf)/2 is an integer: # otherwise it will not be possible to find integers l and u such that the # l'th and u'th ordered values give us exactly a conf% confidence interval. # # syntax: sp.ind.ci <- index.ci.func(sp.bootind.Nrep.df, 0.95) # example: cb.ind.ci <- index.ci.func(cb.bootind.399.10, 0.95) # # Usually called from within the function sp.plot. # Nyears <- ncol(index.matrix) Nrep <- nrow(index.matrix) # # Nrep is the number of bootstrap replicates in the index matrix. # Find alp: alp=(1-conf)/2: alp <- (1 - conf)/2 # # Interpolation is not ideal, so discard some rows of the bootstrap # matrix if (Nrep+1)*alp is not an integer: if(abs((Nrep + 1) * alp - round((Nrep + 1) * alp)) > 1e-05) stop("need to discard rows of index.matrix, or change conf, so that (Nrep+1)*(1-conf)/2 is an integer." ) assign("index.matrix", index.matrix, frame = 1) lowerpt <- (Nrep + 1) * alp upperpt <- (Nrep + 1) * (1 - alp) assign("upperpt", upperpt, frame = 1) assign("lowerpt", lowerpt, frame = 1) # # The confidence interval goes from the (Nrep+1)*alp 'th ordered # bootstrap value (low) to the (Nrep+1)*(1-alp) 'th ordered bootstrap # value (high). inner.func <- function(yr) { sort(index.matrix[, yr])[c(lowerpt, upperpt)] } index.ci <- sapply(seq(1, Nyears), inner.func) dimnames(index.ci) <- list(c("lower", "upper"), NULL) index.ci }