DEV Community

Alex Towell
Alex Towell

Posted on • Originally published at metafunctor.com

flexhaz: Specify the Hazard Function Directly

Survival analysis usually makes you pick from a catalog. Weibull, exponential, log-normal. You choose the family, estimate the parameters, and hope the model fits. flexhaz flips this around. You specify the hazard function directly, and the package computes everything else.

How It Works

Instead of choosing Weibull(shape, scale), you write:

h <- function(t, x) exp(b0 + b1*x + b2*t)  # Your hazard function
model <- dfr_dist(hazard = h)
Enter fullscreen mode Exit fullscreen mode

The package computes survival functions, cumulative hazards, quantiles, and sampling from your custom hazard. You get a full distributional object without committing to a named family.

Why This Is Useful

You are not constrained to parametric families. Want a bathtub curve? Multiple failure-rate peaks? Time-varying covariate effects? Just write the hazard function. No need to force reality into exponential or Weibull boxes.

Covariates can depend on anything:

h <- function(t, age, treatment) {
  baseline * exp(beta_age*age + beta_tx*treatment + gamma*t)
}
Enter fullscreen mode Exit fullscreen mode

And it integrates with the rest of the MLE stack. flexhaz works with algebraic.mle for parameter estimation and likelihood.model for likelihood contributions.

Constraints

Your hazard function needs to satisfy two things:

  1. Non-negative: h(t, x) >= 0 for all t, x
  2. Eventual failure: cumulative hazard goes to infinity as t goes to infinity

That is it. Those are the only requirements for a valid hazard function. The package handles deriving the survival function, density, CDF, and quantile function from the hazard you provide.

Context

This generalizes my thesis work on masked failure data, where I used Weibull and exponential distributions. With flexhaz, you are not limited to parametric families. You specify the actual failure mechanism, and the math adapts.

R package -- Works with algebraic.mle -- Documentation -- GitHub

Top comments (0)