Skip to content

Commit cdc0494

Browse files
committed
15-dgp-dqcp: extend survival curves with flat KM tail to match website
Port the tmax handling from the cvxr_docs survival-ordering example so the tutorial plot matches the website figure. grp_counts() now records the last observation time per group as a tmax attribute, and step_rc() extends each step curve flat out to that time when it exceeds the last failure time. The GP fit is unchanged; only the right-hand extent of each curve differs.
1 parent 9772a9e commit cdc0494

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

15-dgp-dqcp.qmd

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ oropharynx <- readRDS("data/oropharynx.rds")
9898
grp_counts <- function(T, N) { # discrete-hazard counts
9999
s <- subset(oropharynx, tstage == T & nstage == N)
100100
dt <- sort(unique(s$days[s$status == 1])) # distinct failure times
101-
data.frame(t = dt,
101+
out <- data.frame(t = dt,
102102
d = sapply(dt, function(x) sum(s$days == x & s$status == 1)), # failures
103103
n = sapply(dt, function(x) sum(s$days >= x))) # at risk
104+
attr(out, "tmax") <- max(s$days) # last observation, for the flat Kaplan-Meier tail
105+
out
104106
}
105107
```
106108

@@ -165,8 +167,10 @@ rc_order <- fit_rc(g1, list(c(3, 2), c(2, 1))) # S3 <= S2 <= S1
165167
166168
step_rc <- function(fit, groups, panel)
167169
do.call(rbind, lapply(seq_along(groups), function(i) {
168-
g <- groups[[i]]; S <- fit$S[[i]]
169-
data.frame(t = c(0, g$t), S = c(1, S),
170+
g <- groups[[i]]; S <- fit$S[[i]]; tmax <- attr(g, "tmax")
171+
t <- c(0, g$t); s <- c(1, S)
172+
if (tmax > max(g$t)) { t <- c(t, tmax); s <- c(s, tail(S, 1)) } # flat KM tail to last obs
173+
data.frame(t = t, S = s,
170174
grp = paste0("(T,N)=", names(groups)[i]), panel = panel)
171175
}))
172176

0 commit comments

Comments
 (0)