Using Predictive Algorithms to Track Real Time Health Trends

How to build a real-time health dashboard for tracking a person blood pressure readings, do time series analysis, and then graph the trends over time using predictive algorithms.



Adding Predictive Algorithms

 
I needed to make sense of the data as easily as possible. I explored a few services that did machine learning and data analysis. Most were limited to text classification, expensive, or not available as a serverless API.

Then I found Algorithmia, which has a large library of algorithms that run as microservices on your data. You call the algorithm, pass in your data, they run the algorithm over it, and return the results in realtime. They have a Python library, and since it’s an API, it fit perfectly with this serverless project.

I chose two predictive algorithms for this project:

Using Simple Moving Average

I used simple moving average to smooth out the data, and make it easier to spot trends in the graph. Using the moving average also helped reduce the fluctuations and noise in the raw data.

First, we define our simple moving average function:

Then, as part of _fetch_withings() from above, we pass our systolic, diastolic, and pulse data to the function like so:

This creates a smoothed out chart of our vitals data. Then we use the forecast algorithm to project the trends into the future.

Using Forecast

This is where things get really fun. I had about five months of data to work with, and, generally speaking, the more data you have the better!

I first defined my forecast function:

Again, as part of the _fetch_withings() function, I pass in the data as an array to the forecast algorithm. We could stop here, but the forecasted data would be prone to spikes and fluctuations. So, once that’s complete, I run the moving average algorithm on the forecasted data to smooth the results out:

Here’s the graphed results below showing the five months of systolic, diastolic, and pulse data, with a five month forecast for each:

Blood Pressure Averages and Forecast

And, here is the output from the forecast algorithm, but this time using the simple moving average data instead of the raw data:

Blood pressure forecast results using moving average data.

Much better! Blood pressure data is hard to work from as can be quite erratic at times. There are a number of algorithms for smoothing and normalize the data, which I intend to use to improve the predictions in the future. For instance, I could have used Linear Detrend to focus the analysis on the fluctuations in the data, or Autocorrelate to analyze the seasonality of the time series. I could even use Outlier Detection to remove unusual data points in the raw data, which could indicate bad readings.

Conclusion

 
The main takeaway is that it appears my friend's blood pressure isn’t going to get worse, and should stay within an acceptable range for the next few months.

And, thanks to their new health dashboard, my friend now has a set of graphs they can take to their doctor when discussing long term treatment. Blood pressure is something that can be influenced by a range of factors so regular reviews are important for long term management.

This was my first attempt at making forecasts and understanding the many, many ideas behind this kind data processing. I have barely scratched the service with what can be done with the data.

Tools used:

Want to build your own dashboard? Get the code from this Github repo.

Bio: Chris Hannam has been making a living playing with servers and writing code professional for around 15 years. His main language is Python, with a solid history of Java and PHP mixed in.

Original. Reposted with permission.

Related: