Recurrent events

Klaus Holst & Thomas Scheike

2022-09-03

Overview

For recurrent events data it is often of interest to compute basic descriptive quantities as a first go at getting some basic understanding of the phenonmenon studied. We here demonstrate how one can compute

In addition several tools can be used for simulating recurrent events and bivariate recurrent events data, in the case with a possible terminating event.

For bivariate recurrent events we also compute summary measures that describe their dependence such as

We also show how to improve the efficiency of recurrents events marginal mean.

Simulation of recurrents events

We start by simulating some recurrent events data with two type of events with cumulative hazards

where we consider types 1 and 2 and with a rate of the terminal event given by \(\Lambda_D(t)\). We let the events be independent, but could also specify a random effects structure to generate dependence.

When simulating data we can impose various random-effects structures to generate dependence

We return to how to run the different set-ups later and start by simulating independent processes.

Utility functions

We here mention two utility functions

Marginal Mean

We start by estimating the marginal mean \(E(N_1(t \wedge D))\) where \(D\) is the timing of the terminal event.

This is based on a rate model for

and is defined as \(\mu_1(t)=E(N_1^*(t))\) \[\begin{align} \int_0^t S(u) d R_1(u) \end{align}\] where \(S(t)=P(D \geq t)\) and \(dR_1(t) = E(dN_1^*(t) | D > t)\)

and can therefore be estimated by a

\[\begin{align} \hat R_1(t) & = \sum_i \int_0^t \frac{1}{Y_\bullet (s)} dN_{1i}(s) \end{align}\] where \(Y_{\bullet}(t)= \sum_i Y_i(t)\) such that the estimator is \[\begin{align} \hat \mu_1(t) & = \int_0^t \hat S(u) d\hat R_1(u). \end{align}\]

Cook & Lawless (1997), and developed further in Gosh & Lin (2000).

The variance can be estimated based on the asymptotic expansion of \(\hat \mu_1(t) - \mu_1(t)\) \[\begin{align*} & \sum_i \int_0^t \frac{S(s)}{\pi(s)} dM_{i1} - \mu_1(t) \int_0^t \frac{1}{\pi(s)} dM_i^d + \int_0^t \frac{\mu_1(s) }{\pi(s)} dM_i^d, \end{align*}\]

with mean-zero processes

as in Gosh & Lin (2000)

Generating data

We start by generating some data to illustrate the computation of the marginal mean

library(mets)
set.seed(1000) # to control output in simulatins for p-values below.
data(base1cumhaz)
data(base4cumhaz)
data(drcumhaz)
ddr <- drcumhaz
base1 <- base1cumhaz
base4 <- base4cumhaz
rr <- simRecurrent(200,base1,death.cumhaz=ddr)
rr$x <- rnorm(nrow(rr)) 
rr$strata <- floor((rr$id-0.01)/500)
dlist(rr,.~id| id %in% c(1,7,9))
#> id: 1
#>   entry time  status rr dtime fdeath death start stop  x     strata
#> 1 0     132.6 0      1  132.6 1      1     0     132.6 1.386 0     
#> attr(,"class")
#> [1] matrix array 
#> ------------------------------------------------------------ 
#> id: 7
#>   entry time  status rr dtime fdeath death start stop  x      strata
#> 7 0     141.3 0      1  141.3 1      1     0     141.3 0.6628 0     
#> attr(,"class")
#> [1] matrix array 
#> ------------------------------------------------------------ 
#> id: 9
#>     entry time  status rr dtime fdeath death start stop  x       strata
#> 9     0.0 433.5 1      1  558.7 1      0       0.0 433.5 -0.7528 0     
#> 204 433.5 558.7 0      1  558.7 1      1     433.5 558.7 -0.6565 0     
#> attr(,"class")
#> [1] matrix array

The status variable keeps track of the recurrent evnts and their type, and death the timing of death.

To compute the marginal mean we simly estimate the two rates functions of the number of events of interest and death by using the phreg function (to start without covariates). Then the estimates are combined with standard error computation in the recurrentMarginal function

#  to fit non-parametric models with just a baseline 
xr <- phreg(Surv(entry,time,status)~cluster(id),data=rr)
dr <- phreg(Surv(entry,time,death)~cluster(id),data=rr)
par(mfrow=c(1,3))
bplot(dr,se=TRUE)
title(main="death")
bplot(xr,se=TRUE)
# robust standard errors 
rxr <-   robust.phreg(xr,fixbeta=1)
bplot(rxr,se=TRUE,robust=TRUE,add=TRUE,col=4)

# marginal mean of expected number of recurrent events 
out <- recurrentMarginal(xr,dr)
bplot(out,se=TRUE,ylab="marginal mean",col=2)

We can also extract the estimate in different time-points

