---
title: "AccSamplingDesign: Acceptance Sampling Plan Design - R Package"
author: "Ha Truong"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{AccSamplingDesign: Acceptance Sampling Plan Design - R Package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# Introduction

The **AccSamplingDesign** package provides tools to create and evaluate 
acceptance sampling plans for both attribute and variable quality data. The 
focus is on controlling producer's and consumer's risk specifications while 
minimizing required sample sizes.

The package supports:

* Attribute sampling plans (e.g., pass/fail outcomes)
* Variable sampling plans under Normal and Beta distributions
* Easy-to-use functions for computing acceptance probability and visualizing OC curves

# Installation

Install from CRAN:

```r
install.packages("AccSamplingDesign")
```

Or from GitHub:

```r
devtools::install_github("vietha/AccSamplingDesign")
```

# Attributes Sampling Example

```{r}
# Create an attribute plan with binomial assumption
plan_attr <- optPlan(
  PRQ = 0.01,   # Acceptable quality level (1%)
  CRQ = 0.05,   # Rejectable quality level (5%)
  alpha = 0.02, # Producer's risk
  beta = 0.15,  # Consumer's risk
  distribution = "binomial"
)

# Summary of the plan
summary(plan_attr)

# Probability of accepting 3% defective
accProb(plan_attr, 0.03)

# Plot the OC curve
plot(plan_attr)
```

# Variables Sampling Example (Normal Distribution, Known Sigma)

```{r}
# Create a variable plan assuming known sigma
plan_var <- optPlan(
  PRQ = 0.025,
  CRQ = 0.1,
  alpha = 0.05,
  beta = 0.10,
  distribution = "normal",
  sigma_type = "known"
)

# Summary
summary(plan_var)

# Plot OC curve
plot(plan_var)
```

# Variables Sampling Example (Normal Distribution, Unknown Sigma)

```{r}
# Create a variable plan assuming known sigma
plan_var2 <- optPlan(
  PRQ = 0.025,
  CRQ = 0.1,
  alpha = 0.05,
  beta = 0.10,
  distribution = "normal",
  sigma_type = "unknown"
)

# Summary
summary(plan_var2)
```

# Variables Sampling Example (Beta Distribution, Known Theta)

```{r}
# Create a variable plan using Beta distribution
plan_beta <- optPlan(
  PRQ = 0.05,
  CRQ = 0.2,
  alpha = 0.05,
  beta = 0.10,
  distribution = "beta",
  theta = 44000000,
  theta_type = "known",
  LSL = 0.00001          # Lower Specification Limit
)

# Summary
summary(plan_beta)

# Plot OC curve
plot(plan_beta)
# Plot OC curve be the process mean
plot(plan_beta, by = "mean")
```

# Variables Sampling Example (Beta Distribution, Unknown Theta)

```{r}
# Create a variable plan using Beta distribution
plan_beta2 <- optPlan(
  PRQ = 0.05,
  CRQ = 0.2,
  alpha = 0.05,
  beta = 0.10,
  distribution = "beta",
  theta = 44000000,
  theta_type = "unknown",
  LSL = 0.00001
)

# Summary
summary(plan_beta2)
```
# Custom Plan Comparison

```{r}
# Define range of defect rates
pd <- seq(0, 0.15, by = 0.001)

# Generate OC data from optimal plan
oc_opt <- OCdata(plan = plan_attr, pd = pd)

# Compare with manual plans
mplan1 <- manualPlan(n = plan_attr$n, c = plan_attr$c - 1, distribution = "binomial")
oc_alt1 <- OCdata(plan = mplan1, pd = pd)

# Plot comparison
plot(pd, oc_opt$paccept, type = "l", col = "blue", lwd = 2,
     xlab = "Proportion Defective", ylab = "Probability of Acceptance",
     main = "OC Curves Comparison for Attributes Sampling Plan")
lines(pd, oc_alt1$paccept, col = "red", lwd = 2, lty = 2)
legend("topright", legend = c("Optimal Plan", "Manual Plan c - 1"),
       col = c("blue", "red"), lty = c(1, 2), lwd = 2)
```

# Additional Notes
This vignette provides a quick start for using the **AccSamplingDesign** package. 
For a full discussion of the statistical foundations, models, and optimization 
methods used, please refer to the foundation sources such as:

* Schilling, E.G., & Neubauer, D.V. (2017). Acceptance Sampling in Quality Control (3rd ed.). CRC Press.

* Wilrich, P.T. (2004). Single Sampling Plans for Inspection by Variables under a Variance Component Situation. In Frontiers in Statistical Quality Control 7.

* Govindaraju, K., & Kissling, R. (2015). Sampling plans for Beta-distributed compositional fractions. Quality Engineering, 27(1), 1–13.