AccSamplingDesign: Acceptance Sampling Plan Design - R Package

Ha Truong

2025-04-29

1. Introduction

The AccSamplingDesign package provides tools for designing acceptance sampling plans for both attribute and variable data. Key features include:

2. Installation

# Install from GitHub
devtools::install_github("vietha/AccSamplingDesign")

# Load package
library(AccSamplingDesign)

3. Attribute Sampling Plans

3.1 Create Attribute Plan

plan_attr <- optAttrPlan(
  PRQ = 0.01,   # Acceptable Quality Level (1% defects)
  CRQ = 0.05,   # Rejectable Quality Level (5% defects)
  alpha = 0.05, # Producer's risk
  beta = 0.10   # Consumer's risk
)

3.2 Plan Summary

summary(plan_attr)
#> Attribute Acceptance Sampling Plan
#> ----------------------------------
#> Distribution: binomial 
#> Sample Size (n): 132 
#> Acceptance Number (c): 3 
#> Producer's Risk (PR = 0.04425251 ) at PRQ = 0.01 
#> Consumer's Risk (CR = 0.0992283 ) at CRQ = 0.05 
#> ---------------------------------

3.3 Acceptance Probability

# Probability of accepting 3% defective lots
accProb(plan_attr, 0.03)
#> [1] 0.4384354

3.4 OC Curve

plot(plan_attr)

4. Variable Sampling Plans

4.1 Normal Distribution

4.1.1 Find an optimal plan and plot OC chart

norm_plan <- optVarPlan(
  PRQ = 0.025,       # Acceptable quality level (% nonconforming)
  CRQ = 0.1,         # Rejectable quality level (% nonconforming)
  alpha = 0.05,      # Producer's risk
  beta = 0.1,        # Consumer's risk
  distribution = "normal",
  sigma_type = "known"
)

summary(norm_plan)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: normal 
#> Sample Size (n): 19 
#> Acceptance Limit (k): 1.579 
#> Population Standard Deviation: known 
#> Producer's Risk (PR = 0.05 ) at PRQ = 0.025 
#> Consumer's Risk (CR = 0.1 ) at CRQ = 0.1 
#> ---------------------------------

# Generate OC curve data
oc_data_normal <- OCdata(norm_plan)
# show data of Proportion Nonconforming (x_p) vs Probability Acceptance (y)
#head(oc_data_normal, 15) 
plot(norm_plan)

4.1.2 Compare known vs unknown sigma plans

p1 = 0.005
p2 = 0.03
alpha = 0.05
beta = 0.1

# known sigma plan
plan1 <- optVarPlan(
  PRQ = p1,        # Acceptable quality level (% nonconforming)
  CRQ = p2,         # Rejectable quality level (% nonconforming)
  alpha = alpha,      # Producer's risk
  beta = beta,        # Consumer's risk
  distribution = "normal",
  sigma_type = "know")
summary(plan1)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: normal 
#> Sample Size (n): 18 
#> Acceptance Limit (k): 2.185 
#> Population Standard Deviation: known 
#> Producer's Risk (PR = 0.05 ) at PRQ = 0.005 
#> Consumer's Risk (CR = 0.1 ) at CRQ = 0.03 
#> ---------------------------------
plot(plan1)


# unknown sigma plan
plan2 <- optVarPlan(
  PRQ = p1,        # Acceptable quality level (% nonconforming)
  CRQ = p2,         # Rejectable quality level (% nonconforming)
  alpha = alpha,      # Producer's risk
  beta = beta,        # Consumer's risk
  distribution = "normal",
  sigma_type = "unknow")
summary(plan2)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: normal 
#> Sample Size (n): 62 
#> Acceptance Limit (k): 2.192 
#> Population Standard Deviation: unknown 
#> Producer's Risk (PR = 0.05 ) at PRQ = 0.005 
#> Consumer's Risk (CR = 0.1 ) at CRQ = 0.03 
#> ---------------------------------
plot(plan2)


