A representative from a sales department purchases towels from a vendor to give away at trade shows. The sales representative wants to implement a sampling plan so that the entire lot of towels is either accepted or rejected. The count for the analysis will be the number of defects. Each towel can have more than one defect, such as a pulled thread or a variation in color. The sales representative and the vendor agree on the following requirements:
  • The AQL for the sampling plan is 0.01. The alpha for the analysis is 0.05. The probability to accept a lot with 1 defect per 100 towels is at least 95%.
  • The RQL is 0.06. The beta for the analysis is 0.10. The probability to reject a lot with 6 defects per 100 towels is at least 90%.

The script approximates the values from Minitab Statistical Software for Attributes Acceptance Sampling when the measurement type is the number of defects and the unit for quality levels is defects per unit.

The example R script demonstrates the following capabilities of the integration:
  • Read values from columns in Minitab Statistical Software.
  • Send a table of values to the Minitab Output pane.
  • Display warnings and messages from R standard error in the Minitab Output pane.
Use the following files to perform the steps in this section:
File Description
acceptance_sampling.R An R script that takes specifications from columns in a Minitab worksheet and displays a table of values for the OC curve.

All the files referenced in this guide are available in this .ZIP file: r_guide_files.zip.

Prerequisites

  • The script relies on the column names to give the specifications for the analysis: AQL, alpha, RQL, beta.

  • The R script in the below example requires the following R packages:
    mtbr
    The R package that integrates Minitab and R. In the example, functions from this module send R results to Minitab. For information on how to install Minitab's R package, go to Step 2: Install mtbr.

Steps to run the example

  1. Ensure you have installed the required modules: mtbr.
  2. Save the R script file, acceptance_sampling.R, to your Minitab default file location. For more information on where Minitab looks for R script files, go to Default folders for R files for Minitab.
  3. Open the sample data set R_acceptance_sampling.MWX.
  4. In the Minitab Command Line pane, enter RSCR "acceptance_sampling.R".
  5. Select Run.

acceptance_sampling.R

# Load necessary library
#Original code by Valentina Tillman
library(mtbr)

# Define Parameters
AQL <- mtb_get_column("AQL")  # Acceptable Quality Level
alpha <- mtb_get_column("alpha")  # Producer's risk
RQL <- mtb_get_column("RQL")  # Rejectable Quality Level
beta <- mtb_get_column("beta")  # Consumer's risk

# ---- Validate inputs ----

# Validate AQL and RQL
if (is.null(AQL) || length(AQL) != 1 || is.na(AQL)) {
  warning("Define the AQL with a value in the first row of a column with the title AQL.")
}
if (is.null(RQL) || length(RQL) != 1 || is.na(RQL)) {
  warning("Define the RQL with a value in the first row of a column with the title RQL.")
}

# Validate alpha
if (is.null(alpha) || length(alpha) != 1 || is.na(alpha)) {
  alpha <- 0.05
  message("Missing or multiple values for alpha. The analysis uses 0.05. To specify alpha, enter a value in the first row of a column with the title alpha.")
}

# Validate beta
if (is.null(beta) || length(beta) != 1 || is.na(beta)) {
  beta <- 0.10
  message("Missing or multiple values for beta. The analysis uses 0.10. To specify beta, enter a value in the first row of a column with the title beta.")
}

#Simple comparison of AQL and RQL
if (AQL >= RQL) {
  stop("Specify an RQL that is greater than the AQL.")
}

# ---- Calculations ----
# Use algorithm to find n,c for the Poisson distribution
found <- FALSE
for (n in 10:5000) {
  lamA <- n*AQL
  lamR <- n*RQL
  for (c in 0:n) {
    p_accept_AQL <- ppois(c, lamA)
    p_accept_RQL <- ppois(c, lamR)
    if (p_accept_AQL >= 1 - alpha && p_accept_RQL <= beta) {
      found <- TRUE
      break
    }
  }
  if (found) break
}

if (found != TRUE) { 
  mtb_add_message ("No solution found. Results are for the largest values of n and c in the search.")
  }

# Generate OC curve values for number of defects per unit
p_vals <- seq(0.003, 0.117, by = 0.006)
lambda_vals <- p_vals * n
OC_vals <- ppois(c, lambda_vals)

# ---- Outputs ----
#Display analysis inputs
show_AQL <- sprintf("AQL: %s", sub("\\.?0+$", "", formatC(AQL, format = "f", digits = 6)))
mtb_add_message(show_AQL)
show_RQL <- sprintf("RQL: %s", sub("\\.?0+$", "", formatC(RQL, format = "f", digits = 6)))
mtb_add_message(show_RQL)
show_alpha <- sprintf("alpha: %s", sub("\\.?0+$", "", formatC(alpha, format = "f", digits = 6)))
mtb_add_message(show_alpha)
show_beta <- sprintf("beta: %s", sub("\\.?0+$", "", formatC(beta, format = "f", digits = 6)))
mtb_add_message(show_beta)

#Display calculated solutions
show_sample_size <- sprintf("Sample size: %d", n)
mtb_add_message(show_sample_size)
show_defects <- sprintf("Acceptance number: %d", c)
mtb_add_message(show_defects)

# OC Curve Table
oc_table <- data.frame("Lot_Defects_per_Unit" = p_vals, "Probability_of_Acceptance" = OC_vals)
head(oc_table, 10)

mytitle <- "OC Table" 
myheaders <- names(oc_table)
mtb_add_table(columns = oc_table, headers = myheaders, title = mytitle)

Results

R Script

These results are from external software.
AQL: 0.01
RQL: 0.06
alpha: 0.05
beta: 0.1
Sample size: 112
Acceptance number: 3

OC Table

Lot_Defects_per_UnitProbability_of_Acceptance
0.0030.999593
0.0090.980517
0.0150.909779
0.0210.788694
0.0270.641855
0.0330.494988
0.0390.365054
0.0450.259456
0.0510.178812
0.0570.120085
0.0630.078892
0.0690.050862
0.0750.032260
0.0810.020172
0.0870.012457
0.0930.007607
0.0990.004600
0.1050.002757
0.1110.001639
0.1170.000968