<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-runcharting-man/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://johnmackintosh.com/2017-04-06-the-run-chart-ing-man/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	Measurement for improvement in healthcare with R and Qlik – </p>
<h2>My not so secret obsession</h2>
<p>Anyone in the small subset of the Earth’s population who both read this blog AND follow me on Twitter will know that I spend a lot of time at work either producing or analysing <a target="_blank" rel="nofollow" href="http://www.qihub.scot.nhs.uk/knowledge-centre/quality-improvement-tools/run-chart.aspx">run charts</a>, which we use to determine if our quality improvement work is genuinely assisting us achieve our target outcomes.</p>
<p>A run chart is essentially a line chart, with an initial baseline median plotted and extended into the future. Then, as the improvement work continues, you continue to plot the data and look for certain patterns within the data which are statistically unlikely to occur by chance.<br>
Here’s an example (minus the date axis and the y-scale):</p>
<p><img alt="2017-04-06 Basic Run chart.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-04-06%20Basic%20Run%20chart.PNG?w=456" data-recalc-dims="1"></p>
<p>Take a look at the link above and you will see graphics demonstrating the 4 rules (shift,trend, too few runs, extreme data point).</p>
<p>For a long time I’d been thinking about how to automate the process of identifying sustained runs of improvement (signified in this case using the <a target="_blank" rel="nofollow" href="http://www.ihi.org/Pages/default.aspx">IHI</a> run chart rules of 9 consecutive points on the appropriate side of the median), and then calculating a new improvement median from these points.</p>
<p>I’ve actually written SQL queries to automate most of this – including finding the first improvement median but not automatically finding every re-phased run beyond that( e.g. phase 2, then phase 3 etc) without resorting to recursion or looping. Which, as in R, is frowned upon. However, unlike R, calculating medians when there is no native median function (SQL Server 2008 R2) is a bit of a challenge, along with finding trends and every run for comparison with the “too few runs” rule. Got there in the end, and it improved my SQL skills no end, but SQL is not the right tool for the job.</p>
<p>I should mention the fantastic <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=qicharts">qicharts</a> package, which allows you to create virtually every chart you would require, with a host of options. I’ve used this lot, and recommend it. It has some very robust rules built in to identify improvements. It will produce faceted plots, and allows you to specify different periods over which to calculate medians – so, if a an improvement is noted, you can calculate a new median from that point onwards.</p>
<p>Because of the volume of metrics I have to track, it’s not possible (or at least, not desireable) for me to manually specify different improvement ranges for each set of metrics.</p>
<p>So, for the last wee while, I’ve been working away at automating all this using dplyr and ggplot2.</p>
<p>I wondered if this could be done in a one-shot, vectorised approach.<br>
I googled. I considered data.table, which may very well be the ideal package to do the work.<br>
I saw various other possible approaches and solutions. I obsessed over whether I could vectorise it, and if not, would it make me a Bad Person.</p>
<p>And then I stopped worrying about it and just cracked on with dplyr.</p>
<p>I wrote many functions.<br>
I learned some basic R functions that I didn’t know existed (need to RTFM at some point).<br>
I wrote complex base R that future me is going to hate me for, even with the comments.<br>
I grappled with <a target="_blank" rel="nofollow" href="http://adv-r.had.co.nz/Computing-on-the-language.html">Non Standard Evaluation</a>.</p>
<p>I laughed.<br>
I cried. <br>
I <em>nearly</em> hurled (see Non Standard Evaluation).</p>
<p><img alt="2017-04-06 Garth.jpg" src="https://i1.wp.com/johnmackintosh.com/img/2017-04-06%20Garth.jpg?w=456" data-recalc-dims="1"></p>
<p>And finally, I came up with not one but 2 different R based approaches.</p>
<p>The first one is less dplyr intense and works for just one plot at a time – ideal for ad-hoc analysis and plotting. And it’s quick too:</p>
<p><img alt="2017-04-06 SubSecondPlot.PNG" src="https://i0.wp.com/johnmackintosh.com/img/2017-04-06%20SubSecondPlot.PNG?w=456" data-recalc-dims="1"></p>
<p>I also produced a slightly more heavy duty approach for faceted plots within ggplot2.<br>
For one of my metrics I now have run-charts across 99 seperate locations, all plotted nicely in A3 with re-phased medians.<br>
I was literally punching the air the first time I saw this magic plot appear on my monitor.<br>
From start to finish – the analysis and plotting/saving to pdf took around 20 secs. Not too shabby.</p>
<p>I’ve <strong>also</strong> been digging in to <strong>QlikView</strong> a bit more recently. Just over a week ago I sat down to see if I could crack this in QlikView. Here is my progress so far:</p>
<p>A simple run of 9 in a row identified and higlighted:</p>
<p><img alt="2017-04-06-qlikview rephased1.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-04-06-qlikview%20rephased1.PNG?w=456" data-recalc-dims="1"></p>
<p>These pesky points on the median gave me some grief. Ignoring these, in various combinations,was by far the hardest part to implement:</p>
<p><img alt="2017-04-06-qlikview rephased2.PNG" src="https://i0.wp.com/johnmackintosh.com/img/2017-04-06-qlikview%20rephased2.PNG?w=456" data-recalc-dims="1"></p>
<p>From my work with QlikView, I’m going to go back to my R code and see if I can make any more improvements. It’s great how different tools can inspire you to new ideas and looking at things a different way.</p>
<p>I’m not going to stick any code up just yet – but maybe might have a package in the works. I understand that NSE in dplyr is getting revised so I’m going to wait to see what transpires.</p>
<p>In the meantime though – for blazing fast interactive analysis on the fly for any combination within my data set – I can use QlikView.</p>
<p>And for static reports at scale, or simple ad-hoc analysis, yet again, its R, and the ggplot2 / dplyr super-combo that get’s the job done.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://johnmackintosh.com/2017-04-06-the-run-chart-ing-man/"> HighlandR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SJIQZ1L469A"></a></div>