R/chron.ci.R

Defines functions `chron.ci`

`chron.ci` <-
  function(x,biweight=TRUE,conf=0.95,R=100)
{
    samps <- rowSums(!is.na(x))
    if(any(samps<2)){
      stop("x contains row(s) with only one sample. Stopping.")
    } 
    yrs <- as.numeric(row.names(x))
    
    getMean <- function(aRow,ind){
      dat <- aRow[ind]
      mask <- is.na(dat)
      dat <-dat[!mask]
      mean(dat)
    }
    
    getTBRM <- function(aRow,ind){
      dat <- aRow[ind]
      mask <- is.na(dat)
      dat <-dat[!mask]
      tbrm(dat)
    }
    if(biweight){
      res <- do.call(rbind, apply(x, 1, FUN = function(x)
      {
        x1 <- boot.ci(boot(x, statistic = getTBRM, R = R),
                      type="norm",conf=conf)
        res <- data.frame(std = x1$t0, 
                          lowerCI = x1$normal[2], upperCI = x1$normal[3])
      }))
    }
    else{
      res <- do.call(rbind, apply(x, 1, FUN = function(x)
      {
        x1 <- boot.ci(boot(x, statistic = getMean, R = R),
                      type="norm",conf=conf)
        res <- data.frame(std = x1$t0, 
                          lowerCI = x1$normal[2], upperCI = x1$normal[3])
      }))
    }
    res <- data.frame(res,samp.depth=samps)
    rownames(res) <- yrs
    class(res) <- c("crn", "data.frame")
    comment(res) <- "ci"
    return(res)
}

Try the dplR package in your browser

Any scripts or data that you put into this service are public.

dplR documentation built on June 22, 2024, 9:59 a.m.