a b/R/eventModel.R
1
# This file contains the public functions associated with
2
# the fitted Weibull/loglogistic model for the predict from data simulations  
3
4
##' @include eventData.R accrual.R fromDataSimInternal.R simQOutput.R ctrlSpec.R singleSimDetails.R fromDataSimParam.R
5
NULL
6
7
8
##' An S4 class containing a fitted survival model
9
##' of an Eventdata object
10
##' @slot model An S3 survreg object
11
##' @slot event.data An EventData object used to fit a Weibull survial model
12
##' @slot simParams The \code{FromDataSimParam} object which contains the information
13
##' needed to generate the survial times
14
##' @export
15
setClass("EventModel", 
16
         slots=list(model="survreg",
17
                    event.data="EventData",
18
                    simParams="FromDataSimParam")
19
         )
20
21
22
##' @name show
23
##' @rdname show-methods
24
##' @aliases show,EventModel-method
25
##' @export
26
setMethod("show",
27
  "EventModel",
28
  function(object) {
29
    print(object@model)
30
    
31
})
32
33
34
##' @param units Scale for the x-axis. "Days", "Months" or "Years"
35
##' @name plot
36
##' @rdname plot-methods
37
##' @aliases plot,EventModel,missing-method
38
##' @export
39
setMethod( "plot",
40
  signature( x="EventModel", y="missing" ),
41
  function(x, units="Days", xlab=paste("Time in study [",units,"]",sep=""),
42
           ylab="", main="", ylim=NULL, xlim=NULL, ...) { 
43
    
44
    xscale <- GetScaleForKM(units,daysinyear)
45
    daysinyear <- standarddaysinyear()
46
    
47
    KM <- survfit(Surv(time, has.event) ~ 1, data=x@event.data@subject.data,...)
48
    
49
    plot(KM, lwd=c(2,1,1), col=c("red", "black", "black" ), 
50
        xlab=xlab, ylab=ylab,
51
        xlim=xlim, ylim=ylim,
52
        main=main,xscale=xscale)
53
    lines(predict(x@model, type="quantile", p=seq(.01,.99,by=.01))[1,]/xscale, 
54
         seq(.99,.01,by=-.01), col="brown", type="l", lwd=3)
55
    
56
    pos <- if(is.null(xlim)) 0.75*max(KM$time/xscale) else xlim[1] + 0.75*(xlim[2]-xlim[1])
57
    
58
    legend(pos, 1, c( "Data", "Model" ), col=c( "red", "brown" ), lty=c(1,1))
59
  
60
    }
61
)
62
63
64
##' Calculate KM risk.table
65
##' @rdname risk.table-methods
66
##' @name risk.table
67
##' @param x An \code{EventModel} object 
68
##' @param ... Additional arguments to pass to survfit
69
##' @return A data frame contianing the number at risk at the given times
70
##' @export
71
setGeneric("risk.table",
72
           def=function(x,...)
73
             standardGeneric("risk.table"))
74
75
76
##' @rdname risk.table-methods
77
##' @name risk.table
78
##' @aliases risk.table,EventModel-method
79
##' @param units Scale for the risk table: "Days", "Months" or "Years"
80
##' @param risk.times The times for the risk table (in \code{units}). 
81
##' If NULL then the 0, 0.25,0.5,0.75,1 quantiles of the KM survival table are used
82
##' @export 
83
setMethod("risk.table",
84
  signature("EventModel"),
85
  function(x,units="Days",risk.times=NULL,...){
86
    daysinyear <- standarddaysinyear()
87
    xscale <- GetScaleForKM(units,daysinyear)
88
    KM <- survfit(Surv(time, has.event) ~ 1, data=x@event.data@subject.data,...)
89
    if(is.null(risk.times)){
90
      risk.times <- quantile(c(0, max(KM$time)))/xscale
91
      names(risk.times) <- NULL
92
    }  
93
    
94
    scaled.risk.times <-risk.times*xscale 
95
    
96
    
97
    KM <- summary(KM,time=scaled.risk.times)
98
    retVal <- data.frame(time = KM$time/xscale, n.risk = KM$n.risk)
99
    
100
      
101
    n.risk <- unlist(lapply(risk.times,function(x){
102
                 
103
                  if(x %in% retVal$time){
104
                    return(retVal$n.risk[retVal$time==x])
105
                  }
106
                  return("NA")
107
    }))
108
    
109
    data <- rbind(risk.times,n.risk)
110
    rownames(data) <- c("time","n.risk")
111
    return(data)
112
   
113
  }
114
)
115
116