---
title: "Generalized Goodness-of-Fit Test for Censored Data with Gofpt2"
author: "Shikhar Tyagi, Arvind Pandey, Bhupendra Singh, Vrijesh Tripathi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generalized Goodness-of-Fit Test for Censored Data with Gofpt2}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(Gofpt2)
```

## Introduction

The **Gofpt2** package provides a generalized goodness-of-fit test based on spacings for general progressive Type-II censored data. The test statistic is based on the methodology proposed by **Qin et al. (2022)**, extending the foundational work of **Balakrishnan et al. (2003)**.

In life testing and reliability studies, failure data are often progressively Type-II censored. Under general progressive Type-II censoring:
- $n$ total units are placed on test.
- The first $r$ failures are not observed.
- Starting from the $(r+1)$-th failure, $m - r$ failure times are recorded: $X_{r+1:m:n} < X_{r+2:m:n} < \dots < X_{m:m:n}$.
- At each observed failure time $X_{r+j:m:n}$, $R_{r+j}$ surviving units are randomly withdrawn from the test.

The `Gofpt2` package allows users to test whether observed censored failure times follow any hypothesized continuous distribution by providing custom probability density (`pdf_func`), cumulative distribution (`cdf_func`), and survival functions (`survival_func`).

---

## 1. Real Data Example: Insulating Fluid Breakdown Times

We illustrate the basic workflow using the insulating fluid failure dataset from **Example 6.1** of Qin et al. (2022).

In this experiment:
- Total sample size: $n = 19$
- Total failures: $m = 11$
- Initial unobserved failures: $r = 2$
- Removals scheme: $R = (0, 0, 2, 0, 0, 2, 0, 0, 4)$ for the 9 observed failure times.

```r
library(Gofpt2)

# Define censoring scheme
scheme <- list(
  n = 19,
  m = 11,
  r = 2,
  R = c(0, 0, 2, 0, 0, 2, 0, 0, 4)
)

# Observed breakdown times (length m - r = 9)
obs_data <- c(3.16, 4.15, 4.67, 7.35, 8.01, 8.27, 32.52, 33.91, 36.71)

# Perform goodness-of-fit test against Exponential distribution using Normal Approximation
test_res <- gof_test_censored(
  data = obs_data,
  censoring_scheme = scheme,
  method = "normal"
)

# Display results
print(test_res)
summary(test_res)
```

The test statistic $T = 0.35463$ lies well within the critical bounds $[0.41463, 0.86290]$ (with $p$-value $\approx 0.4095$), indicating no evidence to reject the exponential null hypothesis.

---

## 2. Comparing Normal Approximation vs Monte Carlo Simulation

The package supports both analytical **normal approximation** (Theorem 4.1, Qin et al. 2022) and **Monte Carlo simulation**.

```r
# Test using Monte Carlo Simulation (10,000 replicates)
test_sim <- gof_test_censored(
  data = obs_data,
  censoring_scheme = scheme,
  method = "simulation",
  n_sim = 10000
)

# Print comparison
cat("Normal Approx p-value:", test_res$p_value, "\n")
cat("Simulation p-value   :", test_sim$p_value, "\n")
```

---

## 3. Testing Custom Non-Exponential Distributions

Users can test data against any custom distribution by providing its CDF and Survival function.

```r
# Define custom Weibull distribution parameters (shape = 1.5, scale = 10)
weibull_cdf <- function(x, shape, scale) pweibull(x, shape = shape, scale = scale)
weibull_surv <- function(x, shape, scale) 1 - pweibull(x, shape = shape, scale = scale)

# Generate synthetic data from Weibull distribution
set.seed(42)
weibull_data <- generate_progressive_censored(
  censoring_scheme = scheme,
  cdf_func = weibull_cdf,
  parameters = list(shape = 1.5, scale = 10)
)

# Test whether data fits the hypothesized Weibull distribution
fit_weibull <- gof_test_censored(
  data = weibull_data,
  censoring_scheme = scheme,
  cdf_func = weibull_cdf,
  survival_func = weibull_surv,
  parameters = list(shape = 1.5, scale = 10),
  method = "normal"
)

print(fit_weibull)
```

---

## 4. Visualizing Results

The `plot()` method displays the null distribution of the test statistic $T$ with vertical indicators for the observed test statistic and critical bounds:

```r
plot(test_res)
```

---

## References

1. **Qin, X., Gui, W., & Balakrishnan, N. (2022).** A goodness-of-fit test for exponential distribution based on spacings for general progressive Type-II censored data. *Journal of Applied Statistics*, 49(8), 1821-1636. \doi{10.1080/02664763.2020.1821613}
2. **Balakrishnan, N., Ng, H.K.T., & Kannan, N. (2003).** A test of exponentiality based on spacings for progressively type-II censored data. In *Goodness-of-Fit Tests and Model Validity* (pp. 89-111). Birkhäuser, Boston, MA.
3. **Balakrishnan, N., & Aggarwala, R. (2000).** *Progressive Censoring: Theory, Methods, and Applications*. Birkhäuser, Boston.
