Share via

Predicting Hospital Length of Stay (LOS) using SQL Server 2016 with R Services

This post is authored by Bharath Sankaranarayan, Principal Program Manager, at Microsoft.

Today we are excited to announce a Hospital length of Stay solution, leveraging SQL Server 2016 with R Services.  This solution accelerator will enable hospitals and healthcare providers to leverage machine learning to improve the prediction on how long a patient is expected to stay.

Health Care institutions around the world have a need to serve patients who are in dire need of hospitalization. The management at the hospital must coordinate the influx of patients and make staffing decisions to help serve patients and improve their service.  The expected length of stay for each patient is important information to aid in these decisions.

The Hospital Length of Stay Solution, provides the view into how heath care facilities can use information such as patient's vitals, their history, and current symptoms and apply machine learning to accurately predict how long they need to be treated and what type of care they need during their stay.

We have published this solution in the Cortana Intelligence Solutions Gallery. The solution provides a hands-on experience by deploying into your Azure subscription. The deployment takes just a few clicks, getting the solution up and running by configuring it on our most popular VM, namely the Microsoft Data Science VM (DSVM) that comes loaded with all the tools that a data scientist will need. The code is also published on GitHub, so if you prefer to run this on your own machine entirely, you can use the instructions that are available there.

The solution is developed modelling a use case that applies to the real world. Length of Stay (LOS) is defined in number of days from the initial admit date to the date that the patient is discharged from any given hospital facility. There can be significant variation of LOS across various facilities and across disease conditions and specialties even within the same healthcare system. Advanced LOS prediction at the time of admission can greatly enhance the quality of care as well as operational workload efficiency and help with accurate planning for discharges resulting in lowering of various other quality measures such as readmissions.

Different personas, different strokes

The Hospital Length of stay solution provides ability for a Chief Medical Information Officer (CMIO) to accurately predict which facilities are overflowing and which have space. They must deal with the technology and healthcare professionals in a healthcare setting.

To the Care Line Manager, who is directly involved with the care of patients, they are required to monitor individual patients by wards while ensuring that the right staff is available to meet specific care for their patients. They are required to accurately predict the staff resources needed to handle the discharge of the patients and thus having a highly trusted system will save the hospital and the patients time and money.


To the data scientists who are testing and developing solutions can work conveniently from their preferred R IDE on their client machine, while pushing the compute to the SQL Server machine. The completed solutions are deployed to SQL Server 2016 by embedding calls to R in stored procedures. These solutions can then be further automated with SQL Server Integration Services and SQL Server agent.

They can also use PowerShell scripts or Jupyter Notebooks, in addition to using IDEs such as R Tools for Visual Studio. Completed solutions are deployed to SQL Server 2016 by embedding calls to R in stored procedures. These solutions can then be further automated with SQL Server Integration Services and the SQL Server agent.

This solution was developed in collaboration with KenSci, a Risk Prediction Platform for Healthcare, built by doctors and data scientists, by leveraging Machine Learning on SQL, Cortana, Azure and Power BI. Leading health systems leverage KewnSci's Predictive and prescriptive platform to analyze and mitigate clinical as well as operational risk. For more information, visit KenSci

We have made the entire code that powers this solution free to use and modify as well.

To try this out please visit Predicting hospital length of stay and provide us your feedback.