R/exponentialSmooth.R

Defines functions exponentialSmooth

Documented in exponentialSmooth

#'  Weight Smooth Sum function
#'
#'
#' @param x  Numeric vector containing values of the modeled dependent
#'   variable
#' @param t Time values associated with the values of \code{x}.
#' @param t0 Time value for desired prediction.
#' @param rate Exponential weighting parameter. (length-1 numeric scalar).
#'   This gets passed to \code{pexp()}
#'
#' @return exponentially-smoothed prediction of the values in \code{x} for
#'   \code{dt=0}.
#' @export
#'
#' @examples
#'

exponentialSmooth <- function(x, t, t0 = max(t), rate){
  stopifnot(is.numeric(x))
  if(!(is.numeric(rate) & length(rate) == 1)) {
    stop("Parameter 'rate' must be a numeric scalar.")
  }

  dt = timeDifference(t0, t)
  negDtIx <- which(dt < 0)
  if(length(negDtIx)>0){
    warning("Some values of 't' occur at time points after 't0'.")
    x <- x[-negDtIx]
    dt <- dt[-negDtIx]
  }
  #reorder the data based on t
  ord <- order(dt)
  x <- x[ord]
  dt <- dt[ord]

  #compute values of exponential CDF at given values of dt
  expCdfVals <- pexp(dt, rate=rate)

  #rescale ("normalize") weights to sum to 1
  wts <- c(expCdfVals[1],diff(expCdfVals))
  wts <- wts / sum(wts)

  #return weighted sum
  x %*% wts
}
jchen032294/expSmooth documentation built on May 17, 2019, 1:29 p.m.