summary(out,times=c(1000,2000))
#>   times  mean    se-mean  CI-2.5% CI-97.5% strata
#> 1  1000 1.220 0.08472827 1.064739 1.397901      0
#> 2  2000 1.915 0.14069906 1.658166 2.211615      0

The marginal mean can also be estimated in a stratified case:

xr <- phreg(Surv(entry,time,status)~strata(strata)+cluster(id),data=rr)
dr <- phreg(Surv(entry,time,death)~strata(strata)+cluster(id),data=rr)
par(mfrow=c(1,3))
bplot(dr,se=TRUE)
title(main="death")
bplot(xr,se=TRUE)
rxr <-   robust.phreg(xr,fixbeta=1)
bplot(rxr,se=TRUE,robust=TRUE,add=TRUE,col=1:2)

out <- recurrentMarginal(xr,dr)
bplot(out,se=TRUE,ylab="marginal mean",col=1:2)

Furhter, if we adjust for covariates for the two rates we can still do predictions of marginal mean, what can be plotted is the baseline marginal mean, that is for the covariates equal to 0 for both models. Predictions for specific covariates can also be obtained with the recmarg (recurren marginal mean used solely for predictions without standard error computation).

# cox case
xr <- phreg(Surv(entry,time,status)~x+cluster(id),data=rr)
dr <- phreg(Surv(entry,time,death)~x+cluster(id),data=rr)
par(mfrow=c(1,3))
bplot(dr,se=TRUE)
title(main="death")
bplot(xr,se=TRUE)
rxr <- robust.phreg(xr)
bplot(rxr,se=TRUE,robust=TRUE,add=TRUE,col=1:2)

out <- recurrentMarginal(xr,dr)
bplot(out,se=TRUE,ylab="marginal mean",col=1:2)

# predictions witout se's 
outX <- recmarg(xr,dr,Xr=1,Xd=1)
bplot(outX,add=TRUE,col=3)

Improving efficiency

We now simulate some data where there is strong heterogenity such that we can improve the efficiency for censored survival data. The augmentation is a regression on the history for each subject consisting of any of the terms: Nt, Nt2 (Nt squared), expNt (exp(-Nt)), NtexpNt (Nt*exp(-Nt))

rr <- simRecurrentII(200,base1,base4,death.cumhaz=ddr,cens=3/5000,dependence=4,var.z=1)
rr <-  count.history(rr)

rr <- transform(rr,statusD=status)
rr <- dtransform(rr,statusD=3,death==1)
dtable(rr,~statusD+status+death,level=2,response=1)
#> 
#>       statusD
#> status   0   1   2   3
#>      0  93   0   0 107
#>      1   0 280   0   0
#>      2   0   0  21   0
#> 
#>      statusD
#> death   0   1   2   3
#>     0  93 280  21   0
#>     1   0   0   0 107

xr <- phreg(Surv(start,stop,status==1)~cluster(id),data=rr)
dr <- phreg(Surv(start,stop,death)~cluster(id),data=rr)
# marginal mean of expected number of recurrent events 
out <- recurrentMarginal(xr,dr)

times <- 500*(1:10)
recEFF1 <- recurrentMarginalAIPCW(Event(start,stop,statusD)~cluster(id),data=rr,times=times,cens.code=0,
                   death.code=3,cause=1,augment.model=~Nt)
with( recEFF1, cbind(times,muP,semuP,muPAt,semuPAt,semuPAt/semuP))
#>       times       muP     semuP     muPAt   semuPAt          
#>  [1,]   500 0.8419436 0.1138226 0.8342534 0.1127409 0.9904967
#>  [2,]  1000 1.3259159 0.1602411 1.3135511 0.1579272 0.9855596
#>  [3,]  1500 1.6739075 0.2079551 1.6549621 0.2031499 0.9768930
#>  [4,]  2000 2.0087541 0.2758262 1.9898497 0.2655614 0.9627852
#>  [5,]  2500 2.2542760 0.3558186 2.2286651 0.3314850 0.9316125
#>  [6,]  3000 2.4916081 0.4659928 2.4404719 0.4112639 0.8825544
#>  [7,]  3500 2.7141071 0.5380272 2.6399687 0.4557299 0.8470388
#>  [8,]  4000 2.8031067 0.5786283 2.6534900 0.4763625 0.8232616
#>  [9,]  4500 3.0701054 0.7255539 2.6940540 0.5318311 0.7330001
#> [10,]  5000 3.2481045 0.7426535 2.6858796 0.4392249 0.5914264

