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

Switch to unified view

a b/R/ctrlSpec.R
1
#This file contains the code required for the
2
#CtrlSpec class used in predict from parameters
3
#It is not exported for users
4
#and is used to translate the event/drop out rate 
5
#specifications in the Study constructors to a common 
6
#form to be stored within the Study object
7
#it is also used for the dropout specification in predict from
8
#data
9
10
# The CtrlSpec (control arm specification) class
11
# is not exported for user use but is used within the
12
# package to allow different ways of specifying the
13
# contorl arm rate when specifying a Study/CRGI study
14
# It also includes the text to be output in the summary text 
15
# concerning the control (and experimental) arm event rates.
16
# This class is also used when specifying the drop out rate for the 
17
# control arm
18
# @slot median The control arm median event time
19
# @slot text The control arm text to be included in the summary
20
# @slot experimentaltext a function which takes a HR and shape and outputs 
21
# the summary text information for the rate spec of the control arm    
22
setClass( "CtrlSpec", 
23
          slots= list(median="numeric",  
24
                      text="character", 
25
                      experimentaltext="function"
26
                      )
27
)
28
29
30
# Create a \code{CtrlSpec} by giving the control arm median survival
31
# time as used in the Oncology study constructor
32
# @param ctrl.median The control arm median time to event
33
# @return A \code{CtrlSpec} object
34
CtrlSpecfromMedian <- function(ctrl.median){
35
36
  if(!is.na(ctrl.median)){
37
    if(!is.numeric(ctrl.median) || length(ctrl.median) > 1 || ctrl.median < 0){
38
     stop("Invalid ctrl.median argument, it must be numeric and positive")
39
    }
40
  }
41
  
42
  f <- function(HR,shape){
43
    paste("median=",round(ctrl.median/HR^(1/shape),2),sep="")
44
  }
45
  
46
  new("CtrlSpec",median=ctrl.median,text=paste("median=",ctrl.median,sep=""),
47
      experimentaltext=f)  
48
    
49
}
50
51
52
# Create A CtrlSpec object from a list
53
# 
54
# The function calls \code{CtrlSpecfromProportion} with the appropriate
55
# arguments taken from a list. It is used when creating the dropout slot of 
56
# the \code{Study} object in the various Study constructors
57
#
58
# @param eventtext The text to be output in the summary of the ctrlspec object
59
# for example either "had an event by" or "(in absence of events) would drop out by"
60
# @param var.list A list with elements labelled time and proportion
61
# (and optionally shape). Or NULL for creating a CtrlSpec object with infinite median
62
# (i.e. specifying the median for drop outs given no drop outs)
63
# @param n.arms The number of arms for which to create CtrlSpec objects  
64
# @return A list of CtrlSpec objects one for each arm
65
CtrlSpecFromList <- function(var.list,eventtext,n.arms){
66
  
67
  #Infinite median if no "events" (used when specifying the median there are no drop outs)
68
  if(is.null(var.list)){
69
    x <- CtrlSpecfromProportion(time=Inf,proportion.had.event=0.5,shape=1)
70
    return(if(n.arms==1) list(ctrl=x) else list(ctrl=x,active=x))
71
  }
72
  
73
  if(!is.list(var.list) || is.null(var.list$time) || is.null(var.list$proportion) || length(var.list$proportion) > 2
74
     || length(var.list$proportion)!= n.arms){
75
    stop(paste("Invalid argument(s) to CtrlSpecFromList.",
76
               "See help for the dropout argument to Study function",
77
               "for an example of valid arguments."))
78
  }
79
  
80
  if(is.null(var.list$shape)) var.list$shape <- 1
81
  
82
  retVal <- lapply(var.list$proportion,function(x){
83
    CtrlSpecfromProportion(var.list$time,x,var.list$shape,eventtext)
84
  })
85
  
86
  names(retVal) <- if(n.arms==1) "ctrl" else c("ctrl","active")
87
  return(retVal)
88
  
89
}
90
91
# Create a \code{CtrlSpec} object giving the time at which a given 
92
# proportion of subjects have had an event as used in the CRGIStudy constructor
93
# @param time The time at which \code{ctrl.proportion} of the control group have had an event.
94
# Used to set the event rate 
95
# @param proportion.had.event The proportion of control arm subjects who have had an event by time \code{time} 
96
# @param shape The Weibull shape parameter for the control arm survival function
97
# @param eventtext The text to be output in the summary of the ctrlspec object
98
# for example either "had an event by" or "(in absence of events) would drop out by"
99
# @return A \code{CtrlSpec} object
100
CtrlSpecfromProportion <- function(time,proportion.had.event,shape,eventtext="had an event by"){
101
  
102
  lapply(list(shape=shape,time=time,proportion.had.event=proportion.had.event),
103
    function(x){
104
      if(!is.numeric(x) ||  length(x) != 1 || x <= 0){
105
       stop("Arguments to CtrlSpecfromProportion should be numeric, positive and of length 1 ") 
106
      }
107
    }       
108
  )
109
  
110
  if(proportion.had.event >= 1){
111
    stop("Invalid proportion.had.event argument")
112
  }
113
      
114
  text <- paste(proportion.had.event*100,"% of subjects ",eventtext," ",time,sep="")
115
  rate <- ((-log(1-proportion.had.event))^(1/shape))/time
116
  
117
  median <- log(2)^(1/shape)/rate
118
  
119
  f <- function(HR,shape){
120
    paste(round( (1-(1-proportion.had.event)^HR)*100, 2),"% of subjects ",eventtext," ",time,sep="")
121
  }
122
  
123
  new("CtrlSpec",median=median,text=text,experimentaltext=f)
124
  
125
}