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

Switch to unified view

a b/R/timeInternal.R
1
#Functions called by AddTimeColumn - which calculates the 
2
#time on study for EvewntData object creation are found here
3
4
# Validates the function arguments to AddTimeColumn
5
# @inheritParams AddTimeColumn
6
# @param allowed.colnames The names allowed in the time.list argument
7
validate.time.list.arguments <- function(data,rand.date,has.event,withdrawn,time.list,allowed.colnames){
8
  if(length(time.list)==0){
9
    stop("Empty time list argument")
10
  }
11
  
12
  if (!all(names(time.list) %in% allowed.colnames)){
13
    stop(paste("Invalid options to time argument they must be",paste(allowed.colnames,collapse=" ")))
14
  }
15
  
16
  if(!"last.date" %in% names(time.list) ){
17
    stop("last.date option must be included")
18
  }
19
  
20
  for(x in time.list ){
21
    if(!x %in% colnames(data)){
22
      stop(paste("Column name",x,"not found in data frame"))
23
    }
24
  }
25
  
26
  if(any(!data[,has.event] %in% c(0,1))){
27
    stop("All subjects must have 0 or 1 in hasEvent column")
28
  }
29
  if(any(!data[,withdrawn] %in% c(0,1))){
30
    stop("All subjects must have 0 or 1 in withdrawn column")
31
  }
32
}
33
34
35
# Derive the time of the subjects who are censored
36
# @inheritParams AddTimeColumn
37
# @param are.censored A vector of indices of subjects who are censored
38
# @param warning.colnames The names of columns in the time.list argument which
39
# will causes warnings if they contain data foe subjects who are censored
40
# @param ans A vector of calculated times
41
# @return ans with times added for subjects who are censored
42
Time.Deal.With.Censored <- function(ans,data,rand.date,are.censored,time.list,warning.colnames,subject){
43
  
44
  if(length(are.censored) == 0){
45
    return(ans)  
46
  }  
47
  
48
  ans[are.censored] <- data[are.censored,"last.date"] - data[are.censored,rand.date] + 1
49
  
50
  for(x in warning.colnames){
51
    if(!is.null(time.list[[x]])){
52
      r <- intersect(which(!is.na(data[,x])),are.censored)
53
      if(any(r)){
54
        warning(paste("Subjects",paste(data[r,subject],collapse=", "),
55
                      "do not have an event and are not withdrawn and have data in column",x,"which is ignored"))
56
      }
57
    }
58
  }
59
  ans
60
}
61
62
63
# Derive the time of the subjects who have withdrawn
64
# @inheritParams AddTimeColumn
65
# @param has.withdrawn A vector of indices of subjects who have withdrawn
66
# @param ans A vector of calculated times
67
# @return ans with times added for subjects who are censored
68
Time.Deal.With.Withdrawn <- function(ans,data,rand.date,has.withdrawn,time.list,withdrawn,subject,has.event){
69
  if(length(has.withdrawn)==0){
70
    return(ans) 
71
  }  
72
  
73
74
  if(is.null(time.list[["withdrawn.date"]])){
75
    warning(paste("Some subjects have withdrawn and there is no withdrawn date column, using last date instead"))
76
    ans[has.withdrawn] <- data[has.withdrawn,"last.date"] - data[has.withdrawn,rand.date] + 1
77
    return(ans)
78
  }
79
  
80
  ans[has.withdrawn] <- data[has.withdrawn,"withdrawn.date"] - data[has.withdrawn,rand.date] + 1
81
    
82
  r <- is.na(data[,"withdrawn.date"])& data[,withdrawn]==1 & data[,has.event]==0
83
  if(any(r)){
84
    warning(paste("Subjects",paste(data[r,subject],collapse=", "), "who have withdrawn do not have a withdrawn date,",
85
                  "the subjects last.date is used"))
86
      
87
    ans[r] <- data[r,"last.date"] - data[r,rand.date] + 1
88
  }
89
    
90
    
91
  #warn of any inconsistencies between last.date and actual.date
92
  inconsistent <- which(!data[,has.event] & data[,withdrawn] & !is.na(data[,"last.date"]) & 
93
                        ans > data[,"last.date"] - data[,rand.date] +1 )
94
    
95
  if(length(inconsistent)>0){
96
    warning(paste("Subjects",paste(data[inconsistent,subject],collapse=", "),
97
                  "have lastdate earlier than withdrawnDate. ",
98
                  "The lastdate column is ignored"))
99
  }
100
    
101
  ans
102
}  
103
  
104
105
106
107
108
109
# Derive the time of the subjects who have an event
110
# @inheritParams AddTimeColumn
111
# @param subs.had.event A vector of indices of subjects who had an event
112
# @param event.colnames The names of columns in the time.list argument which
113
# contain event details
114
# @param ans A vector of calculated times
115
# @return ans with times added for subjects who had event
116
Time.Deal.With.Had.Event <- function(ans,data,rand.date,subs.had.event,time.list,event.colnames,subject,has.event){
117
  if(length(subs.had.event)==0){
118
    return(ans)  
119
  }  
120
  
121
  for(x in event.colnames){
122
    if(!is.null(time.list[[x]])){
123
      ans <- ifelse(data[,has.event],
124
                    pmin(ans,data[,x] - data[,rand.date] + 1,na.rm=TRUE),
125
                    ans)
126
    }
127
  }
128
  
129
  #who has to use last.date
130
  use.last.date <- which(data[,has.event] & is.na(ans) & !is.na(data[,"last.date"]))
131
  
132
  if(length(use.last.date)>0){
133
    warning(paste("Subjects",paste(data[use.last.date,subject],collapse=", "),
134
                  "have an event but do not have a date of their event.",
135
                  "The lastdate column is used"))
136
    ans[use.last.date] <- data[use.last.date,"last.date"] - data[use.last.date,rand.date] + 1
137
    
138
  }
139
  
140
  #warn of any inconsistencies between last.date and actual.date
141
  inconsistent <- which(data[,has.event] & !is.na(data[,"last.date"]) & 
142
                          ans > data[,"last.date"] - data[,rand.date] +1 )
143
  
144
  if(length(inconsistent)>0){
145
    warning(paste("Subjects",paste(data[inconsistent,subject],collapse=", "),
146
                  "have lastdate earlier than the date of their event.",
147
                  "The lastdate column is ignored"))
148
  }
149
  
150
  ans
151
}