[f2e496]: / tests / testthat / test-fromParametersSystem.R

Download this file

318 lines (208 with data), 11.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
context("fromParam_systemlevel")
test_that("lag.should.equal.non.lag.in.these.cases",{
l1 <- LagEffect(Lag.T=5,L.Ctr.median=3,L.HazardRatio=0.8)
l2 <- LagEffect(Lag.T=20,L.Ctr.median=3,L.HazardRatio=0.8)
s1 <- Study(N=1000,study.duration=10,ctrl.median=3,k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.8,two.sided=TRUE,
lag.settings=l1)
s1.nolag <- Study(N=1000,study.duration=10,ctrl.median=3,k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.8,two.sided=TRUE)
s2 <- Study(N=1000,study.duration=10,ctrl.median=6,k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.75,two.sided=TRUE,
lag.settings=l2)
a1 <- predict(s1.nolag,time.pred=c(5,8,10),event.pred=c(10,15))
a2 <- predict(s1,time.pred=c(5,8,10),event.pred=c(10,15))
a3 <- predict(s2,time.pred=c(5,8,10),event.pred=c(10,15))
#remove atrisk columns for check:
p1 <- a1@predict.data
p2 <- a2@predict.data
p1$at.risk1 <- NULL
p1$at.risk2 <- NULL
p1$atrisk.tot <- NULL
p2$at.risk1 <- NULL
p2$at.risk2 <- NULL
p2$atrisk.tot <- NULL
expect_equal(p1,p2)
#Then check atrisk columns
expect_equal(a1@predict.data$at.risk1,a2@predict.data$at.risk1,tol=1e-7)
expect_equal(a1@predict.data$at.risk2,a2@predict.data$at.risk2,tol=1e-7)
expect_equal(a1@predict.data$atrisk.tot,a2@predict.data$atrisk.tot,tol=1e-7)
expect_equal(a1@grid,a2@grid)
expect_equal(a1@av.hr,a2@av.hr)
expect_equal(a1@critical.events.req,a2@critical.events.req)
expect_equal(a1@critical.data$at.risk1,a2@critical.data$at.risk1,tol=1e-7)
expect_equal(a1@critical.data$at.risk2,a2@critical.data$at.risk2,tol=1e-7)
expect_equal(a1@critical.data$atrisk.tot,a2@critical.data$atrisk.tot,tol=1e-7)
expect_equal(a1@sfns[[1]]@lambda,a2@sfns[[1]]@lambda)
expect_equal(a1@sfns[[2]]@lambda,a2@sfns[[2]]@lambda)
expect_equal(a2@sfns[[1]]@lambda,a2@sfns[[1]]@lambdaot)
expect_equal(a2@sfns[[2]]@lambda,a2@sfns[[2]]@lambdaot)
#remove atrisk columns for check:
p1 <- a1@predict.data
p2 <- a3@predict.data
p1$at.risk1 <- NULL
p1$at.risk2 <- NULL
p1$atrisk.tot <- NULL
p2$at.risk1 <- NULL
p2$at.risk2 <- NULL
p2$atrisk.tot <- NULL
expect_equal(p1,p2)
#Then check atrisk columns
expect_equal(a1@predict.data$at.risk1,a3@predict.data$at.risk1,tol=1e-7)
expect_equal(a1@predict.data$at.risk2,a3@predict.data$at.risk2,tol=1e-7)
expect_equal(a1@predict.data$atrisk.tot,a3@predict.data$atrisk.tot,tol=1e-7)
expect_equal(a1@grid,a3@grid)
expect_equal(a1@av.hr,a3@av.hr)
expect_equal(a1@critical.events.req,a3@critical.events.req)
expect_equal(a1@critical.data,a3@critical.data)
expect_equal(a1@sfns[[1]]@lambda,a3@sfns[[1]]@lambdaot)
expect_equal(a1@sfns[[2]]@lambda,a3@sfns[[2]]@lambdaot)
})
test_that("event.and.time.pred.have.symmetry",{
#Note due to rounding of events the 'symmetry' only works one way...
s1 <- Study(N=1000,study.duration=10,ctrl.median=3,k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.8,two.sided=TRUE)
a1 <- predict(s1,event.pred=c(100,200,300,400))
time.pred <- a1@predict.data[,"time"]
a2 <- predict(s1,time.pred=time.pred)
expect_true(all(a2@predict.data[,"time.pred"]))
expect_false(any(a1@predict.data[,"time.pred"]))
a1@predict.data$time.pred <- rep(TRUE,4)
expect_equal(a1@predict.data,a2@predict.data)
})
test_that("recruit",{
k <- c(0.5,1,2)
lapply(k,function(x){
s <- SingleArmStudy(N=1000,study.duration=10,ctrl.median=3,k=x,acc.period=5,shape=1.2)
a <- predict(s,time.pred=5/(2^(1/x)))
expect_equal(1/(2^x)*1000,a@grid[a@grid$time==2.5,]$recruit.tot)
expect_equal(500,a@predict.data[1,]$recruit.tot)
})
lapply(k,function(x){
s <- Study(N=1000,study.duration=10,ctrl.median=3,k=x,acc.period=6,
shape=1.2,power=0.8,alpha=0.05,two.sided=TRUE,r=1.2,HR=0.8)
a <- predict(s,time.pred=6/(2^(1/x)))
expect_equal(1/(2^x)*1000,a@grid[a@grid$time==3,]$recruit.tot)
expect_equal(500,a@predict.data[1,]$recruit.tot)
})
})
#this fails on versions < 1.0
test_that("max.num.events",{
l <- LagEffect(Lag.T=5,L.Ctr.median=1,L.HazardRatio=0.5)
s <- Study(N=800,study.duration=36,ctrl.median=3,k=1,acc.period=20,
shape=1,power=0.8,alpha=0.05,two.sided=TRUE,r=1.2,HR=0.5,lag.settings=l)
a <- predict(s)
expect_true(all(a@grid$events.tot <= 800))
})
test_that("median",{
ctrl.median <- c(1:5)
lapply(ctrl.median,function(x){
s1 <- SingleArmStudy(N=2000,study.duration=20,ctrl.median=x,k=0.01,acc.period=0.000001,shape=1.5)
a <- predict(s1,event.pred=1000)
expect_equal(x,a@predict.data[1,]$time)
})
})
test_that("single.arm.matches.2.arm",{
s1 <- Study(N=2000,study.duration=10,ctrl.median=3,k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.8,two.sided=TRUE)
s2 <- SingleArmStudy(N=1000,study.duration=10,ctrl.median=3,k=1.2,acc.period=5,shape=2)
s3 <- Study(N=2000,study.duration=10,ctrl.median=3*sqrt(0.8),k=1.2,acc.period=5,shape=2,
power=0.9,alpha=0.05,r=1,HR=0.8,two.sided=TRUE)
a1 <- predict(s1)
a2 <- predict(s2)
a3 <- predict(s3)
expect_equal(a1@grid$events1,a2@grid$events.tot)
expect_equal(a3@grid$events2,a2@grid$events.tot)
})
str1 <- "800 patients recruited, ratio nE/nC=1, 20 months accrual (non-uniform\naccrual, k=2). Lag time: T=4 months, Control for [0,T] median=3 months and\nfor [T,S] Control median=2 months. Exponential survival function.\nHR([0,T])=1 and HR([T,S])=0.5, which gives an average HR=0.77. For a study\nwith no lag and this HR: critical HR value=0.83, alpha(2-sided)=5%,\npower=80%, 466 events required and using the given lag settings: expected\nat time 18.9 months (Experimental/Control: 225/241)."
str2 <- "120 patients recruited, ratio nE/nC=0.5, 20 months accrual (uniform\naccrual, k=1). Control median=2.5 months (lambda=0.29). Experimental\nmedian=4.11 months (lambda=0.18). Weibull survival function shape=1.2.\nHR(Experimental:Control)=0.55, critical HR value=0.71, alpha(1-sided)=5%,\npower=90%, 108 events required expected at time 22.6 months\n(Experimental/Control: 33/75). At (10, 3.2) months the predicted number of\nevents is (37, 5) [Experimental/Control: (10, 1)/(27, 4)]."
str3 <- "100 patients recruited, 5 months accrual (non-uniform accrual, k=1.5). Lag\ntime: T=4 months, Control for [0,T] median=2.5 months and for [T,S]\nControl median=3 months. Weibull survival function shape=2. At 10 months\nthe predicted number of events is 97."
summary_check <- function(study,time.pred,event.pred,stringoutput){
prediction <- predict(study,time.pred=time.pred, event.pred=event.pred)
x <- eventPrediction:::getFromParameterText(prediction,DisplayOptions(text.width=75))
expect_equal(x,stringoutput)
}
test_that("summary_output",{
lagged <- LagEffect(Lag.T = 4,L.Ctr.median = 3,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.8,
HR = 0.5, r = 1,N = 800,
study.duration = 30,ctrl.median = 2,
k = 2,acc.period = 20,two.sided = TRUE,lag.settings=lagged)
summary_check(study,NULL,NULL,str1)
study <- Study(alpha = 0.05,power = 0.9,
HR = 0.55, r = 0.5,N = 120,
study.duration = 25,ctrl.median = 2.5,
k = 1,acc.period = 20,two.sided = FALSE,shape=1.2)
summary_check(study,10,5,str2)
lagged <- LagEffect(Lag.T = 4,L.Ctr.median = 2.5)
study <- SingleArmStudy(N=100,study.duration=10,ctrl.median=3,k=1.5,
acc.period=5,shape=2,lag.settings=lagged)
summary_check(study,10,NULL,str3)
})
test_that("critical_hr",{
# this compares to the output from non-proportional hazards package
#there are slight differences in the rounding
lagged <- LagEffect(Lag.T = 4,L.Ctr.median = 11,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.8036576,
HR = 0.6, r =1,N = 780,
study.duration = 25.5,ctrl.median = 11,
k = 2,acc.period = 18,two.sided = TRUE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.8198656
expect_equal(nph.answer,prediction@critical.HR,tol=5e-3)
lagged <- LagEffect(Lag.T = 6,L.Ctr.median = 8,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.9829889,
HR = 0.5, r =1.5,N = 800,
study.duration = 30,ctrl.median = 8,
k = 0.5,acc.period = 18,two.sided = TRUE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.8534732
expect_equal(nph.answer,prediction@critical.HR,tol=5e-3)
lagged <- LagEffect(Lag.T = 5,L.Ctr.median = 8,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.9989243,
HR = 0.5, r =1.5,N = 800,
study.duration = 30,ctrl.median = 8,
k = 0.5,acc.period = 6,two.sided = TRUE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.8572458
expect_equal(nph.answer,prediction@critical.HR,tol=5e-3)
})
test_that("average hazard ratio",{
# this compares to the output from non-proportional hazards package
#results do not match exactly: nph uses interval bisection to some tolerance
#whereas eventPrediction calculates the exact integral and is accruate up to
#R's integrate functions accruacy
#Also there are slight differences in the rounding of w1 and w2 (see eventPrediction vignette)
#which will cause small differences to the average HR
lagged <- LagEffect(Lag.T = 4,L.Ctr.median = 11,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.9,
HR = 0.6, r =1,N = 780,
study.duration = 25.5,ctrl.median = 11,
k = 2,acc.period = 18,two.sided = FALSE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.7518394
expect_equal(nph.answer,prediction@av.hr,tol=1.1e-3)
lagged <- LagEffect(Lag.T = 6,L.Ctr.median = 8,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.9,
HR = 0.5, r =1.5,N = 800,
study.duration = 30,ctrl.median = 8,
k = 0.5,acc.period = 18,two.sided = FALSE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.7190647
expect_equal(nph.answer,prediction@av.hr,tol=1.1e-3)
lagged <- LagEffect(Lag.T = 5,L.Ctr.median = 8,
L.HazardRatio=1 )
study <- Study(alpha = 0.05,power = 0.5,
HR = 0.75, r =1.5,N = 800,
study.duration = 30,ctrl.median = 8,
k = 0.5,acc.period = 6,two.sided = FALSE,shape=1,lag.settings=lagged)
prediction <- predict(study)
nph.answer <- 0.8417572
expect_equal(nph.answer,prediction@av.hr,tol=1.1e-3)
})