<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Cummings on Medium]]></title>
        <description><![CDATA[Stories by Daniel Cummings on Medium]]></description>
        <link>https://medium.com/@daniel.j.cummings?source=rss-f89fd62a7f25------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mxqd_zmDy8ghgEgE9gKcJw.jpeg</url>
            <title>Stories by Daniel Cummings on Medium</title>
            <link>https://medium.com/@daniel.j.cummings?source=rss-f89fd62a7f25------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 18:32:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.j.cummings/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Predicting hospital length-of-stay at time of admission]]></title>
            <link>https://medium.com/data-science/predicting-hospital-length-of-stay-at-time-of-admission-55dfdfe69598?source=rss-f89fd62a7f25------2</link>
            <guid isPermaLink="false">https://medium.com/p/55dfdfe69598</guid>
            <category><![CDATA[healthcare]]></category>
            <category><![CDATA[hospital]]></category>
            <category><![CDATA[healthcare-analytics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Daniel Cummings]]></dc:creator>
            <pubDate>Sun, 16 Dec 2018 03:55:56 GMT</pubDate>
            <atom:updated>2020-05-11T21:22:40.854Z</atom:updated>
            <content:encoded><![CDATA[<h4>Exploring an important healthcare performance metric</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X7lC_0LS71Jx_kjMKxCNug.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/ZCO_5Y29s8k?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Hush Naidoo</a> on <a href="https://unsplash.com/search/photos/hospital-room?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h4>Project Overview</h4><p>Predictive analytics is an increasingly important tool in the healthcare field since modern machine learning (ML) methods can use large amounts of available data to predict individual outcomes for patients. For example, ML predictions can help healthcare providers determine the likelihoods of disease, aid in the diagnosis, recommend treatment, and <a href="https://www.elsevier.com/connect/seven-ways-predictive-analytics-can-improve-healthcare">predict future wellness</a>. For this project, I chose to focus on a more logistical metric of healthcare, hospital <strong>length-of-stay</strong> (LOS). LOS is defined as the time between hospital admission and discharge measured in days.</p><p>U.S. hospital stays cost the health system at least <a href="https://www.healthcatalyst.com/success_stories/reducing-length-of-stay-in-hospital">$377.5 billion per year</a> and recent <a href="https://go.beckershospitalreview.com/how-predicting-patient-length-of-stay-enables-hospitals-to-save-millions">Medicare legislation</a> standardizes payments for procedures performed, regardless of the number of days a patient spends in the hospital. This incentivizes hospitals to identify patients of high LOS risk at the time of admission. Once identified, patients with high LOS risk can have their treatment plan optimized to minimize LOS and lower the chance of getting a hospital-acquired condition such as staph infection. Another benefit is that prior knowledge of LOS can aid in logistics such as room and bed allocation planning.</p><h4>Problem Statement</h4><blockquote>The goal of this project is to create a model that predicts the length-of-stay for each patient at time of admission.</blockquote><p>My initial thoughts were that using a random forest or gradient tree boosting ensemble method would yield the best results. The reason for this is that ensemble methods combine multiple learning algorithms to obtain better predictive performance than what could be obtained from a single algorithm and are frequently used in Kaggle <a href="https://www.datasciencecentral.com/profiles/blogs/want-to-win-at-kaggle-pay-attention-to-your-ensembles">competitions</a>. This first step, however, was finding a suitable dataset.</p><p>As a starting point for looking for data, my intuition was that the dataset should ideally include features such as the patient’s diagnosis category (e.g. heart disease, delivery, injury/poisoning, etc.), age, gender, marital status, and race. After searching for a useful medical database, I ended up choosing the MIT <a href="https://mimic.physionet.org/">MIMIC-III </a>database due to the robust amount of information it held. MIMIC is described as the following:</p><blockquote>MIMIC is an openly available dataset developed by the MIT Lab for Computational Physiology, comprising de-identified health data associated with ~40,000 critical care patients. It includes demographics, vital signs, laboratory tests, medications, and more.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*bXNicPoXAUVbdArpQgc9JQ.png" /></figure><p>As a side note, access to MIMIC requires taking a <a href="https://about.citiprogram.org/en/homepage/">research ethics and compliance training course</a> and filling out a research <a href="https://physionet.org/">application form</a>. The only obvious downside I found was that the database does not include pediatric information (ages 2–13).</p><p>In order to predict hospital LOS, the MIMIC data needed to be separated into terms of a dependent target variable (length-of-stay in this case) and independent variables (features) to be used as inputs to the model. Since LOS is not a categorical but continuous variable (measured in days), a regression model will be used for prediction. It was likely (as turned out to be true) that the data needed significant cleanup and feature engineering to be in a format compatible with the learning model. For this project, I used the <a href="https://pandas.pydata.org/">Pandas</a> and <a href="https://scikit-learn.org/">scikit-learn</a> libraries for Python.</p><h4>Metrics</h4><p>The expected outcome of this project is to develop a model that will be better at predicting hospital LOS than the industry standards of median and average LOS. The <a href="https://www.ahcancal.org/research_data/trendtracker/Documents/Length%20of%20Stay%20Calculation.pdf">median LOS</a> is simply the median LOS of past admissions to a hospital. Similarly, a second commonly used metric in healthcare is the <a href="https://www.intel.com/content/dam/www/public/us/en/documents/case-studies/predictive-analytics-help-a-hospital-group-reduce-patient-length-of-stay-study.pdf">average</a>, or mean LOS. To measure performance, I’ll compare the prediction model against the median and average LOS using the root-mean-square error (RMSE). The RMSE is a commonly used measure of the differences between values predicted by a model and the values observed, where a lower score implies better accuracy. For example, a perfect prediction model would have an RMSE of 0. The RMSE equation for this work is given as follows, where (n) is the number of hospital admission records, (y-hat) the prediction LOS, and (y) is the actual LOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*OSK9THjxBoK7bvi5Z65AAQ.png" /></figure><p>The ultimate goal is to develop a prediction model that results in a lower RMSE than the average or median models.</p><p>There is a multitude of regression models available for predicting LOS. To determine the best regression model for this work (of the subset of models that will be evaluated), the R2 (R-squared) score will be used. The R2 is a measure of the goodness of the fit of a model. In other words, it is the proportion of the variance in the dependent variable that is predictable from the independent variables. R2 is defined as the following equation where (<em>y_i</em>) is an observed data point, (<em>ŷ</em>) is the mean of the observed data, and (<em>f_i</em>) the predicted model value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/1*h_PSe8bfVhFVbaqmTWq7EQ.png" /></figure><p>Best possible R2 score is 1.0 and a negative value means it is worse than a constant model, average or median in this case.</p><h4>Data Exploration and Feature Engineering</h4><p>After several iterations of reviewing the contents of the various tables in the MIMIC database, I ended up selecting the following tables and loading them into DataFrames using Pandas: <em>ADMISSIONS.csv, PATIENTS.csv, DIAGNOSES_ICD.csv,</em> and <em>ICUSTAYS.csv.</em></p><p>The <em>ADMISSIONS </em>table gives information such as SUBJECT_ID (unique patient identifier), HADM_ID (hospital admission ID), ADMITTIME (admission date/time), DISCHTIME (discharge time), DEATHTIME, and more. The table had 58,976 admission events and 46,520 unique patients which seemed like a reasonable amount of data to do a prediction model study on. To start with, I created a length-of-stay column by taking the difference between the admission and discharge time for each row. I opted to drop rows that had a negative LOS since those were cases where the patient died prior to admission. Additionally, I found that 9.8% of the admission events resulted in death, so I removed these since they are not included as part of typical LOS metrics. The distribution of the LOS in terms of days is right-skewed with a median of 10.13 days, a median of 6.56 days, and max of 295 days.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*9WyVviV49gQj8w6KeaMbmQ.png" /></figure><p>For the admission <em>ethnicity</em> column, there were 30+ categories that could be easily reduced to the five shown below. Interestingly, the <em>Asian</em> category has the lowest median LOS of the dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*cS5PqA9gJ_M1b6dCaBWp1g.png" /></figure><p>For <em>religion</em>, I reduced the list to the three categories of <em>unobtainable</em> (13% of admissions), <em>religious </em>(66% of admissions), or not specified (20% of admissions). The <em>unobtainable</em> group has the lowest median LOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*fjyWSEJ1VtYl2hiha5WvVg.png" /></figure><p>Hospital admissions were reduced down to four categories: <em>urgent, newborn, emergency, elective</em>. <em>Newborns</em> have the lowest median LOS whereas the <em>urgent</em> care category has the highest. <em>Elective</em> admissions have a tighter distribution that favors lower LOS, which makes sense since the severity of such conditions is usually less time-critical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*wnJr9BGNcT2fr11mQEpPnQ.png" /></figure><p>There is an interesting insight for this dataset which shows that self-pay (often means no-pay) admissions have a much shorter LOS than the other insurance categories. Medicare and Medicaid take the top median LOS positions which could be related to the age of patients on those systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*j8NwjfGw1VI3xvn6FPQ8CQ.png" /></figure><p>The <em>PATIENTS</em> table provided a de-identified date of birth and gender information. Because the MIMIC dataset does not provide a real date of birth to protect the identities of the patients, I needed to engineer the age feature using the following decoder: a patient’s age is given by the difference between their ‘DOB’ date of birth and the date of their first admission. This in mind, I merged the <em>PATIENTS </em>and<em> ADMISSIONS </em>DataFrames and used to pandas ‘groupby’ to extract the first admission time for each patient.</p><blockquote>df[[‘SUBJECT_ID’, ‘ADMITTIME’]].groupby(‘SUBJECT_ID’).min().reset_index()</blockquote><p>After taking the difference between the discharge time and first admission time, I was able to take a look at the age distribution of patients. It should be noted that patients &gt;89 years old are put into the same age group in MIMIC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*syEQWVUWUxlLQ1XayVaCxg.png" /></figure><p>Although newborn patient data is included in the MIMIC dataset, pediatric ages are not. To add a dimension to the age distribution plot, I looked that the LOS versus age. The plot highlights the MIMIC groups of newborns and &gt;89 year olds, where there is an increasing amount of admissions going from 20 toward 80 years old. Because of the discrete-like distribution of data on the extremes of age, I decided to convert all ages into the categories of <em>newborn, young adult, middle age,</em> and <em>senior</em> for use in the prediction model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*Joi-hGUmNpTgyT5vVnaa-g.png" /></figure><p>The <em>DIAGNOSES_ICD</em> table provided the largest challenge in terms of feature engineering. The table consists of the patient and admission IDs, and an ICD9-Code which is described as follows (source):</p><blockquote>International Classification of Diseases, Clinical Modification (ICD-9-CM) is an adaption created by the U.S. National Center for Health Statistics (NCHS) and used in assigning diagnostic and procedure codes associated with inpatient, outpatient, and physician office utilization in the United States.</blockquote><p>I could have created <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html">dummy variables</a> for each code but it didn’t make sense in this case. There were 6,984 unique codes used in the MIMIC dataset and 651,047 ICD-9 diagnoses given to patients since most were diagnosed with more than one condition. Looking at the table, you can see that the <em>ICD9_CODE</em> column code takes a variable character length approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*3wdCv2UrApXI0L4PvyrpBg.png" /></figure><p>After some investigation, I found that the true code syntax is three digits followed by a set of decimals for subcategories. For example, the code from the first row is 403.01 which falls in the range of <em>diseases of the circulatory system</em> and the .01 value further specifies <em>hypertensive chronic kidney and related diseases</em>. Additionally, I noticed that ICD-9 has <a href="https://en.wikipedia.org/wiki/List_of_ICD-9_codes">17 primary categories </a>so I decided to sort all the unique codes per admission into these categories. My reasoning was that reducing the ICD-9 codes from 6,984 to 17 would make for a much more understandable ML model. Also, I didn’t want any ICD-9 codes to just have a single LOS target row since that would complicate training/testing. Finally, I transformed the sort ICD-9 codes into an admission-ICD9 matrix, grouped the diagnosis per admission, and merged the new columns back with the primary <em>admissions</em> DataFrame on the HADM_ID (admission ID). You can see that each row (admission) contains multiple diagnoses as they should.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*o4NS8Cp5_0xSUQJZ2tF2uA.png" /><figcaption>Part of the admission-ICD9 category matrix</figcaption></figure><p>Looking at the median LOS for each ICD-9 supercategory shows an impressive spread between <em>pregnancy</em> and <em>skin</em> diagnosis code groups. As will be shown later, the diagnosis categories are the most important features in predicting LOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*bQCavUrm2fJzPmraHvzYKw.png" /></figure><h4>Data Preprocessing</h4><p>Even after completing the feature engineering for age and ICD-9, there were some loose ends that needed tidying up before the data could be used for the prediction model. First, I ensured that no admissions resulting in death were part the cleaned dataset. I dropped all unused columns and verified that no NaNs existed in the data. For the <em>admission type</em>, <em>insurance type, religion, ethnicity, age,</em> and <em>marital status</em> columns, I performed the Pandas <em>get_dummies </em>command to convert these categorical variables into dummy/indicator variables. The final DataFrame size resulted in 48 feature columns and 1 target column with an entry count of 53,104.</p><h4>Prediction Model</h4><p>To implement the prediction model, I split the LOS target variable and features into training and test sets at an 80:20 ratio using the scikit-learn <em>train_test_split</em> function.</p><blockquote>X_train, X_test, y_train, y_test = train_test_split(features, <br> LOS, test_size = .20)</blockquote><p>Using the training set, I fit five different regression models (from the scikit-learn library) using default settings and then compared the R2 scores on the testing set. The GradientBoostingRegressor took the win with an R2 score of ~37% with the testing set so I decided to focus on refining this particular model. Because of past success with the RandomForestRegressor, I played with that model’s parameters but was never able to exceed the GradientBoostingRegressor score.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*91wuRhOIyPJoRmcTTA6y_g.png" /></figure><p>To refine the GradientBoostingRegressor model, I used the GridSearchCV function from scikit-learn to test out various permutations of parameters such as <em>n_estimators</em>, <em>max_depth</em>, and <em>loss</em>. The best estimator result from GridSearchCV was <em>n_estimators</em>=200, <em>max_depth</em>=4, and <em>loss</em>=ls. This resulted in a minor improvement with an R2 score of ~39% with the testing set.</p><h4>Results</h4><p>Before looking at the RMSE benchmark, I wanted to investigate what features were most important in predicting hospital length-of-stay when using the gradient boosting regression model. Diagnoses related to <em>prenatal</em> issues have the highest feature importance coefficient followed by <em>respiratory</em> and <em>injury</em>. As I alluded to earlier, the ICD-9 diagnoses categories are by far the most important features. In fact, in the top 20 top features, only <em>emergency </em>admission type, <em>gender</em>, and <em>Medicaid</em> insurance showed any importance outside of diagnosis groups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*gj4RdPHKGolNtk6aOyNYOg.png" /></figure><p>In the metrics section, I stated that the RMSE would be used to compare the prediction model versus the industry-standard average and median LOS metrics. The gradient boosting model RMSE is better by more than 24% (percent difference) versus the constant average or median models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*OasLu1vLTLvLsQ-wjJeEuA.png" /></figure><p>While the RMSE trend is promising, I also wanted to evaluate the model from a few other perspectives. The following figure takes the first 20 admissions from the test set and directly compares the actual, predicted (gradient boosting model), average, and median LOS values. This gives a more convoluted picture of the prediction model; in some admissions, it predicts well but not as well in others. However, based on the RMSE score, the prediction model will still be generally more accurate than using the median or average LOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*CQ4CQNjOkV3Yb7btXK29Vg.png" /></figure><p>The final way I wanted to look at the model was to plot the proportion of accurate predictions in the test set versus an allowed margin of error. Other <a href="https://www.intel.com/content/dam/www/public/us/en/documents/case-studies/predictive-analytics-help-a-hospital-group-reduce-patient-length-of-stay-study.pdf">studies</a> qualify a LOS prediction as <em>correct</em> if it falls within a certain margin of error. It follows that as the margin of error allowance increases, so should the proportion of accurate predictions for all models. The gradient boosting prediction model performs better than the other constant models across the margin of error range up to 50%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*Zh5nx9ZITQ4uAHYx7sR60Q.png" /></figure><h4>Conclusion</h4><p>The MIMIC database offered surprisingly good depth and detail related to medical admissions which enabled me to create a hospital length-of-stay prediction model that considered a lot of interesting input features. The most surprising aspect of this work was how the patient ICD-9 diagnoses played a more important role than age when predicting the length-of-stay. By far, the most challenging aspect of this project was the feature engineering of the ICD-9 diagnoses into a more practical and interpretable form of supercategories. However, therein also lies the most obvious area for future improvement. Given that the diagnoses have such strong feature importance, it would be worth evaluating whether additional subdividing of the primary ICD-9 categories would yield a better prediction model. My theory is that the prediction model would become more accurate (lower RMSE) with this optimization, so long as there were enough admission records in the dataset to support reasonable diagnoses model training.</p><p>The GitHub repository for this project is available <a href="https://github.com/daniel-codes/hospital-los-predictor">here</a> with a Jupyter Notebook that details all of the sections explored in this post.</p><p><em>MIMIC-III, a freely accessible critical care database. Johnson AEW, Pollard TJ, Shen L, Lehman L, Feng M, Ghassemi M, Moody B, Szolovits P, Celi LA, and Mark RG. Scientific Data (2016). DOI: </em><a href="http://dx.doi.org/10.1038/sdata.2016.35"><em>10.1038/sdata.2016.35</em></a>. Available from: <a href="http://www.nature.com/articles/sdata201635">http://www.nature.com/articles/sdata201635</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55dfdfe69598" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/predicting-hospital-length-of-stay-at-time-of-admission-55dfdfe69598">Predicting hospital length-of-stay at time of admission</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>