R/generate_hazard.R In certara/survivalnma: network meta-analyses of survival data

#'
#' generate_hazard
#'
#' internal function to generate the hazard function based on input distribution family
#'
#' @param family family of distribution \code{c("weibull", "gompertz",
#'                                              "exponential", "log-normal",
#'                                              "log-logistic", "fp1", "fp2")}
#' @param params matrix of parameters
#'
#' @param times sequence of times
#'
#' @param P a vector of powers (only required for fp1, fp2)
#' \itemize{
#'   \item{if family is fp1 then only supply one integer for p1}
#'   \item{if family is fp2 then supply a vector with two digits. ie c(1,-1)}}
#'
#'
#' @return matrix of hazard evaluated multiple points
#' @noRd
#'

generate_hazard <- function(family, params, times, P=NULL){
if (family == "weibull") {
hazard <- exp(params + params*log(times))

}else if (family == "gompertz") {
hazard <- exp(params)* exp(times*(params))
} else if (family == "exponential") {
hazard <- exp(params)
} else if (family == "lognormal") {
numerator <- dnorm( (log(times) - params) / exp(params) )
denominator <- (exp(params)*times)*(1 - pnorm( (log(times) - params)/ exp(params)))
hazard <- numerator/denominator

} else if (family == "loglogistic") {
hazard <- (exp(params)/exp(params))*((times/exp(params))^(exp(params)-1)) /
(1+((times/exp(params)) ^ exp(params)))
} else if (family == "fp1") {
if (is.null(P))                    {stop("Power vector cannot be NULL")
}else if (length(P) > 1)           {stop("Power vector needs to be of length 1")
}else if (P==0)                    {hazard <- exp(params + params*log(times))
}else                              {hazard <- exp(params + params*times^P)
}

} else if (family == "fp2") {
if (is.null(P))                    {stop("Power vector needs to be of length 2")
}else if(length(P) == 1)           {stop("Power vector needs to be of length 2")
}else if(length(P) > 2)            {stop("Power vector needs to be of length 2")
}else if(P ==0 && P ==0)     {hazard <- exp(params + params*log(times)   + params*(log(times))^2)
}else if(P != 0 && P == 0)   {hazard <- exp(params + params*times^P   + params*(log(times)))
}else if(P == 0 && P !=0)    {hazard <- exp(params + params*log(times)   + params*times^P)
}else if(P == P && P !=0) {hazard <- exp(params + params*times^P   + params*(times^P)*log(times))
}else                              {hazard <- exp(params + params*times^P   + params*times^P)
}

} else {
stop("Wrong or unspecified family of distributions")
}
# setting a threshold if hazard values get really large
hazard[hazard > exp(100)] <- exp(100)
return(hazard)
}

certara/survivalnma documentation built on June 5, 2019, 11:02 a.m.