Predicting Postprandial BG in T1Ds

Hello community!

I was diagnosed with Type 1 about a year and a half ago. This means that I have only scratched the surface of things to know and understand about my body in comparison to some of the more experienced users on this forum. That is why I am using this as an outlet for discussion.

A little background about myself. I am a 25 year old electrical engineer working in the Molecular Imaging division of Siemens Healthineers (we all hate the new name). Basically we design PET scanners. I recently decided to continue my education by enrolling in an Online Masters of Computer Science program at Georgia Tech specializing in Machine Learning. The program is tailored to full time individuals who still want to further their education, and I think it is truly the first of its class.

I started learning about a technique called Multivariate Linear Regression in my program. It is basically like finding a line of best-fit for an X-Y scatter plot and then projecting both the independent variable and dependent variable into multiple dimensions.

For example, if you were to plot the price of a house vs. the size in square feet, you would likely see data points that generally trend upwards (price of house increases with the size in square feet). You could then find a linear equation that approximates the data and use it to predict the price of a house given just the size in square feet. This equation probably would not be very accurate, because the price of a house depends on more than just one factor. That is were multiple variable linear regression comes in. You could incorporate the year that the house was built, the number of bedrooms, the size of the lot, etc… to generate an equation with multiple predictor variables. Using this, you could better predict the price of a house given a set of known factors.

For the past few months, I have been playing with my blood sugar and macro-nutrient data to build a model that basically leverages multivariate linear regression to have an idea of how much insulin to take with my meals to stay in my post-meal BG range. I am using sugars, carbs (total - sugars - fiber), protein, fat, and insulin as my variables to predict what my 1.5hr BG and 3.0hr post-meal glucose is.

Some things to consider. Linear regression assumes that everything is linear (I know I probably didn’t need to say that). That is, the model assumes that for each gram of carb that I consume, my post-meal will be x amount higher at a given time. In reality it doesn’t quite work like that. A body can only digest so fast, and the rate at which each gram of carbohydrate will raise glucose depends on several factors. Therefore I have begun to cluster my data into sets. Right now I am clustering based on the number of carbs in a meal.

I am still in the honeymoon phase. Therefore, anything less than 30 carbs my pancreas can handle completely on its own. So I separate that data from the rest. After 30 carbs it is as if external insulin injected is the only way to lower my blood sugar. So between 30 carbs and 60 carbs is my second set of data. After 60 carbs, my insulin to carb ratio seems to increase, so I have separated that data into a third set.

This means that I have three equations that I use to predict my postmeal blood sugars depending on the carb load of the meal. After I log each premeal BG, meal macronutrients, and postmeal BGs the model “learns” a little bit about how my body works and updates the equation coefficients. Of course this is all automated by the App/software I am working on.

I have been having a lot of fun with this project, and I would like to just open up a discussion of what everyone thinks about using the system that I am developing. The correlation of my three data sets isn’t very good right now (67% and 55% for meals between 0-30 and 31-60 carbs respectively). However, the more data I take the higher the correlation gets, because I introduce new predictor variables with every 5 data points to prevent over-fitting of the linear regression model. So the goal is to continue improving and finding out smart ways for the model to learn and cluster data to suit each individual’s personal Diabetes.

My Nutritionist basically told me I’m an idiot for thinking I can automate this stuff, and she was actually really offended when I told her about my idea. Perhaps I am wasting my time on all this, but at least I’m having fun applying my theoretical knowledge to something practical for me personally. My thoughts are endless on this topic so even writing this post was a challenge. Last thought… If you have a CGM or pump I don’t think this really provides any benefits. I think everyone’s goal should be obtaining a CGM and then a pump. In fact, I get my Dexcom G5 early next week and I can’t wait! I want to make a live feed of my predictions vs. actual blood glucose to show my model working.

Thanks for taking the time to hear my rant.

1 Like

Welcome apmeyer27!

Interesting idea. Off the top of my head I can think of three things that should be considered if youre putting together this detailed amount of data. Ill speak in generalities, because, as we say around here, YDMV (Your Diabetes May Vary).

First, fat. The presence of fat in the meal, and how much, can slow down absorption of carbs. A common name for this is “the pizza effect”. To give you an idea, if I eat 30g of carbs from say, a potato, I can simply cover this with one bolus. However, if its a high fat meal, that could be extended an hour. In the case of pizza, for me, its extended over 3.5 hours for the same carbs.

Second, fiber. Now, for many people when they read the label on something, they subtract the grams of fiber from the grams of carbs and bolus for that. The reasoning is the fiber will offset the carbs in the colon during digestion, making the carbs “free” in a sense. HOWEVER! This isnt true for everyone. Myself, for example, I bolus for the total number of carbs with no regard to fiber. This has been through much trial and error.

Lastly, low glycemic carbs vs high glycemic, or complex carbs vs simple carbs. The effect on many people is the same as fat…complex carbs take longer to digest, and typically digest slower thus providing less of a spike.

When I was on shots, I often took multiple shots to cover a meal, based on the info above. Take pizza again. If I took one shot at the beginning of a meal for the entire carb load, I would inevitably go low 90 minutes later, then go high 2-3 hours later. So I usually took 2 or 3 shots spread out over 3 hours.