times <- 500*(1:10)
recEFF14 <- recurrentMarginalAIPCW(Event(start,stop,statusD)~cluster(id),data=rr,times=times,cens.code=0,
death.code=3,cause=1,augment.model=~Nt+Nt2+expNt+NtexpNt)
with(recEFF14,cbind(times,muP,semuP,muPAt,semuPAt,semuPAt/semuP))
#>       times       muP     semuP     muPAt   semuPAt          
#>  [1,]   500 0.8419436 0.1138226 0.8388475 0.1124582 0.9880136
#>  [2,]  1000 1.3259159 0.1602411 1.3247731 0.1568442 0.9788014
#>  [3,]  1500 1.6739075 0.2079551 1.6758563 0.2006916 0.9650718
#>  [4,]  2000 2.0087541 0.2758262 1.9920665 0.2601628 0.9432128
#>  [5,]  2500 2.2542760 0.3558186 2.1802996 0.3181134 0.8940326
#>  [6,]  3000 2.4916081 0.4659928 2.3329310 0.3817348 0.8191862
#>  [7,]  3500 2.7141071 0.5380272 2.4139634 0.4081384 0.7585832
#>  [8,]  4000 2.8031067 0.5786283 2.4435118 0.4192212 0.7245086
#>  [9,]  4500 3.0701054 0.7255539 2.5322985 0.4266387 0.5880180
#> [10,]  5000 3.2481045 0.7426535 2.5505379 0.2288288 0.3081232

bplot(out,se=TRUE,ylab="marginal mean",col=2)
k <- 1
for (t in times) {
    ci1 <- c(recEFF1$muPAt[k]-1.96*recEFF1$semuPAt[k],
             recEFF1$muPAt[k]+1.96*recEFF1$semuPAt[k])
    ci2 <- c(recEFF1$muP[k]-1.96*recEFF1$semuP[k],
             recEFF1$muP[k]+1.96*recEFF1$semuP[k])
    lines(rep(t,2)-2,ci2,col=2,lty=1,lwd=2)
    lines(rep(t,2)+2,ci1,col=1,lty=1,lwd=2)
    k <- k+1
}
legend("bottomright",c("Eff-pred"),lty=1,col=c(1,3))

In the case where covariates might be important but we are still interested in the marginal mean we can also augment wrt these covariates

rr <- mets:::simMarginalMeanCox(200,cens=3/5000,Lam1=base1,LamD=ddr,beta1=c(0.3,-0.3),
                betad=c(-0.3,0.3))
dtable(rr,~statusG+status+death,level=2,response=1)
#> 
#>       statusG
#> status   0   1   3
#>      0  95   0 105
#>      1   0  87   0
#> 
#>      statusG
#> death   0   1   3
#>     0  95  87   0
#>     1   0   0 105

times <- seq(500,5000,by=500)
rr <- transform(rr,statusD=status)
rr <- dtransform(rr,statusD=2,death==1)
dtable(rr,~statusD+status+death,level=2)
#> 
#>       statusD
#> status   0   1   2
#>      0  95   0 105
#>      1   0  87   0
#> 
#>      statusD
#> death   0   1   2
#>     0  95  87   0
#>     1   0   0 105
#> 
#>      status
#> death   0   1
#>     0  95  87
#>     1 105   0
recEFF1x <- recurrentMarginalAIPCW(Event(start,stop,statusD)~cluster(id),data=rr,times=times,
                   cens.code=0,death.code=2,cause=1,augment.model=~X1+X2)
with(recEFF1x, cbind(muP,muPA,muPAt,semuP,semuPA,semuPAt,semuPAt/semuP))
#>             muP      muPA     muPAt      semuP     semuPA    semuPAt          
#>  [1,] 0.1518353 0.1518529 0.1511457 0.02948052 0.02945165 0.02943720 0.9985304
#>  [2,] 0.2562828 0.2596232 0.2557855 0.04004262 0.03996702 0.03989066 0.9962052
#>  [3,] 0.3270346 0.3288994 0.3263434 0.05188583 0.05186797 0.05178425 0.9980422
#>  [4,] 0.5023451 0.5037651 0.5056017 0.07203201 0.07179332 0.07145162 0.9919426
#>  [5,] 0.7052234 0.6731841 0.6652085 0.11001917 0.10848677 0.10783477 0.9801453
#>  [6,] 0.7867792 0.7353194 0.7162445 0.12817048 0.12572431 0.12428666 0.9696980
#>  [7,] 0.9169280 0.8261260 0.7792783 0.15950878 0.15290158 0.15117680 0.9477648
#>  [8,] 1.1224213 0.9802906 0.8780498 0.18229765 0.16276738 0.16036283 0.8796758
#>  [9,] 1.3133061 1.1691099 0.9365609 0.23468133 0.20617317 0.20079447 0.8556048
#> [10,] 1.3133061 1.2587935 0.9365609 0.23468133 0.20670560 0.20079447 0.8556048

