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

Switch to unified view

a b/R/eventDataDiagnostic.R
1
#The Diagnostic functions (output site/censor information and the lagplot)
2
#for the predict from data EventData object
3
4
##' @include eventData.R
5
NULL
6
7
##' Output a data frame containing all subjects who been censored before
8
##' a given date
9
##' 
10
##' Output a data frame containing all subjects who been censored before
11
##' a given date not including subjects censored at end of follow up period
12
##' 
13
##' @param object An \code{EventData} object
14
##' @param ... Additional parameters to be passed to the method
15
##' @return A data frame with the required subject's data 
16
##' @rdname censorInformation-methods
17
##' @name censorInformation
18
##' @export
19
setGeneric("censorInformation",function(object,...) standardGeneric("censorInformation"))
20
21
22
# Simple internal function for validating the analysis/censor date
23
# arguments for the Diagnostic functions
24
# @param date.arg The user's input date
25
# @param default.date The default date to use if date.arg is null
26
# @return date.arg or the default.date if date.arg is null
27
# errors validating date.arg will throw an exception
28
DiagDataArg <- function(date.arg,default.date){
29
  if(is.null(date.arg)){
30
    return(as.Date(default.date,origin="1970-01-01")) 
31
  }
32
  FixDates(date.arg)
33
}
34
35
36
##' @param censor.date All subjects who have a censor date before this
37
##' date should be output, by default (i.e. when NULL) the latest date for which 
38
##' any subject information (withdrawal/event/censor) is known
39
##' @name censorInformation
40
##' @aliases censorInformation,EventData-method
41
##' @rdname censorInformation-methods
42
##'@export
43
setMethod("censorInformation", "EventData",
44
  function(object, censor.date=NULL){
45
    
46
    censor.date <- DiagDataArg(censor.date,max(LastDate(object@subject.data)))
47
      
48
    data <-GetLaggedSubjects(object@subject.data,censor.date)
49
          
50
    if(nrow(data)==0){
51
      return(data.frame(subject=character(0),
52
                        timelag=numeric(0),
53
                        rand.date=numeric(0),
54
                        time=numeric(0)))
55
    }
56
            
57
            
58
    last.date <- LastDate(data)
59
    data <- data.frame(subject=data$subject,
60
                       timelag=as.numeric(censor.date-last.date),
61
                       rand.date=data$rand.date,
62
                       time=data$time)
63
            
64
    ans <- data[order(data$timelag,decreasing=TRUE),]
65
    rownames(ans) <- 1:nrow(ans)
66
    ans
67
})
68
69
70
##' Output a plot showing the lag between censoring and the date the analysis
71
##' is being performed
72
##' @param object An \code{EventData} object
73
##' @param ... Additional arguments for the function 
74
##' @rdname DiagnosticPlot-methods
75
##' @name DiagnosticPlot
76
##' @export
77
setGeneric("DiagnosticPlot",function(object,...) standardGeneric("DiagnosticPlot"))
78
79
80
81
##' @param window.size An optional integer. If used an additional 2 lines at y=x-window.size 
82
##' and y = x-2*window.size are drawn on the graph. If \code{window.size} is chosen to be the
83
##' visit schedule (in days) then these lines provide an easy way to determine the number of subjects
84
##' who have missed one or two visits.
85
##' @param analysis.date The date the analysis is being perfomed on, by default (i.e. when NULL) it is the
86
##' the latest date at which any subject is censored/is known to have had an event
87
##' @param separate.events Logical, if FALSE then all events are coloured the same with label "Had Event", if
88
##' TRUE then the different event types (object@@subject.data$event.type) are coloured individually.
89
##' @rdname DiagnosticPlot-methods
90
##' @aliases DiagnosticPlot,EventData-method
91
##' @name DiagnosticPlot
92
##' @export
93
setMethod("DiagnosticPlot","EventData",
94
          function(object, window.size=NULL, analysis.date=NULL,separate.events=TRUE){
95
            if(nrow(object@subject.data)==0)stop("Empty data frame!")    
96
            
97
            analysis.date <- DiagDataArg(analysis.date,max(LastDate(object@subject.data)))
98
            
99
            xlab <- paste("Days on study if subjects censored on",as.character(analysis.date)) 
100
            ylab <- "Known days on study"
101
            
102
            time.on.study <- object@subject.data$time
103
            
104
            
105
            status <- rep("Ongoing",nrow(object@subject.data))
106
            status <- ifelse(object@subject.data$has.event==1,
107
                              if(separate.events)as.character(object@subject.data$event.type) else "Had Event",status)
108
            status <- ifelse(object@subject.data$censored.at.follow.up==1,"Censored after follow up period",status)
109
            status <- ifelse(object@subject.data$withdrawn==1,"Withdrawn from Study",status)
110
            
111
       
112
            my.data <- data.frame(subject=object@subject.data$subject,
113
                                  rand.date=object@subject.data$rand.date,
114
                                  time.on.study=time.on.study,
115
                                  t.max=as.numeric(analysis.date - object@subject.data$rand.date + 1,origin="1970-01-01"),
116
                                  status=status,
117
                                  site=object@subject.data$site,
118
                                  date.of.event.censor.or.withdrawal=LastDate(object@subject.data))
119
            
120
            
121
            p <- ggplot(my.data, aes_string(x="t.max", y="time.on.study", color="status")) +
122
              geom_point() + geom_abline(intercept=0, slope=1, col="black") +
123
              xlab(xlab) + ylab(ylab)
124
            
125
            if(!is.null(window.size)){      
126
              if(window.size<=0) stop("window.size should be positive")
127
              p <- p + geom_abline(intercept=-window.size, slope=1, col="black",linetype = 2)+
128
                geom_abline(intercept=-2*window.size, slope=1, col="black",linetype = 2)
129
            }
130
            p     
131
          }
132
)
133
134
##' Output information about how up to date subject censor dates
135
##' are for each site
136
##'
137
##' Output information about how up to date subject censor dates
138
##' are for each site in the study subjects who are censored at the 
139
##' end of their follow up period are
140
##' not included in this analysis
141
##' @param object An \code{EventData} object
142
##' @param ... Additional arguments to be passed to the method
143
##' @rdname siteInformation-methods
144
##' @name siteInformation
145
##' @export
146
setGeneric("siteInformation",function(object,...) standardGeneric("siteInformation"))
147
148
 