Anyhow, these are things to think about, and to pay attention to as you navigate how different carbs and different foods AFFECT YOU.

The bottom line? Not all carbs are created equal.

1 Like

Interesting stuff for sure. But there are a lot of other factors though. Are you looking to add more variables?

1 Like

I find your quest interesting even if I don’t understand all the underlying math concepts. Diabetes metabolism in humans is a complex system. We all understand, in general terms, that insulin drives blood glucose down and food drives it up.

The extent that blood sugar rises or falls is also dependent on our sensitivity to insulin. I’ve learned from observing my blood glucose movements over many years that insulin sensitivity is not a static factor. If I let a day or two pass without some exercise, my insulin sensitivity goes down and all other factors held equal, I need more insulin for the same level of blood glucose.

You may be interested in the discussions taking place on a Gitter channel devoted to developing and maintaining an automated insulin dosing system.

Finally, you may find comments from TuD member @Dragan1 interesting. Good luck with your project!

Interesting stuff for sure. But there are a lot of other factors though. Are you looking to add more variables?

Yes, although it is hard to know exactly how to quantify things such as exercise, sleep, previous few meals, etc… because it varies so much.

My first version of the algorithm took all macronutrients from all prior meals of a given day into account and it was just too complex for a user friendly experience. The less you account for the worse the correlation of the data is. So I am trying to find a balance between a model that is accurate enough to be useful and a user experience that is not overly time consuming.

Love the feedback Dan!

Your comment about fiber is really informative. I assumed fiber never counted. It is easy enough to add a separate fiber predictor to the model. If it does not affect an individual, the coefficient will approach zero over time. For someone like yourself, the coefficient should approach its actual affect on your body at a given time.

The delay in spike caused by fat is constantly breaking my brain on how to capture it. So what I do see in my model is that for every gram of fat I consume, my postmeal BG is some coefficient less than it would have been without the fat. This means that the carbs have yet to break down. I’m still constantly wondering how to deal with this.

High glycemic vs. low glycemic carbs is an area where I’m still wondering where to draw the line between accuracy vs. user experience. Right now I just separate carbs and sugar. It would be a pain to enter the GI info for each meal but I know that it would improve the accuracy of the model tremendously.

The reason I don’t use bolus calculators - there are so many factors that they don’t look at. It’s interesting and fun, but if you want to make it accurate, there are a lot of things it needs to factor.

An example is that I ran across a carb recommendation website that told you how many carbs you needed before exercising - based on your weight and the duration and intensity of the exercise. But the thing didn’t even factor in current BG or IOB. Talk about a pointless app! In general, all of those calculator things are missing so many factors.

So after your first few versions, maybe you can keep adding variables?

It is actually very easy to add a variable. Less than 5 lines of code actually, but I guess adding it to the user interface and database is little more time consuming.

The gradient descent algorithm used to find the least amount of error in the equation is agnostic to how many predictors you account for.

One big problem with Linear Regression is something called over-fitting. Where if you have less than 5 data points per predictor variable your equation might make absolutely no sense.

So the equation scales and adds more predictors with the amount of data you have. However, something that really helps train the model is consuming meals that are heavy one particular macronutrient.

On a side note, is your A1C really 4.9??

Yes, a few times. The last one I did was in November and it was 4.9. It’s not always, just occasionally.

BTW, I am a Yellow Jacket too. But you said online, so I am not sure if you ever actually get to go to North Ave. When I was there, “online” didn’t exist. :grinning:

Linear modeling is a good thing and goes far when modeling both food absorption and insulin absorption.

There are very real nonlinearities when low or even moderately high (say, 180+) bg’s are reached but the idea is that we want to stay in the linear range.

The “rules of thumb”, e.g. rule of 1700, and others all come from linear modeling. Your CDE or dietician almost certainly doesn’t know what a linear model is but she does use rules of thumb all the time.

A decent “rule of thumb” reference: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2956816/

Some techie bg modeling references (including a mix of both diabetic and non-diabetic applications):

At one point there was a decent (if maybe a bit oversimplified) linear modeling tool available on the web where you got to model the absorption of food, adjust insulin doses according to a variety of dosing strategies, and see the resulting bg curve.

3 Likes

Were you thinking about GlucoDyn?

1 Like

I use xdrip+ prediction tool. It doesn’t consider food, just cgm data and carbs and other treatments entered by the user, as well as typical parameters such as isf. It works well for me and the graph is updated every 5 minutes with the new cgm reading.

Looks like you are doing something similar to PredictBGL, as described on site manage bgl dot com.

I think the creator of that used to post on this site.

1 Like

This is, if fact, a very worthwhile effort. I went through a similar routine about 15 years ago. I’m a 70 YO T1d (and an EE bioengineer) so I developed a mobile app to help me in meal and insulin dose planning. Mine is an iPhone app that is open source on GitHub called Diabetes Compass. I’ve used it with both syringes and pumps and found it helpful.
The modeling used is what I call piecewise linear slope in which the rate of BG change (per minute) is constant for time intervals. The published bio activity curves of insulin are available in the scientific literature. My model doesn’t factor in fiber or fat, although I believe that would increase accuracy. Perhaps your algorithm could provide that insight if those data were included.
PredictBGL is a pretty good app in my view, although I believe it costs a monthly fee (I’m too cheap).

4 Likes