xr <- phreg(Surv(start,stop,status==1)~cluster(id),data=rr)
dr <- phreg(Surv(start,stop,death)~cluster(id),data=rr)
out <- recurrentMarginal(xr,dr)
mets::summaryTimeobject(out$times,out$mu,times=times,se.mu=out$se.mu)
#>    times      mean    se-mean   CI-2.5%  CI-97.5%
#> 1    500 0.1518353 0.02948052 0.1037766 0.2221499
#> 2   1000 0.2562828 0.04004262 0.1886783 0.3481103
#> 3   1500 0.3270346 0.05188583 0.2396313 0.4463176
#> 4   2000 0.5023451 0.07203201 0.3792668 0.6653642
#> 5   2500 0.7052234 0.11001917 0.5194355 0.9574626
#> 6   3000 0.7867792 0.12817048 0.5717223 1.0827312
#> 7   3500 0.9169280 0.15950878 0.6520148 1.2894751
#> 8   4000 1.1224213 0.18229765 0.8164052 1.5431425
#> 9   4500 1.3133061 0.23468133 0.9252469 1.8641218
#> 10  5000 1.3133061 0.23468133 0.9252469 1.8641218

Regression models for the marginal mean

One can also do regression modelling , using the model \[\begin{align*} E(N_1(t) | X) & = \Lambda_0(t) \exp(X^T \beta) \end{align*}\] then Ghost-Lin suggested IPCW score equations that are implemented in the recreg function of mets.

First we generate data that from a cox model with \(\beta=(-0.3,0.3)\)

rr <- mets:::simMarginalMeanCox(200,cens=3/5000,Lam1=base1,LamD=ddr,beta1=c(0.3,-0.3),
                betad=c(-0.3,0.3))
dtable(rr,~statusG+status+death,level=2,response=1)
#> 
#>       statusG
#> status   0   1   3
#>      0 100   0 100
#>      1   0  65   0
#> 
#>      statusG
#> death   0   1   3
#>     0 100  65   0
#>     1   0   0 100

 out  <- recreg(Event(start,stop,statusG)~X1+X2+cluster(id),data=rr,cause=1,death.code=3,cens.code=0)
 outs <- recreg(Event(start,stop,statusG)~X1+X2+cluster(id),data=rr,cause=1,death.code=3,cens.code=0,
        cens.model=~strata(X1,X2))

 summary(out)$coef
#>     Estimate      S.E.   dU^-1/2   P-value
#> X1 0.2679789 0.3584144 0.2522670 0.4546539
#> X2 0.3531921 0.3352655 0.2492851 0.2921257
 summary(outs)$coef
#>     Estimate      S.E.   dU^-1/2   P-value
#> X1 0.2613928 0.3440964 0.2567159 0.4474639
#> X2 0.1125436 0.3199334 0.2613827 0.7250093

We note that for the extended censoring model we gain a little efficiency and that the estimates are close to the true values.

Other marginal properties

The mean is a useful summary measure but it is very easy and useful to look at other simple summary measures such as the probability of exceeding \(k\) events

that is thus equivalent to a certain cumulative incidence of \(T_k\) occurring before \(D\). We denote this cumulative incidence as \(\hat F_k(t)\).

We note also that \(N_1^*(t)^2\) can be written as \[\begin{align*} \sum_{k=0}^K \int_0^t I(D > s) I(N_1^*(s-)=k) f(k) dN_1^*(s) \end{align*}\] with \(f(k)=(k+1)^2 - k^2\), such that its mean can be written as \[\begin{align*} \sum_{k=0}^K \int_0^t S(s) f(k) P(N_1^*(s-)= k | D \geq s) E( dN_1^*(s) | N_1^*(s-)=k, D> s) \end{align*}\] and estimated by \[\begin{align*} \tilde \mu_{1,2}(t) & = \sum_{k=0}^K \int_0^t \hat S(s) f(k) \frac{Y_{1\bullet}^k(s)}{Y_\bullet (s)} \frac{1}{Y_{1\bullet}^k(s)} d N_{1\bullet}^k(s)= \sum_{i=1}^n \int_0^t \hat S(s) f(N_{i1}(s-)) \frac{1}{Y_\bullet (s)} d N_{i1}(s), \end{align*}\] That is very similar to the “product-limit” estimator for \(E( (N_1^*(t))^2 )\) \[\begin{align} \hat \mu_{1,2}(t) & = \sum_{k=0}^K k^2 ( \hat F_{k}(t) - \hat F_{k+1}(t) ). \end{align}\]

