diff --git a/NAMESPACE b/NAMESPACE index 43d31537..76b6c12d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(ExpHazOS) export(ExpQuantOS) export(ExpSurvOS) export(ExpSurvPFS) diff --git a/R/hazardFunctions.R b/R/hazardFunctions.R new file mode 100644 index 00000000..8b82a4f2 --- /dev/null +++ b/R/hazardFunctions.R @@ -0,0 +1,21 @@ +#' OS Hazard Function from Constant Transition Hazards +#' +#' @param t (`numeric`)\cr study time-points. +#' @param h01 (positive `number`)\cr transition hazard for 0 to 1 transition. +#' @param h02 (positive `number`)\cr transition hazard for 0 to 2 transition. +#' @param h12 (positive `number`)\cr transition hazard for 1 to 2 transition. +#' +#' @return This returns the value of the OS hazard function at time t. +#' @export +#' +#' @examples +#' ExpHazOS(c(1:5), 0.2, 1.1, 0.8) +ExpHazOS <- function(t, h01, h02, h12) { + assert_numeric(t, lower = 0, any.missing = FALSE) + assert_positive_number(h01) + assert_positive_number(h02) + assert_positive_number(h12) + + h012 <- h12 - h01 - h02 + ((h12 - h02) * (h01 + h02) - h01 * h12 * exp(-h012 * t)) / ((h12 - h02) - h01 * exp(-h012 * t)) +} diff --git a/_pkgdown.yaml b/_pkgdown.yaml index 9ffeb1c0..1d5659e5 100644 --- a/_pkgdown.yaml +++ b/_pkgdown.yaml @@ -63,3 +63,6 @@ reference: - PWCsurvPFS - PwcOSInt - PWCsurvOS + - title: Hazard functions + contents: + - ExpHazOS diff --git a/man/ExpHazOS.Rd b/man/ExpHazOS.Rd new file mode 100644 index 00000000..5b6b99ed --- /dev/null +++ b/man/ExpHazOS.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hazardFunctions.R +\name{ExpHazOS} +\alias{ExpHazOS} +\title{OS Hazard Function from Constant Transition Hazards} +\usage{ +ExpHazOS(t, h01, h02, h12) +} +\arguments{ +\item{t}{(\code{numeric})\cr study time-points.} + +\item{h01}{(positive \code{number})\cr transition hazard for 0 to 1 transition.} + +\item{h02}{(positive \code{number})\cr transition hazard for 0 to 2 transition.} + +\item{h12}{(positive \code{number})\cr transition hazard for 1 to 2 transition.} +} +\value{ +This returns the value of the OS hazard function at time t. +} +\description{ +OS Hazard Function from Constant Transition Hazards +} +\examples{ +ExpHazOS(c(1:5), 0.2, 1.1, 0.8) +} diff --git a/tests/testthat/test-hazardFunctions.R b/tests/testthat/test-hazardFunctions.R new file mode 100644 index 00000000..3115533c --- /dev/null +++ b/tests/testthat/test-hazardFunctions.R @@ -0,0 +1,15 @@ +# ExpHazOS ---- + +test_that("ExpHazOS works as expected", { + actual <- ExpHazOS(1, 0.2, 1.1, 0.8) + expect_equal(actual, 1.038192, tolerance = 1e-3) + + actual2 <- ExpHazOS(2, 0.4, 1.4, 0.1) + expect_equal(actual2, 0.266345, tolerance = 1e-3) +}) + +test_that("ExpHazOS works also with vector of times t", { + result <- ExpHazOS(t = c(1:3), 0.2, 1.1, 0.8) + expected <- c(1.0381919, 0.9777975, 0.9253826) + expect_equal(result, expected, tolerance = 1e-3) +})