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

Download this file

129 lines (85 with data), 4.3 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
context("singlesimdetails")
test_that("simplecase",{
data(event.data)
data <- EventData(data=event.data,
subject="subject", rand.date="randDate",
has.event="hasEvent", withdrawn="withdrawn",
time="time", site="site")
my.fit <- fit(data)
res <- simulate(my.fit,Nsim=500,seed=123)
ssD <- res@singleSimDetails
#Corret dimensions
expect_equal(500,ncol(ssD@event.type))
expect_equal(979,nrow(ssD@event.times))
#No dropouts or withdrawn
expect_true(all(ssD@event.type==0))
#First 699 rows are subjects who had events
expect_equal(1,length(unique(ssD@event.times[1,])))
expect_equal(1,length(unique(ssD@event.times[698,])))
expect_equal(1,length(unique(ssD@event.times[699,])))
expect_equal(ssD@event.times[1:699,1],as.numeric(LastDate(data@subject.data)[1:699],origin="1970-01-01") )
#All simulated event times are after Last Date for other subjects
expect_true(all(ssD@event.times[700:979,1] >= as.numeric(LastDate(data@subject.data)[700:979],origin="1970-01-01") ))
expect_true(all(ssD@event.times[700:979,41] >= as.numeric(LastDate(data@subject.data)[700:979],origin="1970-01-01") ))
expect_true(all(ssD@event.times[700:979,401] >= as.numeric(LastDate(data@subject.data)[700:979],origin="1970-01-01") ))
expect_true(all(ssD@event.times[700:979,500] >= as.numeric(LastDate(data@subject.data)[700:979],origin="1970-01-01") ))
})
test_that("subject_withdrawn_and_accrual",{
data(event.data)
e <- event.data
e$hasEvent[1:4] <- 0
e$withdrawn[1:4] <- 1
data <- EventData(data=e,
subject="subject", rand.date="randDate",
has.event="hasEvent", withdrawn="withdrawn",
time="time", site="site")
my.fit <- fit(data)
Naccrual <- 21
my.accrual <- Generate.PoissonAccrual("2015-11-29",rate=1)
res <- simulate(my.fit,Nsim=500,seed=123,Naccrual=Naccrual,accrualGenerator=my.accrual)
ssD <- res@singleSimDetails
#Corret dimensions
expect_equal(500,ncol(ssD@event.type))
expect_equal(1000,nrow(ssD@event.times))
#correct withdrawn
expect_true(all(ssD@event.type[5:1000,]==0))
expect_true(all(ssD@event.type[1:4,]==1))
})
test_that("fixed_follow_up",{
data(event.data)
e <- event.data
expect_warning(data <- EventData(data=e,
subject="subject", rand.date="randDate",
has.event="hasEvent", withdrawn="withdrawn",
time="time", site="site",followup=50))
my.fit <- fit(data)
res <- simulate(my.fit,Nsim=500,seed=123)
ssD <- res@singleSimDetails
timematrix <- matrix(rep(as.numeric(data@subject.data$rand.date,origin="1970-01-01"),500),ncol=500)
timematrix <- ssD@event.times - timematrix + 1
expect_true(all(ssD@event.type[700:979,]==2 | timematrix[700:979,]<=50 ))
expect_true(all(ssD@event.type[700:979,]==0 | timematrix[700:979,]==50 ))
})
test_that("dropout",{
my.data <- EmptyEventData()
Naccrual <- 1000
f <- function(N){
return(rep(as.Date("2015-01-01"),N))
}
my.a <- new("AccrualGenerator",f=f,model="temp",text="temp")
#small enough rate so no events with this seed
res <- simulate(data=my.data,Nsim=500,seed=123,Naccrual=Naccrual,accrualGenerator=my.a,
SimParams=FromDataParam(type="weibull",rate=0.00000002,shape=1),
dropout=list(proportion=0.5,time=10))
ssD <- res@singleSimDetails
expect_true(all(ssD@event.type==1))
})
test_that("calculate.at.risk",{
rec.times <- matrix(c(rep(16436,4),rep(16500,4),17000,17025,17050,17025),nrow=3,byrow=TRUE)
event.times <- matrix(c(rep(16750,4),17000,16750,16750,17250,18000,17400,17600,17125),nrow=3,byrow=TRUE)
event.type <- matrix(c(rep(0,4),1,2,0,0,1,2,0,0),nrow=3,byrow=TRUE)
ssD <- SingleSimDetails(event.type,event.times,rec.times)
retVal <- CalculateDaysAtRisk(ssD,c(16000,16436,16437,16500,16750,16751,17020,17030,18000,20000))
answer <- c(0,1,2,66,566,566.5,701,709.5,1142,1142)
expect_equal(retVal,answer)
})