We use the esimator of the probabilty of exceeding “k” events based on the fact that \(I(N_1^*(t) \geq k)\) is equivalent to \[\begin{align*} \int_0^t I(D > s) I(N_1^*(s-)=k-1) dN_1^*(s), \end{align*}\] suggesting that its mean can be computed as \[\begin{align*} \int_0^t S(s) P(N_1^*(s-)= k-1 | D \geq s) E( dN_1^*(s) | N_1^*(s-)=k-1, D> s) \end{align*}\] and estimated by \[\begin{align*} \tilde F_k(t) = \int_0^t \hat S(s) \frac{Y_{1\bullet}^{k-1}(s)}{Y_\bullet (s)} \frac{1}{Y_{1\bullet}^{k-1}(s)} d N_{1\bullet}^{k-1}(s). \end{align*}\]

To compute these estimators we need to set up the data by computing the number of previous events of type “1” by the count.history function

###cor.mat <- corM <- rbind(c(1.0, 0.6, 0.9), c(0.6, 1.0, 0.5), c(0.9, 0.5, 1.0))
rr <- simRecurrentII(200,base1,base4,death.cumhaz=ddr,cens=3/5000,dependence=4,var.z=1)
rr <-  count.history(rr)
dtable(rr,~death+status)
#> 
#>       status   0   1   2
#> death                   
#> 0            106 333  31
#> 1             94   0   0

oo <- prob.exceedRecurrent(rr,1)
bplot(oo)

We can also look at the mean and variance based on the estimators just described

par(mfrow=c(1,2))
with(oo,plot(time,mu,col=2,type="l"))
#
with(oo,plot(time,varN,type="l"))

We could also use the product-limit estimator to estimate the probability of exceeding “k” events, and then standard errors are also returned:

 oop <- prob.exceed.recurrent(rr,1)
 bplot(oo)
 matlines(oop$times,oop$prob,type="l")

 summaryTimeobject(oop$times,oop$prob,se.mu=oop$se.prob,times=1000)
#>            times        mean     se-mean     CI-2.5%   CI-97.5%
#> N=0         1000 0.480467585 0.040739041 0.406901268 0.56733443
#> exceed>=1   1000 0.519532415 0.040739041 0.445517335 0.60584383
#> exceed>=2   1000 0.291855819 0.037107797 0.227478818 0.37445165
#> exceed>=3   1000 0.211343529 0.034042683 0.154126400 0.28980166
#> exceed>=4   1000 0.148969406 0.030255486 0.100049753 0.22180848
#> exceed>=5   1000 0.091817280 0.024882086 0.053981910 0.15617108
#> exceed>=6   1000 0.046765356 0.017561743 0.022401009 0.09762946
#> exceed>=7   1000 0.039360920 0.015860441 0.017867680 0.08670863
#> exceed>=8   1000 0.026275556 0.012979453 0.009978663 0.06918811
#> exceed>=9   1000 0.020668856 0.011822177 0.006736543 0.06341556
#> exceed>=10  1000 0.014837717 0.010459184 0.003726830 0.05907375
#> exceed>=11  1000 0.007418664 0.007371104 0.001058194 0.05200989
#> exceed>=12  1000 0.007515010 0.007463180 0.001072959 0.05263517
#> exceed>=13  1000 0.007613891 0.007560521 0.001087317 0.05331595
#> exceed>=14  1000 0.009420701 0.009345693 0.001347856 0.06584503
#> exceed>=15  1000 0.009601868 0.009519328 0.001375485 0.06702790
#> exceed>=16  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=17  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=18  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=19  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=20  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=21  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=22  1000 0.000000000 0.000000000         NaN        NaN
#> exceed>=23  1000 0.000000000 0.000000000         NaN        NaN

We note from the plot that the estimates are quite similar.

Finally, we make a plot with 95% confidence intervals

matplot(oop$times,oop$prob,type="l")
for (i in seq(ncol(oop$prob))) 
    plotConfRegion(oop$times,cbind(oop$se.lower[,i],oop$se.upper[,i]),col=i)

Multiple events

We now generate recurrent events with two types of events. We start by generating data as before where all events are independent.

rr <- simRecurrentII(200,base1,cumhaz2=base4,death.cumhaz=ddr)
rr <-  count.history(rr)
dtable(rr,~death+status)
#> 
#>       status   0   1   2
#> death                   
#> 0             32 684 110
#> 1            168   0   0

Based on this we can estimate also the joint distribution function, that is the probability that \((N_1(t) \geq k_1, N_2(t) \geq k_2)\)

# Bivariate probability of exceeding 
oo <- prob.exceedBiRecurrent(rr,1,2,exceed1=c(1,5),exceed2=c(1,2))
with(oo, matplot(time,pe1e2,type="s"))
nc <- ncol(oo$pe1e2)
legend("topleft",legend=colnames(oo$pe1e2),lty=1:nc,col=1:nc)

Dependence between events: Covariance

The dependence can also be summarised in other ways. For example by computing the covariance and comparing it to the covariance under the assumption of independence among survivors.

