deriv.ci.func <- function(deriv.matrix, conf = 0.95) { # deriv.ci.func: # Given a matrix of bootstrapped 2nd derivative values (of the form # deriv.matrix = sp.bootderiv.Nrep.df), this function extracts # the lower and upper (100*conf)% confidence limits for the second # derivatives. # # 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.deriv.ci <- deriv.ci.func(sp.bootderiv.Nrep.df, 0.95) # example: cb.deriv.ci <- deriv.ci.func(cb.bootderiv.399.10, 0.95) # # However, this function is rarely called directly: almost always called # from within the function sp.plot. # Nyears <- ncol(deriv.matrix) Nrep <- nrow(deriv.matrix) # # Nrep is the number of bootstrap replicates in the 2nd derivative # 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 deriv.matrix, or change\n\tconf, so that (Nrep+1)*(1-conf)/2 is an integer." ) assign("deriv.matrix", deriv.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(deriv.matrix[, yr])[c(lowerpt, upperpt)] } deriv.ci <- sapply(seq(1, Nyears), inner.func) dimnames(deriv.ci) <- list(c("lower", "upper"), NULL) deriv.ci }