Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/johnmackintosh/cusumcharter

Easier CUSUM control charts. Returns simple CUSUM statistics, CUSUMs with control limit calculations, and function to generate faceted CUSUM Control Charts
https://github.com/johnmackintosh/cusumcharter

cusum ggplot2 health-informatics healthcare quality-improvement r r-package rdatatable rstats statistical-process-control

Last synced: 3 months ago
JSON representation

Easier CUSUM control charts. Returns simple CUSUM statistics, CUSUMs with control limit calculations, and function to generate faceted CUSUM Control Charts

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 300,
fig.width = 5
)
```

# cusumcharter

[![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)

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

```