Covariance among two types of events \[\begin{align} \rho(t) & = \frac{ E(N_1^*(t) N_2^*(t) ) - \mu_1(t) \mu_2(t) }{ \mbox{sd}(N_1^*(t)) \mbox{sd}(N_2^*(t)) } \end{align}\] where \(E(N_1^*(t) N_2^*(t))\) can be computed as \[\begin{align*} E(N_1^*(t) N_2^*(t)) & = E( \int_0^t N_1^*(s-) dN_2^*(s) ) + E( \int_0^t N_2^*(s-) dN_1^*(s) ) \end{align*}\]

Recall that we might have a terminal event present such that we only see \(N_1^*(t \wedge D)\) and \(N_2^*(t \wedge D)\).

To compute the covariance we thus compute \[\begin{align*} E(\int_0^t N_1^*(s-) dN_2^*(s) ) & = \sum_k E( \int_0^t k I(N_1^*(s-)=k) I(D \geq s) dN_2^*(s) ) \end{align*}\] \[\begin{align*} = \sum_k \int_0^t S(s) k P(N_1^*(s-)= k | D \geq s) E( dN_2^*(s) | N_1^*(s-)=k, D \geq s) \end{align*}\] estimated by \[\begin{align*} & \sum_k \int_0^t \hat S(s) k \frac{Y_1^k(s)}{Y_\bullet (s)} \frac{1}{Y_1^k(s)} d \tilde N_{2,k}(s), \end{align*}\] * \(Y_j^k(t) = \sum Y_i(t) I( N_{ji}^*(s-)=k)\) for \(j=1,2\), * \(\tilde N_{j,k}(t) = \sum_i \int_0^t I(N_{ij^o}(s-)=k) dN_{ij}(s)\) * \(j^o\) gives the other type so that \(1^o=2\) and \(2^o=1\).

We thus estimate $ E(N_1^(t) N_2^(t))$ by \[\begin{align*} \sum_k \int_0^t \hat S(s) k \frac{Y_1^k(s)}{Y_\bullet (s)} \frac{1}{Y_1^k(s)} d \tilde N_{2,k}(s) + \sum_k \int_0^t \hat S(s) k \frac{Y_2^k(s)}{Y_\bullet (s)} \frac{1}{Y_2^k(s)} d \tilde N_{1,k}(s). \end{align*}\]

If the two processes are independent among survivors then \[\begin{align} E( dN_2^*(t) | N_1^*(t-)=k, D \geq t) = E( dN_2^*(t) | D \geq t) \end{align}\] so \[\begin{align*} E( \int_0^t N_1^*(s-) dN_2^*(s) ) & = \int_0^t S(s) E(N_1^*(s-) | D \geq s) E( dN_2^*(s) | D \geq s) \end{align*}\] and \[\begin{align*} \int_0^t \hat S(s) \{ \sum_k k \frac{Y_1^k(s)}{Y_\bullet (s)} \} \frac{1}{Y_\bullet (s)} dN_{2\bullet}(s), \end{align*}\] where \(N_{j\bullet}(t) = \sum_i \int_0^t dN_{j,i}(s)\).

Under the independence \(E(N_1^*(t) N_2^*(t))\) is estimated \[\begin{align*} \int_0^t \hat S(s) \{ \sum_k k \frac{Y_1^k(s)}{Y_\bullet (s)} \} \frac{1}{Y_\bullet (s)} dN_{2\bullet}(s) + \int_0^t \hat S(s) \{ \sum_k k \frac{Y_2^k(s)}{Y_\bullet (s)} \} \frac{1}{Y_\bullet (s)} dN_{1\bullet}(s). \end{align*}\]

Both estimators, \(\hat E(N_1^*(t) N_2^*(t))\) and \(\hat E_I(N_1^*(t) N_2^*(t))\), as well as \(\hat E(N_1^*(t))\) and \(\hat E(N_2^*(t))\), have asymptotic expansions that can be written as a sum of iid processes, similarly to the arguments of Ghosh & Lin 2000, \(\sum_i \Psi_i(t)\).
We here, however, use a simple block bootstrap to get standard errors.

We can thus estimate the standard errors and of the estimators and their difference \(\hat E(N_1^*(t) N_2^*(t))- \hat E_I(N_1^*(t) N_2^*(t))\).

Note that we have terms for whether * \(N_1\) is predicitive for \(N_2\) * N1 -> N2 : \(E( \int_0^t N_1^*(s-) dN_2^*(s) )\) * this is equivalent to a weighted log-rank test * \(N_2\) is predicitive for \(N_1\) * N2 -> N1 : \(E( \int_0^t N_2^*(s-) dN_1^*(s) )\) * this is equivalent to a weighted log-rank test

