-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
206 lines (148 loc) · 6.18 KB
/
README.Rmd
File metadata and controls
206 lines (148 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
output:
github_document:
toc: true
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# flexhaz
<!-- badges: start -->
[](https://CRAN.R-project.org/package=flexhaz)
[](https://github.com/queelius/flexhaz/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
**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?
| Feature | flexhaz | survival | flexsurv |
|---------|----------|----------|----------|
| Custom hazard functions | **Yes** | No | Limited |
| Built-in distributions | Exp, Weibull, Gompertz, Log-logistic | Weibull, Exp | Many |
| User-supplied derivatives | **score + Hessian** | No | No |
| Censoring support | Right + Left | Right | Right |
| Model diagnostics | Cox-Snell, Martingale, Q-Q | Limited | Limited |
| Likelihood model interface | **Full** | Basic | Partial |
## 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:
```r
install.packages("flexhaz")
```
Or the development version from r-universe:
```r
install.packages("flexhaz", repos = "https://queelius.r-universe.dev")
```
## Quick Start
```{r setup, message=FALSE, warning=FALSE}
library(flexhaz)
```
### Built-in Distributions
Use the convenient constructors for classic survival distributions:
```{r builtin}
# 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:
```{r methods}
S <- surv(exp_dist)
S(2) # Survival probability at t=2
h <- hazard(weib_dist)
h(1) # Hazard at t=1
```
### Maximum Likelihood Estimation
```{r mle}
# 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
```
### Custom Hazard Functions
Model complex failure patterns like bathtub curves:
```{r bathtub, fig.height=4}
# 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:
```{r diagnostics}
# 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:
| 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)$ | `density()` |
## Likelihood for Survival Data
For exact observations: $\log L = \log h(t) - H(t)$
For right-censored: $\log L = -H(t)$
```{r likelihood}
# 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))
```
## Documentation
**Start Here:**
- [Package Overview & Quick Start](https://queelius.github.io/flexhaz/articles/flexhaz-package.html) - Motivation, complete example, and quick start guide
**Real-World Applications:**
- [Reliability Engineering](https://queelius.github.io/flexhaz/articles/reliability_engineering.html) - Five case studies
**Going Deeper:**
- [Dynamic Failure Rate Distributions](https://queelius.github.io/flexhaz/articles/failure_rate.html) - Mathematical foundations
- [Creating Custom Distributions](https://queelius.github.io/flexhaz/articles/custom_distributions.html) - The three-level optimization paradigm
- [Custom Derivatives for MLE](https://queelius.github.io/flexhaz/articles/custom_derivatives.html) - Analytical score and Hessian functions
**Reference:**
- [Function Reference](https://queelius.github.io/flexhaz/reference/)
## Related Packages
- [`algebraic.dist`](https://github.com/queelius/algebraic.dist): Generic distribution interface
- [`likelihood.model`](https://github.com/queelius/likelihood.model): Likelihood model framework
- [`algebraic.mle`](https://github.com/queelius/algebraic.mle): MLE utilities