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

Switch to unified view

a b/R/lag.R
1
#In predict from parameters it is possible to include a 
2
#lag in the study definition when calculating event times
3
#the code associated with this lag is contained here.
4
5
6
#' @include ctrlSpec.R
7
NULL
8
9
##' Parameter settings for when including a lagged effect. 
10
##' 
11
##' Note the lambda and HR in the \code{Study} class will be used
12
##' for time > T
13
##' @slot Lag.T Lagtime (T)
14
##' @slot ctrlSpec Control median specification for time period [0,T]
15
##' @slot L.HazardRatio Hazard for time period [0,T]
16
##' @seealso \code{\link{show,LagEffect-method}}
17
##' @export
18
setClass( "LagEffect", 
19
          slots=c( 
20
            Lag.T = "numeric",
21
            ctrlSpec = "CtrlSpec",
22
            L.HazardRatio = "numeric"
23
                     
24
          ), prototype = prototype( 
25
            Lag.T = 0,
26
            ctrlSpec = CtrlSpecfromMedian(as.numeric(NA)),
27
            L.HazardRatio = as.numeric(NA)
28
          ),
29
          validity = function(object){
30
            ans <- ""
31
            if(object@Lag.T < 0) ans <- paste(ans,"Invalid Lag.T")
32
            if(!is.na(object@L.HazardRatio) && (object@L.HazardRatio < 0 || object@L.HazardRatio > 1))
33
              ans <- paste(ans,"Hazard Ratio must be in [0,1]")
34
            
35
            if(object@Lag.T!=0){
36
              if(is.na(object@ctrlSpec@median)){
37
                ans <- paste(ans,"Invalid LagEffect") 
38
              } 
39
            }
40
            else{
41
              if(!is.na(object@ctrlSpec@median) || !is.na(object@L.HazardRatio)){
42
                ans <- paste(ans,"Invalid nullLagEffect") 
43
              } 
44
            }
45
            
46
            if(ans=="") return(TRUE)
47
            ans
48
            
49
          }
50
)
51
52
53
54
##' @name show
55
##' @rdname show-methods
56
##' @aliases show,LagEffect-method
57
##' @export
58
setMethod("show", signature(object="LagEffect"), 
59
  function(object) {
60
    if(isNullLag(object)){
61
      cat("No Lag\n")
62
    }
63
    else{
64
      cat(paste(object@Lag.T,"months of lag during which\n"))
65
      cat(paste("control group survival",object@ctrlSpec@text,"months\n"))
66
      if(!is.na(object@L.HazardRatio)){
67
        cat(paste("and the hazard ratio is",object@L.HazardRatio,"\n"))
68
      }
69
          
70
    }
71
  }
72
)
73
          
74
75
##' LagEffect constructor
76
##'
77
##' Note the lambda and HR in the study class will be used
78
##' for time > T
79
##' @param Lag.T Lagtime (T)
80
##' @param L.Ctr.median Control median for time period [0,T]
81
##' @param L.HazardRatio Hazard for time period [0,T]
82
##' @return A LagEffect object
83
##' @export
84
LagEffect <- function(Lag.T,L.Ctr.median=as.numeric(NA),L.HazardRatio=as.numeric(NA)){
85
  ctrlSpec <- CtrlSpecfromMedian(L.Ctr.median)
86
  new("LagEffect",Lag.T=Lag.T,ctrlSpec=ctrlSpec,L.HazardRatio=L.HazardRatio)
87
}
88
89
##' Create a LagEffect object with no lag
90
##' @return A LagEffect object for which \code{isNullLag()} is TRUE
91
##' @export
92
NullLag <- function(){
93
  NAnumeric <- as.numeric(NA)
94
  LagEffect(Lag.T=0,L.Ctr.median=NAnumeric,L.HazardRatio=NAnumeric)
95
}
96
97
##' Function to check whether Lag is a null lag
98
##' 
99
##' This checks whether the Lag.T slot is 0 
100
##'    
101
##' @name isNullLag
102
##' @docType methods
103
##' @rdname isNullLag-methods
104
##' @param Lag A LagEffect object
105
##' @return TRUE if null lag, FALSE otherwise
106
##' @export
107
setGeneric( "isNullLag",
108
            def=function( Lag )
109
              standardGeneric( "isNullLag" ) )
110
111
112
##' @rdname isNullLag-methods
113
##' @aliases isNullLag,LagEffect-method
114
##' @name isNullLag
115
##' @export
116
setMethod( "isNullLag",signature=(Lag="LagEffect"), 
117
  function( Lag ) {
118
    Lag@Lag.T==0
119
})
120