# Generate OC curve data
oc_data1 <- OCdata(plan1)
oc_data2 <- OCdata(plan2)

# Plot the first OC curve (solid red line)
plot(oc_data1@pd, oc_data1@paccept, type = "l", col = "red", lwd = 2,
     main = "Operating Characteristic (OC) Curve", 
     xlab = "Proportion Nonconforming", 
     ylab = "P(accept)")

# Add the second OC curve (dashed blue line)
lines(oc_data2@pd, oc_data2@paccept, col = "blue", lwd = 2, lty = 2)

abline(v = c(p1, p2), lty = 1, col = "gray")
abline(h = c(1 - alpha, beta), lty = 1, col = "gray")

legend1 = paste0("Known Sigma (n=", plan1$sample_size, ", k=", plan1$k, ")")
legend2 = paste0("Unknown Sigma (n=", plan2$sample_size, ", k=", plan2$k, ")")
# Add a legend to distinguish the two curves
legend("topright", legend = c(legend1, legend2), 
       col = c("red", "blue"), lwd = 2, lty = c(1, 2))

# Add a grid
grid()

4.2 Beta Distribution

4.2.1 Find an optimal plan and plot OC chart

beta_plan <- optVarPlan(
  PRQ = 0.05,        # Target quality level (% nonconforming)
  CRQ = 0.2,         # Minimum quality level (% nonconforming)
  alpha = 0.05,      # Producer's risk
  beta = 0.1,        # Consumer's risk
  distribution = "beta",
  theta = 44000000,
  theta_type = "known",
  LSL = 0.00001
)
summary(beta_plan)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: beta 
#> Sample Size (n): 14 
#> Acceptance Limit (k): 1.186 
#> Population Precision Parameter (theta): known 
#> Producer's Risk (PR = 0.04999926 ) at PRQ = 0.05 
#> Consumer's Risk (CR = 0.09976476 ) at CRQ = 0.2 
#> Lower Specification Limit: 1e-05 
#> ---------------------------------

# Plot OC use plot function
plot(beta_plan)


# Generate OC data
p_seq <- seq(0.005, 0.5, by = 0.005)
oc_data <- OCdata(beta_plan, pd = p_seq)
#head(oc_data)

# plot use S3 method
plot(oc_data)


# Plot the OC curve with Mean Level (x_m) and Probability of Acceptance (y)
plot(oc_data@pd, oc_data@paccept, type = "l", col = "blue", lwd = 2,
     main = "OC Curve by the mean levels (plot by data)", xlab = "Mean Levels",
     ylab = "P(accept)")
grid()

4.2.2 Compare known vs unknown theta plans

p1 = 0.005
p2 = 0.03
alpha = 0.05
beta = 0.1
spec_limit = 0.05 # use for Beta distribution
theta = 500

# My package for beta plan
beta_plan1 <- optVarPlan(
  PRQ = p1,       # Target quality level (% nonconforming)
  CRQ = p2,       # Minimum quality level (% nonconforming)
  alpha = alpha,      # Producer's risk
  beta = beta,        # Consumer's risk
  distribution = "beta",
  theta = theta,
  theta_type = "known",
  USL = spec_limit
)
summary(beta_plan1)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: beta 
#> Sample Size (n): 16 
#> Acceptance Limit (k): 2.484 
#> Population Precision Parameter (theta): known 
#> Producer's Risk (PR = 0.04999959 ) at PRQ = 0.005 
#> Consumer's Risk (CR = 0.09980403 ) at CRQ = 0.03 
#> Uper Specification Limit: 0.05 
#> ---------------------------------

