R/zzz.R

Defines functions sample_bias_corr acfy

# add autocorrelation (AR1)
acfy <- function(d, ac) {
  if (ac < -1 || ac > 1) stop("Autocorrelation value not between -1 and 1.",
                              call. = FALSE)
  nd <- length(d)
  d_ac <- numeric(length = nd)
  d_ac[1] <- d[1]
  for (i in seq(2, nd)) {
    d_ac[i] <- ac * d_ac[i - 1] + d[i]
  }
  d_ac
}


# bias correct using the sample mean
# argument must be pre-exponentiation (or in log-space, depending on
# which way you look at it)
sample_bias_corr <- function(d) {
  d_exp <- exp(d)
  if (any(is.infinite(d_exp))) {
    stop("Mean cannot be estimated for bias correction as draws are ",
      "too heavy-tailed: at least one draw approximates 'Inf' when ",
      "exponentiated. Please adjust distribution parameters accordingly.",
      call. = FALSE)
  }
  bias_corr <- log(mean(d_exp))
  d_corr <- d - bias_corr
  d_corr_exp <- exp(d_corr)
  d_corr_exp
}
sebpardo/rtails documentation built on Dec. 22, 2021, 11:17 p.m.