|
a |
|
b/R/simulate.R |
|
|
1 |
#This file contains the simulate functions for the predict from |
|
|
2 |
#data part of the package |
|
|
3 |
|
|
|
4 |
##' @include eventModel.R |
|
|
5 |
NULL |
|
|
6 |
|
|
|
7 |
##' The simulate methods for EventPrediction package |
|
|
8 |
##' |
|
|
9 |
##' |
|
|
10 |
##' These methods are for the predict from data part of the package |
|
|
11 |
##' and simulate. All functions described here are wrappers for the |
|
|
12 |
##' missing,missing,EventData,FromDataSimParams-method |
|
|
13 |
##' |
|
|
14 |
##' See the stats::simulate method for details of the stats simulate function |
|
|
15 |
##' |
|
|
16 |
##' @name simulate |
|
|
17 |
##' @param object An \code{EventModel} object which contains both the data (slot event.data) and |
|
|
18 |
##' the simulation parameters (slot simParams). If not stats::simulate will be called |
|
|
19 |
##' @param data If used then this \code{EventData} object will be used when performing the simulations |
|
|
20 |
##' instead of the \code{EventData} object within \code{fit} |
|
|
21 |
##' @param SimParams A \code{FromDataSimParam} object, |
|
|
22 |
##' overrides the simulated parameters from the \code{EventModel} object |
|
|
23 |
##' @rdname simulate-methods |
|
|
24 |
##' @param accrualGenerator An AccrualGenerator object used for recruiting additional subjects |
|
|
25 |
##' @param Naccrual The number of additional subjects to be recruited |
|
|
26 |
##' @param Nsim Number of simulations to run |
|
|
27 |
##' @param limit Limit for the percentiles, default is 0.05 which corresponds |
|
|
28 |
##' to [0.05, 0.95] |
|
|
29 |
##' @param seed Integer for random number generator (for reproducability) By default NULL. |
|
|
30 |
##' @param longlagsettings A LongLagSettings object to control the behaviour of the algorithm for subjects whos last date |
|
|
31 |
##' is a long time from the analysis date. Using this argument can have a large impact on the results - see vignette for further details |
|
|
32 |
##' @param HR The hazard ratio: an advanced option which allows two arm trials to be simulated. This replicates the |
|
|
33 |
##' Predict from parameters functionality but uses the recruitment times found in \code{data}. See the vignette for |
|
|
34 |
##' further details |
|
|
35 |
##' @param r The allocation ratio: see \code{HR} argument. |
|
|
36 |
##' @param dropout if subjects drop out in study (due to competing risks not as there is a finite follow up time) |
|
|
37 |
##' then this argument should contain a list with proportion and time and optionally shape i.e. |
|
|
38 |
##' \code{dropout=list(proportion=0.03,time=365,shape=1.2)} meaning in the absence of events 3% of subjects |
|
|
39 |
##' will have dropped out after 365 days with a Weibull hazard rate with shape=1.2. If shape is not included then |
|
|
40 |
##' it defaults to 1 (exponential rate). If dropout is NULL then no subjects will drop out |
|
|
41 |
##' @param ... Additional arguments to be passed to the method |
|
|
42 |
##' @docType methods |
|
|
43 |
##' @return A \code{FromDataResults} object |
|
|
44 |
##' @export |
|
|
45 |
if(!isGeneric("simulate")){ |
|
|
46 |
setGeneric("simulate", function(object,data,SimParams,...) standardGeneric("simulate")) |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
##' @rdname simulate-methods |
|
|
52 |
##' @name simulate |
|
|
53 |
##' @aliases simulate,ANY,missing,missing-method |
|
|
54 |
##' @export |
|
|
55 |
setMethod("simulate",signature=c("ANY","missing","missing"), |
|
|
56 |
function(object,...){ |
|
|
57 |
stats::simulate(object=object,...) |
|
|
58 |
}) |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
##' @rdname simulate-methods |
|
|
62 |
##' @name simulate |
|
|
63 |
##' @aliases simulate,EventModel,missing,missing-method |
|
|
64 |
##' @export |
|
|
65 |
setMethod("simulate",signature=c("EventModel","missing","missing"),function(object,...){ |
|
|
66 |
simulate(data=object@event.data,SimParams=object@simParams,...) |
|
|
67 |
}) |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
##' @rdname simulate-methods |
|
|
71 |
##' @name simulate |
|
|
72 |
##' @aliases simulate,EventModel,EventData,missing-method |
|
|
73 |
##' @export |
|
|
74 |
setMethod("simulate",signature=c("EventModel","EventData","missing"),function(object,data,...){ |
|
|
75 |
if(data@followup!=object@event.data@followup){ |
|
|
76 |
warning("Model data followup does not equal EventData followup. EventData followup will be used") |
|
|
77 |
} |
|
|
78 |
simulate(data=data,SimParams=object@simParams,...) |
|
|
79 |
}) |
|
|
80 |
|
|
|
81 |
##' @rdname simulate-methods |
|
|
82 |
##' @name simulate |
|
|
83 |
##' @aliases simulate,EventModel,missing,FromDataSimParam-method |
|
|
84 |
##' @export |
|
|
85 |
setMethod("simulate",signature=c("EventModel","missing","FromDataSimParam"),function(object,SimParams,...){ |
|
|
86 |
simulate(data=object@event.data,SimParams=SimParams,...) |
|
|
87 |
}) |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
##' @rdname simulate-methods |
|
|
91 |
##' @name simulate |
|
|
92 |
##' @aliases simulate,missing,EventData,FromDataSimParam-method |
|
|
93 |
##' @export |
|
|
94 |
setMethod("simulate",signature=c("missing","EventData","FromDataSimParam"), |
|
|
95 |
function(data,SimParams,accrualGenerator=NULL,Naccrual=0, Nsim=1e4, seed=NULL, limit=0.05, |
|
|
96 |
longlagsettings=NULL,HR=NULL,r=NULL,dropout=NULL){ |
|
|
97 |
|
|
|
98 |
#validate the arguments |
|
|
99 |
validate.simulate.arguments(accrualGenerator,Naccrual,Nsim,seed, |
|
|
100 |
limit,longlagsettings,HR,r,data) |
|
|
101 |
|
|
|
102 |
#calculate the dropout rate and shape for drop out |
|
|
103 |
dropoutctrlSpec <- CtrlSpecFromList(dropout,eventtext="",1)[[1]] |
|
|
104 |
dropout.shape <- if(is.null(dropout) || is.null(dropout$shape)) 1 else dropout$shape |
|
|
105 |
dropout.rate <- log(2)^(1/dropout.shape)/dropoutctrlSpec@median |
|
|
106 |
|
|
|
107 |
#set seed to be used |
|
|
108 |
if(!is.null(seed)) set.seed(seed) |
|
|
109 |
|
|
|
110 |
#pre-process data to deal with subjects censored |
|
|
111 |
#a long time in the past |
|
|
112 |
indat <- DealWithReportingLag(data@subject.data,longlagsettings) |
|
|
113 |
|
|
|
114 |
#create matrix of subject recruitment times including additional |
|
|
115 |
#accrual we have a matrix with 1 row per simulation, 1 column per subject |
|
|
116 |
rec.details <- CalculateAccrualTimes(Naccrual,Nsim,indat$rand.date,accrualGenerator) |
|
|
117 |
|
|
|
118 |
#calculate quantiles from the recruitment details matrix for storing in output |
|
|
119 |
recQuantiles <- SimQOutputFromMatrix(rec.details,limit,Nsim) |
|
|
120 |
|
|
|
121 |
#subset the recruitment details to get the new subjects |
|
|
122 |
newrecs <- if(Naccrual!= 0) rec.details[,(ncol(rec.details)-Naccrual+1):ncol(rec.details)] |
|
|
123 |
else NULL |
|
|
124 |
|
|
|
125 |
#generate the simulation specific parameters |
|
|
126 |
#e.g. rate and shape Weibull parameters used for each simulation |
|
|
127 |
singleSimParams <- SimParams@generateParameterFunction(Nsim) |
|
|
128 |
|
|
|
129 |
#perform the simulations |
|
|
130 |
outcomes <-apply(singleSimParams, 1, PerformOneSimulation, |
|
|
131 |
number.subjects=nrow(indat), |
|
|
132 |
Naccrual=Naccrual, |
|
|
133 |
indat=indat,newrecs=newrecs,HR=HR,r=r, |
|
|
134 |
dropout.rate=dropout.rate, |
|
|
135 |
dropout.shape=dropout.shape, followup=data@followup, |
|
|
136 |
conditionalFunction=SimParams@conditionalFunction) |
|
|
137 |
|
|
|
138 |
#post process the output |
|
|
139 |
event.type <- sapply(outcomes,function(x){x$event.type}) |
|
|
140 |
if(class(event.type)=="numeric") event.type <- matrix(event.type,ncol=Nsim) |
|
|
141 |
times <- sapply(outcomes,function(x){x$event.times}) |
|
|
142 |
if(class(times)=="numeric") times <- matrix(times,ncol=Nsim) |
|
|
143 |
|
|
|
144 |
#calculate the quantiles for the events and dropouts |
|
|
145 |
event.times <- t(times) |
|
|
146 |
eventQuantiles <- SimQOutputFromMatrix(event.times,limit,Nsim,event.type=t(event.type),non.inf.event.type=0) |
|
|
147 |
dropoutQuantiles <- SimQOutputFromMatrix(event.times,limit,Nsim,event.type=t(event.type),non.inf.event.type=1) |
|
|
148 |
|
|
|
149 |
#use a dummy AccrualGenerator if one not given |
|
|
150 |
if(is.null(accrualGenerator)) |
|
|
151 |
accrualGenerator <- new("AccrualGenerator",f=function(N){NULL},model="NONE",text="NONE") |
|
|
152 |
|
|
|
153 |
return(new("FromDataResults", |
|
|
154 |
eventQuantiles = eventQuantiles, |
|
|
155 |
recQuantiles=recQuantiles, |
|
|
156 |
limit = limit, |
|
|
157 |
event.data = data, |
|
|
158 |
accrualGenerator=accrualGenerator, |
|
|
159 |
Naccrual=Naccrual, |
|
|
160 |
time.pred.data=EmptyPredictionDF(), |
|
|
161 |
event.pred.data=EmptyPredictionDF(), |
|
|
162 |
singleSimDetails=SingleSimDetails(event.type=event.type,event.times=times,rec.times=t(rec.details)), |
|
|
163 |
dropout.shape=dropout.shape, |
|
|
164 |
dropout.rate=dropout.rate, |
|
|
165 |
dropoutQuantiles=dropoutQuantiles, |
|
|
166 |
simParams=SimParams |
|
|
167 |
)) |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
}) |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|