% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/recurrent.marginal.R
\name{count_history}
\alias{count_history}
\title{Compute cumulative event counts as time-dependent covariates}
\usage{
count_history(
  data,
  status = "status",
  id = "id",
  types = 1,
  names.count = "Count",
  lag = TRUE,
  multitype = FALSE,
  marks = NULL
)
}
\arguments{
\item{data}{A data frame in counting-process format, with one row per event
interval per subject.}

\item{status}{Name of the column containing event status codes. Default is
\code{"status"}.}

\item{id}{Name of the column containing subject identifiers. Default is
\code{"id"}.}

\item{types}{Integer vector of status codes to count. Each value in
\code{types} generates one new count column (when \code{multitype = FALSE})
or contributes to a single combined count (when \code{multitype = TRUE}).
Default is \code{1}.}

\item{names.count}{Prefix for the names of the new count columns. The
status code is appended, e.g. \code{"Count1"}, \code{"Count2"}.
Default is \code{"Count"}.}

\item{lag}{Logical. If \code{TRUE} (default), the count at each row is the
number of events strictly before the current time (\eqn{N(t-)}). If
\code{FALSE}, events at the current time are included (\eqn{N(t)}).}

\item{multitype}{Logical. If \code{TRUE}, events with status in \code{types}
are aggregated into a single count column, optionally weighted by
\code{marks}. If \code{FALSE} (default), a separate count column is created
for each value in \code{types}.}

\item{marks}{Optional numeric vector of weights applied to events when
\code{multitype = TRUE}. If \code{NULL} (default), each event has weight 1.}
}
\value{
The input data frame \code{data} with one or more new integer columns
  appended. With \code{multitype = FALSE}, columns are named
  \code{paste0(names.count, k)} for each \code{k} in \code{types}; with
  \code{multitype = TRUE}, a single column named
  \code{paste0(names.count, types[1])} is added. An internal bookkeeping
  column \code{lbnr__id} is also added.
}
\description{
For each subject and each row of \code{data}, counts the number of prior
events of specified types in the recurrent event history. The resulting
count columns can be used as time-dependent covariates in subsequent models,
e.g. to capture event-history dependence in the recurrent event rate.
}
\details{
When \code{lag = TRUE} (default), the count at each row reflects events that
occurred strictly before the current time point (i.e. \eqn{N(t-)}), making
it suitable as a left-continuous covariate in counting-process models.
}
\examples{
data(hfactioncpx12)
hf <- hfactioncpx12
dtable(hf, ~status)

## Separate counts for event types 1 and 2
rr <- count_history(hf, types = 1:2, id = "id", status = "status")
dtable(rr, ~"Count*" + status, level = 1)

}
\author{
Thomas Scheike
}
