---
title: "Monitoring Improvement"
author: "John MacKintosh"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Monitoring Improvement}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
  
```{r, echo = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/"
)
```


## Use of the package to monitor improvement

Let's mimic the start of an improvement initiative. We have *some* data, but not enough to derive a meaningful baseline median:


```{r not-enough-data, fig.height=5, fig.width=8}
library(runcharter)
library(data.table)

testdf <- signals # rename the built in data set
setDT(testdf)
testdf <- testdf[grp == "WardX",head(.SD,7)]

 runcharter(testdf,
  med_rows = 0,
  runlength = 0,
  direction = "both",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("1 month"))


```



## Baseline data recorded

After 13 data points have been collected, we can calculate our initial median. 
We'll use this for analysis  - future points will be assessed against the median and we will look for a run of 9 consecutive points below the median.

```{r baseline-data, fig.height=5, fig.width=8}
library(runcharter)
library(data.table)

testdf <- signals # rename the built in data set
setDT(testdf)
testdf <- testdf[grp == "WardX",head(.SD,13)]

 runcharter(testdf,
  med_rows = 13,
  runlength = 0,
  direction = "both",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("1 month"))
```

## Random variation

We have extended our original baseline median, but no signals of improvement are visible yet

```{r random-variation, fig.height=5, fig.width=8}
library(runcharter)
library(data.table)

testdf <- signals # rename the built in data set
setDT(testdf)
testdf <- testdf[grp == "WardX",head(.SD,30)]

 runcharter(testdf,
  med_rows = 13,
  runlength = 9,
  direction = "below",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("3 months"))

```


## On the road to improvement

We have been working hard to deliver our improvement initiative, and we're encouraged by a run of 5 consecutive points below the median.

```{r road-to-improvement, fig.height=5, fig.width=8}
library(runcharter)
library(data.table)

testdf <- signals # rename the built in data set
setDT(testdf)
testdf <- testdf[grp == "WardX",head(.SD,40)]

 runcharter(testdf,
  med_rows = 13,
  runlength = 9,
  direction = "below",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("3 months"))
```

Finally, thanks to our ongoing improvement efforts, a run of 9 points on the correct side of the median line is achieved.
A new median is calculated using the points from this run:

```{r run-noted, fig.height=5, fig.width=8}
testdf <- signals # rename the built in data set
data.table::setDT(testdf)
testdf <- testdf[grp == "WardX",head(.SD,44)]

 runcharter(testdf,
  med_rows = 13,
  runlength = 9,
  direction = "below",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("3 months"))
```

## Success


```{r improvement-noted, fig.height=5, fig.width=8}
testdf <- signals # rename the built in data set
data.table::setDT(testdf)

 runcharter(testdf[grp == "WardX",],
  med_rows = 13,
  runlength = 9,
  direction = "below",
  datecol = date,
 grpvar = grp,
  yval = y,
  facet_cols = 1,
  chart_breaks = ("6 months"))

```



