class: title-slide, left, bottom
# NHS-R — Statistical Process Control Charts ---- ## **Examples of how to produce SPCs in R** ### Zoë Turner ### March 2021 --- ## Package details ```r install.packages("qicharts2") library(qicharts2) ``` </br> - Available on CRAN - Comes with sample healthcare datasets - Created by Jacob Anhøj, a doctor in Denmark --- # Why I like this package ... ## Reasons to be joyful * I skim instructions * I just _get_ healthcare data (not plants and certainly not cars) * Reproducible examples in the vignette * More than one type of SPC available --- ## Like this | Snapshot of the vignette: <img src="data:image/png;base64,#img/FirstRunChart2.png" width="1000px" /> --- # Nightingale's Crimea... ## Getting data Copied data manually from this [site](https://understandinguncertainty.org/node/214) using a GUI (graphical user interface) package called `library(datapasta)` to copy directly to R Saved in a script for quick retrieval later ```r source("Crimea.R") ``` --- ## Being #rstats There is a package for that... even for historical data! ```r install.packages("HistData") library(HistData) data("Nightingale") ``` --- ## And the coxcomb can be recreated... ### ... and animated as a GIF Credit: Sian Bladon, Winner of the most [innovative FloViz Competition 2020 ](https://statsyss.wordpress.com/2020/05/11/floviz-challenge-entries/) from the Young Statisticians Section of the Royal Statistical Society <img src="data:image/png;base64,#https://github.com/sianbladon/Data-Viz/blob/master/FloViz-Challenge-May-2020/floviz.gif?raw=true" style="display: block; margin: auto;" /> --- # Replot in qicharts2 ## Run chart code Built on ggplot2 ```r qic(startMonth,number, data = crimeaLong, chart = 'run', decimals = 0, x.angle = 90, title = 'Run chart deaths in Crimea War', ylab = 'Number of deaths', xlab = 'Month' ) ``` --- # Replot in qicharts2 ## Run chart code ![](data:image/png;base64,#spc-presentation_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- # Sprinkle ggplot2 magic... ## Facets ```r qic(startMonth,number, data = crimeaLong, chart = 'run', decimals = 0, * facets = ~death_cause, x.angle = 90, title = 'Run chart deaths in Crimea War', ylab = 'Number of deaths', xlab = 'Month' ) ``` --- # Sprinkle ggplot2 magic... ## Facets ![](data:image/png;base64,#spc-presentation_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- # Just imagine... Producing... * 30 incident categories -- * 10 wards -- * 25 LSOAs -- Looking for for signals. That's a lot of individual plots to do in SQL or Excel. --- ## Moving Range SPC ```r qic(startMonth,number, data = crimeaLong, * chart = 'mr', decimals = 0, facet = ~death_cause, x.angle = 90, title = 'SPC mr chart deaths in Crimea War', ylab = 'Number of deaths', xlab = 'Month' ) ``` --- ## mr Chart (or is that Mr Chart!) As seen on Twitter @Letxuga007 ![](data:image/png;base64,#spc-presentation_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- # Other SPC charts are available ## Rare events - time between The code requires a count of days difference. .footnote[Cabg = coronary artery bypass operations] ```r # Altered slightly from the qicharts2 vignette fatalities <- cabg %>% filter(death) %>% mutate(dt = date - lag(date)) ``` --- ## T-chart Time between used for rare events like deaths from operations ```r tchart <- qic(dt, data = fatalities, * chart = 't', title = 'Days between deaths (T chart)', ylab = 'Days', xlab = 'Death #') tchart ``` --- ## T-chart A point above the upper control limit is a good thing! ![](data:image/png;base64,#spc-presentation_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- # Breaking into the chart ## Getting the data The numbers generated to create the charts can be viewed/saved by typing: ```r newObject <- tchart$data ``` --- ## When is this useful? In this blog: https://www.r-bloggers.com/backed-by-data/ @_johnmackintosh shows how to: * tweak the charts -- <br> And I used this code * to include only those charts with a signal * to link back to the data to say which patients/incidents/wards were behind the points. This data can be automatically generated in a report -- John Mackintosh has also created 2 specific R packages: [runcharter](https://github.com/johnmackintosh/runcharter) </br> -- [spccharter](https://github.com/johnmackintosh/spccharter) --- # Other interesting things ## Further information SPC Shiny dashboards are being discussed on the NHS-R Slack site in the channel #proj-shiny-spc Will be based on some of the work started by [Chris Reading](https://github.com/chrisreading01/SPCwizard) ([patch](https://github.com/ChrisBeeley/SPCwizard/tree/patch-1) by Chris Beeley) and Simon Wellesley-Miller. - please join us and contribute link: [nhsrcommunity.slack.com](nhsrcommunity.slack.com) - Twitter: [@NHSrCommunity](https://twitter.com/NHSrCommunity) - Me: [@Letxuga007](https://twitter.com/Letxuga007) .footnote[The **nhsr theme** was designed by [Silvia Canelon](https://silvia.rbind.io) for, and with feedback from, the [NHS-R Community](https://github.com/nhs-r-community), following the [NHS identity guidelines](https://www.england.nhs.uk/nhsidentity/identity-guidelines/).]