beta_plan2 <- optVarPlan(
  PRQ = p1,       # Target quality level (% nonconforming)
  CRQ = p2,       # Minimum quality level (% nonconforming)
  alpha = alpha,      # Producer's risk
  beta = beta,        # Consumer's risk
  distribution = "beta",
  theta = theta,
  theta_type = "unknown",
  USL = spec_limit
)
summary(beta_plan2)
#> Variable Acceptance Sampling Plan
#> ---------------------------------
#> Distribution: beta 
#> Sample Size (n): 66 
#> Acceptance Limit (k): 2.484 
#> Population Precision Parameter (theta): unknown 
#> Producer's Risk (PR = 0.04653701 ) at PRQ = 0.005 
#> Consumer's Risk (CR = 0.09489874 ) at CRQ = 0.03 
#> Uper Specification Limit: 0.05 
#> ---------------------------------

# Generate OC curve data
oc_beta_data1 <- OCdata(beta_plan1)
oc_beta_data2 <- OCdata(beta_plan2)

# Plot the first OC curve (solid red line)
plot(oc_beta_data1@pd, oc_beta_data1@paccept, type = "l", col = "red", lwd = 2,
     main = "Operating Characteristic (OC) Curve", 
     xlab = "Proportion Nonconforming", 
     ylab = "P(accept)")

# Add the second OC curve (dashed blue line)
lines(oc_beta_data2@pd, oc_beta_data2@paccept, col = "blue", lwd = 2, lty = 2)

abline(v = c(p1, p2), lty = 1, col = "gray")
abline(h = c(1 - alpha, beta), lty = 1, col = "gray")

legend1 = paste0("Known Theta (n=", beta_plan1$sample_size, ", k=", beta_plan1$k, ")")
legend2 = paste0("Unknown Theta (n=", beta_plan2$sample_size, ", k=", beta_plan2$k, ")")
# Add a legend to distinguish the two curves
legend("topright", legend = c(legend1, legend2), 
       col = c("red", "blue"), lwd = 2, lty = c(1, 2))

# Add a grid
grid()

4.3 Variable Plan Acceptance Probability

# Probability of accepting 10% defective
accProb(norm_plan, 0.1)
#> [1] 0.0997

# Probability of accepting 5% defective
accProb(beta_plan, 0.05)
#> [1] 0.9498741

6. Technical Specifications

6.1 Attribute Plan:

The Probability of Acceptance (\(Pa\)) is given by: \[Pa(p) = \sum_{i=0}^c \binom{n}{i}p^i(1-p)^{n-i}\]
where:

6.2 Normal Variable Plan (Case of Known \(\sigma\)):

The Probability of Acceptance (\(Pa\)) is given by:

\[ Pa(p) = \Phi\left( -\sqrt{n_{\sigma}} \cdot (\Phi^{-1}(p) + k_{\sigma}) \right) \]

Or we could write:

\[ Pa(p) = 1 - \Phi\left( \sqrt{n_{\sigma}} \cdot (\Phi^{-1}(p) + k_{\sigma}) \right) \]

where:

The required sample size (\(n_{\sigma}\)) and acceptance constant (\(k_{\sigma}\)) are: \[ n_{\sigma} = \left( \frac{\Phi^{-1}(1 - \alpha) + \Phi^{-1}(1 - \beta)}{\Phi^{-1}(1 - PRQ) - \Phi^{-1}(1 - CRQ)} \right)^2 \]

\[ k_{\sigma} = \frac{\Phi^{-1}(1 - PRQ) \cdot \Phi^{-1}(1 - \beta) + \Phi^{-1}(1 - CRQ) \cdot \Phi^{-1}(1 - \alpha)}{\Phi^{-1}(1 - \alpha) + \Phi^{-1}(1 - \beta)} \] where:

6.3 Normal Variable Plan (Case of Unknown \(\sigma\)):

The formula for the probability of acceptance (\(Pa\)) is:

\[ Pa(p) = \Phi \left( \sqrt{\frac{n_s}{1 + \frac{k_s^2}{2}}} \left( \Phi^{-1}(1 - p) - k_s \right) \right) \]

where:

\[ n_s = n_{\sigma} \times \left( 1 + \frac{k_s^2}{2} \right) \]

