
R Under development (unstable) (2023-03-05 r83940) -- "Unsuffered Consequences"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> # Tests of the Brier score.
> #  Start with the example in the vignette
> library(survival)
> 
> rott2 <- rotterdam
> ignore <- with(rott2, recur ==0 & death==1 & rtime < dtime)
> rott2$rfs <- with(rott2, ifelse(recur==1 | ignore, recur, death))
> rott2$rfstime <- with(rott2, ifelse(recur==1 | ignore, rtime, dtime))/365.25
> 
> rsurv <- survfit(Surv(rfstime, rfs) ~1, rott2)  #KM
> rfit <- coxph(Surv(rfstime, rfs) ~ pspline(age) + meno + size + pmin(nodes,12),
+               rott2)
> 
> tau <- c(2,4,6, 8)  # four tau values
> bfit <- brier(rfit, times=tau)
> 
> # Now by hand
> wtmat <- rttright(Surv(rfstime, rfs) ~ 1, rott2, times=tau)
> psurv <- survfit(rfit, newdata= rott2) # one curve per subject
> yhat  <- 1- summary(psurv, times=tau)$surv
> ybar  <- 1- summary(rsurv, times=tau)$surv
> 
> y <- with(rott2, cbind(rfstime <=tau[1] & rfs==1,
+                        rfstime <=tau[2] & rfs==1,
+                        rfstime <=tau[3] & rfs==1,
+                        rfstime <=tau[4] & rfs==1)) * 1L
> ss1 <- colSums(wtmat * (y - t(yhat))^2)
> ss2 <- colSums(wtmat * (y - rep(ybar, each=nrow(y)))^2)
> 
> all.equal(unname(1- ss1/ss2), bfit$rsquare)
[1] TRUE
> all.equal(unname(ss1), bfit$brier)
[1] TRUE
> 
> proc.time()
   user  system elapsed 
  8.134   0.738   8.871 
