| Type: | Package |
| Title: | Statistical Process Control with Tidyverse-Native Workflows |
| Version: | 1.3.0 |
| Description: | A comprehensive toolkit for Statistical Process Control (SPC) that combines the rigor of classical Shewhart methodology with modern tidyverse-native interfaces. Provides classical control charts for variables (I-MR, Xbar-R, Xbar-S) and attributes (p, np, c, u), as well as regression-based control charts for processes with trend. Includes Nelson runs tests, Average Run Length (ARL) simulation, process capability indices with bootstrap confidence intervals, Box-Cox transformation guidance, and a clean Phase I / Phase II workflow. All chart objects integrate with broom via 'tidy', 'glance' and 'augment' methods. References: Shewhart (1931, ISBN:0-87389-076-0); Montgomery (2019, ISBN:978-1-119-39930-8); Nelson (1984) <doi:10.1080/00224065.1984.11978921>; Woodall (2000) <doi:10.1080/00224065.2000.11980013>; Box & Cox (1964) <doi:10.1111/j.2517-6161.1964.tb00553.x>. |
| License: | GPL-3 |
| URL: | https://castlaboratory.github.io/shewhartr/, https://github.com/castlaboratory/shewhartr |
| BugReports: | https://github.com/castlaboratory/shewhartr/issues |
| Encoding: | UTF-8 |
| LazyData: | true |
| Depends: | R (≥ 4.1.0) |
| Imports: | rlang (≥ 1.1.0), cli (≥ 3.6.0), dplyr (≥ 1.1.0), purrr (≥ 1.0.0), tibble (≥ 3.2.0), tidyselect (≥ 1.2.0), ggplot2 (≥ 3.5.0), broom (≥ 1.0.0), slider (≥ 0.3.0), grid, grDevices, stats, utils |
| Suggests: | patchwork (≥ 1.1.0), plotly (≥ 4.10.0), qcc (≥ 2.7), lubridate (≥ 1.9.0), testthat (≥ 3.2.0), vdiffr (≥ 1.0.0), knitr, rmarkdown, pkgdown, MASS |
| Config/testthat/edition: | 3 |
| RoxygenNote: | 7.3.3 |
| VignetteBuilder: | knitr |
| NeedsCompilation: | no |
| Packaged: | 2026-05-09 01:02:39 UTC; leite |
| Author: | André Leite [aut, cre], Hugo Vasconcelos [aut], Raydonal Ospina [aut], Cristiano Ferraz [aut], Castlab [cph, fnd] |
| Maintainer: | André Leite <leite@castlab.org> |
| Repository: | CRAN |
| Date/Publication: | 2026-05-13 08:10:09 UTC |
shewhartr: Statistical Process Control with Tidyverse-Native Workflows
Description
The shewhartr package brings classical Statistical Process Control
(SPC) methodology into a modern, tidy-friendly R workflow. It implements
the full family of classical Shewhart control charts (I-MR, Xbar-R,
Xbar-S, p, np, c, u), regression-based charts for processes with trend,
runs tests (Nelson 1-8), Average Run Length (ARL) simulation, process
capability indices, Box-Cox guidance, and an explicit Phase I / Phase II
workflow.
Design principles
- Tidyverse-native
All constructors take
dataas the first argument, support tidy evaluation for column references via{{ }}, and accept a.byargument where group-aware behaviour makes sense.- Object-oriented
Every chart returns an S3 object of class
shewhart_chart(with a specific subclass likeshewhart_i_mr) supportingprint(),summary(),plot(),autoplot(),tidy(),glance()andaugment().- Diagnostic-rich
Every chart can be passed through
shewhart_runs()(configurable rule sets),shewhart_arl()(Monte Carlo ARL),shewhart_diagnostics()(Tukey-style residual panel) andshewhart_capability()(Cp, Cpk, Pp, Ppk with bootstrap CI).- Statistically honest
Counts are charted with exact Poisson limits when requested (
limits = "poisson"), per Box's advice to model the right distribution rather than transform.- Robust alternatives
Most charts accept
sigma_method = "biweight"for Tukey-style robust scale estimation as an alternative to the classical moving range.- Internationalisation
Plot labels and informative messages accept a
localeargument ("en","pt","es","fr").
The four families of functions
- Classical charts (variables)
- Classical charts (attributes)
- Regression-based chart
shewhart_regression()for trended processes, with optional automatic phase detection via the runs test.- Diagnostics & methodology
shewhart_runs(),shewhart_arl(),shewhart_diagnostics(),shewhart_box_cox(),shewhart_capability(),calibrate(),monitor().
Recommended starting points
New users should start with vignette("getting-started", package = "shewhartr") for a 5-minute overview, then move to the chart family
that matches their data type. The vignette "phase1-phase2"
explains the central distinction between estimation and monitoring,
and "arl-simulation" shows how to evaluate the operating
characteristics of any chart configuration.
Online documentation
The full reference manual, articles for every chart family, diagnostics and the case study are at https://castlaboratory.github.io/shewhartr/. Source, issues and contribution guide are at https://github.com/castlaboratory/shewhartr.
Author(s)
Maintainer: André Leite leite@castlab.org
Authors:
Hugo Vasconcelos hugo.vasconcelos@ufpe.br
Raydonal Ospina raydonal@castlab.org
Cristiano Ferraz cferraz@castlab.org
Other contributors:
Castlab [copyright holder, funder]
References
Shewhart, W. A. (1931). Economic Control of Quality of Manufactured Product. D. Van Nostrand.
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. ISBN: 978-1-119-39930-8.
Nelson, L. S. (1984). The Shewhart Control Chart - Tests for Special Causes. Journal of Quality Technology, 16(4), 237-239. doi:10.1080/00224065.1984.11978921
Woodall, W. H. (2000). Controversies and Contradictions in Statistical Process Control. Journal of Quality Technology, 32(4), 341-350. doi:10.1080/00224065.2000.11980013
Box, G. E. P., & Cox, D. R. (1964). An Analysis of Transformations. Journal of the Royal Statistical Society, Series B, 26(2), 211-252. doi:10.1111/j.2517-6161.1964.tb00553.x
Tukey, J. W. (1977). Exploratory Data Analysis. Addison-Wesley.
Wheeler, D. J., & Chambers, D. S. (1992). Understanding Statistical Process Control (2nd ed.). SPC Press.
See Also
Useful links:
Report bugs at https://github.com/castlaboratory/shewhartr/issues
Gompertz growth function
Description
Computes the value of the Gompertz curve parameterised in terms of starting value, asymptote, growth rate and lag:
G(x) = y_0 + (y_{\max} - y_0)\,
\exp\!\left[-\exp\!\left(\frac{k(\mathrm{lag} - x)}{y_{\max} - y_0}
+ 1\right)\right].
Usage
Gompertz(x, y0, ymax, k, lag)
Arguments
x |
Numeric vector. The independent variable (e.g. time). |
y0 |
Lower asymptote. |
ymax |
Upper asymptote. |
k |
Maximum specific growth rate. |
lag |
Lag time. |
Details
This parameterisation, often called the "Zwietering Gompertz" form
after Zwietering et al. (1990), gives directly interpretable
parameters: y0 is the lower asymptote, ymax the upper
asymptote, k the maximum specific growth rate, and lag the lag
time before exponential growth.
Value
A numeric vector the same length as x.
References
Gompertz, B. (1825). On the Nature of the Function Expressive of the Law of Human Mortality. Philosophical Transactions of the Royal Society of London, 115, 513-583.
Zwietering, M. H., Jongenburger, I., Rombouts, F. M., & van 't Riet, K. (1990). Modeling of the Bacterial Growth Curve. Applied and Environmental Microbiology, 56(6), 1875-1881. doi:10.1128/aem.56.6.1875-1881.1990
See Also
SSgompertzDummy() for an nls-friendly self-starting
variant that allows a covariate shift.
Examples
x <- seq(0, 30, by = 0.5)
y <- Gompertz(x, y0 = 0, ymax = 100, k = 5, lag = 5)
plot(x, y, type = "l", main = "Gompertz growth curve")
Self-starting Gompertz with an additive dummy term
Description
Extends the classical Gompertz form by adding a linear contribution from a dummy covariate:
y = \mathrm{Asym}\,\exp(-b_2\,\exp(-b_3 x)) + \beta\,d.
Designed for use inside stats::nls(): starting values for the four
parameters are computed automatically from the data, and the
analytic gradient is supplied for faster, more reliable
convergence.
Usage
SSgompertzDummy(x, dummy, Asym, b2, b3, Beta)
Arguments
x |
Numeric vector. Independent variable. |
dummy |
Numeric (typically 0/1) vector of the same length as |
Asym |
Upper asymptote. |
b2 |
Curvature parameter. |
b3 |
Rate parameter. |
Beta |
Dummy effect size. |
Value
A numeric vector of fitted values, with attributes for self-starting and an analytic gradient.
References
Same as Gompertz().
See Also
stats::SSgompertz() for the standard self-starting
Gompertz without a dummy term, fit_gompertz_dummy() for a
convenience wrapper.
Examples
set.seed(42)
n <- 50
x <- seq(1, 10, length.out = n)
d <- rep(c(0, 1), each = n / 2)
y <- 100 * exp(-2 * exp(-0.3 * x)) + 20 * d + rnorm(n, 0, 3)
df <- data.frame(x = x, y = y, dummy = d)
fit <- nls(y ~ SSgompertzDummy(x, dummy, Asym, b2, b3, Beta), data = df)
summary(fit)
Convert a Shewhart chart to an interactive plotly figure
Description
Produces an interactive HTML plotly version of the chart that
ggplot2::autoplot() would build for the same object. Useful for
dashboards, reports, and any context where hovering, zooming and
panning matters.
Usage
as_plotly(x, ...)
## Default S3 method:
as_plotly(x, ...)
## S3 method for class 'shewhart_chart'
as_plotly(x, tooltip = c("x", "y"), ...)
Arguments
x |
A shewhart_chart object. |
... |
Additional arguments forwarded to |
tooltip |
Character vector of aesthetics to display in the
hover tooltip, as accepted by |
Details
This is a separate function rather than an autoplot() argument
so that loading shewhartr does not pull plotly (and its full
transitive dependency tree) into every R session that uses the
package. plotly lives in Suggests; install it explicitly if
you want to use this function.
Value
A plotly object (S3 class plotly / htmlwidget) ready
to print, embed in a Shiny app, or save with
htmlwidgets::saveWidget().
Examples
if (requireNamespace("plotly", quietly = TRUE)) {
set.seed(1)
df <- data.frame(t = 1:50, y = rnorm(50, mean = 100, sd = 2))
fit <- shewhart_i_mr(df, value = y, index = t)
as_plotly(fit)
}
Augment new data with control-chart annotations
Description
Returns the per-observation augmented tibble, optionally re-aligned
against fresh data passed via newdata (Phase II monitoring). When
newdata is NULL, returns the in-sample augmented tibble.
Usage
## S3 method for class 'shewhart_chart'
augment(x, newdata = NULL, ...)
Arguments
x |
A shewhart_chart object. |
newdata |
Optional data frame with the same columns as the
data used to fit |
... |
Currently unused. |
Value
A tibble. When newdata = NULL, the chart's augmented
tibble; otherwise the same shape but for newdata.
Examples
set.seed(1)
df <- data.frame(y = rnorm(50))
fit <- shewhart_i_mr(df, value = y)
broom::augment(fit)
Plot a Shewhart chart with ggplot2
Description
Generic autoplot method that dispatches on chart subclass. All
versions return a ggplot object that the user can further
customise with the usual ggplot2 grammar.
Usage
autoplot.shewhart_chart(
object,
show_violations = TRUE,
show_sigma_zones = FALSE,
locale = NULL,
...
)
Arguments
object |
A shewhart_chart object. |
show_violations |
Logical. Highlight violations with red points?
Default |
show_sigma_zones |
Logical. Show 1- and 2-sigma zones as
shaded bands? Default |
locale |
Optional override for the chart's stored locale. |
... |
Passed to subclass-specific methods. |
Value
A ggplot object (or, for I-MR / Xbar-R / Xbar-S charts,
a list of two ggplot objects with class shewhart_plot_pair
that prints them stacked).
Examples
fit <- shewhart_i_mr(data.frame(y = rnorm(50)), value = y)
ggplot2::autoplot(fit)
Bacterial growth curve (optical density)
Description
A synthetic dataset of optical density (OD) measurements from a bacterial culture, sampled at 80 evenly spaced time points across a 24-hour incubation. The true mean follows a Gompertz growth curve with asymptote 1.2.
Usage
bacterial_growth
Format
A tibble with 80 rows and 2 columns:
- hour
Numeric time in hours since inoculation.
- od
Numeric optical density at 600 nm.
Source
Synthetic. See data-raw/build_all.R.
See Also
shewhart_regression() with model = "gompertz",
Gompertz().
Examples
fit <- shewhart_regression(bacterial_growth,
value = od, index = hour,
model = "gompertz")
ggplot2::autoplot(fit)
Bottle filling volumes
Description
A synthetic dataset of 100 individual fill volumes (in millilitres). Process target is 500 ml with sigma 1.2 ml. A linear drift begins around observation 65, simulating a slowly miscalibrating filler.
Usage
bottle_fill
Format
A tibble with 100 rows and 2 columns:
- observation
Integer observation index.
- ml
Numeric volume in millilitres.
Source
Synthetic. See data-raw/build_all.R.
See Also
Examples
fit <- shewhart_i_mr(bottle_fill, value = ml, index = observation)
ggplot2::autoplot(fit)
Apply / invert a Box-Cox power transformation
Description
The Box-Cox transformation (Box & Cox, 1964) is
y(\lambda) = \begin{cases}
(x^\lambda - 1) / \lambda & \lambda \neq 0 \\
\log(x) & \lambda = 0.
\end{cases}
Usage
box_cox(x, lambda)
Arguments
x |
Numeric vector of strictly positive values. |
lambda |
Numeric scalar. Power parameter. |
Details
For lambda = 0 this returns log(x); for lambda = 1 it returns
x - 1 (no shape change). Use shewhart_box_cox() to estimate
lambda from the data via profile log-likelihood.
Value
A numeric vector of transformed values.
References
Box, G. E. P., & Cox, D. R. (1964). An Analysis of Transformations. Journal of the Royal Statistical Society, Series B, 26(2), 211-252. doi:10.1111/j.2517-6161.1964.tb00553.x
See Also
shewhart_box_cox() to estimate lambda from data.
Examples
box_cox(1:10, lambda = 0) # equivalent to log(1:10)
box_cox(1:10, lambda = 0.5)
Phase I calibration of a control chart
Description
Convenience wrapper that fits a control chart and tags its phase as
"phase_1" (the default for any chart constructor). The intent is
to make Phase I usage explicit in code: the practitioner
acknowledges that limits are being estimated.
Usage
calibrate(data, ..., chart = "i_mr", trim_outliers = FALSE, max_trim_iter = 5L)
Arguments
data |
A data frame. |
... |
Arguments passed to a chart constructor. |
chart |
A character key naming the chart constructor:
|
trim_outliers |
Logical. If |
max_trim_iter |
Integer. Maximum trimming iterations. |
Details
Optionally drops violations from the in-control estimate ("trimmed" calibration), per the iterative procedure described in Montgomery (2019) Section 6.2.3: if any observation falls outside the limits, it is removed and the limits are recomputed; iterate until either all remaining points are in control or no further trimming is possible.
Value
A shewhart_chart object with $phase = "phase_1".
References
Woodall, W. H. (2000). Controversies and Contradictions in Statistical Process Control. Journal of Quality Technology, 32(4), 341-350. doi:10.1080/00224065.2000.11980013
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Section 6.2.3.
Examples
set.seed(1)
df <- data.frame(y = c(rnorm(40, mean = 100, sd = 2), 110, rnorm(20, 100, 2)))
calib <- calibrate(df, value = y, chart = "i_mr", trim_outliers = TRUE)
calib$n # 60 if trim was avoided, fewer if outliers were removed
Daily insurance claim error rates
Description
A synthetic dataset of 30 days. Each day, a variable number of claims is processed (80-150) and the count of claims with errors is recorded. Underlying error rate is 5% for the first 22 days and 9% thereafter.
Usage
claims_p
Format
A tibble with 30 rows and 3 columns:
- day
Integer day index (1-30).
- n
Integer total claims processed that day.
- defects
Integer claims found with errors.
Source
Synthetic. See data-raw/build_all.R.
See Also
Examples
fit <- shewhart_p(claims_p, defects = defects, n = n, index = day)
ggplot2::autoplot(fit)
Generate a qualitative HCL palette
Description
Internal palette used as a default when a number of phases or subgroups need distinct colours. For most plots, ggplot2's default palette is fine; this is provided for backward compatibility and quick prototyping.
Usage
color_hue(n)
Arguments
n |
Integer (>= 1). Number of colours to generate. |
Value
A character vector of n hex colour strings.
Examples
color_hue(5)
COVID-19 daily mortality, Recife, Brazil, 2020
Description
Daily count of new COVID-19 deaths officially recorded in Recife
(capital of Pernambuco state, Brazil) between 28 March 2020 and
31 December 2020. Distributed with v0.1.x as
inst/extdata/recife_2020_covid19.rds and preserved in v1.0.0
as a vignette case study illustrating the regression-based control
chart on non-stationary epidemiological counts.
Usage
cvd_recife
Format
A tibble with 279 rows and 3 columns:
- date
Date of the bulletin.
- new_deaths
Integer count of new deaths reported that day.
- .t
Integer row index (1..N), useful as a continuous predictor for
shewhart_regression().
Source
Castlab (Universidade Federal de Pernambuco) compiled the original series from the Brazilian Ministry of Health daily bulletins. See https://covid.saude.gov.br/.
See Also
shewhart_regression(),
vignette("covid-recife", package = "shewhartr").
Examples
fit <- shewhart_regression(cvd_recife,
value = new_deaths, index = .t,
model = "loglog")
ggplot2::autoplot(fit)
Convenience wrapper to fit SSgompertzDummy to a data frame
Description
Convenience wrapper to fit SSgompertzDummy to a data frame
Usage
fit_gompertz_dummy(data, x, y, dummy, start = NULL, ...)
Arguments
data |
A data frame with the columns referenced by |
x, y, dummy |
Tidy-eval column references. |
start |
Optional named list of starting values ( |
... |
Additional arguments passed to |
Value
An object of class nls.
Examples
set.seed(42)
df <- data.frame(
x = seq(1, 10, length.out = 50),
d = rep(c(0, 1), each = 25)
)
df$y <- 100 * exp(-2 * exp(-0.3 * df$x)) + 20 * df$d + rnorm(50, 0, 3)
fit <- fit_gompertz_dummy(df, x = x, y = y, dummy = d)
coef(fit)
Glance at a Shewhart chart's overall diagnostics
Description
Returns a one-row tibble with overall chart-level diagnostics suitable for filling a row in a comparison table.
Usage
## S3 method for class 'shewhart_chart'
glance(x, ...)
Arguments
x |
A shewhart_chart object. |
... |
Currently unused. |
Value
A one-row tibble with columns type, n, phase,
sigma_hat, sigma_method, n_violations, n_rules,
pct_violations.
Examples
fit <- shewhart_i_mr(data.frame(y = rnorm(50)), value = y)
broom::glance(fit)
Inverse log-log transformation
Description
Inverts loglog():
x = \alpha \,[\exp(\exp(y) - 1) - 1].
Usage
iloglog(x, alpha = 1)
Arguments
x |
Numeric vector previously transformed via |
alpha |
Same |
Value
A numeric vector on the original scale.
Examples
original <- c(0, 1, 5, 10, 100, 1000)
all.equal(original, iloglog(loglog(original)))
Inverse Box-Cox transformation
Description
Inverse Box-Cox transformation
Usage
inv_box_cox(x, lambda)
Arguments
x |
Numeric vector of strictly positive values. |
lambda |
Numeric scalar. Power parameter. |
Value
A numeric vector on the original scale.
Shewhart chart S3 class
Description
All chart constructors in the package return an object of class
shewhart_chart with a more specific subclass (shewhart_i_mr,
shewhart_xbar_r, shewhart_p, shewhart_regression, ...). The
shared slots are type, augmented, limits, violations, fits,
rules, sigma_hat, sigma_method, phase, n, call, and
metadata. is_shewhart_chart() tests inheritance.
Usage
is_shewhart_chart(x)
Arguments
x |
An object. |
Value
Logical scalar.
Examples
fit <- shewhart_i_mr(data.frame(v = rnorm(30)), v)
is_shewhart_chart(fit)
Log-log transformation
Description
Applies the stabilising transformation
y = \log\!\bigl(\log\bigl(x/\alpha + 1\bigr) + 1\bigr).
Useful for very right-skewed non-negative data, particularly count processes with heavy tails. The +1 inside each log makes the transformation well-defined at zero.
Usage
loglog(x, alpha = 1)
Arguments
x |
Numeric vector. Values must satisfy |
alpha |
Positive scaling parameter. Default: 1. |
Value
A numeric vector of transformed values.
See Also
iloglog() for the inverse, shewhart_box_cox() for a
data-driven transformation choice.
Examples
x <- c(0, 1, 5, 10, 100, 1000)
loglog(x)
Phase II monitoring against pre-calibrated limits
Description
Applies the control limits (and rule set) from a calibrated shewhart_chart object to fresh data. The new data must contain the same columns used by the original chart constructor.
Usage
monitor(data, chart)
Arguments
data |
A data frame with the same columns as the data used to
fit |
chart |
A pre-calibrated shewhart_chart object. |
Details
Limits are not re-estimated; they are the limits stored on the calibration object. Only the violation table is recomputed against the new observations.
Value
A shewhart_chart object with $phase = "phase_2" and
limits inherited from chart.
References
Woodall, W. H. (2000). Controversies and Contradictions in Statistical Process Control. Journal of Quality Technology, 32(4), 341-350.
Examples
set.seed(1)
base <- data.frame(y = rnorm(50, mean = 100, sd = 2))
new_obs <- data.frame(y = rnorm(20, mean = 102, sd = 2)) # small shift
calib <- calibrate(base, value = y, chart = "i_mr")
alarms <- monitor(new_obs, calib)
alarms$violations
Solder defects on printed circuit boards
Description
A synthetic Poisson dataset: number of defective solder joints on each of 50 inspected printed circuit boards. The mean is 6, ideal for either the 3-sigma c chart or its more honest cousin with exact Poisson limits.
Usage
pcb_solder
Format
A tibble with 50 rows and 2 columns:
- board
Integer board identifier.
- defects
Integer count of defective joints.
Source
Synthetic. See data-raw/build_all.R.
See Also
Examples
fit <- shewhart_c(pcb_solder, defects = defects, index = board)
fit_exact <- shewhart_c(pcb_solder, defects = defects, index = board,
limits = "poisson")
Print a Shewhart chart object
Description
Concise summary including chart type, sample size, sigma estimate and any rule violations. For full per-row results, use augment(); for a tabular limit summary, use tidy().
Usage
## S3 method for class 'shewhart_chart'
print(x, ...)
Arguments
x |
A shewhart_chart object. |
... |
Currently unused. |
Value
Returns x invisibly (for chaining).
Examples
set.seed(1)
fit <- shewhart_i_mr(data.frame(y = rnorm(50)), value = y)
print(fit)
Rolling sum with a configurable window
Description
Slides over x summing the last .window elements (including the
current one). Treats NA as zero. Used internally by the runs tests.
Usage
rolling_sum(x, .window = 7L)
Arguments
x |
Numeric vector. |
.window |
Integer window size (>= 1). Default: 7. |
Value
A numeric vector the same length as x.
Examples
rolling_sum(c(1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), .window = 7)
Estimate Average Run Length via Monte Carlo simulation
Description
Simulates the run length of a Shewhart-type chart configuration
under a sequence of mean shifts. For each shift size, replicates
draw normal observations with mean shift * sigma (relative to
the in-control centre) and stop at the first observation where any
of the supplied rules fires. Returns a tibble with the average run
length and a Monte Carlo confidence interval.
Usage
shewhart_arl(
shift = seq(0, 3, by = 0.5),
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
n_sim = 5000L,
max_run = 1000L,
sigma = 1,
seed = NULL
)
Arguments
shift |
Numeric vector of mean shifts to evaluate, in units of
|
rules |
Character vector of rule keys (see
|
n_sim |
Integer. Number of simulation replicates per shift. Default: 5000. |
max_run |
Integer. Maximum run length before truncation (a censored alarm). Default: 1000. |
sigma |
Numeric. In-control sigma. Default: 1 (the relevant
quantity is |
seed |
Optional integer seed for reproducibility. |
Details
Computational note: for n_sim = 1e4 and a moderate set of rules,
a single configuration takes a fraction of a second; a fine shift
grid (10-15 points) takes a few seconds. For tighter intervals or
larger rule sets, increase n_sim and/or set parallel = TRUE
(currently a placeholder for future implementation).
Value
A tibble with columns shift, arl, arl_se,
arl_lower, arl_upper, n_truncated. n_truncated counts
how many replicates hit max_run before alarming (a sign that
max_run should be raised).
References
Champ, C. W., & Woodall, W. H. (1987). Exact Results for Shewhart Control Charts with Supplementary Runs Rules. Technometrics, 29(4), 393-399. doi:10.1080/00401706.1987.10488262
Wald, A. (1947). Sequential Analysis. Wiley.
Examples
# In-control ARL of Nelson 1 (closed-form ARL_0 ~= 370.4)
set.seed(1)
shewhart_arl(shift = 0, rules = "nelson_1_beyond_3s",
n_sim = 2000, max_run = 2000)
# Adding Nelson 2 sharpens detection but lowers ARL_0
shewhart_arl(shift = c(0, 0.5, 1, 1.5, 2),
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
n_sim = 2000)
Box-Cox profile log-likelihood
Description
Computes the profile log-likelihood for the Box-Cox power parameter lambda, applied to a single positive numeric series (regression to a constant) or to the residuals of a linear model.
Usage
shewhart_box_cox(data, value = NULL, lambda_grid = seq(-2, 2, by = 0.05))
Arguments
data |
A data frame, or a numeric vector. If a data frame,
|
value |
Tidy-eval column reference for the response (only when
|
lambda_grid |
Numeric vector of lambda values to evaluate. Default: a fine grid from -2 to 2. |
Details
Reports the lambda that maximises the profile likelihood and a 95% confidence interval based on the chi-square approximation to twice the log-likelihood drop (Box & Cox 1964, eq. 9).
Value
An object of class shewhart_box_cox with components
profile (tibble of lambda vs. log-likelihood), lambda_hat
(the maximiser), ci (95% CI). The object has its own
print() method.
References
Box, G. E. P., & Cox, D. R. (1964). An Analysis of Transformations. Journal of the Royal Statistical Society, Series B, 26(2), 211-252. doi:10.1111/j.2517-6161.1964.tb00553.x
Examples
set.seed(1)
bc <- shewhart_box_cox(rlnorm(200, meanlog = 0, sdlog = 0.5))
bc$lambda_hat # should be near 0 (log-normal data)
c chart for the number of nonconformities
Description
Constructs a c chart for counts of nonconformities (defects) per
inspection unit, where the unit (area, length, time, etc.) is
constant across observations. For variable inspection size use
shewhart_u().
Usage
shewhart_c(
data,
defects,
index = NULL,
limits = c("3sigma", "poisson"),
rules = c("nelson_1_beyond_3s"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
defects |
Tidy-eval column reference for the count of nonconformities per inspection unit. |
index |
Optional tidy-eval column reference for the x-axis. |
limits |
One of |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Value
A shewhart_chart object of subclass shewhart_c.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 7.3.
Ryan, T. P. (2011). Statistical Methods for Quality Improvement (3rd ed.). Wiley. Chapter 6 (on the inadequacy of 3-sigma limits for low-mean Poisson counts).
Examples
set.seed(1)
df <- data.frame(
unit = 1:40,
defects = rpois(40, lambda = 6)
)
fit <- shewhart_c(df, defects = defects, index = unit)
fit_exact <- shewhart_c(df, defects = defects, index = unit,
limits = "poisson")
Process capability indices Cp, Cpk, Pp, Ppk
Description
Computes the four classical capability indices for a Shewhart chart or a raw vector. Optionally returns bootstrap confidence intervals.
Usage
shewhart_capability(
data,
lsl = NA_real_,
usl = NA_real_,
target = NA_real_,
ci_level = 0.95,
n_boot = 2000L,
seed = NULL
)
Arguments
data |
A shewhart_chart object or a numeric vector. |
lsl, usl |
Numeric scalars. Lower and upper specification limits. At least one must be supplied. |
target |
Numeric scalar. Optional process target. If missing,
defaults to the midpoint of |
ci_level |
Numeric. Confidence level for bootstrap intervals.
Default |
n_boot |
Integer. Number of bootstrap replicates. Default 2000. |
seed |
Optional integer for reproducibility. |
Details
For a shewhart_chart of type i_mr, xbar_r, or xbar_s, the
within-subgroup sigma stored on the chart object is used for
Cp/Cpk; the overall standard deviation of the raw data is used for
Pp/Ppk. For a numeric vector data, a single sigma is used for
both pairs (so Cp = Pp and Cpk = Ppk).
Capability indices are only meaningful when the process is in statistical control (Phase I). The function emits a warning if the supplied chart has any rule violations.
Value
A list of class shewhart_capability with point estimates
and (optionally) bootstrap CIs.
References
Kotz, S., & Lovelace, C. R. (1998). Process Capability Indices in Theory and Practice. Arnold.
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 8.
Pearn, W. L., & Kotz, S. (2006). Encyclopedia and Handbook of Process Capability Indices. World Scientific.
Examples
set.seed(1)
df <- data.frame(y = rnorm(100, mean = 50, sd = 0.8))
fit <- shewhart_i_mr(df, value = y)
cap <- shewhart_capability(fit, lsl = 47, usl = 53, target = 50)
print(cap)
Look up Shewhart control chart constants
Description
Returns the classical Shewhart constants (A2, A3, c4, d2, d3, B3-B6,
D3-D4) for a given subgroup size n. Tabulated values are used for
n <= 25; for larger samples, c4 is computed from its closed form
and the remaining constants are derived analytically when known
(otherwise NA is returned with a warning).
Usage
shewhart_constants(n)
Arguments
n |
Integer scalar or vector. Subgroup size (>= 2). |
Value
A data frame with columns n, A2, A3, c4, d2, d3,
B3, B4, B5, B6, D3, D4.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Appendix VI.
Examples
shewhart_constants(5)
shewhart_constants(c(2, 5, 10, 25))
Tabular CUSUM control chart
Description
Constructs a two-sided tabular CUSUM chart for a single column of
individual measurements. Two cumulative statistics, C+ (upward)
and C- (downward), are accumulated against a target with a
reference value k; an alarm fires when either crosses the
decision interval h * sigma.
Usage
shewhart_cusum(
data,
value,
index = NULL,
target = NULL,
sigma = NULL,
k = 0.5,
h = 4,
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the measurement. |
index |
Optional tidy-eval column reference for the x-axis. |
target |
Numeric. Process target. Defaults to |
sigma |
Numeric. Process sigma. Defaults to |
k |
Numeric. Reference value in units of sigma. Default |
h |
Numeric. Decision interval in units of sigma. Default |
locale |
One of |
verbose |
Logical. Print progress messages? |
Details
By default, sigma is estimated from the moving range of value
(MR_bar / 1.128); the target is the mean of value. Either can
be overridden via target and sigma for Phase II monitoring
against pre-calibrated values.
Value
A shewhart_chart object of subclass shewhart_cusum. The
augmented slot has columns .value, .cusum_pos, .cusum_neg
(the two accumulated statistics, both non-negative), .upper
(the decision interval h * sigma), and .flag_signal.
References
Page, E. S. (1954). Continuous Inspection Schemes. Biometrika, 41(1-2), 100-115. doi:10.1093/biomet/41.1-2.100
Hawkins, D. M., & Olwell, D. H. (1998). Cumulative Sum Charts and Charting for Quality Improvement. Springer.
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 9.
Examples
set.seed(1)
df <- data.frame(
day = 1:80,
y = c(rnorm(40, mean = 100, sd = 2),
rnorm(40, mean = 101, sd = 2)) # 0.5 sigma shift
)
fit <- shewhart_cusum(df, value = y, index = day)
print(fit)
ggplot2::autoplot(fit)
Tukey-style residual diagnostic panel
Description
For chart objects whose residuals are meaningful (shewhart_i_mr,
shewhart_xbar_r, shewhart_xbar_s, shewhart_regression),
produces the five-panel residual diagnostic favoured by exploratory
data analysis: residuals vs. fitted, normal Q-Q, autocorrelation,
moving-range plot of residuals, residual histogram. The aim is to
make the assumptions that the chart is making visible: independence
(ACF), normality (Q-Q, histogram), constant variance (residuals
vs. fitted), and the absence of trend in dispersion (moving range).
Usage
shewhart_diagnostics(chart, locale = NULL)
Arguments
chart |
A shewhart_chart object. |
locale |
Optional override for the chart's stored locale. |
Value
A list of ggplot objects with class
shewhart_diagnostics. The print method composes the panels.
References
Tukey, J. W. (1977). Exploratory Data Analysis. Addison-Wesley.
Box, G. E. P., Hunter, W. G., & Hunter, J. S. (2005). Statistics for Experimenters: Design, Innovation, and Discovery (2nd ed.). Wiley.
Examples
fit <- shewhart_i_mr(data.frame(y = rnorm(100)), value = y)
print(shewhart_diagnostics(fit))
Exponentially Weighted Moving Average (EWMA) control chart
Description
Constructs an EWMA chart for a single column of individual measurements. The chart is more sensitive than a Shewhart I chart to small but persistent shifts in the process mean, at the cost of a longer reaction time to large shifts.
Usage
shewhart_ewma(
data,
value,
index = NULL,
target = NULL,
sigma = NULL,
lambda = 0.2,
L = 2.7,
steady_state = FALSE,
rules = "nelson_1_beyond_3s",
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the measurement. |
index |
Optional tidy-eval column reference for the x-axis. |
target |
Numeric. Process target / centre line. Defaults to
|
sigma |
Numeric. Process sigma. Defaults to |
lambda |
Numeric in |
L |
Numeric. Width of the limits in standard errors of the
EWMA. Default |
steady_state |
Logical. Use asymptotic (constant) limits instead of time-varying ones? |
rules |
Character vector of runs rules to flag. Defaults to Nelson 1 only — the EWMA's own limits already encode most of the diagnostic power and the higher-order Nelson rules are not designed for autocorrelated statistics. |
locale |
One of |
verbose |
Logical. Print progress messages? |
Details
By default, sigma is estimated from the moving range of value
(Wheeler 1992 convention, MR_bar / 1.128); the centre is the mean
of value. Either can be overridden via target and sigma for
Phase II monitoring against pre-calibrated values.
Limits are time-varying by default — they widen out from target
as the EWMA "warms up" — converging to the asymptotic limits as
i -> infinity. Set steady_state = TRUE to use the asymptotic
limits everywhere (commonly chosen when calibrating from a long
baseline).
Value
A shewhart_chart object of subclass shewhart_ewma. The
augmented slot has columns .value (the original observation),
.ewma (the smoothed statistic z_i, plotted on the chart), and
the usual .center, .upper, .lower, .flag_*.
References
Roberts, S. W. (1959). Control Chart Tests Based on Geometric Moving Averages. Technometrics, 1(3), 239-250. doi:10.1080/00401706.1959.10489860
Lucas, J. M., & Saccucci, M. S. (1990). Exponentially Weighted Moving Average Control Schemes: Properties and Enhancements. Technometrics, 32(1), 1-12. doi:10.1080/00401706.1990.10484583
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 9.
Examples
set.seed(1)
df <- data.frame(
day = 1:80,
y = c(rnorm(40, mean = 100, sd = 2),
rnorm(40, mean = 101, sd = 2)) # 0.5 sigma shift
)
fit <- shewhart_ewma(df, value = y, index = day)
print(fit)
ggplot2::autoplot(fit)
Hotelling T-squared multivariate control chart
Description
Constructs a Hotelling T² chart for joint monitoring of p
correlated quality characteristics. Use this chart when the
variables genuinely co-vary — a classical example is a chemical
process where temperature, pressure and flow rate are mechanically
coupled, and a fault that breaks the coupling moves them off the
joint distribution but possibly stays inside each marginal limit.
Usage
shewhart_hotelling(
data,
vars,
subgroup = NULL,
index = NULL,
phase = c("phase_1", "phase_2"),
alpha = 0.0027,
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
vars |
Tidy-select expression for the columns containing the
variables to monitor jointly ( |
subgroup |
Optional tidy-eval column for rational subgrouping.
If supplied, all rows sharing a value of this column are treated
as a single subgroup. If |
index |
Optional tidy-eval column for the x-axis. If
supplied, must vary across observations (or across subgroups, if
|
phase |
One of |
alpha |
Type-I error rate per observation. Default |
locale |
One of |
verbose |
Logical. Print progress messages? |
Details
Both individual observations (subgroup = NULL) and rationally
subgrouped observations (subgroup supplied) are supported. The
chart selects the appropriate exact small-sample limits for the
selected phase (Phase I uses retrospective limits derived from
a Beta or F distribution; Phase II uses the slightly wider limits
that propagate the Phase I parameter uncertainty to a fresh
observation).
Value
A shewhart_chart object of subclass shewhart_hotelling.
The augmented tibble has columns .t2 (the statistic), .upper
(UCL — constant within a chart), .flag_signal and .flag_any,
and one .contrib_<var> column per monitored variable giving
that variable's marginal contribution to the alarm (Mason et al.
1995). The limits slot contains the chart-level UCL; the
metadata slot stores the variable names, subgroup column name,
and the parameters p, m, n, phase, alpha that
determined the limit.
References
Hotelling, H. (1947). Multivariate quality control. In: Techniques of Statistical Analysis. McGraw-Hill.
Tracy, N. D., Young, J. C., & Mason, R. L. (1992). Multivariate control charts for individual observations. Journal of Quality Technology, 24(2), 88-95. doi:10.1080/00224065.1992.11979383
Mason, R. L., Tracy, N. D., & Young, J. C. (1995). Decomposition
of T² for multivariate control chart interpretation. Journal of
Quality Technology, 27(2), 99-108.
doi:10.1080/00224065.1995.11979573
Mason, R. L., & Young, J. C. (2002). Multivariate Statistical Process Control with Industrial Applications. SIAM/ASA.
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 11.
Examples
set.seed(1)
Sigma <- matrix(c(1, 0.7, 0.7, 1), 2, 2)
Z <- MASS::mvrnorm(60, c(0, 0), Sigma)
df <- tibble::tibble(t = 1:60, x1 = Z[, 1], x2 = Z[, 2])
fit <- shewhart_hotelling(df, vars = c(x1, x2), index = t)
print(fit)
ggplot2::autoplot(fit)
Individuals and Moving Range (I-MR) control chart
Description
Constructs an I-MR chart for a single column of individual
measurements. Returns a shewhart_chart object that supports
print(), summary(), autoplot(), tidy(), glance() and
augment().
Usage
shewhart_i_mr(
data,
value,
index = NULL,
sigma_method = c("mr", "median_mr", "biweight", "sd"),
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the measurement. |
index |
Optional tidy-eval column reference for the x-axis
(date or integer). If |
sigma_method |
One of |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Details
Sigma is estimated from the moving range with d2(2) = 1.128; the
classical 3-sigma limits are equivalent to
x_bar +/- 2.660 * MR_bar. The MR chart limits are
[0, D4(2) * MR_bar] with D4(2) = 3.267.
Value
A shewhart_chart object of subclass shewhart_i_mr.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 6.
Wheeler, D. J., & Chambers, D. S. (1992). Understanding Statistical Process Control (2nd ed.). SPC Press.
Examples
set.seed(1)
df <- data.frame(
day = seq.Date(as.Date("2024-01-01"), by = "day", length.out = 60),
y = c(rnorm(40, mean = 100, sd = 2),
rnorm(20, mean = 103, sd = 2)) # shift after position 40
)
fit <- shewhart_i_mr(df, value = y, index = day)
print(fit)
ggplot2::autoplot(fit)
Multivariate CUSUM control chart (Crosier 1988)
Description
Constructs a multivariate CUSUM chart for jointly monitoring p
correlated variables. Like the univariate CUSUM it accumulates
deviations from a target with a reference value k that decides
when the accumulator resets; unlike a Hotelling T^2 chart it
carries memory across observations and so detects small persistent
shifts faster.
Usage
shewhart_mcusum(
data,
vars,
index = NULL,
target = NULL,
cov = NULL,
k = 0.5,
h = NULL,
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
vars |
Tidy-select expression for the columns to monitor jointly. At least 2 columns. |
index |
Optional tidy-eval column for the x-axis. |
target |
Optional length- |
cov |
Optional |
k |
Reference value, in sigma units. Default |
h |
Decision interval. If |
locale |
One of |
verbose |
Logical. Print progress messages? |
Value
A shewhart_chart object of subclass shewhart_mcusum.
The augmented tibble has columns .y (the chart statistic),
.upper (the decision interval h), and .flag_signal.
References
Crosier, R. B. (1988). Multivariate Generalizations of Cumulative Sum Quality-Control Schemes. Technometrics, 30(3), 291-303. doi:10.1080/00401706.1988.10488402
Pignatiello, J. J., & Runger, G. C. (1990). Comparisons of Multivariate CUSUM Charts. Journal of Quality Technology, 22(3), 173-186. doi:10.1080/00224065.1990.11979237
Examples
set.seed(1)
Sigma <- matrix(c(1, 0.6, 0.6, 1), 2, 2)
base <- MASS::mvrnorm(60, c(0, 0), Sigma)
shift <- MASS::mvrnorm(40, c(0.6, 0.6), Sigma)
df <- data.frame(t = 1:100,
x1 = c(base[, 1], shift[, 1]),
x2 = c(base[, 2], shift[, 2]))
fit <- shewhart_mcusum(df, vars = c(x1, x2), index = t,
target = c(0, 0), cov = Sigma)
print(fit)
ggplot2::autoplot(fit)
Multivariate EWMA control chart
Description
Constructs a multivariate Exponentially Weighted Moving Average
(MEWMA) chart for jointly monitoring p correlated variables.
The chart is more sensitive than the Hotelling T^2 chart to
small persistent shifts in the vector mean, in the same way the
univariate EWMA is more sensitive than a Shewhart I chart.
Usage
shewhart_mewma(
data,
vars,
index = NULL,
target = NULL,
cov = NULL,
lambda = 0.1,
h = NULL,
steady_state = FALSE,
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
vars |
Tidy-select expression for the columns to monitor jointly. Must select at least 2 columns. |
index |
Optional tidy-eval column for the x-axis. |
target |
Optional length- |
cov |
Optional |
lambda |
Smoothing constant in |
h |
Decision interval (UCL on the |
steady_state |
Logical. Use the steady-state covariance
|
locale |
One of |
verbose |
Logical. Print progress messages? |
Details
By default target (the in-control mean vector) and cov (the
in-control covariance) are estimated from the data. For Phase II
monitoring, supply both explicitly so the limits use the
calibration values. The decision interval h is calibrated by
lookup in the Prabhu & Runger (1997) table for ARL_0 ~ 200; if
the (lambda, p) combination is outside the tabulated range, the
user must pass h explicitly.
Value
A shewhart_chart object of subclass shewhart_mewma.
The augmented tibble has columns .t2 (the MEWMA statistic),
.upper (the decision interval h), and .flag_signal.
References
Lowry, C. A., Woodall, W. H., Champ, C. W., & Rigdon, S. E. (1992). A Multivariate Exponentially Weighted Moving Average Control Chart. Technometrics, 34(1), 46-53. doi:10.1080/00401706.1992.10485232
Prabhu, S. S., & Runger, G. C. (1997). Designing a Multivariate EWMA Control Chart. Journal of Quality Technology, 29(1), 8-15. doi:10.1080/00224065.1997.11979721
Examples
set.seed(1)
Sigma <- matrix(c(1, 0.6, 0.6, 1), 2, 2)
base <- MASS::mvrnorm(60, c(0, 0), Sigma)
shift <- MASS::mvrnorm(40, c(0.4, 0.4), Sigma) # 0.4 sigma shift
df <- data.frame(t = 1:100,
x1 = c(base[, 1], shift[, 1]),
x2 = c(base[, 2], shift[, 2]))
fit <- shewhart_mewma(df, vars = c(x1, x2), index = t,
target = c(0, 0), cov = Sigma,
lambda = 0.1)
print(fit)
ggplot2::autoplot(fit)
np chart for the number of nonconforming items
Description
Constructs an np chart from counts of nonconforming items in
subgroups of constant size n. For variable subgroup sizes, use
shewhart_p() instead.
Usage
shewhart_np(
data,
defects,
n,
index = NULL,
rules = c("nelson_1_beyond_3s"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
defects |
Tidy-eval column reference for the count of nonconforming items in each subgroup. |
n |
Numeric scalar. The (constant) subgroup size. |
index |
Optional tidy-eval column reference for the x-axis. |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Value
A shewhart_chart object of subclass shewhart_np.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 7.2.2.
Examples
set.seed(1)
df <- data.frame(
day = 1:30,
defects = rbinom(30, size = 200, prob = 0.04)
)
fit <- shewhart_np(df, defects = defects, n = 200, index = day)
print(fit)
p chart for the proportion of nonconforming items
Description
Constructs a p chart from counts of nonconforming items in subgroups
of size n. Subgroup sizes may vary; in that case, control limits
are computed per observation.
Usage
shewhart_p(
data,
defects,
n,
index = NULL,
limits = c("3sigma", "binomial"),
rules = c("nelson_1_beyond_3s"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
defects |
Tidy-eval column reference for the count of nonconforming items in each subgroup. |
n |
Tidy-eval column reference for the subgroup size. |
index |
Optional tidy-eval column reference for the x-axis. |
limits |
One of |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Details
Standard 3-sigma limits use the normal approximation to the binomial:
\bar p \pm 3 \sqrt{\bar p (1 - \bar p) / n_i}.
For very small n_i or very small / very large p_bar, the
approximation deteriorates and exact binomial limits should be
preferred (limits = "binomial").
Value
A shewhart_chart object of subclass shewhart_p.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 7.
Examples
set.seed(1)
df <- data.frame(
day = 1:30,
defects = rbinom(30, size = 100, prob = 0.05),
n = 100
)
fit <- shewhart_p(df, defects = defects, n = n, index = day)
print(fit)
Access the package's named colour palettes
Description
Returns one of the curated colour vectors that every chart in the
package draws from. Useful when extending an autoplot() chart
with your own layers and you want them to match the rest of the
package's visual identity.
Usage
shewhart_palette(
name = c("phase_seq", "family", "signal", "neutral"),
n = NULL
)
Arguments
name |
One of |
n |
Optional integer. Number of colours to return. If |
Details
phase_seqSequential palette for time-ordered phase indices in regression / multi-phase charts. Cool blues at the baseline, warming through neutral to terracotta as phase index grows. Avoids saturated red (reserved for
signal).familyNamed categorical palette for the four chart families: variables, attributes, memory-based, multivariate. Identical to the colours used in the architecture diagram.
signalTwo-colour palette:
in_control(deep blue) andout_of_control(firebrick). Used to colour violation points on every chart so the alarm signal is consistent.neutralStructural greys used by
shewhart_theme()for backgrounds, gridlines and text colour.
Value
A character vector of colour hex codes; for family and
signal, the vector is named.
Examples
shewhart_palette("phase_seq", n = 4)
shewhart_palette("family")["multivariate"]
shewhart_palette("signal")
Regression-based control chart for processes with trend
Description
Fits a chosen model to the data (linear, log, log-log, Gompertz, logistic, or a user-supplied formula), then constructs control limits around the fitted curve using the moving-range estimator on the residuals (Wheeler 1992). Optionally detects phase changes automatically via runs tests on the residuals and re-fits each phase.
Usage
shewhart_regression(
data,
value,
index,
model = c("auto", "linear", "log", "loglog", "gompertz", "logistic"),
formula = NULL,
dummy = NULL,
start_base = 10L,
phase_changes = NULL,
phase_rule = "nelson_2_nine_same",
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
sigma_method = c("mr", "median_mr", "biweight", "sd"),
lower_bound = NA_real_,
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the response. |
index |
Tidy-eval column reference for the predictor (typically time, but can be any continuous variable). |
model |
Character. One of |
formula |
Optional one-sided or two-sided formula referencing
columns in |
dummy |
Optional tidy-eval column reference for an additive covariate (a "dummy" in the original v0.1 nomenclature; can be any factor or numeric covariate the user wants to adjust for, such as day-of-week effects or treatment indicators). |
start_base |
Integer. Number of initial observations used to estimate the first phase. Defaults to 10. |
phase_changes |
Optional vector of index positions or values
at which to force a phase change. If |
phase_rule |
Character. Runs rule used to detect new phases.
See |
rules |
Character vector of rules to flag on the final chart. |
sigma_method |
One of |
lower_bound |
Numeric scalar or |
locale |
Character. One of |
verbose |
Logical. Print progress messages? |
Details
This is the package's flagship chart, intended for trended or
non-stationary processes for which classical Shewhart charts give
systematically wrong limits. See the vignette
regression-charts for a thorough discussion and examples.
Value
A shewhart_chart object of subclass shewhart_regression.
The fits slot contains a list of fitted model objects (one per
phase).
References
Mandel, B. J. (1969). The Regression Control Chart. Journal of Quality Technology, 1(1), 1-9. doi:10.1080/00224065.1969.11980341
Wheeler, D. J., & Chambers, D. S. (1992). Understanding Statistical Process Control (2nd ed.). SPC Press.
Box, G. E. P., & Cox, D. R. (1964). An Analysis of Transformations. Journal of the Royal Statistical Society, Series B, 26(2), 211-252. doi:10.1111/j.2517-6161.1964.tb00553.x
Examples
set.seed(1)
df <- data.frame(
t = 1:60,
y = c(1:30 * 0.5 + rnorm(30, sd = 0.5), # phase 1: linear trend
15 + 1:30 * 0.1 + rnorm(30, sd = 0.5)) # phase 2: shift + slowdown
)
fit <- shewhart_regression(df, value = y, index = t, model = "linear")
print(fit)
ggplot2::autoplot(fit)
List available runs rules
Description
List available runs rules
Usage
shewhart_rules_available()
Value
A tibble with columns rule (the key) and description.
Examples
shewhart_rules_available()
Apply runs tests to a chart object or to raw vectors
Description
Implements the eight rules of Nelson (1984, 1985) plus a Western Electric "7 in a row" variant for backward compatibility. Returns a tidy tibble of rule violations.
Usage
shewhart_runs(
x,
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
center = NULL,
sigma = NULL
)
Arguments
x |
Either a |
rules |
Character vector of rule keys to apply. Use
|
center, sigma |
Numeric scalars or vectors. Required only when
|
Value
A tibble with columns position (integer, the index where
the rule fired), rule (character key), description (character
label), value (the value at that position) and severity
(currently always "out_of_control"; reserved for future
warning-level rules).
References
Nelson, L. S. (1984). The Shewhart Control Chart – Tests for Special Causes. Journal of Quality Technology, 16(4), 237-239. doi:10.1080/00224065.1984.11978921
Nelson, L. S. (1985). Interpreting Shewhart Xbar Control Charts. Journal of Quality Technology, 17(2), 114-117. doi:10.1080/00224065.1985.11978941
Western Electric Co. (1956). Statistical Quality Control Handbook.
Examples
set.seed(1)
x <- c(rnorm(20), 5, rnorm(20)) # one outlier at position 21
shewhart_runs(x, center = 0, sigma = 1)
Editorial-style ggplot2 theme used by every autoplot.shewhart_*
Description
Shared across the package so charts look like one family. The visual choices are inspired by data-journalism graphics (FT, Pew Research, The Economist): off-white background, only horizontal grid lines, axis line on the data side, left-aligned title block, and tonal grey for non-data ink.
Usage
shewhart_theme(base_size = 10.5, base_family = "")
Arguments
base_size |
Base font size, in points. |
base_family |
Base font family. Empty string uses the system default sans-serif. We do not hard-code a Google Font so the theme works in offline / CRAN-check environments. |
Details
Use it from your own layers when you want a chart that matches the package's identity:
ggplot(d, aes(x, y)) + geom_line() + shewhart_theme()
Value
A ggplot2::theme() object.
Examples
library(ggplot2)
df <- data.frame(x = 1:50, y = cumsum(rnorm(50)))
ggplot(df, aes(x, y)) + geom_line() + shewhart_theme()
u chart for nonconformities per unit, variable inspection size
Description
Constructs a u chart from defect counts and a per-observation
"exposure" (inspection size: square metres of fabric, hours of
operation, lines of code, etc.). For constant exposure use
shewhart_c().
Usage
shewhart_u(
data,
defects,
exposure,
index = NULL,
limits = c("3sigma", "poisson"),
rules = c("nelson_1_beyond_3s"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
defects |
Tidy-eval column reference for raw defect counts. |
exposure |
Tidy-eval column reference for the inspection size (must be positive). |
index |
Optional tidy-eval column reference for the x-axis. |
limits |
One of |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Value
A shewhart_chart object of subclass shewhart_u.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 7.3.2.
Examples
set.seed(1)
df <- data.frame(
roll = 1:25,
defects = rpois(25, lambda = 4 * runif(25, 0.5, 1.5)),
m2 = runif(25, 0.5, 1.5) # variable inspection size
)
fit <- shewhart_u(df, defects = defects, exposure = m2, index = roll)
Xbar-R control chart for rational subgroups
Description
Constructs a paired Xbar (subgroup mean) and R (subgroup range) chart for measurements organised in rational subgroups of size 2 to 10. Sigma is estimated from the average within-subgroup range.
Usage
shewhart_xbar_r(
data,
value,
subgroup,
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the measurement. |
subgroup |
Tidy-eval column reference identifying the subgroup (e.g. shift, batch, hour). All subgroups must have equal size. |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Details
Xbar-chart limits use A2(n); R-chart limits use D3(n) and
D4(n). See shewhart_constants() for the tabulated values.
Value
A shewhart_chart object of subclass shewhart_xbar_r.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 6.
Shewhart, W. A. (1931). Economic Control of Quality of Manufactured Product. D. Van Nostrand.
Examples
set.seed(1)
df <- data.frame(
batch = rep(1:25, each = 5),
y = rnorm(125, mean = 50, sd = 1.5)
)
fit <- shewhart_xbar_r(df, value = y, subgroup = batch)
print(fit)
Xbar-S control chart for rational subgroups
Description
Like shewhart_xbar_r(), but uses the subgroup standard deviation
(S) instead of the range. Recommended for subgroup sizes greater
than 10, or when subgroup sizes differ.
Usage
shewhart_xbar_s(
data,
value,
subgroup,
sigma_method = c("sbar", "pooled_sd"),
rules = c("nelson_1_beyond_3s", "nelson_2_nine_same"),
locale = getOption("shewhart.locale", "en"),
verbose = NULL
)
Arguments
data |
A data frame. |
value |
Tidy-eval column reference for the measurement. |
subgroup |
Tidy-eval column reference identifying the subgroup (e.g. shift, batch, hour). All subgroups must have equal size. |
sigma_method |
One of |
rules |
Character vector of rule keys to apply. See
|
locale |
One of |
verbose |
Logical. Print progress messages? Defaults to the
|
Details
Xbar-chart limits use A3(n); S-chart limits use B3(n) and
B4(n). When sigma_method = "pooled_sd", sigma is estimated as
the pooled within-subgroup standard deviation.
Value
A shewhart_chart object of subclass shewhart_xbar_s.
References
Montgomery, D. C. (2019). Introduction to Statistical Quality Control (8th ed.). Wiley. Chapter 6.4.
Examples
set.seed(1)
df <- data.frame(
batch = rep(1:30, each = 12),
y = rnorm(360, mean = 80, sd = 0.6)
)
fit <- shewhart_xbar_s(df, value = y, subgroup = batch)
print(fit)
Compact tibble-like summary
Description
Compact tibble-like summary
Usage
## S3 method for class 'shewhart_chart'
summary(object, ...)
Arguments
object |
A shewhart_chart object. |
... |
Currently unused. |
Value
A list with elements limits and violations.
Examples
fit <- shewhart_i_mr(data.frame(y = rnorm(50)), value = y)
summary(fit)
Pharmaceutical tablet weights
Description
A synthetic dataset modelled on classical pharmaceutical quality control. Tablet weights are recorded in subgroups of 5 tablets each, across 25 production batches. Target weight is 250 mg with a process sigma of 1.5 mg; a small mean shift to 251.5 mg is embedded in subgroups 18-25.
Usage
tablet_weight
Format
A tibble with 125 rows and 3 columns:
- subgroup
Integer batch identifier (1-25).
- tablet
Integer tablet position within the batch (1-5).
- weight
Numeric tablet weight in milligrams.
Source
Synthetic. See data-raw/build_all.R.
See Also
shewhart_xbar_r(), shewhart_xbar_s().
Examples
fit <- shewhart_xbar_r(tablet_weight, value = weight, subgroup = subgroup)
ggplot2::autoplot(fit)
Curing oven temperature drift
Description
A synthetic dataset of 200 sensor readings on a curing oven. The true temperature exhibits a slow linear drift superimposed on a periodic component. A classical Shewhart chart will misjudge the limits because the process is non-stationary - a regression control chart is the right tool.
Usage
temperature_drift
Format
A tibble with 200 rows and 2 columns:
- minute
Integer minute since start.
- temp_c
Numeric temperature in degrees Celsius.
Source
Synthetic. See data-raw/build_all.R.
See Also
Examples
fit <- shewhart_regression(temperature_drift,
value = temp_c, index = minute,
model = "linear")
ggplot2::autoplot(fit)
Tidy the control limits of a Shewhart chart
Description
Returns a tibble of the chart's control limits in tall format. Each row corresponds to one line of one chart panel (CL / UCL / LCL).
Usage
## S3 method for class 'shewhart_chart'
tidy(x, ...)
Arguments
x |
A shewhart_chart object. |
... |
Currently unused. |
Value
A tibble with at least columns chart, line, value.
Examples
fit <- shewhart_i_mr(data.frame(y = rnorm(50)), value = y)
broom::tidy(fit)