(See Wilrich, PT. (2004) for more detail about calculation used in sessions 6.2 and 6.3)

6.4 Beta Variable Plan (Case of Known \(\theta\)):

Traditional acceptance sampling using normal distributions can be inadequate for compositional data bounded within [0,1]. Govindaraju and Kissling (2015) proposed Beta-based plans, where component proportions (e.g., protein content) follow \(X \sim \text{Beta}(a, b)\), with density:

\[ f(x; a, b) = \frac{x^{a-1} (1 - x)^{b-1}}{B(a, b)}, \]

where \(B(a, b)\) is the Beta function. The distribution is reparameterized via mean \(\mu\) and precision \(\theta\):

\[ \mu = \frac{a}{a + b}, \quad \theta = a + b, \quad \sigma^2 \approx \frac{\mu(1 - \mu)}{\theta} \quad (\text{for large } \theta). \]

Higher \(\theta\) reduces variance, concentrating values around \(\mu\). The probability of acceptance (\(Pa\)) parallels normal-based plans:

\[ Pa = P(\mu - k \sigma \geq L \mid \mu, \theta, m, k), \]

where \(L\) is the lower specification limit, \(m\) is the sample size, and \(k\) is the acceptability constant. Parameters \(m\) and \(k\) ensure:

\[ Pa(\mu_{PRQ}) = 1 - \alpha, \quad Pa(\mu_{CRQ}) = \beta, \]

with \(\alpha\) (producer’s risk) and \(\beta\) (consumer’s risk) at specified quality levels (PRQ/CRQ).

Note that: this problem is solved to find \(m\) and \(k\) used Non-linear programming and Monte Carlo simulation.

Implementation Note:

For a nonconforming proportion \(p\) (e.g.,PRQ or CRQ), the mean \(\mu\) at a quality level (PRQ/CRQ) is derived by solving:

\[ P(X \leq L \mid \mu, \theta) = p, \]

where \(X \sim \text{Beta}(\theta \mu, \theta (1 - \mu))\). This links \(\mu\) to \(p\) via the Beta cumulative distribution function (CDF) at \(L\).

6.5 Beta Variable Plan (Case of Unknown \(\theta\)):

For a beta distribution, the required sample size \(m_s\) (unknown \(\theta\)) is derived from the known-\(\theta\) sample size \(m_\theta\) using the formula:
\[ m_s = \left(1 + 0.5k^2\right)m_\theta \]
where \(k\) is unchanged. This adjustment accounts for the variance ratio \(R = \frac{\text{Var}(S)}{\text{Var}(\hat{\mu})}\), which quantifies the relative variability of the sample standard deviation \(S\) compared to the estimator \(\hat{\mu}\). Unlike the normal distribution, where \(\text{Var}(S) \approx \frac{\sigma^2}{2n}\), for beta distribution’s, the ratio \(R\) depends on \(\mu\), \(\theta\), and sample size \(m\). The conservative factor \(0.5k^2\) approximates this ratio for practical use.

7. References

  1. Schilling, E.G., & Neubauer, D.V. (2017). Acceptance Sampling in Quality Control (3rd ed.). Chapman and Hall/CRC. https://doi.org/10.4324/9781315120744
  2. Wilrich, PT. (2004). Single Sampling Plans for Inspection by Variables under a Variance Component Situation. In: Lenz, HJ., Wilrich, PT. (eds) Frontiers in Statistical Quality Control 7. Frontiers in Statistical Quality Control, vol 7. Physica, Heidelberg. https://doi.org/10.1007/978-3-7908-2674-6_4
  3. K. Govindaraju and R. Kissling (2015). Sampling plans for Beta-distributed compositional fractions. Quality Engineering, vol. 27, no. 1, pp. 1-13. https://doi.org/10.1016/j.chemolab.2015.12.009
  4. ISO 2859-1:1999 - Sampling procedures for inspection by attributes
  5. ISO 3951-1:2013 - Sampling procedures for inspection by variables