| Title: | Drift-Aware Risk-Guaranteed Acceptance Sampling |
|---|---|
| Description: | Implements drift-aware risk-guaranteed acceptance sampling methods for adaptive lot disposition. The package provides tools for classical acceptance sampling, producer-risk and consumer-risk assessment, exponentially weighted moving average drift monitoring, adaptive tightening policies, decision fragility analysis, and safety-cost Pareto frontier construction. Classical binomial acceptance-plan design is described in the NIST/SEMATECH e-Handbook of Statistical Methods <https://www.itl.nist.gov/div898/handbook/pmc/section2/pmc232.htm>. Exponentially weighted moving averages follow Roberts (1959) <doi:10.1080/00401706.1959.10489860>. The methods are intended for simulation studies, supplier quality monitoring, and audit-oriented inspection decision support. |
| Authors: | Muhammad Zahir Khan [aut, cre] |
| Maintainer: | Muhammad Zahir Khan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-11-06 21:15:07 UTC |
| Source: | https://github.com/cran/drasR |
Apply a Fixed Acceptance Sampling Plan
apply_fixed_plan(lots, n, c, aql = 0.01)apply_fixed_plan(lots, n, c, aql = 0.01)
lots |
Data frame with column |
n |
Sample size. |
c |
Acceptance number. |
aql |
Acceptable quality level. |
Data frame with lot decisions.
plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(20) lots <- simulate_lots(p) apply_fixed_plan(lots, n = plan$n, c = plan$c)plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(20) lots <- simulate_lots(p) apply_fixed_plan(lots, n = plan$n, c = plan$c)
Computes consumer risk, defined as the probability of accepting a lot at the limiting quality level.
consumer_risk(n, c, ltpd)consumer_risk(n, c, ltpd)
n |
Sample size. |
c |
Acceptance number. |
ltpd |
Limiting quality level. |
Consumer risk.
consumer_risk(n = 132, c = 3, ltpd = 0.05)consumer_risk(n = 132, c = 3, ltpd = 0.05)
Computes whether an accept-favouring decision changes under plausible uncertainty around an estimated defect probability.
decision_fragility( n, c, p_hat, uncertainty_width = 0.01, threshold = 0.5, grid_points = 101 )decision_fragility( n, c, p_hat, uncertainty_width = 0.01, threshold = 0.5, grid_points = 101 )
n |
Sample size. |
c |
Acceptance number. |
p_hat |
Estimated defect probability. |
uncertainty_width |
Half-width of uncertainty interval. |
threshold |
Acceptance probability threshold. |
grid_points |
Number of grid points in uncertainty interval. |
Integer value: 1 if fragile, 0 if stable.
decision_fragility(n = 132, c = 3, p_hat = 0.03)decision_fragility(n = 132, c = 3, p_hat = 0.03)
Applies a drift-aware acceptance sampling policy using an EWMA statistic to trigger tightened inspection.
dras_ewma( lots, n0, c0, lambda = 0.2, warning_threshold = 0.03, tighten_factor = 1.5, aql = 0.01 )dras_ewma( lots, n0, c0, lambda = 0.2, warning_threshold = 0.03, tighten_factor = 1.5, aql = 0.01 )
lots |
Data frame with column |
n0 |
Baseline sample size. |
c0 |
Baseline acceptance number. |
lambda |
EWMA smoothing parameter. |
warning_threshold |
Drift warning threshold. |
tighten_factor |
Multiplicative factor applied to |
aql |
Acceptable quality level. |
Data frame with adaptive lot decisions.
plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(50, "sudden_deterioration") lots <- simulate_lots(p) dras_ewma(lots, n0 = plan$n, c0 = plan$c)plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(50, "sudden_deterioration") lots <- simulate_lots(p) dras_ewma(lots, n0 = plan$n, c0 = plan$c)
Applies an EWMA-based DRAS monitoring rule to real lot-level inspection
data prepared by prepare_lot_data(). Unlike simulation functions, this
function does not require the true defect probability.
dras_monitor( data, baseline_sample_size, acceptance_number, lambda = 0.2, warning_threshold = 0.03, tighten_factor = 1.5, initial_rate = 0.01 )dras_monitor( data, baseline_sample_size, acceptance_number, lambda = 0.2, warning_threshold = 0.03, tighten_factor = 1.5, initial_rate = 0.01 )
data |
A data frame returned by |
baseline_sample_size |
Baseline sample size under normal inspection. |
acceptance_number |
Acceptance number used for lot disposition. |
lambda |
EWMA smoothing parameter. |
warning_threshold |
EWMA warning threshold. |
tighten_factor |
Multiplicative tightening factor used when the EWMA statistic exceeds the warning threshold. |
initial_rate |
Initial EWMA value. Defaults to 0.01. |
A data frame with EWMA statistic, inspection mode, and recommended next sample size.
raw_data <- data.frame( lot_id = paste0("L", 1:6), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found" ) dras_monitor(prepared, baseline_sample_size = 132, acceptance_number = 3)raw_data <- data.frame( lot_id = paste0("L", 1:6), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found" ) dras_monitor(prepared, baseline_sample_size = 132, acceptance_number = 3)
Evaluates false acceptance, false rejection, inspection burden, and tightened-inspection frequency.
evaluate_sampling(results, method_name = "method")evaluate_sampling(results, method_name = "method")
results |
Data frame returned by |
method_name |
Name of method. |
One-row data frame of performance metrics.
plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(20) lots <- simulate_lots(p) res <- apply_fixed_plan(lots, plan$n, plan$c) evaluate_sampling(res, "Fixed ASP")plan <- find_plan(0.01, 0.05) p <- simulate_defect_process(20) lots <- simulate_lots(p) res <- apply_fixed_plan(lots, plan$n, plan$c) evaluate_sampling(res, "Fixed ASP")
A simulated lot-level inspection dataset for demonstrating drift-aware risk-guaranteed acceptance sampling. The data represent 200 production lots under a sudden deterioration scenario, where the true defect probability increases halfway through the sequence.
example_lot_dataexample_lot_data
A data frame with 200 rows and 4 variables:
Lot index.
True lot-level defect probability used in simulation.
Number of units in the lot.
Simulated number of true defective units in the lot.
This dataset is intended for examples, tests, and vignettes. It is not a real industrial dataset. Users can apply the same functions to their own lot-level inspection data containing defect rates or defect counts.
data(example_lot_data) head(example_lot_data)data(example_lot_data) head(example_lot_data)
Finds the smallest sample size and acceptance number satisfying producer-risk and consumer-risk constraints.
find_plan(aql, ltpd, alpha = 0.05, beta = 0.1, max_n = 500)find_plan(aql, ltpd, alpha = 0.05, beta = 0.1, max_n = 500)
aql |
Acceptable quality level. |
ltpd |
Limiting quality level. |
alpha |
Maximum producer risk. |
beta |
Maximum consumer risk. |
max_n |
Maximum sample size to search. |
A data frame with sample size, acceptance number, producer risk, and consumer risk.
find_plan(aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.10)find_plan(aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.10)
Identifies Pareto-efficient policies where lower inspection burden and lower false acceptance are preferred.
pareto_frontier( x, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts" )pareto_frontier( x, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts" )
x |
Data frame containing policy results. |
cost_col |
Column name for inspection burden. |
safety_col |
Column name for false acceptance burden. |
Data frame of Pareto-efficient rows.
df <- data.frame(cost = c(100, 120, 150), risk = c(5, 3, 2)) pareto_frontier(df, cost_col = "cost", safety_col = "risk")df <- data.frame(cost = c(100, 120, 150), risk = c(5, 3, 2)) pareto_frontier(df, cost_col = "cost", safety_col = "risk")
Plots the safety-cost trade-off from a DRAS tuning table. The x-axis represents inspection burden and the y-axis represents false-acceptance burden.
plot_dras_frontier( tuning_results, selected_policies = NULL, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts", main = "DRAS Safety-Cost Frontier" )plot_dras_frontier( tuning_results, selected_policies = NULL, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts", main = "DRAS Safety-Cost Frontier" )
tuning_results |
Data frame returned by |
selected_policies |
Optional data frame returned by |
cost_col |
Column name for inspection burden. |
safety_col |
Column name for false-acceptance burden. |
main |
Plot title. |
Invisibly returns tuning_results.
tuned <- tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50, 2.00) ) selected <- select_dras_policy(tuned) plot_dras_frontier(tuned, selected)tuned <- tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50, 2.00) ) selected <- select_dras_policy(tuned) plot_dras_frontier(tuned, selected)
Converts real industrial or pharmaceutical inspection data into a standard format suitable for DRAS analysis.
prepare_lot_data( data, lot_id, sample_size, defects, lot_size = NULL, date = NULL, supplier = NULL )prepare_lot_data( data, lot_id, sample_size, defects, lot_size = NULL, date = NULL, supplier = NULL )
data |
A data frame containing lot-level inspection records. |
lot_id |
Name of the lot identifier column. |
sample_size |
Name of the inspected sample-size column. |
defects |
Name of the observed defect-count column. |
lot_size |
Optional name of the lot-size column. |
date |
Optional name of the date column. |
supplier |
Optional name of the supplier identifier column. |
A data frame in DRAS-ready format.
raw_data <- data.frame( lot_id = paste0("L", 1:5), date = as.Date("2026-01-01") + 0:4, supplier = c("A", "A", "A", "B", "B"), lot_size = rep(1000, 5), sample_size = rep(100, 5), defects_found = c(1, 0, 2, 5, 4) ) prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", lot_size = "lot_size", date = "date", supplier = "supplier" )raw_data <- data.frame( lot_id = paste0("L", 1:5), date = as.Date("2026-01-01") + 0:4, supplier = c("A", "A", "A", "B", "B"), lot_size = rep(1000, 5), sample_size = rep(100, 5), defects_found = c(1, 0, 2, 5, 4) ) prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", lot_size = "lot_size", date = "date", supplier = "supplier" )
Computes the probability of accepting a lot under a binomial acceptance sampling plan.
prob_accept(n, c, p)prob_accept(n, c, p)
n |
Sample size. |
c |
Acceptance number. The lot is accepted if observed defects
are less than or equal to |
p |
Defect probability. |
Probability of lot acceptance.
prob_accept(n = 132, c = 3, p = 0.01)prob_accept(n = 132, c = 3, p = 0.01)
Computes producer risk, defined as the probability of rejecting a lot at the acceptable quality level.
producer_risk(n, c, aql)producer_risk(n, c, aql)
n |
Sample size. |
c |
Acceptance number. |
aql |
Acceptable quality level. |
Producer risk.
producer_risk(n = 132, c = 3, aql = 0.01)producer_risk(n = 132, c = 3, aql = 0.01)
Runs repeated simulations comparing fixed acceptance sampling with EWMA-based drift-aware risk-guaranteed acceptance sampling.
run_dras_experiment( n_reps = 100, n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.1, lambda = 0.4, warning_threshold = 0.02, tighten_factor = 2, seed = NULL )run_dras_experiment( n_reps = 100, n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.1, lambda = 0.4, warning_threshold = 0.02, tighten_factor = 2, seed = NULL )
n_reps |
Number of Monte Carlo replications. |
n_lots |
Number of lots per replication. |
scenario |
Defect-process scenario. |
aql |
Acceptable quality level. |
ltpd |
Limiting quality level. |
alpha |
Maximum producer risk. |
beta |
Maximum consumer risk. |
lambda |
EWMA smoothing parameter. |
warning_threshold |
EWMA warning threshold. |
tighten_factor |
Multiplicative tightening factor. |
seed |
Optional random seed. |
A data frame of summary results by method.
run_dras_experiment(n_reps = 10, scenario = "sudden_deterioration")run_dras_experiment(n_reps = 10, scenario = "sudden_deterioration")
Selects three representative policies from a DRAS tuning table: a low-cost policy, a balanced policy, and a high-safety policy.
select_dras_policy( tuning_results, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts" )select_dras_policy( tuning_results, cost_col = "mean_avg_sample_number", safety_col = "mean_false_accepts" )
tuning_results |
Data frame returned by |
cost_col |
Column name for inspection burden. |
safety_col |
Column name for false-acceptance burden. |
A data frame containing three selected DRAS policies.
tuned <- tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50, 2.00) ) select_dras_policy(tuned)tuned <- tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50, 2.00) ) select_dras_policy(tuned)
Simulates lot-level defect probabilities under stable, sudden deterioration, or gradual drift scenarios.
simulate_defect_process( n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), p0 = 0.01, p1 = 0.06 )simulate_defect_process( n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), p0 = 0.01, p1 = 0.06 )
n_lots |
Number of lots. |
scenario |
One of |
p0 |
Initial defect probability. |
p1 |
Deteriorated defect probability. |
Numeric vector of true defect probabilities.
simulate_defect_process(10, scenario = "gradual_drift")simulate_defect_process(10, scenario = "gradual_drift")
Simulate Lot Inspection Data
simulate_lots(p_true, lot_size = 1000)simulate_lots(p_true, lot_size = 1000)
p_true |
Numeric vector of true defect probabilities. |
lot_size |
Lot size. |
Data frame of simulated lots.
p <- simulate_defect_process(20) simulate_lots(p)p <- simulate_defect_process(20) simulate_lots(p)
Summarises the output from dras_monitor() to provide an audit-friendly
overview of lot decisions, warnings, inspection modes, and defect rates.
summarise_dras_monitor(monitored_data, by_supplier = FALSE)summarise_dras_monitor(monitored_data, by_supplier = FALSE)
monitored_data |
A data frame returned by |
by_supplier |
Logical. If TRUE and supplier information is available, returns summaries by supplier. |
A data frame containing DRAS monitoring summary statistics.
raw_data <- data.frame( lot_id = paste0("L", 1:6), supplier = c("A", "A", "A", "B", "B", "B"), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", supplier = "supplier" ) monitored <- dras_monitor( prepared, baseline_sample_size = 132, acceptance_number = 3 ) summarise_dras_monitor(monitored)raw_data <- data.frame( lot_id = paste0("L", 1:6), supplier = c("A", "A", "A", "B", "B", "B"), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", supplier = "supplier" ) monitored <- dras_monitor( prepared, baseline_sample_size = 132, acceptance_number = 3 ) summarise_dras_monitor(monitored)
Searches over EWMA smoothing parameters, warning thresholds, and tightening factors to identify safety-cost trade-offs for DRAS.
tune_dras_policy( n_reps = 100, n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), lambda_values = c(0.05, 0.1, 0.2, 0.3, 0.4), warning_values = c(0.02, 0.03, 0.04), tighten_values = c(1.25, 1.5, 2), aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.1, seed = NULL )tune_dras_policy( n_reps = 100, n_lots = 200, scenario = c("stable", "sudden_deterioration", "gradual_drift"), lambda_values = c(0.05, 0.1, 0.2, 0.3, 0.4), warning_values = c(0.02, 0.03, 0.04), tighten_values = c(1.25, 1.5, 2), aql = 0.01, ltpd = 0.05, alpha = 0.05, beta = 0.1, seed = NULL )
n_reps |
Number of Monte Carlo replications per policy. |
n_lots |
Number of lots per replication. |
scenario |
Defect-process scenario. |
lambda_values |
Candidate EWMA smoothing values. |
warning_values |
Candidate warning thresholds. |
tighten_values |
Candidate tightening factors. |
aql |
Acceptable quality level. |
ltpd |
Limiting quality level. |
alpha |
Maximum producer risk. |
beta |
Maximum consumer risk. |
seed |
Optional random seed. |
A data frame of policy performance results.
tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50) )tune_dras_policy( n_reps = 2, n_lots = 30, scenario = "sudden_deterioration", lambda_values = c(0.20, 0.40), warning_values = c(0.03), tighten_values = c(1.50) )
Writes DRAS monitoring outputs and audit summaries to CSV files. This function is intended for industrial and quality-assurance workflows where lot-level monitoring decisions must be saved for review.
write_dras_report(monitored_data, output_dir, include_supplier_summary = TRUE)write_dras_report(monitored_data, output_dir, include_supplier_summary = TRUE)
monitored_data |
A data frame returned by |
output_dir |
Required, non-empty directory path supplied by the user. No default is provided. |
include_supplier_summary |
Logical. If TRUE and supplier information is available, writes a supplier-level summary. |
Invisibly returns a character vector of written file paths.
raw_data <- data.frame( lot_id = paste0("L", 1:6), supplier = c("A", "A", "A", "B", "B", "B"), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", supplier = "supplier" ) monitored <- dras_monitor( prepared, baseline_sample_size = 132, acceptance_number = 3 ) temp_dir <- tempfile("dras_report_") write_dras_report(monitored, output_dir = temp_dir)raw_data <- data.frame( lot_id = paste0("L", 1:6), supplier = c("A", "A", "A", "B", "B", "B"), sample_size = rep(100, 6), defects_found = c(1, 0, 1, 4, 5, 6) ) prepared <- prepare_lot_data( raw_data, lot_id = "lot_id", sample_size = "sample_size", defects = "defects_found", supplier = "supplier" ) monitored <- dras_monitor( prepared, baseline_sample_size = 132, acceptance_number = 3 ) temp_dir <- tempfile("dras_report_") write_dras_report(monitored, output_dir = temp_dir)