-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
227 lines (160 loc) · 7.48 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 300,
fig.width = 5
)
```
# cusumcharter <img src="man/figures/logo.png" width="160px" align="right" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/johnmackintosh/cusumcharter/workflows/R-CMD-check/badge.svg)](https://github.com/johnmackintosh/cusumcharter/actions)
[![Codecov test coverage](https://codecov.io/gh/johnmackintosh/cusumcharter/branch/master/graph/badge.svg)](https://app.codecov.io/gh/johnmackintosh/cusumcharter?branch=master)
[![Render README](https://github.com/johnmackintosh/cusumcharter/actions/workflows/render-readme.yaml/badge.svg)](https://github.com/johnmackintosh/cusumcharter/actions/workflows/render-readme.yaml)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![CRAN status](https://www.r-pkg.org/badges/version/cusumcharter)](https://CRAN.R-project.org/package=cusumcharter)
[![CRAN Downloads](https://cranlogs.r-pkg.org/badges/cusumcharter)](https://cran.r-project.org/package=cusumcharter)
[![Total Downloads](https://cranlogs.r-pkg.org/badges/grand-total/cusumcharter)](https://cran.r-project.org/package=cusumcharter)
<!-- badges: end -->
The goal of cusumcharter is to create both simple CUSUM charts, with and without control limits from a vector, or to create multiple CUSUM charts, with or without control limits, from a grouped dataframe, tibble or data.table.
CUSUM charts detect small changes over time, and will alert quicker than a Statistical Process Control chart. They are an excellent alternative to run and control charts, particularly where data is scarce, infrequent, or expensive to obtain.
They monitor the difference between each data point, relative to a target value, which is often the mean of all the currently available data points. Using these variances and targets, control limits are calculated.
Any points outside these limits are an indication that the process is out of control.
## Installation
Install the latest stable version from CRAN :
```{r, eval = FALSE}
install.packages("cusumcharter")
```
Install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("johnmackintosh/cusumcharter")
```
## A Simple CUSUM calculation
This returns the CUSUM statistics for a single vector, centred on a supplied target value:
```{r example}
library(cusumcharter)
test_vec <- c(0.175, 0.152, 0.15, 0.207, 0.136, 0.212, 0.166)
cusum_res <- cusum_single(test_vec, target = 0.16)
cusum_res
```
## Expanded outputs with cusum_single_df
This function takes a single vector as input and returns a data.frame with additional information used to calculate the CUSUM statistic
```{r, example2}
test_vec2 <- c(0.175, 0.152, 0.15, 0.207, 0.136, 0.212, 0.166)
cusum_single_df(test_vec2, target = 0.16)
```
Here we don't supply a target, so the mean is used
```{r, out.width='100%'}
test_vec3 <- c(1,1,2,11,3,5,7,2,4,3,5)
cusum_single_df(test_vec3)
```
## CUSUM control limits
Two additional functions allow you to calculate control limits from a single vector and plot a CUSUM chart with control limits. As before, the mean is used to determine the target if none is provided. Alternate functions are available if you wish to use the median instead.
```{r, cusum_control_example}
test_vec3 <- c(1,1,2,3,5,7,11,7,5,7,8,9,5)
controls <- cusum_control(test_vec3, target = 4)
controls
```
Also see the ```cusum_control_median``` function
## CUSUM Control Chart
```{r single-control_chart, fig.width=5, fig.height=3}
test_vec3 <- c(1,1,2,3,5,7,11,7,5,7,8,9,5)
controls <- cusum_control(test_vec3, target = 4)
cusum_control_plot(controls,
xvar = obs,
title_text = "CUSUM out of control since 7th observation")
```
## Multiple CUSUM Control Charts
```{r faceted_chart1, fig.width=5, fig.height= 3}
library(dplyr)
library(tibble)
library(ggplot2)
library(cusumcharter)
testdata <- tibble::tibble(
N = c(1L,2L,1L,3L,1L,1L,1L,1L,1L,
1L,3L,2L,3L,2L,7L,11L,7L,9L),
metric = c("metric1","metric1","metric1","metric1","metric1",
"metric1","metric1","metric1","metric1","metric2",
"metric2","metric2","metric2","metric2","metric2",
"metric2","metric2","metric2"))
testres <- testdata %>%
dplyr::group_by(metric) %>%
dplyr::mutate(cusum_control(N)) %>%
dplyr::ungroup()
p <- cusum_control_plot(testres,
xvar = obs,
facet_var = metric,
title_text = "Faceted CUSUM Control plots")
p
```
## Flexible x axis
Here we add a date column, specify that the ```scale_type``` is ```'date'```, and provide the ```datebreaks``` argument to plot our data over time
```{r faceted_chart2, fig.width=5, fig.height= 3}
library(dplyr)
library(ggplot2)
library(cusumcharter)
testdata <- tibble::tibble(
N = c(1L,2L,1L,3L,1L,1L,1L,1L,1L,
1L,3L,2L,3L,2L,7L,11L,7L,9L),
metric = c("metric1","metric1","metric1","metric1","metric1",
"metric1","metric1","metric1","metric1","metric2",
"metric2","metric2","metric2","metric2","metric2",
"metric2","metric2","metric2"))
datecol <- as.Date(c("2021-01-01","2021-01-02", "2021-01-03", "2021-01-04" ,
"2021-01-05", "2021-01-06","2021-01-07", "2021-01-08",
"2021-01-09"))
testres <- testdata %>%
dplyr::group_by(metric) %>%
dplyr::mutate(cusum_control(N)) %>%
dplyr::ungroup() %>%
dplyr::group_by(metric) %>%
dplyr::mutate(report_date = datecol) %>%
ungroup()
p2 <- cusum_control_plot(testres,
xvar = report_date,
facet_var = metric,
title_text = "Faceted plots with date axis",
scale_type = "date",
datebreaks = '4 days')
p2 <- p2 + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90,
hjust = 1,
vjust = 0.5))
p2
```
## Highlight above and below control limits
Points outside the Upper Control Limit are always highlighted.
Use the ```show_below``` option to enable highlighting points below the Lower Control Limit
```{r highlightbelow,fig.width=5, fig.height= 3}
library(dplyr)
library(ggplot2)
library(cusumcharter)
testdata <- tibble::tibble(
N = c(-15L,2L,-11L,3L,1L,1L,-11L,1L,1L,
2L,1L,1L,1L,10L,7L,9L,11L,9L),
metric = c("metric1","metric1","metric1","metric1","metric1",
"metric1","metric1","metric1","metric1","metric2",
"metric2","metric2","metric2","metric2","metric2",
"metric2","metric2","metric2"))
datecol <- as.Date(c("2021-01-01","2021-01-02", "2021-01-03", "2021-01-04" ,
"2021-01-05", "2021-01-06","2021-01-07", "2021-01-08",
"2021-01-09"))
testres <- testdata %>%
dplyr::group_by(metric) %>%
dplyr::mutate(cusum_control(N)) %>%
dplyr::ungroup() %>%
dplyr::group_by(metric) %>%
dplyr::mutate(report_date = datecol) %>%
ungroup()
p5 <- cusum_control_plot(testres,
xvar = report_date,
show_below = TRUE,
facet_var = metric,
title_text = "Highlights above and below control limits")
p5
```