| Title: | K-Sample Tests for Truncated and/or Censored Data |
|---|---|
| Description: | Nonparametric analysis and comparison of distributions under left truncation and right censoring. The package includes simulation routines for truncated and/or censored survival data, nonparametric distribution comparison methods based on Kolmogorov-Smirnov-type and Cramér-von Mises-type statistics, and bootstrap routines for p-value approximation. For methodological details, see Lago, de Uña-Álvarez and Pardo-Fernández (2025) <doi:10.1007/s11749-024-00948-4> and Lago, Pardo-Fernández and de Uña-Álvarez (2026) <doi:10.1007/s10985-026-09713-1>. |
| Authors: | Adrián Lago [aut, cre], Jacobo de Uña-Álvarez [aut], Juan Carlos Pardo-Fernández [aut] |
| Maintainer: | Adrián Lago <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.1 |
| Built: | 2026-09-18 12:26:04 UTC |
| Source: | https://github.com/adrian-lago/ksamplesltrc |
Performs tests for data subject to left truncation, right censoring or both.
ksample.test( time, entry = NULL, status = NULL, group, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )ksample.test( time, entry = NULL, status = NULL, group, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )
time |
Numeric vector of event or follow-up times. |
entry |
Optional entry time (for left truncation). |
status |
Event indicator (1 = event, 0 = censored). |
group |
Group indicator (factor or vector). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
tests |
Character: "ks", "cvm", "logrank". |
B |
Number of bootstrap replications for approximating the |
plot.curves |
Logical, default to FALSE. If TRUE, the function depicts the functions to be compared. |
holes |
One of 'holes' (default), 'add', or 'conditional'. See |
p, q
|
Weights for the log-rank-type tests to be performed. If NULL, the classical log-rank. test is performed. |
seed |
Seed for the bootstrap statistics. |
keep.boot |
Logical, default to FALSE. If TRUE, the statistics computed from the bootstrap resamples are stored. |
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov and/or Cramér–von Mises statistics.
p.value: approximated -value of the tests.
stat.boot: bootstrap statistics for the -value approximation.
logrank: a matrix with weights p and q, test statistics, and -values.
B: number of bootstrap replications.
tests: tests performed, as specified in the input.
curves: distribution function estimators of the samples to be compared.
groups: group indicators.
holes: method for handling holes (if appropriate).
stat.boot: bootstrap statistics (if keep.boot = TRUE).
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") aa <- ksample.test(time = channing$age,entry = channing$ageentry, status = channing$death, group = channing$gender, weights=rep(1/2,2), B = 200, holes='conditional', tests=c('ks','cvm','logrank'),plot.curves=TRUE, p=c(0,1,0.5,0),q=c(0,0,0.5,1),keep.boot = FALSE) summary(aa) }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") aa <- ksample.test(time = channing$age,entry = channing$ageentry, status = channing$death, group = channing$gender, weights=rep(1/2,2), B = 200, holes='conditional', tests=c('ks','cvm','logrank'),plot.curves=TRUE, p=c(0,1,0.5,0),q=c(0,0,0.5,1),keep.boot = FALSE) summary(aa) }
Adds the survival estimator to an existing plot.
## S3 method for class 'estimators' lines(x, conf.int = FALSE, ...)## S3 method for class 'estimators' lines(x, conf.int = FALSE, ...)
x |
An object of class |
conf.int |
Logical, default to FALSE. If TRUE, pointwise confidence bands are depicted for the curves. |
... |
Additional graphical parameters passed to |
Invisibly returns x.
Adds the survival estimator to an existing plot.
## S3 method for class 't.estimators' lines(x, ...)## S3 method for class 't.estimators' lines(x, ...)
x |
An object of class |
... |
Additional graphical parameters passed to |
Invisibly returns x.
-sample problem under left truncationComputes the Cramér–von Mises statistic adapted to left truncation.
lt.cvm( p.sample, weights = NULL, holes = c("holes", "add", "conditional"), plot.curves = FALSE )lt.cvm( p.sample, weights = NULL, holes = c("holes", "add", "conditional"), plot.curves = FALSE )
p.sample |
Matrix or data frame with columns (U, X, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
holes |
Way to handle holes, if present. See |
plot.curves |
Logical, default to FALSE. If true, the |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation.
Numerical value of the test statistic computed from the samples.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.cvm(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE) aa }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.cvm(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE) aa }
Computes the survival function estimator accounting for left truncation. It also returns pointwise confidence bands and includes the possibility of dealing with holes in the sample.
lt.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog"), holes = c("holes", "add", "conditional") )lt.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog"), holes = c("holes", "add", "conditional") )
o.sample |
Matrix or data frame with columns (U, X), such that |
conf.int |
Logical, set as default to FALSE. If TRUE, the function returns a pointwise confidence band. |
alpha |
Pointwise confidence bands with |
conf.type |
One of 'plain' (default) or 'loglog'. Type of pointwise confidence band. |
holes |
One of 'add', 'conditional', or 'holes' (default) (see details). |
The estimator of the survival function when data are subject to left truncation is defined as (Lynden-Bell, 1971)
where are the distinct observed event times,
and . One
should note that this definition of the risk sets is not equal to that in survival::survfit,
where , so these two implementations may not be
equal when ties are present in the data.
The variance for the confidence bands follows a Greenwood's-like formula:
A log-log transformation is also possible. The confidence band is based on the
Gaussianity of the process .
The function also includes a way to deal with possible holes. A hole is an event time with associated
unitary risk set, namely for some . We exclude the largest
event time, since it is always a hole. If a hole is present
in a sample, the estimator degenerates and takes the value 1 from that time on. Different techniques
are available in the literature for the treatment of holes. For instance, in Stute and Wang (2008),
authors suggest to increase the risk set by 1 (holes='add').
Other references, like Klein and Moeschberger (2003), suggest conditioning on the largest hole different
from the largest event time (holes='conditional'). The possibility of computing the degenerate statistic
is also included (holes='holes').
An object with the following content:
fail.time: ordered distinct observed event times
estimation: estimated survival function at each observed event time
prob: jumps of the estimator
If conf.int = TRUE, it also returns the confidence band lower and upper limits
for both the distribution and survival function.
Klein, J. P. and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer.
Lynden-Bell, D. (1971). A method of allowing for known observational selection in small samples applied to 3CR quasars. Monthly Notices of the Royal Astronomical Society, 155:95-118.
Stute, W. and Wang, J. L. (2008). The central limit theorem under random truncation. Bernoulli, 14:604–622.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] plot( lt.estimator(pooled.sample[pooled.sample$group==0,],conf.int=TRUE,conf.type='loglog'), xlab='Time (in weeks)',ylab='Estimated survival probability' ) lines( lt.estimator(pooled.sample[pooled.sample$group==1,],conf.int=TRUE,conf.type='loglog'),col=2 ) }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] plot( lt.estimator(pooled.sample[pooled.sample$group==0,],conf.int=TRUE,conf.type='loglog'), xlab='Time (in weeks)',ylab='Estimated survival probability' ) lines( lt.estimator(pooled.sample[pooled.sample$group==1,],conf.int=TRUE,conf.type='loglog'),col=2 ) }
-sample problem under left truncationComputes the Kolmogorov–Smirnov statistic adapted to left truncation.
lt.ks( p.sample, weights = NULL, plot.curves = FALSE, holes = c("holes", "add", "conditional") )lt.ks( p.sample, weights = NULL, plot.curves = FALSE, holes = c("holes", "add", "conditional") )
p.sample |
Matrix or data frame with columns (U, X, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
plot.curves |
Logical, default to FALSE. If true, the |
holes |
What to do when a hole is present in a sample. See |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation. The two-sample version of this statistic was studied in Lago et al. (2025).
Note that, when , one has
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistic.
time: time when the maximum is attained.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
Lago, A., de Uña-Álvarez, J., & Pardo-Fernández, J. C. (2025). A Kolmogorov–Smirnov-type test for the two-sample problem with left-truncated data. Test, 34(1), 69-90.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.ks(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE) aa }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.ks(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE) aa }
Computes weighted log-rank-type tests (Fleming–Harrington family)
for groups with left truncation.
lt.logrank( p.sample, p = 0, q = 0, plot.curves = FALSE, holes = c("holes", "conditional") )lt.logrank( p.sample, p = 0, q = 0, plot.curves = FALSE, holes = c("holes", "conditional") )
p.sample |
Matrix or data frame with columns (U, X, group). |
p, q
|
Nonnegative tuning parameters. If one has length 1 and the other is a vector, the scalar is recycled. |
plot.curves |
Logical, default to FALSE. If true, the the |
holes |
How the function deals with holes when estimating the survival function with the pooled sample. One of 'add' (default), 'conditional', and 'holes'. If no holes are present in the sample, this argument will not be used. |
The test statistic is based on the quantity
for . Denote by the distinct
observed event times from the samples. For a time and a group ,
denotes the total count of individuals that experience the event,
and is the number of individuals at risk. In addition,
and , which are
the total number of events observed and the total number of individuals at risk
at time , respectively.
Finally, the weights are taken as
with the pooled survival just before the event time. The choice
results in the classical version of the log-rank test.
The test statistic is given by
where is the matrix with the variance-covariance estimators. Each
term in the matrix includes the correction factor for tied
event times. The test statistic follows an asymptotic distribution under
the null hypothesis of equal target distributions in the populations.
A matrix with columns statistic, detailing the choices of p and
q, in addition to the corresponding p.value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) lt.logrank(p.sample=pooled.sample,p=c(0,1,0.5,5),q=c(0,0,0.5,1),plot.curves=TRUE) }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) lt.logrank(p.sample=pooled.sample,p=c(0,1,0.5,5),q=c(0,0,0.5,1),plot.curves=TRUE) }
-sample problem under left truncationComputes the Cramér–von Mises statistic adapted to left truncation,
and the corresponding -value.
lt.pv.cvm( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), seed = NULL, keep.boot = FALSE )lt.pv.cvm( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), seed = NULL, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, default to FALSE. If TRUE, the |
holes |
How to handle holes if present in a sample. One of |
seed |
Seed for the bootstrap resamples. |
keep.boot |
Logical, default to FALSE. If TRUE, the bootstrap statistics are returned in the output of the function. |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation.
The -value is approximated by means of the obvious bootstrap accounting for
left truncation, as a particular case of the obvious bootstrap proposed for left-truncated
and right-censored data in Bilker and Wang (1997). The exact
algorithm for the two-sample problem can be consulted in Lago et al. (2025).
A list with the following elements:
statistic: numerical value of the Cramér–von Mises test statistic
pvalue: -value of the test
curves: A list with the estimators of the distribution functions to be
compared.
groups: Group indicator
stat.boot: If keep.boot = TRUE, the function returns the statistic evaluated
in the bootstrap resamples.
Bilker, W.B., and Wang, M.C. (1997). Bootstrapping left truncated and right censored data. Communications in Statistics - Simulation and Computation, 26:141-171.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
Lago, A., de Uña-Álvarez, J., & Pardo-Fernández, J. C. (2025). A Kolmogorov–Smirnov-type test for the two-sample problem with left-truncated data. TEST, 34, 69-90.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.pv.cvm(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE, B = 200) aa$pvalue plot(aa) }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.pv.cvm(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE, B = 200) aa$pvalue plot(aa) }
-sample problem under left truncationComputes the Kolmogorov–Smirnov test adapted to left truncation and the corresponding -value.
lt.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), seed = NULL, keep.boot = FALSE )lt.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), seed = NULL, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, X, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, default to FALSE. If TRUE, the |
holes |
How to handle holes if present in a sample. One of |
seed |
Seed for the bootstrap resamples. |
keep.boot |
Logical, default to FALSE. If TRUE, the test statistics evaluated in the bootstrap resamples are included in the output. |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation. The two-sample version of this test was studied in Lago et al. (2025).
The -value is approximated by means of the obvious bootstrap accounting for
left truncation, studied in Bilker and Wang (1997). The exact algorithm for the
two-sample problem can be consulted in Lago et al. (2025).
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistic.
pvalue: approximated p-value of the test.
curves: A list with the estimators of the distribution functions to be
compared.
groups: Group indicator
stat.boot: statistics computed from the bootstrap resamples. Included in the output
only if keep.boot = TRUE
Bilker, W.B., and Wang, M.C. (1997). Bootstrapping left truncated and right censored data. Communications in Statistics - Simulation and Computation, 26:141-171.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026). A Kolmogorov–Smirnov-type test for the two-sample problem with left-truncated data. TEST: 34:69-90.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.pv.ks(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE, B = 200) aa$pvalue plot(aa) }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") pooled.sample <- abortion[abortion$cause==3, 2:4] head(pooled.sample) aa <- lt.pv.ks(p.sample=pooled.sample,weights=rep(1/2,2),plot.curves=TRUE, B = 200) aa$pvalue plot(aa) }
Simulates data under left truncation from prespecified variables for the truncation and target variables.
lt.sim( n, rtrunc, trunc.args = list(), rtarget, target.args = list(), seed = NULL )lt.sim( n, rtrunc, trunc.args = list(), rtarget, target.args = list(), seed = NULL )
n |
Sample size |
rtrunc |
A function that simulates data from a distribution |
trunc.args |
A list with the parameters of the truncation variable |
rtarget |
A function that simulates data from a distribution |
target.args |
A list with the parameters of the target distribution |
seed |
Optional seed. |
A data frame with rows and two columns: the truncation times are in the first column,
and the event times, in the second one.
aa <- lt.sim(1000,rnorm,trunc.args=list(4,1),rnorm,target.args = list(4,1)) head(aa) plot(lt.estimator(aa),col=2,xlab='t',ylab='Estimated survival probiblity') curve(1-pnorm(x,4,1),add=TRUE)aa <- lt.sim(1000,rnorm,trunc.args=list(4,1),rnorm,target.args = list(4,1)) head(aa) plot(lt.estimator(aa),col=2,xlab='t',ylab='Estimated survival probiblity') curve(1-pnorm(x,4,1),add=TRUE)
-sample problem under left truncationComputes -sample tests adapted to left truncation, and the corresponding -value.
lt.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )lt.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, X, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
tests |
Tests to perform. One of Kolmogorov–Smirnov (ks), Cramér–von Mises (cvm), or weighted log-rank-type tests (logrank). |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, FALSE by default. If set to TRUE, the |
holes |
Treatment of possible holes. See details in |
p, q
|
Weights of the log-rank-type tests. The choice |
seed |
Seed for the bootstrap resamples. |
keep.boot |
Logical. If |
This function collects the three tests implemented in this package: Kolmogorov–Smirnov,
Cramér–von Mises and weighted log-rank-type tests, (Klein and Moeschberger, 2003)
adapted to left truncation. The -values of the first two are
approximated by bootstrap, and the -value of the log-rank tests is computed by
the asymptotic distribution of the test statistic, where
is the number of groups.
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov and/or Cramér–von Mises statistics.
p.value: approximated p-value of the tests.
stat.boot: bootstrap statistics for the p-value approximation.
logrank: a matrix with weights p and q, test statistics, and -values.
B: number of bootstrap replications.
tests: tests performed, as specified in the input.
curves: distribution function estimators of the samples to be compared.
groups: group indicators.
holes: method for handling holes.
The summary method returns a data frame with one row per test
and the following columns:
Name of the test. For the weighted log-rank-type tests, the weights
p and q are detailed.
Observed value of the test statistic.
Corresponding -value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
Lago, A., de Uña-Álvarez, J., and Pardo-Fernández, J.C. (2025). A Kolmogorov–Smirnov-type test for the two-sample problem with left-truncated data. TEST: 34:69-90.
if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") head(abortion) pooled.sample <- abortion[abortion$cause==3, 2:4] aa <- lt.test(pooled.sample,weights=rep(1/2,2),B=200, holes='conditional', tests=c('ks','cvm','logrank'), p=c(0,1,0.5,0),q=c(0,0,0.5,1)) summary(aa) plot(aa) }if (requireNamespace("mvna", quietly = TRUE)) { data("abortion", package = "mvna") head(abortion) pooled.sample <- abortion[abortion$cause==3, 2:4] aa <- lt.test(pooled.sample,weights=rep(1/2,2),B=200, holes='conditional', tests=c('ks','cvm','logrank'), p=c(0,1,0.5,0),q=c(0,0,0.5,1)) summary(aa) plot(aa) }
It computes the estimator of the truncation survival function for data subject to left truncation
lt.truncation.estimator(o.sample, holes = c("holes", "add", "conditional"))lt.truncation.estimator(o.sample, holes = c("holes", "add", "conditional"))
o.sample |
Matrix or data frame with columns (U, X). |
holes |
Way to handle holes, if present. One of |
This estimator is based on the idea that if is left-truncated by ,
then is left-truncated by . In addition, note that
where is the survival function of
and is the survival function of . Thus, since lt.estimator()
computes the estimator of the survival function, one can apply the Lynden-Bell
estimator to the sample formed by reversing the roles of and by
merely multiplying by -1.
The nontruncation probability is defined as
where and . Then, an estimator of
is given by (Woodroofe, 1985)
where are the distinct observed event times,
are the weights assigned by the Lynden-Bell estimator
to the observed event times, and are estimated
distribution function of the observed variable evaluated at the distinct observed
event times.
A list with the following:
trunc.time: observed truncation times.
estimation: estimated survival probability.
prob: jumps of the estimator at each observed truncation time.
gamma: estimator of the nontruncation probability.
Lynden-Bell, D. (1971). A method of allowing for known observational selection in small samples applied to 3CR quasars. Monthly Notices of the Royal Astronomical Society, 155:95-118.
Woodroofe, M. (1985). Estimating a distribution function with truncated data. The Annals of Statistics, 13: 163-177.
aa <- lt.sim(500,rnorm,trunc.args=list(4,1),rnorm,target.args = list(4,1)) g.est <- lt.truncation.estimator(o.sample=aa) plot(g.est$trunc.time,g.est$estimation,type='s',lwd=2,col=2) curve(1-pnorm(x,4,1),add=TRUE,lwd=2, xlab='t',ylab='Estimated distribution function') # nontruncation probability estimator: g.est$gammaaa <- lt.sim(500,rnorm,trunc.args=list(4,1),rnorm,target.args = list(4,1)) g.est <- lt.truncation.estimator(o.sample=aa) plot(g.est$trunc.time,g.est$estimation,type='s',lwd=2,col=2) curve(1-pnorm(x,4,1),add=TRUE,lwd=2, xlab='t',ylab='Estimated distribution function') # nontruncation probability estimator: g.est$gamma
Computes the Cramér–von Mises statistic adapted to left truncation and right censoring.
ltrc.cvm( p.sample, weights = NULL, holes = c("holes", "add", "conditional"), plot.curves = FALSE )ltrc.cvm( p.sample, weights = NULL, holes = c("holes", "add", "conditional"), plot.curves = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
holes |
How to handle holes if present in a sample. One of |
plot.curves |
Logical, default to FALSE. If TRUE, the |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation and right censoring, as studied in Lago et al. (2026).
Numerical value of the test statistic computed from the samples.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026). Kolmogorov–Smirnov and Cramér–von Mises tests for the k-sample problem for left-truncated and right-censored data. Lifetime Data Analysis: 32(2).
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.cvm(data.channing,weights=rep(1/2,2), holes='conditional', plot.curves = TRUE) }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.cvm(data.channing,weights=rep(1/2,2), holes='conditional', plot.curves = TRUE) }
Computes the survival function estimator accounting for late entry times and censored observations. It also returns pointwise confidence bands.
ltrc.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog"), holes = c("holes", "add", "conditional") )ltrc.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog"), holes = c("holes", "add", "conditional") )
o.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
conf.int |
Logical, set as default to FALSE. If set to TRUE, the function returns a pointwise confidence band. |
alpha |
Pointwise confidence bands with |
conf.type |
Type of pointwise confidence band. Set as default to |
holes |
One of 'add','conditional', or 'holes' (default) (see details). |
The estimator is defined as
where are the distinct observed event times,
and . It reduces to the
Kaplan–Meier estimator if data are not subject to truncation, and to the Lynden-Bell
estimator if no censoring is present.
The variance for the confidence bands follows a Greenwood's like formula:
The confidence intervals are based on the asymptotic Gaussianity of the process
. Different transformations are also possible.
The function also includes a way to deal with possible holes. A hole is an event time with associated unitary risk set. If a hole is present in a sample, the estimator degenerates and takes the value 1 from that time on. Different techniques are available in the literature for the treatment of holes. For instance, in Stute and Wang (2008), authors suggest to increase the risk set by 1 (holes='add'). Other references, like Klein and Moeschberger (2003), suggest conditioning on the largest hole different from the largest event time (holes='conditional'). The possibility of computing the degenerate statistic is also included (holes='holes').
An object with the following content:
fail.time: ordered distinct observed event times
estimation: estimated survival function at each observed event time
prob: jumps of the estimator
If conf.int is set to TRUE, it also returns the confidence band lower and upper limits
Klein, J. P. and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer
Wang, M.C. (1991). Nonparametric estimation from cross-sectional survival data. Journal of the American Statistical Association, 86:130-143.
Stute, W. and Wang, J. L. (2008). The central limit theorem under random truncation. Bernoulli, 14:604–622.
observed.sample <- ltrc.sim(n=1000,rtrunc=rnorm,rtarget=rnorm,rcens=rexp, trunc.args=list(4,1),target.args = list(4,1),cens.args = list(1)) est <- ltrc.estimator(observed.sample,conf.int=TRUE,alpha=0.05,conf.type='loglog') str(est) plot(est,col='firebrick',xlab='t', ylab='Estimated survival probability') if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") o.sample <- channing[channing$gender==1,c(3,4,2)] # subgroup of men head(o.sample) plot( ltrc.estimator(o.sample,holes='holes'),col=2,xlab='t', ylab='Estimated survival probability' , main = 'Estimated survival probability for men') # no correction lines( ltrc.estimator(o.sample,holes='add'),col=3 ) # +1 to risk set lines( ltrc.estimator(o.sample,holes='conditional'),col=4 ) legend('topright',legend=c('holes="holes" ','holes="add" ', 'holes="conditional"'),col=2:4,lwd=rep(1,3)) }observed.sample <- ltrc.sim(n=1000,rtrunc=rnorm,rtarget=rnorm,rcens=rexp, trunc.args=list(4,1),target.args = list(4,1),cens.args = list(1)) est <- ltrc.estimator(observed.sample,conf.int=TRUE,alpha=0.05,conf.type='loglog') str(est) plot(est,col='firebrick',xlab='t', ylab='Estimated survival probability') if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") o.sample <- channing[channing$gender==1,c(3,4,2)] # subgroup of men head(o.sample) plot( ltrc.estimator(o.sample,holes='holes'),col=2,xlab='t', ylab='Estimated survival probability' , main = 'Estimated survival probability for men') # no correction lines( ltrc.estimator(o.sample,holes='add'),col=3 ) # +1 to risk set lines( ltrc.estimator(o.sample,holes='conditional'),col=4 ) legend('topright',legend=c('holes="holes" ','holes="add" ', 'holes="conditional"'),col=2:4,lwd=rep(1,3)) }
-sample problem under left truncation
and right censoringComputes the Kolmogorov–Smirnov statistic adapted to left truncation and right censoring.
ltrc.ks( p.sample, weights = NULL, plot.curves = FALSE, holes = c("holes", "add", "conditional") )ltrc.ks( p.sample, weights = NULL, plot.curves = FALSE, holes = c("holes", "add", "conditional") )
p.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
plot.curves |
Logical, default to FALSE. If TRUE, the |
holes |
How to handle holes if present in a sample. One of |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation and right censoring, as studied in Lago et al. (2026).
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistic.
time: time when the maximum is attained.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026) Kolmogorov–Smirnov and Cramér–von Mises tests for the k-sample problem for left-truncated and right-censored data. Lifetime Data Analysis: 32(2).
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.ks(data.channing,weights=rep(1/2,2),holes='conditional') }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.ks(data.channing,weights=rep(1/2,2),holes='conditional') }
Computes weighted log-rank-type tests (Fleming–Harrington family) for k groups with left truncation and right censoring.
ltrc.logrank( p.sample, p = 0, q = 0, plot.curves = FALSE, holes = c("holes", "conditional") )ltrc.logrank( p.sample, p = 0, q = 0, plot.curves = FALSE, holes = c("holes", "conditional") )
p.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
p, q
|
Nonnegative weight parameters. If one has length 1 and the other is a vector, the scalar is recycled. |
plot.curves |
Logical, default to FALSE. If true, the the |
holes |
One of 'holes' (default) or 'conditional'. In contrast to other functions of this package the possibility of adding 1 to the risk set is not considered. |
The test statistic is based on the quantity
for . Denote by the distinct
observed event times from the samples. For a time and a group ,
denotes the total count of individuals that experience the event,
and is the number of individuals at risk. In addition,
and , which are
the total number of events observed and the total number of individuals at risk
at time , respectively.
Finally, the weights are taken as
with the pooled survival just before the event time. The choice
results in the classical version of the log-rank test.
The test statistic is given by
where is the matrix with the variance-covariance estimators. Each
term in the matrix includes the correction factor for tied
event times. The test statistic follows an distribution under
the null hypothesis of equal target distributions in the populations.
A matrix with columns statistic and p.value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.logrank(data.channing, plot.curves=TRUE, p = c(0,1,0.5,0),q = c(0,0,0.5,1), holes = 'conditional') }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) ltrc.logrank(data.channing, plot.curves=TRUE, p = c(0,1,0.5,0),q = c(0,0,0.5,1), holes = 'conditional') }
-sample problem under left truncation
and right censoringComputes the Cramér–von Mises test adapted to left truncation and right
censoring and the corresponding -value.
ltrc.pv.cvm( p.sample, weights = NULL, B = 500, holes = c("holes", "add", "conditional"), plot.curves = FALSE, keep.boot = FALSE )ltrc.pv.cvm( p.sample, weights = NULL, B = 500, holes = c("holes", "add", "conditional"), plot.curves = FALSE, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
holes |
How to deal with holes when estimating the distribution function of the
target variable. One of 'holes' (default), 'add', and 'conditional'. See
|
plot.curves |
Logical, default to FALSE. If TRUE, the |
keep.boot |
Logical, default to FALSE. If TRUE, the function returns the statistics computed from the bootstrap resamples. |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation and right censoring, as studied in Lago et al. (2026).
The -value is approximated by means of the obvious bootstrap accounting for
left truncation and right censoring, studied in Bilker and Wang (1997). The exact
algorithm for the -sample problem can be consulted in Lago et al. (2026).
A list with the following:
statistic: Evaluation of the Cramér–von Mises statistic on the samples.
pvalue: -value approximated by bootstrap.
curves: A list with the estimators of the distribution functions to
be compared.
groups: Group indicator
stat.boot: Evaluation of the Cramér–von Mises statistic on the bootstrap resamples.
Only included in the output if keep.boot = TRUE.
Bilker, W.B., and Wang, M.C. (1997). Bootstrapping left truncated and right censored data. Communications in Statistics - Simulation and Computation, 26:141-171.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026). Kolmogorov–Smirnov and Cramér–von Mises tests for the k-sample problem for left-truncated and right-censored data. Lifetime Data Analysis: 32(2).
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame( channing$ageentry,channing$age, channing$death,channing$gender ) aa <- ltrc.pv.cvm(data.channing, holes = 'conditional', plot.curves = FALSE, B = 200) aa$pvalue }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame( channing$ageentry,channing$age, channing$death,channing$gender ) aa <- ltrc.pv.cvm(data.channing, holes = 'conditional', plot.curves = FALSE, B = 200) aa$pvalue }
-sample problem under left truncation
and right censoringComputes the Kolmogorov–Smirnov test adapted to left truncation and right
censoring and the corresponding -value.
ltrc.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), keep.boot = FALSE )ltrc.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, default to FALSE. If TRUE, the |
holes |
How to handle holes if present in a sample. One of |
keep.boot |
Logical, default to FALSE. If TRUE, the function returns the statistics evaluated in the bootstrap resamples. |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for left
truncation and right censoring, as studied in Lago et al. (2026).
The -value is approximated by means of the obvious bootstrap accounting for
left truncation and right censoring, studied in Bilker and Wang (1997). The exact
algorithm for the -sample problem can be consulted in Lago et al. (2026).
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistic.
p.value: approximated p-value of the tests.
curves: A list with the estimators of the distribution functions to
be compared.
groups: Group indicator
stat.boot: the statistics computed from the bootstrap resamples. Included in the output
only if keep.boot = TRUE.
Bilker, W.B., and Wang, M.C. (1997). Bootstrapping left truncated and right censored data. Communications in Statistics - Simulation and Computation, 26:141-171.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026). Kolmogorov–Smirnov and Cramér–von Mises tests for the k-sample problem for left-truncated and right-censored data. Lifetime Data Analysis: 32(2).
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry,channing$age, channing$death,channing$gender) aa <- ltrc.pv.ks(data.channing,weights=rep(1/2,2), holes = 'conditional', plot.curves = FALSE, B = 200) plot(aa) aa$pvalue }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry,channing$age, channing$death,channing$gender) aa <- ltrc.pv.ks(data.channing,weights=rep(1/2,2), holes = 'conditional', plot.curves = FALSE, B = 200) plot(aa) aa$pvalue }
Simulates data under left truncation and right censoring.
ltrc.sim( n, rtrunc, trunc.args = list(), rtarget, target.args = list(), rcens, cens.args = list(), seed = NULL )ltrc.sim( n, rtrunc, trunc.args = list(), rtarget, target.args = list(), rcens, cens.args = list(), seed = NULL )
n |
Sample size. |
rtrunc |
A function generating truncation times. |
trunc.args |
A list of arguments passed to |
rtarget |
A function generating target/event times. |
target.args |
A list of arguments passed to |
rcens |
A function generating residual censoring times. |
cens.args |
A list of arguments passed to |
seed |
Optional seed. |
A data frame with columns u, y, and delta.
aa <- ltrc.sim(n=500,rtrunc=rnorm,trunc.args=list(4,1), rtarget=rnorm,target.args = list(4,1), rcens=rexp,cens.args=list(1),seed=12) head(aa)aa <- ltrc.sim(n=500,rtrunc=rnorm,trunc.args=list(4,1), rtarget=rnorm,target.args = list(4,1), rcens=rexp,cens.args=list(1),seed=12) head(aa)
-sample problem under left truncation
and right censoringComputes -sample tests adapted to left truncation and right
censoring and the corresponding -value.
ltrc.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )ltrc.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, holes = c("holes", "add", "conditional"), p = 0, q = 0, seed = NULL, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
tests |
Tests to perform. One of Kolmogorov–Smirnov (ks), Cramér–von Mises (cvm), or weighted log-rank-type tests (logrank). |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, FALSE by default. If set to TRUE, the |
holes |
One of |
p, q
|
Weights of the log-rank-type tests. The choice |
seed |
Seed for the bootstrap resamples. |
keep.boot |
Logical. If |
This function collects the three tests implemented in this package: Kolmogorov–Smirnov,
Cramér–von Mises (Lago et al., 2026) and weighted log-rank-type tests, (Klein and Moeschberger, 2003)
adapted to left truncation and right censoring. The -values of the first two are
approximated by bootstrap, and the -value of the log-rank tests is computed by
the asymptotic distribution of the test statistic, where
is the number of groups.
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov and/or Cramér–von Mises statistics.
p.value: approximated p-value of the tests.
stat.boot: bootstrap statistics for the p-value approximation.
logrank: a matrix with weights p and q, test statistics, and -values.
B: number of bootstrap replications.
tests: tests performed, as specified in the input.
curves: distribution function estimators of the samples to be compared.
groups: group indicators.
holes: method for handling holes.
The summary method returns a data frame with one row per test
and the following columns:
Name of the test. For the weighted log-rank-type tests, the weights
p and q are detailed.
Observed value of the test statistic.
Corresponding -value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
Lago, A., Pardo-Fernández, J.C., and de Uña-Álvarez, J. (2026). Kolmogorov–Smirnov and Cramér–von Mises tests for the k-sample problem for left-truncated and right-censored data. Lifetime Data Analysis: 32(2).
if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) aa <- ltrc.test(data.channing,weights=rep(1/2,2), holes='conditional',tests=c('ks','cvm','logrank'), B = 200, p=c(0,1,0.5,0),q=c(0,0,0.5,1)) plot(aa) summary(aa) }if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") data.channing <- data.frame(channing$ageentry, channing$age, channing$death, channing$gender) aa <- ltrc.test(data.channing,weights=rep(1/2,2), holes='conditional',tests=c('ks','cvm','logrank'), B = 200, p=c(0,1,0.5,0),q=c(0,0,0.5,1)) plot(aa) summary(aa) }
It computes the estimator of the truncation distribution function for data subject to left truncation and right censoring
ltrc.truncation.estimator(o.sample, holes = c("holes", "add", "conditional"))ltrc.truncation.estimator(o.sample, holes = c("holes", "add", "conditional"))
o.sample |
Matrix or data frame with columns (U, Y, Delta). |
holes |
One of 'add', 'conditional' or 'holes' (default). How to deal with holes
in the estimation of the target variable survival function. See
|
The estimator of the distribution function of the truncation variable is given by
where are the distinct truncation times and is the
survival function estimator of the target variable.
The nontruncation probability is defined as
where is the distribution function of the target
variable. Then, it can be estimated as
where are
the distinct observed event times and are
the weights that the estimator assigns to .
In practice, it is possible to observe truncation times larger than the largest
event time. This would break the formula of the estimator defined
above. In such a case, following the recommendations in Wang (1991), we reduce
the distribution function of the truncation variable to a conditional distribution
on the identifiable region, by trimming a small amount of data. This is analogous to
conditioning to times largest than the largest hole when estimating the distribution
function of the target variable.
An object with the observed truncation times, the estimator at those times, the jumps of the estimator and an estimator of the nontruncation probability
Wang, M.C. (1991). Nonparametric Estimation from Cross-Sectional Survival Data. Journal of the American Statistical Association, 86:130-143.
aa <- ltrc.sim(n=5000,rtrunc=rnorm,trunc.args = list(4,1),rtarget=rnorm, target.args=list(4,1),rcens=rexp,cens.args=list(1)) head(aa) plot(ltrc.truncation.estimator(aa)) curve(pnorm(x,4,1),add=TRUE,col=2) if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") o.sample <- channing[channing$gender==1,c(3,4,2)] # subgroup of men plot(ltrc.estimator(o.sample)) # hole in the second event time plot(ltrc.truncation.estimator(o.sample,holes='add')) lines(ltrc.truncation.estimator(o.sample,holes='conditional'),col=2) }aa <- ltrc.sim(n=5000,rtrunc=rnorm,trunc.args = list(4,1),rtarget=rnorm, target.args=list(4,1),rcens=rexp,cens.args=list(1)) head(aa) plot(ltrc.truncation.estimator(aa)) curve(pnorm(x,4,1),add=TRUE,col=2) if (requireNamespace("KMsurv", quietly = TRUE)) { data("channing", package = "KMsurv") o.sample <- channing[channing$gender==1,c(3,4,2)] # subgroup of men plot(ltrc.estimator(o.sample)) # hole in the second event time plot(ltrc.truncation.estimator(o.sample,holes='add')) lines(ltrc.truncation.estimator(o.sample,holes='conditional'),col=2) }
Produces a step plot of the survival estimator.
## S3 method for class 'estimators' plot(x, conf.int = FALSE, ...)## S3 method for class 'estimators' plot(x, conf.int = FALSE, ...)
x |
An object of class |
conf.int |
Logical, default to FALSE. If TRUE, pointwise confidence bands are depicted for the curves. |
... |
Additional graphical parameters passed to |
Invisibly returns x.
Plots the estimated survival curves for the groups involved in
ltrc.test().
## S3 method for class 'ksamples_test' plot( x, y = NULL, col = NULL, lwd = 2, lty = 1, xlab = "Time", ylab = "Survival probability", main = "Estimated survival curves", legend.pos = "topright", ... )## S3 method for class 'ksamples_test' plot( x, y = NULL, col = NULL, lwd = 2, lty = 1, xlab = "Time", ylab = "Survival probability", main = "Estimated survival curves", legend.pos = "topright", ... )
x |
An object of class |
y |
Ignored. |
col |
Vector of colours for the groups. |
lwd |
Line width. |
lty |
Line type. |
xlab |
X-axis label. |
ylab |
Y-axis label. |
main |
Plot title. |
legend.pos |
Position of the legend. |
... |
Additional graphical parameters passed to |
Invisibly returns x.
Produces a step plot of the survival estimator.
## S3 method for class 't.estimators' plot(x, ...)## S3 method for class 't.estimators' plot(x, ...)
x |
An object of class |
... |
Additional graphical parameters passed to |
Invisibly returns x.
Computes the Cramér–von Mises statistic adapted to left truncation and right censoring.
rc.cvm(p.sample, weights = NULL, plot.curves = FALSE)rc.cvm(p.sample, weights = NULL, plot.curves = FALSE)
p.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
plot.curves |
Logical, default to FALSE. If TRUE, the |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for
right censoring.
Numerical value of the test statistic computed from the samples.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
Schumacher, M. (1984). Two-Sample Tests of Cramér–von Mises-and Kolmogorov–Smirnov-Type for Randomly Censored Data. International Statistical Review/Revue Internationale de Statistique, 263-281.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.cvm(data,weights=table(data$group)/nrow(data)) }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.cvm(data,weights=table(data$group)/nrow(data)) }
Computes the survival function estimator accounting for censored observations. It also returns pointwise confidence bands.
rc.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog") )rc.estimator( o.sample, conf.int = FALSE, alpha = 0.05, conf.type = c("plain", "loglog") )
o.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
conf.int |
Logical, set as default to FALSE. If set to TRUE, the function returns a pointwise confidence band. |
alpha |
Pointwise confidence bands with |
conf.type |
Type of pointwise confidence band. Set as default to |
The estimator is defined as
where are the distinct observed event times,
and . It reduces to the
Kaplan–Meier estimator if data are not subject to truncation, and to the Lynden-Bell
estimator if no censoring is present.
The variance for the confidence bands follows a Greenwood's like formula:
Different transformations are also possible.
An object with the following content:
fail.time: ordered distinct observed event times
estimation: estimated survival function at each observed event time
prob: jumps of the estimator
If conf.int is set to TRUE, it also returns the confidence band lower and upper limits
Klein, J. P. and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer
Wang, M.C. (1991). Nonparametric estimation from cross-sectional survival data. Journal of the American Statistical Association, 86:130-143.
Stute, W. and Wang, J. L. (2008). The central limit theorem under random truncation. Bernoulli, 14:604–622.
x <- rnorm(500,4,1) c <- rnorm(500,4,1) o.sample <- data.frame(y = pmin(x,c),delta = as.numeric(x <= c)) plot(rc.estimator(o.sample,conf.int=TRUE,conf.type='loglog')) curve(1-pnorm(x,4,1),add=TRUE,col=2)x <- rnorm(500,4,1) c <- rnorm(500,4,1) o.sample <- data.frame(y = pmin(x,c),delta = as.numeric(x <= c)) plot(rc.estimator(o.sample,conf.int=TRUE,conf.type='loglog')) curve(1-pnorm(x,4,1),add=TRUE,col=2)
Computes the Kolmogorov–Smirnov statistic adapted to right censoring.
rc.ks(p.sample, weights = NULL, plot.curves = FALSE)rc.ks(p.sample, weights = NULL, plot.curves = FALSE)
p.sample |
Matrix or data frame with columns (Y, Delta, group). |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
plot.curves |
Logical, FALSE by default. Otherwise, it represents the k distribution functions to be compared. |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for
right censoring.
For the particular case of , the test statistic reduces to
thus the test indeed extends the classical two-sample Kolmogorov–Smirnov test. A two-sample Kolmogorov–Smirnov-type test for right-censored data was proposed in Schumacher (1984).
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistics.
time: time when the maximum is attained.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
Schumacher, M. (1984). Two-Sample Tests of Cramér–von Mises-and Kolmogorov–Smirnov-Type for Randomly Censored Data. International Statistical Review/Revue Internationale de Statistique, 263-281.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.ks(p.sample=data,weights=table(data$group)/nrow(data),plot.curves=TRUE) }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.ks(p.sample=data,weights=table(data$group)/nrow(data),plot.curves=TRUE) }
Computes weighted log-rank-type tests (Fleming–Harrington family) for k groups with left truncation and right censoring.
rc.logrank(p.sample, p = 0, q = 0, plot.curves = FALSE)rc.logrank(p.sample, p = 0, q = 0, plot.curves = FALSE)
p.sample |
Matrix or data frame with columns (U, Y, Delta, group). |
p, q
|
Nonnegative weight parameters. If one has length 1 and the other is a vector, the scalar is recycled. |
plot.curves |
Logical, default to FALSE. If TRUE, the |
The test statistic is based on the quantity
for . Denote by the distinct
observed event times from the samples. For a time and a group ,
denotes the total count of individuals that experience the event,
and is the number of individuals at risk. In addition,
and , which are
the total number of events observed and the total number of individuals at risk
at time , respectively.
Finally, the weights are taken as
with the pooled survival just before the event time. The choice
results in the classical version of the log-rank test.
The test statistic is given by
where is the matrix with the variance-covariance estimators. Each
term in the matrix includes the correction factor for tied
event times. The test statistic follows an distribution under
the null hypothesis of equal target distributions in the populations.
A matrix with columns statistic and p.value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.logrank(data, p = c(0, 1, 2), q = 0) }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) rc.logrank(data, p = c(0, 1, 2), q = 0) }
-sample problem under right censoringComputes the Kolmogorov–Smirnov test adapted to left truncation and right
censoring and the corresponding -value.
rc.pv.cvm(p.sample, weights = NULL, B = 500, keep.boot = FALSE)rc.pv.cvm(p.sample, weights = NULL, B = 500, keep.boot = FALSE)
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
keep.boot |
Logical, default to FALSE. If TRUE, the function returns the evaluation of the Cramér–von Mises statistic on the bootstrap resamples. |
In Kiefer (1959), the -sample version of the Cramér–von Mises test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for
right censoring.
The -value is approximated by means of the obvious bootstrap accounting for
left truncation and right censoring, studied in Efron (1981).
A list with the following:
statistic: Cramér–von Mises statistic evaluated on the the samples.
pvalue: -value of the test approximated via bootstrap.
curves: A list with the estimators of the distribution functions to
be compared.
groups: Group indicator.
stat.boot: The Cramér–von Mises statistic evaluated from the bootstrap resamples.
Efron, B. (1981). Censored data and the bootstrap. Journal of the American Statistical Association, 76: 312-319.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.pv.cvm(data,weights=table(data$group)/nrow(data), B = 200) plot(aa) aa$pvalue }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.pv.cvm(data,weights=table(data$group)/nrow(data), B = 200) plot(aa) aa$pvalue }
-sample problem under right censoringComputes the Kolmogorov–Smirnov test adapted to right censoring and the
corresponding -value.
rc.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, keep.boot = FALSE )rc.pv.ks( p.sample, weights = NULL, B = 500, plot.curves = FALSE, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (U, Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
B |
Number of bootstrap replications to approximate the |
plot.curves |
logical, set to FALSE by default. If TRUE, the function depicts
the |
keep.boot |
Logical, default to FALSE. If TRUE, the statistics evaluated on the bootstrap resamples are included in the output. |
In Kiefer (1959), the -sample version of the Kolmogorov–Smirnov test is defined as
where is the cumulative distribution function estimator of calculated
only with the -th sample, and is given by
where correspond to the
input weights. This function implements the previous test statistic accounting for
right censoring.
For the particular case of , the test statistic reduces to
thus the test indeed extends the classical two-sample Kolmogorov–Smirnov test. A two-sample Kolmogorov–Smirnov-type test for right-censored data was proposed in Schumacher (1984).
The -value is approximated by means of the obvious bootstrap accounting for
right censoring, studied in Efron (1981).
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov statistic.
pvalue: approximated p-value of the test.
curves: A list with the estimators of the distribution functions to
be compared.
groups: Group indicator
stat.boot: statistics computed from the bootstrap resamples.
Efron, B. (1981). Censored data and the bootstrap. Journal of the American Statistical Association, 76: 312-319.
Kiefer, J. (1959). K-sample analogues of the Kolmogorov–Smirnov and Cramér–von Mises tests. The Annals of Mathematical Statistics, 30:420-447.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.pv.ks(data,weights=table(data$group)/nrow(data), B = 200) plot(aa) aa$pvalue }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time, delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.pv.ks(data,weights=table(data$group)/nrow(data), B = 200) plot(aa) aa$pvalue }
Simulates right-censored survival data from prespecified event-time and censoring-time distributions.
rc.sim( n, rtarget, target.args = list(), rcens, cens.args = list(), seed = NULL )rc.sim( n, rtarget, target.args = list(), rcens, cens.args = list(), seed = NULL )
n |
Sample size. |
rtarget |
A function that simulates event/survival times. |
target.args |
A list with the parameters of the target distribution. |
rcens |
A function that simulates censoring times. |
cens.args |
A list with the parameters of the censoring distribution. |
seed |
Optional seed for reproducibility. |
A data frame with rows and two columns:
Observed follow-up time, i.e. .
Event indicator, equal to 1 if the event is observed and 0 if censored.
aa <- rc.sim(n = 1000, rtarget = rweibull, target.args = list(shape = 2, scale = 3), rcens = rexp, cens.args = list(rate = 0.2), seed = 123) head(aa) mean(aa$delta == 0) # empirical censoring rateaa <- rc.sim(n = 1000, rtarget = rweibull, target.args = list(shape = 2, scale = 3), rcens = rexp, cens.args = list(rate = 0.2), seed = 123) head(aa) mean(aa$delta == 0) # empirical censoring rate
-sample problem under right censoringComputes -sample tests adapted to right
censoring and the corresponding -value.
rc.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, p = 0, q = 0, seed = NULL, keep.boot = FALSE )rc.test( p.sample, weights = NULL, tests = c("ks", "cvm", "logrank"), B = 500, plot.curves = FALSE, p = 0, q = 0, seed = NULL, keep.boot = FALSE )
p.sample |
Matrix or data frame with columns (Y, |
weights |
Vector of weights of the estimator under the null hypothesis.
If no vector is provided, weights proportional to each sample size are assigned automatically
by the function. Not used when |
tests |
Tests to perform. One or more of Kolmogorov–Smirnov ('ks'), Cramér–von Mises ('cvm'), or weighted log-rank-type tests ('logrank'). |
B |
Number of bootstrap replications to approximate the |
plot.curves |
Logical, FALSE by default. If set to TRUE, the |
p, q
|
Weights of the log-rank-type tests. The choice |
seed |
Seed for the bootstrap resamples. |
keep.boot |
Logical. If |
This function collects the three tests implemented in this package: Kolmogorov–Smirnov,
Cramér–von Mises and weighted log-rank-type tests, (Klein and Moeschberger, 2003)
adapted to right censoring. The -values of the first two are
approximated by the obvious bootstrap (Efron, 1981), and the -value of the
log-rank tests is computed by the asymptotic distribution
of the test statistic, where is the number of groups.
A list with the following components:
statistic: numerical value of the Kolmogorov–Smirnov and/or Cramér–von Mises statistics.
p.value: approximated -value of the tests.
logrank: a matrix with weights p and q, test statistics, and -values.
B: number of bootstrap replications.
tests: tests performed, as specified in the input.
curves: distribution function estimators of the samples to be compared.
groups: group indicators.
stat.boot: evaluation of the tests in the bootstrap resamples. Only when keep.boot = TRUE.
The summary method returns a data frame with one row per test
and the following columns:
Name of the test. For the weighted log-rank-type tests, the weights
p and q are detailed.
Observed value of the test statistic.
Corresponding -value.
Klein, J.P., and Moeschberger, M. L. (2003). Survival Analysis: Techniques for Censored and Truncated Data. Springer, New York.
Efron, B. (1981). Censored data and the bootstrap. Journal of the American Statistical Association, 76: 312-319.
if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time,delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.test(p.sample=data,weights=table(data$group)/nrow(data), plot.curves=TRUE,tests=c('ks','cvm','logrank'), B = 200, p=c(0,1,2), q=0 ) plot(aa) summary(aa) }if (requireNamespace("survival", quietly = TRUE)) { library(survival) data <- data.frame(y=stanford2$time,delta=stanford2$status, group=as.numeric(stanford2$age<=35)+as.numeric(stanford2$age<=47)) aa <- rc.test(p.sample=data,weights=table(data$group)/nrow(data), plot.curves=TRUE,tests=c('ks','cvm','logrank'), B = 200, p=c(0,1,2), q=0 ) plot(aa) summary(aa) }
Produces a table with the test name, test statistic and p-value
for the procedures computed by ltrc.test().
## S3 method for class 'ksamples_test' summary(object, ...)## S3 method for class 'ksamples_test' summary(object, ...)
object |
An object of class |
... |
Additional arguments passed to methods. |
A data frame with columns:
Name of the test ("KS", "CvM", or the corresponding
(p, q) values for weighted rank tests).
Observed test statistic.
Associated p-value.