WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

queelius/dfr_dist

Repository files navigation

output
github_document
toc
true

dfr.dist

R-CMD-check

Dynamic Failure Rate Distributions for Survival Analysis

The dfr.dist package provides a flexible framework for specifying survival distributions through their hazard (failure rate) functions. Instead of choosing from a fixed catalog of distributions, you directly specify the hazard function, giving complete control over time-varying failure patterns.

Features

  • Flexible hazard specification: Define any hazard function h(t, par, ...)
  • Complete distribution interface: Automatic computation of survival, CDF, PDF, quantiles
  • Likelihood model support: Log-likelihood, score, Hessian for MLE
  • Censoring support: Handle exact and right-censored survival data
  • Integration with ecosystem: Works with algebraic.dist, likelihood.model, algebraic.mle

Installation

Install from GitHub:

# install.packages("devtools")
devtools::install_github("queelius/dfr_dist")

Quick Start

library(dfr.dist)
library(algebraic.dist)

Define a distribution via its hazard function

# Exponential distribution: constant hazard h(t) = lambda
exp_dist <- dfr_dist(
  rate = function(t, par, ...) rep(par[1], length(t)),
  par = c(lambda = 0.5)
)

# All distribution functions are automatically available
S <- surv(exp_dist)
S(2)  # Survival probability at t=2
#> [1] 0.3678794

Maximum Likelihood Estimation

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

# Create distribution without fixed parameters
dist <- dfr_dist(
  rate = function(t, par, ...) rep(par[1], length(t))
)

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

Custom hazard functions

Model complex failure patterns like bathtub curves (infant mortality + useful life + wear-out):

# h(t) = a*exp(-b*t) + c + d*t^k
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")
plot of chunk bathtub

plot of chunk bathtub

Mathematical Background

For a lifetime $T$, the hazard function is: $$h(t) = \frac{f(t)}{S(t)}$$

From the hazard, all other quantities follow:

Function Formula Method
Cumulative hazard $H(t) = \int_0^t h(u) du$ cum_haz()
Survival $S(t) = e^{-H(t)}$ surv()
CDF $F(t) = 1 - S(t)$ cdf()
PDF $f(t) = h(t) \cdot S(t)$ pdf()

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(dist)
ll(df, par = c(0.5))
#> [1] -9.579442

Documentation

Related Packages