---
title: "Multiple-Steps Step-Stress Accelerated Degradation Modeling"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Multiple-Steps Step-Stress Accelerated Degradation Modeling}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)
```

## Introduction

The **`MultiStepSSAD`** package provides generalized statistical tools for modeling **Multiple-Steps Step-Stress Accelerated Degradation Testing (SSADT)** data, implementing the methods developed by **Pan and Balakrishnan (2010)**.

In step-stress accelerated degradation experiments, products are subjected to elevated stress levels ($S_1 < S_2 < \dots < S_K$). Unlike traditional step-stress tests where stress levels are elevated at pre-determined fixed time points for all units, Pan and Balakrishnan (2010) proposed elevating stress levels when an individual product's degradation crosses pre-specified degradation threshold values ($\omega_1, \dots, \omega_{K-1}$). Consequently, the stress transition times ($\tau_{i,k}$) vary randomly from product to product.

This package supports:
1. **Wiener Process Degradation Models**: Drift parameter changes with stress level; diffusion parameter $\sigma$ remains constant.
2. **Gamma Process Degradation Models**: Monotone degradation process with independent Gamma-distributed increments. Stress transition times are modeled using the **Birnbaum-Saunders** distribution approximation.
3. **Acceleration Models**: Arrhenius model $A(S) = \exp(a + b / (273 + S))$ and Power model $A(S) = a \cdot S^b$.
4. **Estimation Methods**: Maximum Likelihood Estimation (MLE) and Bayesian Markov Chain Monte Carlo (MCMC).
5. **Diagnostics & Lifetime Prediction**: Geweke's MCMC convergence diagnostic, Mean Time To Failure (MTTF), and reliability $R(t)$ evaluation under normal use stress $S_0$.

---

## 1. Package Datasets

The package includes the exact simulated datasets from Tables 2–5 of Pan and Balakrishnan (2010):

```{r datasets}
library(MultiStepSSAD)

# Load Wiener-Arrhenius dataset (Table 2)
data(wiener_arrhenius)
head(wiener_arrhenius)

# Load Gamma-Arrhenius dataset (Table 4)
data(gamma_arrhenius)
head(gamma_arrhenius)
```

---

## 2. Fitting Step-Stress Accelerated Degradation Models

### 2.1. Wiener Process Model with Arrhenius Acceleration

We fit a 3-step step-stress Wiener-Arrhenius model to `wiener_arrhenius` data using Maximum Likelihood Estimation (MLE):

```{r wiener_mle}
fit_w_mle <- ssad_fit(
  data = wiener_arrhenius,
  process = "wiener",
  model = "arrhenius",
  stress_levels = c(45, 65, 85),
  thresholds = c(90, 160),
  method = "mle"
)

summary(fit_w_mle)
```

### 2.2. Gamma Process Model with Arrhenius Acceleration

Next, we fit a 3-step step-stress Gamma-Arrhenius model to `gamma_arrhenius` data using Bayesian MCMC:

```{r gamma_mcmc}
fit_g_mcmc <- ssad_fit(
  data = gamma_arrhenius,
  process = "gamma",
  model = "arrhenius",
  stress_levels = c(45, 65, 85),
  thresholds = c(90, 160),
  method = "mcmc",
  n_iter = 1000,
  burnin = 200
)

summary(fit_g_mcmc)
```

---

## 3. MCMC Convergence Diagnostics

The package includes Geweke's MCMC convergence diagnostic (`geweke_diag()`):

```{r geweke_diag}
if (!is.null(fit_g_mcmc$chain)) {
  g_res <- geweke_diag(fit_g_mcmc$chain)
  print(g_res)
}
```

---

## 4. Lifetime Prediction and Reliability Analysis

Using the fitted parameters, we can predict the reliability function $R(t)$ and Mean Time To Failure (MTTF) under normal operating stress conditions ($S_0 = 25^\circ\text{C}$) and failure threshold $\omega_F = 200$:

```{r reliability_pred}
rel_pred <- predict(
  fit_w_mle,
  t = seq(100, 10000, by = 200),
  S0 = 25,
  omega_F = 200
)

cat("Predicted MTTF under use stress S0 = 25:", round(rel_pred$mttf, 2), "hours\n")
```

---

## 5. Simulating Step-Stress Degradation Data

We can generate simulated step-stress degradation datasets for power calculation and experimental planning using `ssad_simulate()`:

```{r simulation}
set.seed(123)
sim_df <- ssad_simulate(
  n_units = 5,
  times = seq(72, 2160, by = 72),
  stress_levels = c(45, 65, 85),
  thresholds = c(90, 160),
  process = "wiener",
  model = "power",
  params = c(a = 7.39e-4, b = 1.2, sigma = 0.1)
)

head(sim_df)
```

---

## References

- Pan, Z., & Balakrishnan, N. (2010). Multiple-Steps Step-Stress Accelerated Degradation Modeling Based on Wiener and Gamma Processes. *Communications in Statistics - Simulation and Computation*, 39(7), 1384–1402. [doi:10.1080/03610918.2010.496060](https://doi.org/10.1080/03610918.2010.496060)
- Park, C., & Padgett, W. J. (2005). Accelerated degradation models for failure based on geometric Brownian motion and gamma processes. *Lifetime Data Analysis*, 11(4), 511–527.
