eval_marker: Fast Evaluation of Time-varying Marker Mean Term

View source: R/sim.R

eval_markerR Documentation

Fast Evaluation of Time-varying Marker Mean Term

Description

Evaluates the marker mean given by

\vecμ(s, \vec u) = \vec o + B^\top\vec g(s) + U^\top\vec m(s).

Usage

eval_marker(ti, B, g_func, U, m_func, offset)

Arguments

ti

numeric vector with time points.

B

coefficient matrix for time-varying fixed effects. Use NULL if there is no effect.

g_func

basis function for B like poly.

U

random effects matrix for time-varying random effects. Use NULL if there is no effects.

m_func

basis function for U like poly.

offset

numeric vector with non-time-varying fixed effects.

Examples

# compare R version with this function
library(SimSurvNMarker)
set.seed(1)
n <- 100L
n_y <- 3L

ti <- seq(0, 1, length.out = n)
offset <- runif(n_y)
B <- matrix(runif(5L * n_y), nr = 5L)
g_func <- function(x)
  cbind(1, x, x^2, x^3, x^4)
U <- matrix(runif(3L * n_y), nr = 3L)
m_func <- function(x)
  cbind(1, x, x^2)

r_version <- function(ti, B, g_func, U, m_func, offset){
  func <- function(ti)
    drop(crossprod(B, drop(g_func(ti))) + crossprod(U, drop(m_func(ti))))

  vapply(ti, func, numeric(n_y)) + offset
}

# check that we get the same
stopifnot(isTRUE(all.equal(
  c(r_version  (ti[1], B, g_func, U, m_func, offset)),
    eval_marker(ti[1], B, g_func, U, m_func, offset))))
stopifnot(isTRUE(all.equal(
  r_version  (ti, B, g_func, U, m_func, offset),
  eval_marker(ti, B, g_func, U, m_func, offset))))

# check the computation time
system.time(replicate(100, r_version  (ti, B, g_func, U, m_func, offset)))
system.time(replicate(100, eval_marker(ti, B, g_func, U, m_func, offset)))


SimSurvNMarker documentation built on Nov. 10, 2022, 5:12 p.m.