rr$strata <- 1
dtable(rr,~death+status)
#> 
#>       status   0   1   2
#> death                   
#> 0             32 684 110
#> 1            168   0   0

covrp <- covarianceRecurrent(rr,1,2,status="status",death="death",
                        start="entry",stop="time",id="id",names.count="Count")
par(mfrow=c(1,3)) 
plot(covrp)


# with strata, each strata in matrix column, provides basis for fast Bootstrap
covrpS <- covarianceRecurrentS(rr,1,2,status="status",death="death",
        start="entry",stop="time",strata="strata",id="id",names.count="Count")

Bootstrap standard errors for terms

First fitting the model again to get our estimates of interst, and then computing them for some specific time-points

times <- seq(500,5000,500)

coo1 <- covarianceRecurrent(rr,1,2,status="status",start="entry",stop="time")
#
mug <- Cpred(cbind(coo1$time,coo1$EN1N2),times)[,2]
mui <- Cpred(cbind(coo1$time,coo1$EIN1N2),times)[,2]
mu2.1 <- Cpred(cbind(coo1$time,coo1$mu2.1),times)[,2]
mu2.i <- Cpred(cbind(coo1$time,coo1$mu2.i),times)[,2]
mu1.2 <- Cpred(cbind(coo1$time,coo1$mu1.2),times)[,2]
mu1.i <- Cpred(cbind(coo1$time,coo1$mu1.i),times)[,2]
cbind(times,mu2.1,mu2.i)
cbind(times,mu1.2,mu1.i)

To get the bootstrap standard errors there is a quick memory demanding function (with S for speed and strata) BootcovariancerecurrenceS and slower function that goes through the loops in R Bootcovariancerecurrence.

bt1 <- BootcovariancerecurrenceS(rr,1,2,status="status",start="entry",stop="time",K=100,times=times)
#bt1 <- Bootcovariancerecurrence(rr,1,2,status="status",start="entry",stop="time",K=K,times=times)

BCoutput <- list(bt1=bt1,mug=mug,mui=mui,
        bse.mug=bt1$se.mug,bse.mui=bt1$se.mui,
        dmugi=mug-mui,
    bse.dmugi=apply(bt1$EN1N2-bt1$EIN1N2,1,sd),
    mu2.1 = mu2.1 , mu2.i = mu2.i , dmu2.i=mu2.1-mu2.i,
    mu1.2 = mu1.2 , mu1.i = mu1.i , dmu1.i=mu1.2-mu1.i,
    bse.mu2.1=apply(bt1$mu2.i,1,sd), bse.mu2.1=apply(bt1$mu2.1,1,sd),
    bse.dmu2.i=apply(bt1$mu2.1-bt1$mu2.i,1,sd),
    bse.mu1.2=apply(bt1$mu1.2,1,sd), bse.mu1.i=apply(bt1$mu1.i,1,sd),
    bse.dmu1.i=apply(bt1$mu1.2-bt1$mu1.i,1,sd)
    )

We then look at the test for overall dependence in the different time-points. We here have no suggestion of dependence.

tt  <- BCoutput$dmugi/BCoutput$bse.dmugi
cbind(times,2*(1-pnorm(abs(tt))))

We can also take out the specific components for whether \(N_1\) is predictive for \(N_2\) and vice versa. We here have no suggestion of dependence.

t21  <- BCoutput$dmu1.i/BCoutput$bse.dmu1.i 
t12  <- BCoutput$dmu2.i/BCoutput$bse.dmu2.i 
cbind(times,2*(1-pnorm(abs(t21))),2*(1-pnorm(abs(t12))))

We finally plot the boostrap samples

par(mfrow=c(1,2))
matplot(BCoutput$bt1$time,BCoutput$bt1$EN1N2,type="l",lwd=0.3)
matplot(BCoutput$bt1$time,BCoutput$bt1$EIN1N2,type="l",lwd=0.3)

Looking at other simulations with dependence

