knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) set.seed(1233)
The goal of project2 is to to estimate the indirect and direct effects of a treatment A on a time-to-event outcome T with a mediator M, when T is interval censored. The primary purpose of the package is to provide functions for estimating the indirect and direct effects from data. A secondary goal is to provide functions to conduct a simulation study to evaluate the proposed estimators. We deomonstrate the simulation functionality of the package below.
You can install the released version of project2 from the development version from GitHub with:
# install.packages("devtools") devtools::install_github("a-chernofsky/project2")
library(survival) library(interval) library(project2)
sim_cph
with the following arguments:
a. N
simulated sample size
b. formula
or X
defining the covariates
c. beta
covariate effectstfun
time function based on the assumed hazard functionsim_interval
and the following arguments:
a. t
true event times
b. pmiss
probability of missing
c. visits
a vector of study visit timesBender 2005 discussed a method to simulate time-to-event data from a Cox Proportional Hazards model with hazard function based on the probability integral transformation theorem. The method involves inverting the cumulative hazard function. While the method is generalized to any hazard function, choosing from popular survival distributions such as exponential, weibull, and gompertz provides a closed form inverse function that is simple to define. The sim_cph
currently has functions to simulate survival times from these common distributions.
The sim_cph
function takes the following arguments:
N
simulated sample size formula
or X
defining the covariates beta
covariate effects tfun
time function based on the assumed hazard.Assume the following CPH model with a constant baseline hazard function $\lambda$:
$h(t) = \lambda \exp(\beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3).$
The variables $x_1, x_2, x_3$ are simulated from the following distributions:
#simulate covariates x1 <- rbinom(100, 1, 0.5) x2 <- rnorm(100) x3 <- factor(sample(c(1,2,3), size = 100, replace = T, prob = c(0.33, 0.33, 0.33)))
We now set the values for the $N$, $\beta$'s and $\lambda$,
#N n <- 100 #betas b <- c(-0.5, -0.02, 0.01, 0.01) #lambda lam <- 0.1
There are two ways to input the covariates into sim_cph
:
1. X
as a model matrix (use model.matrix
function)
2. formula
as a formula e.g. ~x1 + x2 + x3
.
Make sure that any categorical variables are converted as factors otherwise they will be treated as numeric.
t_data <- sim_cph(N = n, formula = ~ x1 + x2 + x3, beta = b, tfun = texp(lam))
We can use functions from the survival package to inspect the simulated data:
t_data$event <- 1 sfit <- survfit(Surv(t, event) ~ x1, data = t_data) summary(sfit) plot(sfit, col = c("red", "blue"))
We can fit a CPH model to check the results:
coxph(Surv(t, event) ~ x1 + x2 + x3, data = t_data)
int_data <- sim_interval(t_data$t, pmiss = 0.10, visits = c(0, 1, 5, 10, 15, 20)) icfit <- icfit(Surv(l, r, type = "interval2") ~ x1, data = int_data) plot(icfit, XLEG = 15, YLEG = 0.8, shade = F)
i1 <- summary(sfit)$strata == "x1=1" i0 <- summary(sfit)$strata == "x1=0" t1 <- summary(sfit)$time[i1] s1 <- summary(sfit)$surv[i1] t0 <- summary(sfit)$time[i0] s0 <- summary(sfit)$surv[i0]
rmst_ic(left = int_data$l[t_data$x1 == 1], right = int_data$r[t_data$x1 == 1], tau = 20) rmst_ic(left = int_data$l[t_data$x1 == 0], right = int_data$r[t_data$x1 == 0], tau = 20)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.