Skip to content

Commit 68750a8

Browse files
authored
Merge pull request #200 from HorridTom/feature-vignette-plots-176
Feature vignette plots 176
2 parents 854b97f + a1bf1e2 commit 68750a8

4 files changed

Lines changed: 173 additions & 43 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Imports:
2424
tidyr,
2525
tools
2626
Suggests:
27+
bookdown,
2728
knitr,
2829
rmarkdown,
2930
testthat,

data-raw/example_series.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,18 @@ generate_example_series_2 <- function() {
166166
dplyr::mutate(x = as.integer(x),
167167
y = as.integer(y))
168168

169+
df4c <- df4 %>%
170+
dplyr::mutate(y = replace(y,
171+
c(38:43),
172+
y[38:43] + c(6,1,1,-7,-1,-3))) %>%
173+
f_extend_df(rpois,
174+
n = 4,
175+
lambda = 24) %>%
176+
dplyr::mutate(x = as.integer(x),
177+
y = as.integer(y))
178+
169179
return(list(example_series_2a = df4,
170-
example_series_2b = df4b))
180+
example_series_2b = df4b,
181+
example_series_2c = df4c))
171182

172183
}

data/example_series_2c.rda

290 Bytes
Binary file not shown.

vignettes/stable-shift-algorithm.Rmd

Lines changed: 160 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
---
22
title: "The Stable Shift Algorithm"
33
output:
4-
rmarkdown::html_vignette:
4+
bookdown::html_document2:
5+
base_format: rmarkdown::html_vignette
6+
fig_caption: yes
57
toc: true
68
number_sections: true
9+
pkgdown:
10+
as_is: true
711
vignette: >
812
%\VignetteIndexEntry{The Stable Shift Algorithm}
913
%\VignetteEngine{knitr::rmarkdown}
@@ -29,7 +33,10 @@ library(DiagrammeR)
2933
The *autospc* package implements the *Stable Shift Algorithm* for
3034
re-establishing control limits in statistical process control (SPC) analysis.
3135
This vignette describes the problem the algorithm addresses, sets out some
32-
useful terminology, and describes the algorithm.
36+
useful terminology, describes the algorithm, and explains how to use the
37+
algorithm log.
38+
\
39+
\
3340

3441
# The problem
3542

@@ -42,9 +49,11 @@ A standard approach in SPC analysis for quality improvement goes as follows:
4249
2. Extend the baseline limits into the future
4350
3. Add data to the chart as time progresses, without updating the control limits
4451

45-
This is exemplified in the following three charts.
52+
An example is shown in Figure \@ref(fig:extending-limits). This uses the
53+
`ed_attendances_monthly` dataset included with `autospc`. For more information
54+
on this dataset see `?ed_attendances_monthly`.
4655