Using the normally distributed random effects we plot 4 different settings. We have variance \(0.5\) for all random effects and change the correlation. We let the correlation between the random effect associated with \(N_1\) and \(N_2\) be denoted \(\rho_{12}\) and the correlation between the random effects associated between \(N_j\) and \(D\) the terminal event be denoted as \(\rho_{j3}\), and organize all correlation in a vector \(\rho=(\rho_{12},\rho_{13},\rho_{23})\).

  data(base1cumhaz)
  data(base4cumhaz)
  data(drcumhaz)
  dr <- drcumhaz
  base1 <- base1cumhaz
  base4 <- base4cumhaz

  par(mfrow=c(1,3))
  var.z <- c(0.5,0.5,0.5)
  # death related to  both causes in same way 
  cor.mat <- corM <- rbind(c(1.0, 0.0, 0.0), c(0.0, 1.0, 0.0), c(0.0, 0.0, 1.0))
  rr <- simRecurrentII(200,base1,base4,death.cumhaz=dr,var.z=var.z,cor.mat=cor.mat,dependence=2)
  rr <- count.history(rr,types=1:2)
  cor(attr(rr,"z"))
  coo <- covarianceRecurrent(rr,1,2,status="status",start="entry",stop="time")
  plot(coo,main ="Scenario I")
  var.z <- c(0.5,0.5,0.5)
  # death related to  both causes in same way 
  cor.mat <- corM <- rbind(c(1.0, 0.0, 0.5), c(0.0, 1.0, 0.5), c(0.5, 0.5, 1.0))
  rr <- simRecurrentII(200,base1,base4,death.cumhaz=dr,var.z=var.z,cor.mat=cor.mat,dependence=2)
  rr <- count.history(rr,types=1:2)
  coo <- covarianceRecurrent(rr,1,2,status="status",start="entry",stop="time")
  par(mfrow=c(1,3))
  plot(coo,main ="Scenario II")
  var.z <- c(0.5,0.5,0.5)
  # positive dependence for N1 and N2 all related in same way
  cor.mat <- corM <- rbind(c(1.0, 0.5, 0.5), c(0.5, 1.0, 0.5), c(0.5, 0.5, 1.0))
  rr <- simRecurrentII(200,base1,base4,death.cumhaz=dr,var.z=var.z,cor.mat=cor.mat,dependence=2)
  rr <- count.history(rr,types=1:2)
  coo <- covarianceRecurrent(rr,1,2,status="status",start="entry",stop="time")
  par(mfrow=c(1,3))
  plot(coo,main="Scenario III")
  var.z <- c(0.5,0.5,0.5)
  # negative dependence for N1 and N2 all related in same way
  cor.mat <- corM <- rbind(c(1.0, -0.4, 0.5), c(-0.4, 1.0, 0.5), c(0.5, 0.5, 1.0))
  rr <- simRecurrentII(200,base1,base4,death.cumhaz=dr,var.z=var.z,cor.mat=cor.mat,dependence=2)
  rr <- count.history(rr,types=1:2)
  coo <- covarianceRecurrent(rr,1,2,status="status",start="entry",stop="time")
  par(mfrow=c(1,3))
  plot(coo,main="Scenario IV")

SessionInfo

sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-apple-darwin21.5.0 (64-bit)
#> Running under: macOS Monterey 12.5.1
#> 
#> Matrix products: default
#> BLAS:   /usr/local/Cellar/openblas/0.3.21/lib/libopenblasp-r0.3.21.dylib
#> LAPACK: /usr/local/Cellar/r/4.2.1_2/lib/R/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] splines   stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#> [1] ggplot2_3.3.5  cowplot_1.1.1  mets_1.3.0     lava_1.7.0     timereg_2.0.2 
#> [6] survival_3.3-1
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.9          mvtnorm_1.1-3       lattice_0.20-45    
#>  [4] listenv_0.8.0       assertthat_0.2.1    digest_0.6.29      
#>  [7] utf8_1.2.2          parallelly_1.32.1   R6_2.5.1           
#> [10] evaluate_0.15       highr_0.9           pillar_1.7.0       
#> [13] rlang_1.0.2         jquerylib_0.1.4     Matrix_1.4-1       
#> [16] rmarkdown_2.13      labeling_0.4.2      stringr_1.4.0      
#> [19] munsell_0.5.0       compiler_4.2.1      numDeriv_2016.8-1.1
#> [22] xfun_0.30           pkgconfig_2.0.3     globals_0.16.1     
#> [25] htmltools_0.5.2     tidyselect_1.1.2    tibble_3.1.6       
#> [28] codetools_0.2-18    fansi_1.0.2         future_1.27.0      
#> [31] crayon_1.5.0        ucminf_1.1-4        dplyr_1.0.8        
#> [34] withr_2.5.0         MASS_7.3-57         grid_4.2.1         
#> [37] jsonlite_1.8.0      gtable_0.3.0        lifecycle_1.0.1    
#> [40] DBI_1.1.2           magrittr_2.0.2      scales_1.1.1       
#> [43] future.apply_1.9.0  cli_3.3.0           stringi_1.7.6      
#> [46] farver_2.1.0        bslib_0.3.1         ellipsis_0.3.2     
#> [49] vctrs_0.3.8         generics_0.1.2      tools_4.2.1        
#> [52] glue_1.6.2          purrr_0.3.4         parallel_4.2.1     
#> [55] fastmap_1.1.0       yaml_2.3.5          colorspace_2.0-3   
#> [58] isoband_0.2.5       knitr_1.37          sass_0.4.0