MyNixOS website logo
Description

Dynamic Failure Rate Distributions for Survival Analysis.

Flexible framework for specifying survival distributions through their hazard (failure rate) functions. Define arbitrary time-varying hazard functions to model complex failure patterns including bathtub curves, proportional hazards with covariates, and other non-standard hazard behaviors. Provides automatic computation of survival, CDF, PDF, quantiles, and sampling. Implements the likelihood model interface for maximum likelihood estimation with right-censored and left-censored survival data.

flexhaz

CRANstatus R-CMD-check

Dynamic Failure Rate Distributions for Survival Analysis

Capacitors that wear out faster than any Weibull can describe. Software systems with bathtub-shaped crash rates. Post-surgical patients whose risk drops sharply, then slowly climbs again. Standard parametric survival families cannot express these hazard patterns — but flexhaz can.

Write the hazard function you need — any R function of time and parameters — and the package derives everything else: survival curves, CDFs, densities, quantiles, sampling, log-likelihoods, MLE fitting, and residual diagnostics.

Why flexhaz?

Featureflexhazsurvivalflexsurv
Custom hazard functionsYesNoLimited
Built-in distributionsExp, Weibull, Gompertz, Log-logisticWeibull, ExpMany
User-supplied derivativesscore + HessianNoNo
Censoring supportRight + LeftRightRight
Model diagnosticsCox-Snell, Martingale, Q-QLimitedLimited
Likelihood model interfaceFullBasicPartial

Features

  • Flexible hazard specification: Define any hazard function h(t, par, …)
  • Built-in distributions: Exponential, Weibull, Gompertz, Log-logistic with optimized implementations
  • Complete distribution interface: hazard, survival, CDF, PDF, quantiles, sampling
  • Likelihood model support: Log-likelihood, score, Hessian for MLE
  • Custom derivatives: Supply analytical score and Hessian functions, or let the package fall back to numerical differentiation via numDeriv
  • Model diagnostics: Residuals (Cox-Snell, Martingale) and Q-Q plots
  • Censoring support: Handle exact, right-censored, and left-censored survival data
  • Ecosystem integration: Works with algebraic.dist, likelihood.model, algebraic.mle

Installation

Install from CRAN:

install.packages("flexhaz")

Or the development version from r-universe:

install.packages("flexhaz", repos = "https://queelius.r-universe.dev")

Quick Start

library(flexhaz)

Built-in Distributions

Use the convenient constructors for classic survival distributions:

# Exponential: constant hazard (memoryless)
exp_dist <- dfr_exponential(lambda = 0.5)

# Weibull: power-law hazard (wear-out or infant mortality)
weib_dist <- dfr_weibull(shape = 2, scale = 3)

# Gompertz: exponentially increasing hazard (aging)
gomp_dist <- dfr_gompertz(a = 0.01, b = 0.1)

# Log-logistic: non-monotonic hazard (increases then decreases)
ll_dist <- dfr_loglogistic(alpha = 10, beta = 2)

All distribution functions are automatically available:

S <- surv(exp_dist)
S(2)  # Survival probability at t=2
#> [1] 0.3678794

h <- hazard(weib_dist)
h(1)  # Hazard at t=1
#> [1] 0.2222222

Maximum Likelihood Estimation

# Simulate failure times
set.seed(42)
times <- rexp(50, rate = 1)
df <- data.frame(t = times, delta = 1)

# Fit via MLE
solver <- fit(dfr_exponential())
result <- solver(df, par = c(0.5), method = "BFGS")
coef(result)  # Estimated rate
#> [1] 0.8808457

Custom Hazard Functions

Model complex failure patterns like bathtub curves:

# h(t) = a*exp(-b*t) + c + d*t^k
# Infant mortality + useful life + wear-out
bathtub <- dfr_dist(
  rate = function(t, par, ...) {
    par[1] * exp(-par[2] * t) + par[3] + par[4] * t^par[5]
  },
  par = c(a = 1, b = 2, c = 0.02, d = 0.001, k = 2)
)

h <- hazard(bathtub)
curve(sapply(x, h), 0, 15, xlab = "Time", ylab = "Hazard rate",
      main = "Bathtub hazard curve")

Model Diagnostics

Check model fit with residual analysis:

# Fit exponential to data
fitted_exp <- dfr_exponential(lambda = coef(result))

# Cox-Snell residuals Q-Q plot
qqplot_residuals(fitted_exp, df)

Mathematical Background

For a lifetime T, the hazard function is:

h(t) = \frac{f(t)}{S(t)}

From the hazard, all other quantities follow:

FunctionFormulaMethod
Cumulative hazardH(t) = \int_0^t h(u) ducum_haz()
SurvivalS(t) = e^{-H(t)}surv()
CDFF(t) = 1 - S(t)cdf()
PDFf(t) = h(t) \cdot S(t)density()

Likelihood for Survival Data

For exact observations: \log L = \log h(t) - H(t)

For right-censored: \log L = -H(t)

# Mixed data with censoring
df <- data.frame(
  t = c(1, 2, 3, 4, 5),
  delta = c(1, 1, 0, 1, 0)  # 1 = exact, 0 = censored
)

ll <- loglik(dfr_exponential())
ll(df, par = c(0.5))
#> [1] -9.579442

Documentation

Start Here:

Real-World Applications:

Going Deeper:

Reference:

Related Packages

Metadata

Version

0.5.2

License

Unknown

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows