Skip to content

Commit dd234a0

Browse files
Merge pull request #308 from Merck/larry-leon-patch-1
Add vignette on potential discrepancies between simtrial and survdiff
2 parents 13d7fa9 + 97a581a commit dd234a0

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ articles:
9292
- maxcombo
9393
- rmst
9494
- parallel
95+
- discrepancy-between-simtrial-and-survival
9596
- title: "NPH distribution approximations"
9697
contents:
9798
- arbitrary-hazard
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
---
2+
title: "Note on potential discrepancies between simtrial and survdiff"
3+
author: "Larry Leon"
4+
output: rmarkdown::html_vignette
5+
vignette: >
6+
%\VignetteIndexEntry{Note on potential discrepancies between simtrial and survdiff}
7+
%\VignetteEngine{knitr::rmarkdown}
8+
---
9+
10+
## Overview
11+
12+
```{r, message=FALSE}
13+
library(gsDesign)
14+
library(gsDesign2)
15+
library(dplyr)
16+
library(tibble)
17+
library(gt)
18+
#library(ggplot2)
19+
#library(cowplot)
20+
library(simtrial)
21+
library(tidyr)
22+
#library(future.batchtools)
23+
#library(doFuture)
24+
#library(foreach)
25+
26+
#library(tictoc)
27+
28+
library(survival)
29+
30+
31+
```
32+
33+
In the **survival** (base R) package the log-rank and Cox estimation procedures apply (by default) a correction to "fix" roundoff errors. These are implemented with the *timefix* option (by default *timefix = TRUE*) via the *aeqSurv* function.
34+
However in the **simtrial** package (and also **Hmisc**) such a correction is not implemented; Consequently, there can be discrepancies between **simtrial** and base R *survival* (*survdiff*, *coxph*, and *survfit*).
35+
36+
For details on the *aeqSurv* function see [Therneau, 2016](https://cran.r-project.org/web/packages/survival/vignettes/tiedtimes.pdf) and [R documentation, version 3.803](https://www.rdocumentation.org/packages/survival/versions/3.8-3/topics/aeqSurv)
37+
38+
In the following we describe a simulation scenario where a discrepancy is generated and illustrate how discrepancies can be resolved (if desired) by pre-processing survival times with *aeqSurv* and thus replicating *survdiff* and *coxph* default calculations.
39+
40+
In the simulated dataset two observations are generated:
41+
42+
- Observation $i=464$ with survival time $Y=0.306132722582$
43+
- Observation $i=516$ with survival time $Y=0.306132604679$
44+
- Per "aeqSurv" these times are tied and set to $Y=0.306132604679$
45+
- The log-rank and Cox estimates can therefore differ between other approaches without the "timefix" correction
46+
47+
48+
## Scenario definitions
49+
50+
We define various true data generating model scenarios and convert for use in **gsDesign2**. Here we are using a single scenario where discrepancies were found. This is just for illustration to inform the user of **simtrial** that discrepancies can occur and how to resolve via *aeqSurv*, if desired.
51+
52+
53+
```{r}
54+
survival_at_24_months <- 0.35
55+
hr <- log(.35)/log(.25)
56+
control_median <- 12
57+
control_rate <- c(log(2) / control_median, (log(.25) - log(.2)) / 12)
58+
59+
scenarios <- tribble(
60+
~Scenario, ~Name, ~Period, ~duration, ~Survival,
61+
0, "Control", 0, 0, 1,
62+
0, "Control", 1, 24, .25,
63+
0, "Control", 2, 12, .2,
64+
1, "PH", 0, 0, 1,
65+
1, "PH", 1, 24, .35,
66+
1, "PH", 2, 12, .2^hr,
67+
2, "3-month delay", 0, 0, 1,
68+
2, "3-month delay", 1, 3, exp(-3 * control_rate[1]),
69+
2, "3-month delay", 2, 21, .35,
70+
2, "3-month delay", 3, 12, .2^hr,
71+
3, "6-month delay", 0, 0, 1,
72+
3, "6-month delay", 1, 6, exp(-6 * control_rate[1]),
73+
3, "6-month delay", 2, 18, .35,
74+
3, "6-month delay", 3, 12, .2^hr,
75+
4, "Crossing", 0, 0, 1,
76+
4, "Crossing", 1, 3, exp(-3 * control_rate[1] * 1.3),
77+
4, "Crossing", 2, 21, .35,
78+
4, "Crossing", 3, 12, .2^hr,
79+
5, "Weak null", 0, 0, 1,
80+
5, "Weak null", 1, 24, .25,
81+
5, "Weak null", 2, 12, .2,
82+
6, "Strong null", 0, 0, 1,
83+
6, "Strong null", 1, 3, exp(-3 * control_rate[1] * 1.5),
84+
6, "Strong null", 2, 3, exp(-6 * control_rate[1]),
85+
6, "Strong null", 3, 18, .25,
86+
6, "Strong null", 4, 12, .2,
87+
)
88+
# scenarios |> gt()
89+
```
90+
91+
92+
```{r}
93+
fr <-
94+
scenarios |>
95+
group_by(Scenario) |>
96+
# filter(Scenario == 2) |>
97+
mutate(Month = cumsum(duration),
98+
x_rate = -(log(Survival) - log(lag(Survival, default = 1))) /
99+
duration,
100+
rate = ifelse(Month > 24, control_rate[2], control_rate[1]),
101+
hr = x_rate / rate) |>
102+
select(-x_rate) |>
103+
filter(Period > 0, Scenario > 0) |> ungroup()
104+
#fr |> gt() |> fmt_number(columns = everything(), decimals = 2)
105+
106+
fr <- fr |> mutate(fail_rate = rate, dropout_rate =0.001, stratum = "All")
107+
108+
109+
# MWLR
110+
mwlr <- fixed_design_mb(
111+
tau = 12,
112+
enroll_rate = define_enroll_rate(duration = 12, rate = 1),
113+
fail_rate = fr |> filter(Scenario == 2),
114+
alpha = 0.025, power = .85, ratio = 1,
115+
study_duration = 36
116+
) |> to_integer()
117+
118+
119+
er <- mwlr$enroll_rate
120+
121+
```
122+
123+
# A scenario that generates a discrepancy
124+
125+
126+
```{r}
127+
128+
set.seed(3219)
129+
130+
dgm <- fr[c(14:17),]
131+
132+
fail_rate <- data.frame(stratum = rep("All", 2 * nrow(dgm)),
133+
period = rep(dgm$Period, 2),
134+
treatment = c(rep("control", nrow(dgm)),
135+
rep("experimental", nrow(dgm))),
136+
duration = rep(dgm$duration, 2),
137+
rate = c(dgm$rate, dgm$rate * dgm$hr)
138+
)
139+
140+
dgm$stratum <- "All"
141+
# Constant dropout rate for both treatment arms and all scenarios
142+
dropout_rate <- data.frame(stratum = rep("All", 2),
143+
period = rep(1, 2),
144+
treatment = c("control", "experimental"),
145+
duration = rep(100, 2),
146+
rate = rep(.001, 2)
147+
)
148+
```
149+
150+
151+
Simulated dataset with discrepancy between logrank test of *wlr* (**simtrial**) and *survdiff* (also compare to score test of *coxph* [same as survdiff with default *timefix=TRUE*])
152+
153+
```{r}
154+
155+
ss <- 395
156+
157+
set.seed(8316951+ss*1000)
158+
159+
# Generate a dataset
160+
dat <- sim_pw_surv(n = 698, enroll_rate = er,
161+
fail_rate = fail_rate, dropout_rate = dropout_rate)
162+
163+
analysis_data <- cut_data_by_date(dat, 36)
164+
165+
dfa <- analysis_data
166+
167+
dfa$treat <- ifelse(dfa$treatment=="experimental",1,0)
168+
169+
z1 <- dfa |> wlr(weight=fh(rho=0,gamma=0))
170+
171+
check <- survdiff(Surv(tte,event)~ treat, data=dfa)
172+
173+
# Note, for coxph use
174+
#cph.score <- summary(coxph(Surv(tte,event)~ treat, data=dfa, control=coxph.control(timefix=TRUE)))$sctest
175+
176+
cat("Log-rank wlr() vs survdiff()",c(z1$z^2,check$chisq),"\n")
177+
```
178+
179+
Verify that *timefix=FALSE* in *coxph* agrees with *wlr*
180+
181+
```{r}
182+
cph.score <- summary(coxph(Surv(tte,event)~ treat, data=dfa, control=coxph.control(timefix=FALSE)))$sctest
183+
cat("Log-rank wlr() vs Cox score z^2",c(z1$z^2,cph.score["test"]),"\n")
184+
```
185+
186+
187+
Pre-processing survival times with *aeqSurv* to implement *timefix=TRUE* procedure.
188+
189+
Verify *wlr* and *survdiff* now agree.
190+
191+
```{r}
192+
Y <- dfa[,"tte"]
193+
Delta <- dfa[,"event"]
194+
195+
tfixed <- aeqSurv(Surv(Y,Delta))
196+
Y<- tfixed[,"time"]
197+
Delta <- tfixed[,"status"]
198+
# Use aeqSurv version
199+
dfa$tte2 <- Y
200+
dfa$event2 <- Delta
201+
202+
# wlr() after "timefix"
203+
dfa2 <- dfa
204+
dfa2$tte <- dfa2$tte2
205+
dfa2$event <- dfa2$event2
206+
z1new <- dfa2 |> wlr(weight=fh(rho=0,gamma=0))
207+
cat("Log-rank wlr() with timefix vs survdiff() z^2",c(z1new$z^2,check$chisq),"\n")
208+
209+
```
210+
211+
212+
Where do they differ (tte2 are times after *aeqSurv*) ?
213+
214+
```{r}
215+
216+
dfa <- dfa[order(dfa$tte2),]
217+
218+
id <- seq(1,nrow(dfa))
219+
220+
diff <- exp(dfa$tte) - exp(dfa$tte2)
221+
id_diff <- which(abs(diff)>0)
222+
223+
tolook <- seq(id_diff-2,id_diff+2)
224+
225+
dfcheck <- dfa[tolook,c("tte","tte2","event","event2","treatment")]
226+
print(dfcheck,digits=12)
227+
```
228+
229+
230+
231+
232+
Verify *coxph* (default) and *coxph* with aeqSurv pre-processing (using tte2 as outcome and setting *timefix=FALSE*) are identical:
233+
234+
Also note that here ties do not have impact because in separate arms
235+
236+
```{r}
237+
# Check Cox with ties
238+
cox_breslow <- summary(coxph(Surv(tte,event)~treatment,data=dfa,ties="breslow"))$conf.int
239+
cox_efron <- summary(coxph(Surv(tte,event)~treatment,data=dfa,ties="efron"))$conf.int
240+
cat("Cox Breslow and Efron hr (tte, timefix=TRUE):",c(cox_breslow[1],cox_efron[1]),"\n")
241+
242+
# Here ties do not have impact because in separate arms
243+
cox_breslow <- summary(coxph(Surv(tte2,event2)~treatment,data=dfa,ties="breslow", control=coxph.control(timefix=FALSE)))$conf.int
244+
cox_efron <- summary(coxph(Surv(tte2,event2)~treatment,data=dfa,ties="efron", control=coxph.control(timefix=FALSE)))$conf.int
245+
cat("Cox Breslow and Efron hr (tte2, timefix=FALSE):",c(cox_breslow[1],cox_efron[1]),"\n")
246+
```
247+
248+
**So here there is a difference between tte and tte2 times, but there is not an impact of ties for Cox between *breslow* and *efron* because the ties (single tie in tte2) are in separate arms**.
249+
250+
Lastly, artificially change treatment so that two observations are tied within the same treatment arm which generates difference between *breslow* and *efron* options for ties:
251+
252+
253+
```{r}
254+
# Create tie within treatment arm by changing treatment
255+
dfa3 <- dfa
256+
dfa3[19,"treat"] <- 1.0
257+
258+
cox_breslow <- summary(coxph(Surv(tte,event)~treat, data=dfa3,ties="breslow", control=coxph.control(timefix=TRUE)))$conf.int
259+
cox_efron <- summary(coxph(Surv(tte,event)~treat, data=dfa3,ties="efron", control=coxph.control(timefix=TRUE)))$conf.int
260+
cat("Cox Breslow and Efron hr (tte, timefix=TRUE)=",c(cox_breslow[1],cox_efron[1]),"\n")
261+
262+
```
263+
264+
265+
Same as
266+
267+
```{r}
268+
269+
cox_breslow <- summary(coxph(Surv(tte2,event2)~treat, data=dfa3,ties="breslow", control=coxph.control(timefix=FALSE)))$conf.int
270+
cox_efron <- summary(coxph(Surv(tte2,event2)~treat, data=dfa3,ties="efron", control=coxph.control(timefix=FALSE)))$conf.int
271+
cat("Cox Breslow and Efron hr (tte2, timefix=FALSE)=",c(cox_breslow[1],cox_efron[1]),"\n")
272+
273+
```
274+

0 commit comments

Comments
 (0)