R/00_aux_funs.R

Defines functions iterate_process

iterate_process <- function(t, gen, uni_cens) {
  n_states <- nrow(gen)
  to_states <- which(!is.na(rowSums(gen)))

  a <- gen[to_states,1]
  b <- gen[to_states,2]
  haz <- function(t) a * b * t^(b-1)
  cumhaz <- function(t) a * t^b

  h_t <- Vectorize(haz)(t)
  H_t <- Vectorize(cumhaz)(t)

  if (is.null(dim(H_t))) {
    H_t <- t(as.matrix(H_t))
  }

  # sojourn_times
  u <- array(runif(n = length(H_t)), dim = dim(H_t))
  t_sjn <- ((H_t - log(u))/a)^(1/b)
  # t_sjn <- (-log(u)/a)^(1/b)

  to_index <- apply(t_sjn, 2, which.min)

  t_sjn <- t_sjn[to_index + nrow(t_sjn)*seq(0, ncol(t_sjn) - 1)]
  to <- to_states[to_index]

  return(list(to, t_sjn))
}
vsritter/sim.etm documentation built on Dec. 11, 2019, 9:42 a.m.