# Markovchart: an R package for cost-optimal patient monitoring and treatment using control charts This post is a follow-up on our previous one from 2019:

In that one we discussed Markov chain-based cost-optimal control charts briefly and gave an example for their use on the data of diabetic patients. The goal of control charts (and generally of statistical process control) is to improve a process from a certain point of view e.g. minimise the number of faulty products, or in patient monitoring to minimise health care burden related to elevated lipid levels. There are different approaches to the measuring of control chart performance, one of which is statistical optimality. Another approach – which is also used here – is to define costs to various parts of the control chart model and try to optimise the setup by minimising the total cost. Our method models processes which can only shift to one (positive) direction, with sample size n = 1. This aims to model the monitoring of a characteristic where the shift is possible to only one direction, and the monitoring is focused at one patient at a time. From a control chart point of view, this framework results in two free parameters for optimisation: the time between samplings h, and the critical value k. When the observed process exceeds the critical level, it induces and alarm signal and repair. An alarm can be true or false (due to measurement error) this gives each distance from the target value an additional quality. The advantage of the Markov chain-based framework is that it allows for extensions that are required in the monitoring of several health care process types. The methods are described in Zempléni et al. (2004), Dobi and Zempléni (2019a) and Dobi and Zempléni (2019b).

Recently, an R package has been developed based on the mathematical background of the Markov chain-based control charts. The package is available at https://CRAN.R-project.org/package=Markovchart. This post will highlight the features and uses of the package. The most important generalisations implemented in the package are the random shift size (degradation), random repair (treatment) and random sampling time (patient non-compliance). The package is thus capable of modelling and simulating many different illnesses and therapies.

The package provides functions for cost-optimal control charts with a focus on health care applications. As mentioned above, we allow random shift sizes, random repair and random sampling times. The Markovchart package features three main functions, all connected to the Markovchart() function:

• Markovchart() for process behaviour estimation and cost calculation with optimisation.

• Markovcontour() for plotting the results of Markovchart() as the function of the free parameters (h and k).

• Makrovsim(), a function for simulating processes handled by Markovchart()

The Markovchart() function is the main function of the package. The function comes with a plethora of arguments, which is due to the fact that the function incorporates the generalised methods in a highly customisable way. However, if a complex model with all generalisations is not feasible or necessary, the user can leave out generalisations and/or leave several arguments blank and the model will still work for the given options and data.

The Markovchart() function can be used for both stationary distribution and cost calculation. The cost calculation itself is based on the stationary distribution of the monitored characteristic (i.e. we base the costs on the expected patient health).  Two examples for stationary distribution comparison with the empirical blood sugar (HbA1c) distribution of diabetic patients can be seen on the figure below. The example shows the distribution for therapies with insulin analogues and glucagon-like peptide (GLP) therapies. For better visualisation the stationary distributions produced by the Markovchart() function  are transformed in a way that does not take into account the type of the state (whether there was an alarm signal), only the distance.

Markovcontour() is a convenience function for plotting values in a contour plot as the function of h and k. The function uses the ggplot2 and the metR package for visualisation. As the Markovchart() function provides vectorised method for calculating the expected cost or cost standard deviation per unit time, one can easily create the data required by the Markovcontour() function. Markovcontour() is mainly a convenience function for quick plotting with optimal h and k parameters and exploration of the relationship between the parameters and the resulting costs. It can be modified like any other plot produced by the ggplot2 package as can be seen in the added dot in the example below (costs are in EUR).

The Markovsim() function is able to simulate processes with the same assumptions as Markovchart(). The same type of processes are handled by the Markovchart() function, but in that case all calculations are analytic. In practice it was found that comparing simulations with theoretical results can be useful for parameter tuning (e.g. finding discretisation parameters that give accurate results but keep running times low). Below we compare an exponential-geometric model with simulated results using the same parameters. A good basis of comparison is the stationary distribution, as all further results can be calculated from it. Again, the stationary distributions produced by the Markovchart() and Markovsim() functions  are be transformed in a way that does not take into account the type of the state, only the distance. The theoretical and simulated stationary distributions can be seen in the figure below.

Overall, the Markovchart package aims to give health care professionals and biostatisticians a tool to model, simulate and visualise complex processes involving disease development, treatment, patient non-compliance and related costs. The package’s main use is the development of cost-optimal monitoring and treatment regimens, focusing on individual patients (and homogeneous patient groups), by optimising the time between samplings and the treatment-inducing critical disease level.

Zempléni A, Véber M, Duarte B, Saraiva P (2004). “Control Charts: a Cost-optimization Approach for Processes with Random Shifts.” Applied Stochastic Models in Business and Industry, 20(3), 185–200. doi:10.1002/asmb.521.

Dobi B, Zempléni A (2019a). “Markov Chain-based Cost-Optimal Control Charts for Health Care Data.” Quality and Reliability Engineering International, 35(5), 1379–1395. doi:10.1002/qre.2518.

Dobi B, Zempléni A (2019b). “Markov Chain-based Cost-optimal Control Charts With Different Shift Size Distributions.” Annales Universitatis Scientiarum Budapestinensis de Rolando Eötvös Nominatae, Sectio Computatorica, 49, 129–146. URL http://ac.inf.elte.hu/Vol_049_2019/129_49.pdf.