% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/recurrent.marginal.R
\name{sim_recurrentII}
\alias{sim_recurrentII}
\alias{sim_recurrent_list}
\title{Simulate recurrent events with two event types and a terminal event}
\usage{
sim_recurrentII(
  n,
  cumhaz,
  cumhaz2,
  death.cumhaz = NULL,
  r1 = NULL,
  r2 = NULL,
  rd = NULL,
  rc = NULL,
  dependence = 0,
  var.z = 1,
  cor.mat = NULL,
  cens = NULL,
  gap.time = FALSE,
  max.recurrent = 100,
  ...
)
}
\arguments{
\item{n}{Number of subjects to simulate.}

\item{cumhaz}{Two-column matrix \code{(time, cumhaz)} giving the cumulative
hazard of the first type of recurrent event.}

\item{cumhaz2}{Two-column matrix \code{(time, cumhaz)} giving the cumulative
hazard of the second type of recurrent event.}

\item{death.cumhaz}{Two-column matrix \code{(time, cumhaz)} giving the
cumulative hazard of the terminal event. If \code{NULL}, no terminal event
is simulated and follow-up ends at the end of \code{cumhaz}.}

\item{r1}{Optional numeric vector of length \code{n} with subject-specific
relative risk multipliers for the first event type.}

\item{r2}{Optional numeric vector of length \code{n} with subject-specific
relative risk multipliers for the second event type.}

\item{rd}{Optional numeric vector of length \code{n} with subject-specific
relative risk multipliers for the terminal event.}

\item{rc}{Optional numeric vector of length \code{n} with subject-specific
multipliers for the exponential censoring rate.}

\item{dependence}{Integer specifying the frailty structure. One of \code{0}
(independence), \code{1} (shared gamma frailty), or \code{4} (shared frailty
for recurrent events only). See Details.}

\item{var.z}{Variance of the gamma-distributed frailty. Default is \code{1}.}

\item{cor.mat}{Correlation matrix for the random effects. Used when
\code{dependence = 2} (in \code{\link{sim_recurrent_list}}).}

\item{cens}{Rate of exponential censoring. If \code{NULL} (default), no
additional censoring is applied.}

\item{gap.time}{Logical. If \code{TRUE}, event times are drawn as gap times
(time since the last event) rather than calendar times. Default is
\code{FALSE}.}

\item{max.recurrent}{Maximum number of recurrent events allowed per subject.
Default is \code{100}.}

\item{...}{Further arguments passed to \code{\link{sim_recurrent_list}}.}
}
\value{
A data frame in counting-process format (one row per event interval
  per subject) with columns:
  \item{id}{Subject identifier.}
  \item{start, entry}{Interval start time.}
  \item{stop, time}{Interval end time (event or censoring time).}
  \item{status}{Event type at \code{stop}: \code{1} or \code{2} for a
    recurrent event of the corresponding type, \code{0} for censoring.}
  \item{death}{Indicator for a terminal event (\code{1}) or censoring/survival
    (\code{0}).}
  Attributes \code{"cumhaz"}, \code{"death.cumhaz"}, \code{"rr"}, and
  \code{"rd"} store the inputs used for simulation.
}
\description{
Simulates recurrent event data with up to two distinct event types and an
optional terminal event (death), based on user-supplied cumulative hazard
functions. Dependence between processes can be introduced via shared or
correlated gamma-distributed frailties.
}
\details{
The simulation proceeds by sequentially drawing the next event time from the
specified cumulative hazards, taking the minimum of the two recurrent event
times, and stopping each subject at death or administrative censoring.

Dependence between processes is controlled by \code{dependence}:
\describe{
  \item{\code{0}}{Independence: all subjects have frailty fixed at 1.}
  \item{\code{1}}{Shared frailty: all processes share a single
    gamma-distributed random effect with mean 1 and variance \code{var.z}.}
  \item{\code{4}}{Recurrent-event frailty only: the two recurrent event
    processes share a gamma frailty but the terminal event is independent.}
}
For more complex correlation structures across two event types and death, use
\code{\link{sim_recurrentTS}}.
}
\examples{
data(CPH_HPN_CRBSI)
dr    <- CPH_HPN_CRBSI$terminal
base1 <- CPH_HPN_CRBSI$crbsi
base4 <- CPH_HPN_CRBSI$mechanical

## Single recurrent event type, with and without terminal event
rr <- sim_recurrent(5, base1)
dlist(rr, . ~ id, n = 0)

rr <- sim_recurrent(5, base1, death.cumhaz = dr)
dlist(rr, . ~ id, n = 0)

## Verify that estimated rates recover the true baselines (increase n for precision)
rr <- sim_recurrent(100, base1, death.cumhaz = dr)
par(mfrow = c(1, 3))
mets:::showfitsim(causes = 1, rr, dr, base1, base1)

## Shared frailty across all processes
rr <- sim_recurrent(100, base1, death.cumhaz = dr, dependence = 1, var.z = 0.4)
dtable(rr, ~death + status)

## Two event types; second type uses the mechanical complication rate
set.seed(100)
rr <- sim_recurrentII(100, base1, base4, death.cumhaz = dr)
dtable(rr, ~death + status)
par(mfrow = c(2, 2))
mets:::showfitsim(causes = 2, rr, dr, base1, base4)

## Three event types and two causes of death via sim_recurrent_list
set.seed(100)
cumhaz <- list(base1, base1, base4)
drl    <- list(dr, base4)
rr     <- sim_recurrent_list(100, cumhaz, death.cumhaz = drl, dependence = 0)
dtable(rr, ~death + status)
mets:::showfitsimList(rr, cumhaz, drl)

}
\seealso{
\code{\link{sim_recurrent}}, \code{\link{sim_recurrent_list}},
  \code{\link{sim_recurrentTS}}
}
\author{
Thomas Scheike
}
