---
title: "Getting Started with gpci"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting Started with gpci}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

The `gpci` package provides a distribution-agnostic framework for calculating Process Capability Indices (PCIs), performing bootstrap confidence interval estimation, and running bootstrap cross-validation coverage diagnostics.

This vignette demonstrates standard normal-theory capability analysis.

## Setup

First, load the package and `ggplot2`:

```{r setup}
library(gpci)
library(ggplot2)
```

## Simulating Process Data

We simulate a quality characteristic $X \sim N(10, 1.2^2)$ from a stable process. We set specification limits:
* Lower Specification Limit (LSL) = 7
* Upper Specification Limit (USL) = 13
* Target ($T$) = 10

```{r sim-data}
set.seed(123)
process_data <- rnorm(100, mean = 9.8, sd = 1.1)
```

## Capability Analysis

We construct a standard normal distribution object and fit it to the data using Maximum Likelihood Estimation (MLE):

```{r capability-fit}
# Create standard normal distribution template
dist_norm <- dist_normal()

# Compute capability indices (moment-based and quantile-based)
fit <- capability(
  data = process_data,
  distribution = dist_norm,
  USL = 13,
  LSL = 7,
  target = 10,
  indices = c("Cp", "Cpk", "Cpl", "Cpu", "Cpm", "Cpmk", "Spmk", "Cpc"),
  fit = TRUE,
  fit_method = "mle",
  mode = "moments"
)

# Print results
print(fit)
```

## Bootstrap Confidence Intervals

Next, we compute bootstrap confidence intervals at multiple significance levels ($\alpha = 0.10, 0.05, 0.01$) using the percentile bootstrap:

```{r bootstrap-ci}
# Calculate CIs
ci <- boot_ci(
  fit = fit,
  B = 30, # Optimized B for fast vignette generation
  alpha = c(0.10, 0.05, 0.01),
  method = "percentile",
  type = "parametric"
)

# View CI table
print(ci)
```

## Plotting Results

The package provides S3 plot methods for visualizing the process capability:

### 1. Process Density and Specification Limits

```{r plot-density}
plot(fit, type = "density")
```

### 2. Empirical CDF vs Fitted CDF

```{r plot-cdf}
plot(fit, type = "cdf")
```

### 3. Quantile-Quantile (Q-Q) Plot

```{r plot-qq}
plot(fit, type = "qq")
```

### 4. Process Run Chart

```{r plot-run}
plot(fit, type = "run")
```

### 5. Bootstrap Sampling Distributions and Confidence Intervals

We can also visualize the bootstrap results:

```{r plot-boot}
plot(ci, type = "boot")
```

### 6. Forest Plot of CIs

```{r plot-forest}
plot(ci, type = "forest")
```
