% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Survdt2.R
\name{Survdt2}
\alias{Survdt2}
\alias{survdt2to1}
\alias{[.Survdt2}
\alias{is.na.Survdt2}
\alias{is.Survdt2}
\alias{length.Survdt2}
\alias{as.data.frame.Survdt2}
\alias{plot.Survdt2}
\title{Create a survival object for use with time-varying covariates}
\usage{
Survdt2(start, stop, event, ltrunc, rtrunc, id)

survdt2to1(x)

\method{[}{Survdt2}(x, i, j, ...)

\method{is.na}{Survdt2}(x)

is.Survdt2(x)

\method{length}{Survdt2}(x)

\method{as.data.frame}{Survdt2}(x, ...)

\method{plot}{Survdt2}(x, ...)
}
\arguments{
\item{start}{A vector of start times for the observation windows.}

\item{stop}{A vector of stop times for the observation windows.}

\item{event}{A vector of 0/1 event indicators (1=event).}

\item{ltrunc}{A vector of left truncation times.}

\item{rtrunc}{A vector of right truncation times.}

\item{id}{A vector of subject ids.}

\item{x}{Any R object.}

\item{i, j}{Row and columns index vectors. The result is a \code{Survdt2} object if
\code{j} is missing, otherwise \code{x} is treated as a matrix.}

\item{...}{Additional arguments passed to other methods.}
}
\value{
\itemize{
\item \code{\link[=Survdt2]{Survdt2()}} returns an object of class \code{Survdt2}, with columns \code{start},
\code{stop}, \code{event}, \code{ltrunc}, \code{rtrunc}, and \code{id}.
Indexing a \code{Survdt2} object by rows will return another \code{Survdt2} object,
otherwise it is treated as a matrix.
The length of a \code{Survdt2} object is the total number of observations across
all subjects, and \code{is.na()} returns a logical index vector for the non-complete
rows.
Calling \code{as.data.frame} on a \code{Survdt2} object will return a data frame
stripped of the \code{Survdt2} class attribute.
\item \code{\link[=is.Survdt2]{is.Survdt2()}} returns \code{TRUE} if \code{x} inherits from the class \code{Survdt2} and
\code{FALSE} otherwise.
\item \code{\link[=survdt2to1]{survdt2to1()}} takes a \code{Survdt2} object as input and returns the result from
calling \code{\link[=Survdt]{Survdt()}} on collapsed data with one row per \code{id}.
The subjects in the collapsed data are sorted in the order they first appear
in the \code{Survdt2} object.
If all event indicators for a subject are zero and/or \code{NA}, the event time
in the collapsed data is set to \code{NA}.
Lastly, the \code{id} values are preserved within the row names of the \code{Survdt}
object.
\item The plot method for \code{Survdt2} first collapses the data into \code{Survdt}
format using \code{\link[=survdt2to1]{survdt2to1()}} and then calls \code{\link[=plot.Survdt]{plot.Survdt()}}.
}
}
\description{
Creates a survival object for doubly truncated data with the follow
up time for each subject split across multiple observation windows using
(start, stop] encoding.
}
\details{
The \eqn{i}th subject's survival data consists of the left and right truncation
times \eqn{L_i} and \eqn{R_i} as well as a series of (start, stop] observation
windows \eqn{(t_{i0}}, \eqn{t_{i1}]}, \eqn{(t_{i1}}, \eqn{t_{i2}]},
\eqn{\ldots}, \eqn{(t_{i,j-1}}, \eqn{t_{ij}}\eqn{]} for some \eqn{j}.
Furthermore, each of these observation windows has an associated event indicator
that is equal to one if the event time is equal to the stop time, and zero
otherwise.
Each subject should have an observation window with an event, and multiple
events per subject are not allowed.
Attempting to run \code{\link[=Survdt2]{Survdt2()}} with inputs that do not follow this specification
will result in an error.

This data structure is used for Cox regression with time-varying covariates,
since the subject's covariate values can vary across observation windows.
}
\examples{
# first create (start, stop] data frame;
# could then apply time transformations
# to covariates
splitdat <- time_split(aids, cut = c(20, 40),
                       stop_name = "incu",
                       id_name = "id",
                       origin = -1)

y1 <- with(splitdat,
           Survdt2(start, incu, event, ltrunc, rtrunc, id))
plot(y1)

y2 <- with(aids,
           Survdt(incu, ltrunc, rtrunc))
max(abs(unclass(survdt2to1(y1)) - unclass(y2)))


coxph_indtrunc(Survdt2(start, incu, event, ltrunc, rtrunc, id) ~ age_gp,
               data = splitdat)

}
\seealso{
\itemize{
\item \code{\link[=coxph_indtrunc]{coxph_indtrunc()}}: inverse probability weighted Cox regression with
time-varying covariates.
\item \code{\link[=Survdt]{Survdt()}}: the more basic survival object with one row per subject.
}
}