47-
```{r 2.0, fig.width=7, fig.height=9}
56+
```{r extending-limits, fig.width=7, fig.height=7, fig.cap="Extending baseline control limits"}
4857
facet_stages(
4958
ed_attendances_monthly %>%
5059
filter(row_number() <= 32L),
@@ -70,69 +79,177 @@ the new process. Whilst various textbooks and online resources offer opinions on
7079
this issue, there is no universally accepted approach. The Stable Shift
7180
Algorithm (SSA) offers an automated, consistent and rigorous approach to
7281
re-establishing control limits.
82+
\
83+
\
7384

74-
# The Stable Shift Algorithm: Overview
85+
# The Stable Shift Algorithm
86+
87+
## Overview
7588

7689
The main idea of the SSA is to only re-establish limits where:
7790

78-
1. There is evidence that the process has shifted to a new level
79-
2. This shift persists for long enough to compute new control limits
91+
A. There is evidence that the process has shifted to a new level
92+
93+
B. This shift persists for long enough to compute new control limits
8094

8195
In other words, the SSA re-establishes limits at shift rule breaks, provided
8296
that the shift is not "transient" in some sense. Here "transient" means that
8397
once we calculate the new control limits, there is not a shift rule break back
8498
towards the original process. In the next two sections we make this idea precise
8599
and describe how it is operationalised in the SSA.
86100

87-
# Some terminology
101+
## Some terminology
102+
103+
First, it is useful to introduce some terminology. We will refer to Figure
104+
\@ref(fig:example-1) to illustrate the concepts introduced in this section. This
105+
figure shows a C-chart for the first 35 data points of the simulated
106+
`example_series_2a` data included with `autospc`, which for the purpose of this
107+
section we shall interpret as daily values of a count measure of interest.
88108

89-
First, it is useful to introduce some terminology.
109+
```{r example-1, fig.width=7, fig.height=5, fig.cap="Example 1"}
110+
plot_auto_SPC(example_series_2a %>%
111+
filter(row_number() <= 35L),
112+
override_y_title = "Count",
113+
chartType = "C",
114+
extend_limits_to = 47L)
115+
```
90116

91-
## Calculation and display periods
92117

93-
## Rule-breaking run
94-
A *rule-breaking run* is a run whose length is greater than or equal to the
95-
threshold for a rule break (`runRuleLength`), set to $8$ by default in
96-
`plot_auto_SPC()`.
118+
### Calculation and display periods
119+
120+
The data used to calculate a set of control limits comes from a contiguous
121+
period of time, with the possible exception of some excluded points. This period
122+
of time is referred to as the *calculation period* of the limits.
97123

98-
Runs that are subsets of longer runs count here, so for example with the default
99-
`runRuleLength = 8`, a run of length 10 actually comprises three rule-breaking
100-
runs, commencing at the first, second and third points in the length 10 run.
101-
In this example, the first rule-breaking run has length 10, the second 9, and
102-
the third 8. The run that commences on the fourth point of the length 10 run is
103-
not rule-breaking, since it is of length 7 only.
124+
When the limits are extended into the future, beyond their calculation period,
125+
the period over which they are extended is referred to as the *display period*.
104126

105-
## Triggering rule break
106-
In the SSA, a rule breaking run can trigger consideration of whether to
107-
re-establish limits. Such a run is referred to as a *triggering rule break*.
127+
In charts produced by *autospc*, limits are displayed as black dashed lines over
128+
their calculation period, and grey dashed lines over their display period. For
129+
example, in \@ref(fig:example-1), there is one calculation period, covering
130+
days 1 to 21 inclusive, extended into its display period covering day 22
131+
onwards.
108132

109-
## Candidate limits
133+
### Rule-breaking run
134+
A *rule-breaking run* is a run whose length is greater than or equal to the
135+
threshold for a shift rule break (`runRuleLength`), set to $8$ by default in
136+
`plot_auto_SPC()`. In \@ref(fig:example-1), there is a rule-breaking run of
137+
length 10 starting on day 22. By default, rule-breaking runs are highlighted in
138+
blue by `autospc`.
139+
140+
Runs that are subsets of longer runs with the same end point count here, so for
141+
example with the default `runRuleLength = 8`, the run of length 10 in
142+
\@ref(fig:example-1) actually comprises three rule-breaking runs, commencing at
143+
the first, second and third points in the length 10 run. In this example, the
144+
first rule-breaking run starts on day 22 and has length 10, the second starts on
145+
day 23 and has length 9, and the third starts on day 24 and has length 8. The
146+
run that commences on day 25, the fourth point of the length 10 run, is not
147+
rule-breaking, since it is of length 7 only.
148+
149+
### Triggering rule break
150+
In the SSA, a rule-breaking run commencing during a display period triggers
151+
consideration of whether to re-establish limits. Such a run is referred to as a
152+
*triggering rule break*.
153+
154+
In \@ref(fig:example-1) the highlighted rule-breaking run is a triggering rule
155+
break.
156+
157+
### Candidate limits
110158
In order to decide whether to re-establish control limits at a triggering rule
111-
break, the
112-
SSA requires consideration of the set of limits that _would_ be established.
113-
These are referred to as *candidate limits* until they are either rejected or
114-
accepted. Candidate limits are formed from the first `periodMin` points starting
115-
at the first point of the triggering rule break, and this period is referred to
116-
as the *candidate calculation period*.
117-
118-
## Opposing rule break
159+
break, the SSA requires consideration of the set of limits that _would_ be
160+
established. These are referred to as *candidate limits* until they are either
161+
rejected or accepted. Candidate limits are formed from the first `periodMin`
162+
points starting at the first point of the triggering rule break, and this
163+
period is referred to as the *candidate calculation period*.
164+
165+
In Figure \@ref(fig:example-1), there are fewer than `periodMin` (here 21)
166+
points on or after the start of the triggering rule break (day 22), so it is not
167+
possible to re-establish limits at day 22, and there are no candidate limits to
168+
consider.
169+
170+
In Figure \@ref(fig:example-2) we imagine rolling time forward, so that we have
171+
more data to add to the chart in \@ref(fig:example-1). Figure
172+
\@ref(fig:example-2) shows the data against the (baseline) calculation limits.
173+
Figure \@ref(fig:example-3) shows candidate limits established at the start of
174+
the triggering rule break, i.e. day 22.
175+
176+
```{r example-2, fig.width=7, fig.height=5, fig.cap="Example 2"}
177+
plot_auto_SPC(example_series_2a,
178+
override_y_title = "Count",
179+
chartType = "C",
180+
noRecals = TRUE,
181+
extend_limits_to = 47L)
182+
```
183+
184+
```{r example-3, fig.width=7, fig.height=5, fig.cap="Example 3"}
185+
plot_auto_SPC(example_series_2a,
186+
override_y_title = "Count",
187+
chartType = "C",
188+
extend_limits_to = 47L)
189+
```
190+
191+
### Opposing rule break
119192
If there is a rule break within the candidate calculation period, and that rule
120193
break is in the opposite direction to the triggering rule break, it is referred
121194
to as an *opposing rule break*. We also sometimes refer to such a rule break as
122-
a *reversion*, as in reverting to the original limits. If the rule break only
123-
reaches the `runRuleLength` threshold after the end of the candidate calculation
124-
period, it is referred to as an *overhanging reversion*.
195+
a *reversion*, as in reverting to the original limits.
196+
197+
In Figure \@ref(fig:example-3) there is no opposing rule break within the
198+
candidate calculation period. Figure \@ref(fig:example-4) shows an alternative
199+
continuation of the baseline time series we have considered so far. This series
200+
is identical to the first up to day 26, and differs thereafter. There is still
201+
a triggering rule break against the baseline limits commencing at day 22. Figure
202+
\@ref(fig:example-5) shows candidate limits established from the start of this
203+
triggering rule break, i.e. from day 22. There is an opposing rule break in
204+
Figure \@ref(fig:example-5), commencing on day 31.
205+
206+
```{r example-4, fig.width=7, fig.height=5, fig.cap="Example 4"}
207+
plot_auto_SPC(example_series_2b,
208+
override_y_title = "Count",
209+
chartType = "C",
210+
noRecals = TRUE,
211+
extend_limits_to = 47L)
212+
```
213+
214+
```{r example-5, fig.width=7, fig.height=5, fig.cap="Example 5"}
215+
plot_auto_SPC(example_series_2b,
216+
override_y_title = "Count",
217+
chartType = "C",
218+
recalEveryShift = TRUE,
219+
extend_limits_to = 47L)
220+
```
221+
222+
223+
If an opposing rule break only reaches the `runRuleLength` threshold after the
224+
end of the candidate calculation period, it is referred to as an
225+
*overhanging reversion*. Figure \@ref(fig:example-6) shows another alternative
226+
continuation of our example time series, this time showing an
227+
overhanging reversion commencing on day 40, against the candidate limits.
125228

126-
## Minimum period length
229+
```{r example-6, fig.width=7, fig.height=5, fig.cap="Example 6"}
230+
plot_auto_SPC(example_series_2c,
231+
override_y_title = "Count",
232+
chartType = "C",
233+
recalEveryShift = TRUE)
234+
```
235+
236+
### Minimum period length
127237
The SSA requires specification of a minimum number of data points to be used
128238
for calculation of control limits, $n_{min}$. Whilst those using SPC in practice
129239
may not often make such a minimum explicit, in a way it is always there
130240
implicitly - nobody would compute control limits from two data points would
131241
they? In fact, various authors offer guidance on what such a minimum should be,
132-
with values ranging from 17 to 25 points. In `plot_auto_SPC()`, $n_{min}$ is
133-
specified by the `periodMin` argument, defaulting to 21.
242+
with values ranging from 17 to 25 points.
243+
244+
In `plot_auto_SPC()`, $n_{min}$ is specified by the `periodMin` argument,
245+
defaulting to 21. This default is above the commonly recommended minimum values
246+
and represents a convenient choice for daily data, since it is a multiple of 7.
247+
This means that in the presence of weekly "seasonal" (periodic) variation over
248+
the 7 day period the limits are not unduly affected by which day of the week the
249+
limit calculation period begins on. When using monthly data, it is preferable to
250+
use $n_{min} = 24$, for the same reason.
134251

135-
# The Stable Shift Algorithm: Details
252+
## Details of the algorithm
136253

137254
The steps of the algorithm are as follows:
138255

@@ -168,7 +285,7 @@ points beginning at the counter.
168285

169286
7. Decide whether to accept or reject the candidate limits:
170287

171-
i. If there at least one opposing rule break commencing within the candidate
288+
i. If there is at least one opposing rule break commencing within the candidate
172289
calculation period, then the candidate limits are rejected, the counter is
173290
set to the next rule-breaking run (after the triggering rule break) against
174291
the prevailing limits, and the algorithm continues from (3).
@@ -190,7 +307,7 @@ The algorithm is visualised in the flow chart below.
190307
```{r 5.1, fig.width=7, fig.height=7}
191308
grViz(autospc:::algorithm_flow_chart_string)
192309
```
193-
310+
\
194311

195312
# Using the algorithm log
196313

@@ -211,7 +328,8 @@ plot_auto_SPC(
211328
y = Att_All,
212329
verbosity = 1,
213330
x_break = 365,
214-
x_date_format = "%Y-%b"
331+
x_date_format = "%Y-%b",
332+
point_size = 1L
215333
)
216334
```
217335

0 commit comments

Comments
 (0)