Diff of /R/singleSimDetails.R [000000] .. [f2e496]

Switch to unified view

a b/R/singleSimDetails.R
1
#Contains the functions for SingleSimDetails class
2
#part of the predict from data part of the package
3
#see class definition below
4
5
##' Class containing the subject level event data
6
##' for each simulation
7
##' 
8
##' A class which contains the recruitment times
9
##' event times and event types of all subjects and all
10
##' simulations for a predict from data simulation. 
11
##' This class is created when performing simulation and does not 
12
##' need to be constructed manually 
13
##'  
14
##' @slot event.type A matrix (1 row per subject, 1 column per simulation) of
15
##' the reason subjects leave trial. 0 is have event, 1 is drop out and 2 is drop out due to follow up
16
##' @slot event.times A matrix (1 row per subject, 1 column per simulation) of
17
##' dates (as numeric) subjects leave the trial
18
##' @slot rec.times A matrix (1 row per subject, 1 column per simulation) of
19
##' recruitment dates (as numeric). 
20
##' Therefore event.times[i,j] - rec.times[i,j] + 1 is the time on study for
21
##' subject i in simulation j
22
##' @export
23
setClass("SingleSimDetails", 
24
         slots=list(
25
           event.type="matrix",
26
           event.times="matrix",
27
           rec.times="matrix"
28
         )
29
) 
30
31
32
# Constructor of SingleSimDetails object
33
# @param event.type A matrix (1 row per subject, 1 column per simulation) of
34
# the reason subjects leave trial. 0 is have event, 1 is drop out and 2 is censored due to 
35
# completing follow up period
36
# @param event.times A matrix (1 row per subject, 1 column per simulation) of
37
# dates (as numeric) subjects leave the trial
38
# @param rec.times A matrix (1 row per subject, 1 column per simulation) of
39
# recruitment dates (as numeric). 
40
# Therefore event.times[i,j] - rec.times[i,j] + 1 is the time on study for
41
# subject i in simulation j
42
SingleSimDetails <- function(event.type,event.times,rec.times){
43
  
44
  new("SingleSimDetails",event.type=event.type,
45
      event.times=event.times,rec.times=rec.times)
46
  
47
}
48
49
50
51
##' @name CalculateDaysAtRisk
52
##' @rdname CalculateDaysAtRisk-methods  
53
##' @aliases CalculateDaysAtRisk,SingleSimDetails-method
54
##' @export
55
setMethod("CalculateDaysAtRisk","SingleSimDetails",
56
 function(object,times){
57
  
58
  unlist(lapply(times,function(x){
59
    cutoff <- as.numeric(x,origin="1970-01-01")
60
    time.at.risk <-  matrix(pmax(0, pmin(cutoff,object@event.times) - object@rec.times + 1),
61
                            ncol=ncol(object@event.times))
62
    median(apply(time.at.risk,2,sum))
63
  }))
64
  
65
})