Using drasR with Real Inspection Data

library(drasR)

Overview

This vignette demonstrates how drasR can be used with lot-level inspection data.

In real manufacturing or pharmaceutical inspection settings, users may not know the true defect probability. Instead, they usually observe the number of inspected units and the number of defects found in each lot.

Example inspection data

raw_data <- data.frame(
  lot_id = paste0("L", 1:12),
  date = as.Date("2026-01-01") + 0:11,
  supplier = c(rep("Supplier_A", 6), rep("Supplier_B", 6)),
  lot_size = rep(1000, 12),
  sample_size = rep(100, 12),
  defects_found = c(1, 0, 1, 2, 1, 2, 4, 5, 6, 4, 7, 8)
)

raw_data
##    lot_id       date   supplier lot_size sample_size defects_found
## 1      L1 2026-01-01 Supplier_A     1000         100             1
## 2      L2 2026-01-02 Supplier_A     1000         100             0
## 3      L3 2026-01-03 Supplier_A     1000         100             1
## 4      L4 2026-01-04 Supplier_A     1000         100             2
## 5      L5 2026-01-05 Supplier_A     1000         100             1
## 6      L6 2026-01-06 Supplier_A     1000         100             2
## 7      L7 2026-01-07 Supplier_B     1000         100             4
## 8      L8 2026-01-08 Supplier_B     1000         100             5
## 9      L9 2026-01-09 Supplier_B     1000         100             6
## 10    L10 2026-01-10 Supplier_B     1000         100             4
## 11    L11 2026-01-11 Supplier_B     1000         100             7
## 12    L12 2026-01-12 Supplier_B     1000         100             8

Prepare the data

prepared <- prepare_lot_data(
  raw_data,
  lot_id = "lot_id",
  sample_size = "sample_size",
  defects = "defects_found",
  lot_size = "lot_size",
  date = "date",
  supplier = "supplier"
)

prepared
##    lot lot_id sample_size sample_defects lot_size       date supplier_id
## 1    1     L1         100              1     1000 2026-01-01  Supplier_A
## 2    2     L2         100              0     1000 2026-01-02  Supplier_A
## 3    3     L3         100              1     1000 2026-01-03  Supplier_A
## 4    4     L4         100              2     1000 2026-01-04  Supplier_A
## 5    5     L5         100              1     1000 2026-01-05  Supplier_A
## 6    6     L6         100              2     1000 2026-01-06  Supplier_A
## 7    7     L7         100              4     1000 2026-01-07  Supplier_B
## 8    8     L8         100              5     1000 2026-01-08  Supplier_B
## 9    9     L9         100              6     1000 2026-01-09  Supplier_B
## 10  10    L10         100              4     1000 2026-01-10  Supplier_B
## 11  11    L11         100              7     1000 2026-01-11  Supplier_B
## 12  12    L12         100              8     1000 2026-01-12  Supplier_B
##    observed_rate
## 1           0.01
## 2           0.00
## 3           0.01
## 4           0.02
## 5           0.01
## 6           0.02
## 7           0.04
## 8           0.05
## 9           0.06
## 10          0.04
## 11          0.07
## 12          0.08

Find a baseline acceptance sampling plan

plan <- find_plan(
  aql = 0.01,
  ltpd = 0.05,
  alpha = 0.05,
  beta = 0.10
)

plan
##     n c producer_risk consumer_risk
## 1 132 3    0.04425251     0.0992283

Apply DRAS monitoring

monitored <- dras_monitor(
  prepared,
  baseline_sample_size = plan$n,
  acceptance_number = plan$c,
  lambda = 0.40,
  warning_threshold = 0.02,
  tighten_factor = 2.00
)

monitored
##    lot lot_id sample_size sample_defects lot_size       date supplier_id
## 1    1     L1         100              1     1000 2026-01-01  Supplier_A
## 2    2     L2         100              0     1000 2026-01-02  Supplier_A
## 3    3     L3         100              1     1000 2026-01-03  Supplier_A
## 4    4     L4         100              2     1000 2026-01-04  Supplier_A
## 5    5     L5         100              1     1000 2026-01-05  Supplier_A
## 6    6     L6         100              2     1000 2026-01-06  Supplier_A
## 7    7     L7         100              4     1000 2026-01-07  Supplier_B
## 8    8     L8         100              5     1000 2026-01-08  Supplier_B
## 9    9     L9         100              6     1000 2026-01-09  Supplier_B
## 10  10    L10         100              4     1000 2026-01-10  Supplier_B
## 11  11    L11         100              7     1000 2026-01-11  Supplier_B
## 12  12    L12         100              8     1000 2026-01-12  Supplier_B
##    observed_rate  ewma_rate inspection_mode recommended_next_sample_size
## 1           0.01 0.01000000          normal                          132
## 2           0.00 0.00600000          normal                          132
## 3           0.01 0.00760000          normal                          132
## 4           0.02 0.01256000          normal                          132
## 5           0.01 0.01153600          normal                          132
## 6           0.02 0.01492160          normal                          132
## 7           0.04 0.02495296       tightened                          264
## 8           0.05 0.03497178       tightened                          264
## 9           0.06 0.04498307       tightened                          264
## 10          0.04 0.04298984       tightened                          264
## 11          0.07 0.05379390       tightened                          264
## 12          0.08 0.06427634       tightened                          264
##    lot_decision dras_signal
## 1        accept      normal
## 2        accept      normal
## 3        accept      normal
## 4        accept      normal
## 5        accept      normal
## 6        accept      normal
## 7        reject     warning
## 8        reject     warning
## 9        reject     warning
## 10       reject     warning
## 11       reject     warning
## 12       reject     warning

Summarise monitoring results

summarise_dras_monitor(monitored)
##   group n_lots accepted_lots rejected_lots normal_lots tightened_lots
## 1   All     12             6             6           6              6
##   warning_lots mean_observed_rate max_observed_rate mean_sample_size
## 1            6         0.03416667              0.08              100
##   total_sampled total_defects_found
## 1          1200                  41

Supplier-level summary

summarise_dras_monitor(monitored, by_supplier = TRUE)
##        group n_lots accepted_lots rejected_lots normal_lots tightened_lots
## 1 Supplier_A      6             6             0           6              0
## 2 Supplier_B      6             0             6           0              6
##   warning_lots mean_observed_rate max_observed_rate mean_sample_size
## 1            0         0.01166667              0.02              100
## 2            6         0.05666667              0.08              100
##   total_sampled total_defects_found
## 1           600                   7
## 2           600                  34

Write an audit report

temp_dir <- tempfile("dras_report_")
files <- write_dras_report(monitored, output_dir = temp_dir)
files
## [1] "/var/folders/j2/1c8pj_8j587ckrwklw3k4dnw0000gn/T//Rtmp3uw31a/dras_report_b55857e9a898/dras_lot_monitoring.csv"  
## [2] "/var/folders/j2/1c8pj_8j587ckrwklw3k4dnw0000gn/T//Rtmp3uw31a/dras_report_b55857e9a898/dras_overall_summary.csv" 
## [3] "/var/folders/j2/1c8pj_8j587ckrwklw3k4dnw0000gn/T//Rtmp3uw31a/dras_report_b55857e9a898/dras_supplier_summary.csv"

Interpretation

The DRAS workflow provides an audit-oriented way to monitor whether observed defect rates are drifting upward. When the EWMA statistic exceeds the warning threshold, the system recommends tightened inspection for subsequent lots.