knitr::opts_chunk$set( echo = FALSE, cache = TRUE, collapse = TRUE, comment = ">" )
library(sim.etm) library(tidyverse)
Let $(X,S) = {(x_i, s_i)}{i = 0,1,2}$, with $(x_i, s_i) \subset [0,\tau] \times {0,1,2,3}$, be numerical vectors storing an individual's transition times and status until death or censoring. Here, $\tau < \infty$ is a fixed stopping time and status values correspond to _censured (0), healthy (1), ill (2), and dead (3). Also, let $A_{ij}(t) = \int_0^t q_{ij}(u)du$ be the cumulative transition intensity from state $i$ to $j$ at time $t$ (strata indexes omitted). In order to simulate an individual's path, we do
I to D: $\lambda_{1,23} = 1$; $\gamma_{1,23} = 1.5$
Strata 2 (70% of the population):
I to D: $\lambda_{1,23} = 1$; $\gamma_{1,23} = 1 + 0.5x_i$
Strata 2 (70% of the population):
with $x_i \sim \mathrm{Bernoulli}(0.5)$
# ----------------------------------------------------------------------------- # ETM # ----------------------------------------------------------------------------- Q <- array(NA, dim = c(3, 3, 2)) Q[1,2,] <- c(1, 1) Q[1,3,] <- c(2, .5) Q[2,3,] <- c(1, 1.5) data <- simulate_process(N = 2000, gen = Q)
dt_etm <- data %>% arrange(id, time) %>% group_by(id) %>% mutate(from = paste0("T", state), to = paste0("T", c(state[-1], NA)), entry = time, exit = c(time[-1], NA)) %>% filter(!is.na(exit)) %>% select(id, from, to, entry, exit) %>% ungroup() tra <- apply(Q, 1:2, function(x) sum(!is.na(x)) > 0) n_states <- nrow(tra) Q_index <- which(tra) P_index <- sort(c(seq(1, n_states^2, n_states + 1), Q_index)) state_names <- paste0("T", 1:n_states) trans <- paste0("T", rep(1:n_states, times = n_states), rep(1:n_states, each = n_states)) poss_trans <- trans[P_index] etm_p <- etm::etm(dt_etm, state_names, tra, "cens", s=0, covariance = F) etm_p <- data.frame(p = as.vector(etm_p$est), t = rep(etm_p$time, each = n_states^2), trans = rep(trans, times = length(t)), stringsAsFactors = F) %>% filter(trans %in% poss_trans)
etm_p %>% ggplot(aes(x = t, y = p)) + geom_step() + # geom_line(aes(x = t, y = p), color = "red", data = p) + # geom_step(aes(x = t, y = p), color = "blue", data = etm_p) + facet_wrap(~ trans, scales = "free")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.