Skip to content

Commit 1fb09ff

Browse files
committed
updates
1 parent 4e41264 commit 1fb09ff

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

R/accuracy_measures.R

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,24 +420,20 @@ calibration_plot <- function (object, newdata, Tstart, Thoriz = NULL,
420420
pi_u_t <- preds$pred
421421
names(pi_u_t) <- preds$id
422422
pi_u_t <- pi_u_t[preds$times > Tstart]
423-
424423
id <- newdata[[id_var]]
425424
Time <- newdata[[Time_var]]
426425
event <- newdata[[event_var]]
427426
f <- factor(id, levels = unique(id))
428427
Time <- tapply(Time, f, tail, 1L)
429428
event <- tapply(event, f, tail, 1L)
430429
names(Time) <- names(event) <- as.character(unique(id))
431-
cal_DF <- data.frame(Time = Time, event = event, preds = pi_u_t[names(Time)])
430+
pi_u_t <- pi_u_t[names(Time)]
432431
cloglog <- function (x) log(-log(1.0 - x))
433-
Bounds <- quantile(cloglog(pi_u_t), probs = c(0.05, 0.95))
434-
form <- paste0("ns(cloglog(preds), df = ", df_ns,
435-
", B = c(", round(Bounds[1L], 2), ", ",
436-
round(Bounds[2L], 2), "))")
437-
form <- paste("Surv(Time, event) ~", form)
432+
cal_DF <- data.frame(Time = Time, event = event, preds = pi_u_t)
433+
form <- paste0("Surv(Time, event) ~ nsk(cloglog(preds), df = ", df_ns, ")")
438434
cal_Cox <- coxph(as.formula(form), data = cal_DF)
439435
qs <- quantile(pi_u_t, probs = c(0.01, 0.99))
440-
probs_grid <- data.frame(preds = seq(qs[1L], qs[2L], length.out = 100L))
436+
probs_grid <- data.frame(preds = seq(qs[1L], qs[2L], length.out = 101L))
441437
obs <- 1 - c(summary(survfit(cal_Cox, newdata = probs_grid), times = Thoriz)$surv)
442438
low <- 1 - c(summary(survfit(cal_Cox, newdata = probs_grid), times = Thoriz)$low)
443439
upp <- 1 - c(summary(survfit(cal_Cox, newdata = probs_grid), times = Thoriz)$upp)
@@ -465,6 +461,26 @@ calibration_plot <- function (object, newdata, Tstart, Thoriz = NULL,
465461
}
466462
}
467463

464+
if (FALSE) {
465+
cal_DF <- data.frame(Time = Time, event = event, preds = pi_u_t)
466+
cuts <- as.vector(quantile(cal_DF$Time, c(0.33, 0.66)))
467+
cal_DF <- survSplit(Surv(Time, event) ~ ., data = cal_DF, zero = Tstart,
468+
cut = cuts, episode = "grp")
469+
form <- paste0("Surv(tstart, Time, event) ~ nsk(cloglog(preds), df = ",
470+
df_ns, "):strata(grp)")
471+
cal_Cox <- coxph(as.formula(form), data = cal_DF)
472+
qs <- quantile(pi_u_t, probs = c(0.01, 0.99))
473+
probs_grid <- expand.grid(
474+
tstart = c(Tstart, cuts),
475+
event = 0,
476+
grp = 1:3,
477+
preds = seq(qs[1L], qs[2L], length.out = 4)
478+
)
479+
probs_grid$Time <- rep(c(cuts, max(cal_DF$Time)), length.out = nrow(probs_grid))
480+
obs <- 1 - c(summary(survfit(cal_Cox, newdata = probs_grid), times = Thoriz)$surv)
481+
482+
}
483+
468484
calibration_metrics <- function (object, newdata, Tstart, Thoriz = NULL,
469485
Dt = NULL, df_ns = 3, ...) {
470486
comps <- calibration_plot(object, newdata, Tstart, Thoriz = Thoriz, Dt = Dt,

0 commit comments

Comments
 (0)