R/baselines.R

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
################################################################################
#  Baseline hazard distributions                                               #
################################################################################
#                                                                              #
#  These are functions with parameters                                         #
#   - pars: the vector of parameters                                           #
#   - t   : the time point                                                     #
#   - what: the quantity to be returned by the function                        #
#           either  "H", the cumulated hazard,                                 #
#               or "lh", the log-hazard                                        #
#                                                                              #
#                                                                              #
#   Date:                 December 19, 2011                                    #
#   Last modification on: January 25, 2017                                     #
################################################################################


################################################################################
################################################################################


################################################################################
#                                                                              #
#   Weibull baseline hazard function                                           #
#                                                                              #
#   Parameters:                                                                #
#    [1] rho    > 0                                                            #
#    [2] lambda > 0                                                            #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \rho \lambda t ^ (\rho-1)                                          #
#                                                                              #
#                                                                              #
#   Date: December, 19, 2011                                                   #
################################################################################

weibull <- function(pars,
                    t, 
                    what) {
    if (what == "H")
        return(pars[2] * t ^ (pars[1]))
    else if (what == "lh")
        return(log(pars[1]) + log(pars[2]) + ((pars[1] - 1) * log(t)))
}


################################################################################
#                                                                              #
#   Fr├ęchet or Inverse Weibull baseline hazard function                        #
#                                                                              #
#   Parameters:                                                                #
#    [1] rho    > 0                                                            #
#    [2] lambda > 0                                                            #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \lambda \rho t ^ -(\rho + 1) / (exp(\lambda t ^ -\rho) - 1)        #
#    H(t) = -log[1 - exp{- \lambda t ^ -\rho}]                                 #
#                                                                              #
#                                                                              #
#   Date:              June, 26, 2012                                          #
#   Last modification: January 31, 2017                                        #
################################################################################
inweibull <- frechet <- function(pars,
                                 t, 
                                 what) {
    if (what == "H")
        return(-log(1 - exp(-pars[2] * (t ^ -pars[1]))))
    else if (what == "lh")
        return(sum(log(pars[1:2])) - log(t) * (pars[1] + 1) -
                   log(exp(pars[2] * (t ^ -pars[1])) - 1))
}


################################################################################
#                                                                              #
#   Exponential baseline hazard function                                       #
#                                                                              #
#   Parameters:                                                                #
#    [1] lambda > 0                                                            #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \lambda                                                            #
#                                                                              #
#                                                                              #
#   Date: December, 19, 2011                                                   #
################################################################################

exponential <- function(pars,
                        t, 
                        what) {
    if (what == "H")
        return(pars * t)
    else if (what == "lh") 
        return(log(pars))
}



################################################################################
#                                                                              #
#   Gompertz baseline hazard function                                          #
#                                                                              #
#   Parameters:                                                                #
#    [1] gamma  > 0                                                            #
#    [2] lambda > 0                                                            #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \lambda exp(\gamma t)                                              #
#                                                                              #
#                                                                              #
#   Date: December, 19, 2011                                                   #
################################################################################

gompertz <- function(pars,
                     t, 
                     what) {
    if (what == "H") 
        return(pars[2] / pars[1] * (exp(pars[1] * t) - 1))
    else if (what == "lh") 
        return(log(pars[2]) + pars[1] * t)
}



################################################################################
#                                                                              #
#   Lognormal baseline hazard function                                         #
#                                                                              #
#   Parameters:                                                                #
#    [1] mu     \in \mathbb R                                                  #
#    [2] sigma  > 0                                                            #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \frac{ \phi(\log t -\mu \over \sigma) }{                           #
#             \sigma t [1-\Phi(\log t -\mu \over \sigma)] }                    #
#                                                                              #
#   with \phi the density and \Phi the distribution function                   #
#   of a standard Normal                                                       #
#                                                                              #
#                                                                              #
#   Date: December, 19, 2011                                                   #
################################################################################

lognormal <- function(pars,
                      t, 
                      what) {
    if (what == "H")  #return - log (S)
        return(- log(1 - plnorm(t, meanlog = pars[1], sdlog = pars[2])))
    else if (what == "lh")  #return log(f) - log(S)
        return(dlnorm(t, meanlog = pars[1], sdlog = pars[2], log = TRUE) -
                   log(1 - plnorm(t, meanlog = pars[1], sdlog = pars[2])))
}



################################################################################
#                                                                              #
#   Loglogistic baseline hazard function                                       #
#                                                                              #
#   Parameters:                                                                #
#    [1] alpha \in \mathbb R                                                   #
#    [2] kappa > 0                                                             #
#                                                                              #
#   Hazard:                                                                    #
#    h(t) = \frac{ \exp(\alpha) \kappa t^{\kappa-1} }{                         #
#             1 + \exp(\alpha) t^\kappa }                                      #
#                                                                              #
#                                                                              #
#   Date: December, 19, 2011                                                   #
################################################################################

loglogistic <- function(pars,
                        t, 
                        what) {
    if (what == "H") 
        return(log(1 + exp(pars[1]) * t ^ (pars[2])))
    else if (what == "lh") 
        return(pars[1] + log(pars[2]) + (pars[2] - 1) * log(t) - 
                   log(1 + exp(pars[1]) * t ^ pars[2]) )
}




################################################################################
#                                                                              #
#   Log-skewNormal baseline hazard function                                    #
#   Azzalini, Adelchi. (1985). A class of distributions which includes         #
#     the normal ones. Scandinavian Journal of Statistics, 12:171-178          # 
#                                                                              #
#   Parameters:                                                                #
#    [1] xi \in \mathbb R, the location parameter                              #
#    [2] omega > 0, the scale parameter (called omega in original paper)       #
#    [3] alpha \in \mathbb R, the shape parameter                              #
#                                                                              #
#   Density:                                                                   #
#    f(t) = 2  / (\omega t) \phi_d((\log(t) - \xi) / \omega)                   #
#                                        \Phi(\alpha (log(t) - \xi) / \omega)  #
#                                                                              #
#                                                                              # 
#   Author: Andrea Callegaro                                                   #
#   Date: November, 22, 2016                                                   #
################################################################################

logskewnormal <- function(pars,
                          t, 
                          what) {
    # library(sn)
    #log-skew normal density
    dlsn <- Vectorize(function(t, pars) {
        dsn(log(t), xi = pars[1], omega = pars[2], alpha = pars[3]
        ) / t
    }, 't')
    #log-skew normal cdf
    plsn <- Vectorize(function(t, pars) {
        psn(log(t), xi = pars[1], omega = pars[2], alpha = pars[3])
    }, 't')
    
    if (what == "H")  #return -log(S)
        return(-log(1 - plsn(t, pars = pars)))
    
    else if (what == "lh")  #return (log(f) - log(S))
        return(log(dlsn(t, pars = pars)) -
                   log(1 - plsn(t, pars = pars)))
}


# setGeneric('basehaz', package = 'survival')
# setClass('parfm')
# setMethod('basehaz', signature(fit = 'parfm'), function(fit, centered = TRUE) {
#     return(fit)
# })

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.