149
150
151
##'@param analysis.date The date the analysis is being perfomed on, by default (i.e. when NULL) it is the
152
##' the latest date at which any subject is censored/is known to have had an event
153
##'@param ndays The acceptable lag between \code{analysis.date} and censor date. If the lag is 
154
##'greater than this then the subject will be included in the output data frame.
155
##'@return A data frame with each row containing a site name and the number
156
##'of subjects at this site with censor date before \code{analysis.date}-\code{ndays}
157
##'@rdname siteInformation-methods
158
##'@aliases siteInformation,EventData-method
159
##'@name siteInformation
160
##'@export
161
setMethod("siteInformation", "EventData",
162
          function(object, analysis.date=NULL, ndays){
163
            if(ndays < 0 || class(ndays)!="numeric") stop("ndays must be numeric and non-negative")
164
            if(all(is.na(object@subject.data$site))) stop("No site information")
165
            
166
            analysis.date <- DiagDataArg(analysis.date,max(LastDate(object@subject.data)))
167
            
168
            data <-GetLaggedSubjects(object@subject.data,analysis.date-ndays)$site
169
            
170
            if(length(data)==0){
171
              return(data.frame(site=character(0),count=character(0)))
172
            }
173
            
174
            sites <- as.data.frame(table(data))
175
            colnames(sites) <- c("site","count")
176
            sites <- sites[order(sites$count,decreasing=TRUE),]
177
            rownames(sites) <- NULL
178
            sites[sites$count > 0,]
179
          })