R/internal.R

Defines functions resample_idx lininterp_vec trapz_cum

trapz_cum <- function(time, Y) {
  stopifnot(is.matrix(Y), length(time) == ncol(Y))
  dt <- diff(time)
  mids <- (Y[, -1, drop = FALSE] + Y[, -ncol(Y), drop = FALSE]) / 2
  inc <- mids * matrix(dt, nrow(Y), length(dt), byrow = TRUE)
  cbind(0, t(apply(inc, 1, cumsum)))
}

lininterp_vec <- function(x, y, xout) {
  stats::approx(x = x, y = y, xout = xout, rule = 2, ties = "ordered")$y
}

resample_idx <- function(n, group = NULL) {
  if (is.null(group)) {
    sample.int(n, n, replace = TRUE)
  } else {
    group <- as.factor(group)
    unlist(lapply(levels(group), function(g) {
      idx <- which(group == g)
      sample(idx, length(idx), replace = TRUE)
    }), use.names = FALSE)
  }
}

Try the tvrmst package in your browser

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

tvrmst documentation built on March 17, 2026, 1:07 a.m.