% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/casewise.R
\name{test_casewise}
\alias{test_casewise}
\title{Test for Independence Using Casewise Concordance}
\usage{
test_casewise(conc, marg, test = "no-test", p = 0.01)
}
\arguments{
\item{conc}{An object containing concordance estimates (e.g., from \code{bicomprisk}). 
Must contain \code{time}, \code{P1}, \code{se.P1}, and ideally \code{P1.iid}.}

\item{marg}{An object containing marginal cumulative incidence estimates (e.g., from \code{comp.risk} or \code{prodlim}). 
Must contain \code{time}, \code{P1}, \code{se.P1}, and ideally \code{P1.iid}.}

\item{test}{Type of test: \code{"case"} (test on casewise concordance) or \code{"conc"} (test on concordance). 
Default is \code{"no-test"}.}

\item{p}{Threshold for checking that marginal probability is greater than this value at some point 
(default 0.01). Used to avoid division by near-zero probabilities.}
}
\value{
An object of class \code{"casewise"} containing:
  \item{casewise}{Matrix with time, casewise concordance, and standard errors.}
  \item{marg}{Matrix with time, marginal CIF, and standard errors.}
  \item{conc}{Matrix with time, concordance, and standard errors.}
  \item{casewise.iid}{Influence function decomposition for casewise concordance.}
  \item{test}{Test results (if \code{test} is specified): Pepe-Mori type test statistics, 
    standard errors, z-values, and p-values.}
  \item{mintime, maxtime}{Time range used for the analysis.}
  \item{same.cluster}{Logical indicating if clusters were assumed identical.}
  \item{testtype}{Type of test performed.}
}
\description{
Estimates the casewise concordance based on concordance and marginal estimates, 
and performs tests for the independence assumption. This is particularly useful 
in twin studies to assess genetic vs. environmental contributions to disease risk.
}
\details{
The casewise concordance is defined as:
\deqn{ C(t) = \frac{P(T_1 \leq t, T_2 \leq t)}{P(T_1 \leq t)} }
where the numerator is the joint probability of both twins having the event by time \eqn{t}, 
and the denominator is the marginal probability.

The function supports two types of tests:
\itemize{
  \item \code{"case"}: Tests on the casewise concordance scale (difference between observed and expected under independence).
  \item \code{"conc"}: Tests on the concordance scale (difference between observed concordance and squared marginal).
}

Standard errors are computed using cluster-based conservative estimates or influence 
functions (IID) if available from the input objects.
}
\examples{
\donttest{ ## Reduce Ex.Timings
library("timereg")
data("prt",package="mets");
prt <- force_same_cens(prt,cause="status")

prt <- prt[which(prt$id \%in\% sample(unique(prt$id),7500)),]
### marginal cumulative incidence of prostate cancer
times <- seq(60,100,by=2)
outm <- timereg::comp.risk(Event(time,status)~+1,data=prt,cause=2,times=times)

cifmz <- predict(outm,X=1,uniform=0,resample.iid=1)
cifdz <- predict(outm,X=1,uniform=0,resample.iid=1)

### concordance for MZ and DZ twins
cc <- bicomprisk(Event(time,status)~strata(zyg)+id(id),
                 data=prt,cause=c(2,2))
cdz <- cc$model$"DZ"
cmz <- cc$model$"MZ"

### To compute casewise cluster argument must be passed on,
###  here with a max of 100 to limit comp-time
outm <- timereg::comp.risk(Event(time,status)~+1,data=prt,
                 cause=2,times=times,max.clust=100)
cifmz <- predict(outm,X=1,uniform=0,resample.iid=1)
cc <- bicomprisk(Event(time,status)~strata(zyg)+id(id),data=prt,
                cause=c(2,2),se.clusters=outm$clusters)
cdz <- cc$model$"DZ"
cmz <- cc$model$"MZ"

cdz <- test_casewise(cdz,cifmz,test="case") ## test based on casewise
cmz <- test_casewise(cmz,cifmz,test="conc") ## based on concordance

plot(cmz,ylim=c(0,0.7),xlim=c(60,100))
par(new=TRUE)
plot(cdz,ylim=c(0,0.7),xlim=c(60,100))

}
}
\references{
Scheike, T. H. (2024). Casewise concordance estimation and testing. mets package documentation.
}
\seealso{
\code{\link{bicomprisk}}, \code{\link{casewise}}, \code{\link{test_conc}}
}
\author{
Thomas Scheike
}
