---
title: "Bayesian Estimation of Generalized Process Capability Indices for Hybrid Type-II Censored Data via Importance Sampling"
author: "Shikhar Tyagi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Bayesian Estimation of Generalized Process Capability Indices for Hybrid Type-II Censored Data via Importance Sampling}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# Introduction

The **`gpcihybridIIImpSam`** package provides a comprehensive, generalized Bayesian framework for evaluating **Generalized Process Capability Indices (GPCIs)** under **Hybrid Type-II Censored Lifetime Data** using **Importance Sampling (Sampling Importance Resampling, SIR)**.

Under Hybrid Type-II censoring, $n$ identical units are placed on life test. The experiment terminates at:
$$T^* = \max(x_r, T_c)$$
where $r \le n$ is the target number of failures and $T_c > 0$ is the pre-fixed censoring time. The likelihood function is:
$$L(\theta \mid \mathbf{x}, r, T_c, n) = \left[ \prod_{i=1}^d f(x_i; \theta) \right] [S(T^*; \theta)]^{n - d}$$
where $d \ge r$ is the number of observed failures up to time $T^*$.

# Getting Started

```{r example-exponential}
library(gpcihybridIIImpSam)

# 1. User-supplied custom probability density, CDF, and survival functions
my_pdf  <- function(x, rate) stats::dexp(x, rate = rate)
my_cdf  <- function(q, rate) stats::pexp(q, rate = rate)
my_surv <- function(q, rate) stats::pexp(q, rate = rate, lower.tail = FALSE)

# 2. Observed Hybrid Type-II Censored Data
# n = 10 units placed on test, target r = 3, fixed censoring time Tc = 2.5
x_data <- c(0.4, 0.9, 1.5, 2.3)

# 3. Fit Importance Sampling Model
fit <- gpci_hybrid2_impsam(
  x = x_data,
  r = 3,
  tc = 2.5,
  n = 10,
  pdf = my_pdf,
  cdf = my_cdf,
  surv = my_surv,
  param_names = "rate",
  start = c(rate = 0.5),
  chain_length = 500,
  burn_in = 100,
  thinning = 1,
  USL = 8,
  LSL = 0,
  target = 4,
  indices = c("Cpy", "Cp", "Cpk", "Cpm", "CNpmc")
)

# 4. Print Summary
print(fit)
```

# Statistical Summary and Diagnostics

The package automatically reports:
- Posterior point estimate (posterior mean)
- Bias and Mean Squared Error (MSE)
- Bayes risk under squared error loss (posterior variance)
- Highest Posterior Density (HPD) credible intervals at 90%, 95%, and 99% levels
- Heidelberger and Welch's MCMC convergence diagnostics (stationarity test and half-width test)
- Convergence probability and empirical coverage probability

```{r summary-methods}
# Full posterior summary
summary(fit)

# Extract 95% HPD credible intervals for capability indices
confint(fit, what = "indices", level = 0.95)
```

# Goodness-of-Fit Testing

Goodness-of-fit testing for Hybrid Type-II censored data is supported via `gofPHCS`:

```{r gof-test}
dist_exp <- dist_exponential(rate = 0.5)
gof_res <- gof_test_hybrid2(
  fit = fit,
  statistic = "auto",
  p.method = "montecarlo",
  nsim = 50
)
print(gof_res)
```

# References

1. Alotaibi, N., Elbatal, I., Shrahili, M., et al. (2022). Estimation and Confidence Intervals of a New PCI $C_{Npmc}$ for Logistic-Exponential. *Journal of Mathematics*, 2022:3135264. <doi:10.1155/2022/3135264>.
2. Childs, A., Chandrasekar, B., Balakrishnan, N., & Kundu, D. (2003). Exact likelihood inference based on Type-I and Type-II hybrid censored samples from the exponential distribution. *Annals of the Institute of Statistical Mathematics*, 55(2), 319-330. <doi:10.1080/0266476032000053637>.
3. Dey, S., & Saha, M. (2019). The generalized process capability index $S_{pmk}$ for Gompertz distribution. *Quality and Reliability Engineering International*, 35(8), 2603-2615. <doi:10.1007/s41872-019-00081-4>.
4. Kundu, D., & Pradhan, B. (2009). Estimating the parameters of the generalized exponential distribution in presence of hybrid censoring. *Communications in Statistics - Simulation and Computation*, 38(10), 2030-2041. <doi:10.1016/j.spl.2008.09.006>.
5. Maiti, S. S., Saha, M., & Nanda, A. K. (2010). On generalizing process capability indices. *Quality Technology & Quantitative Management*, 7(3), 279-300. <doi:10.1080/16843703.2010.11673233>.
6. Saha, M., Smarandache, F., & Dey, S. (2022). Process capability index $C_{pc}$ for lifetime data. *Journal of Applied Statistics*, 49(8), 2011-2032. <doi:10.1080/02664763.2021.1971632>.
7. Saha, M., Tyagi, S., et al. (2024). Generalized process capability index $C_{Npmkc}$ for quality evaluation. *International Journal of Quality & Reliability Management*. <doi:10.1142/S021853932450013X>.
