drinkR: Estimate your Blood Alcohol Concentration using R and Shiny.
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.
Inspired by events that took place at UseR 2014 last month I decided to implement an app that estimates one’s blood alcohol concentration (BAC). Today I present to you drinkR, implemented using R and Shiny, Rstudio’s framework for building web apps using R. So, say that I had a good dinner, drinking a couple of glasses of wine, followed by an evening at a divy karaoke bar, drinking a couple of red needles and a couple of beers. By entering my sex, height and weight and the times when I drank the drinks in the drinkR app I end up with this estimated BAC curve:
(Now I might be totally off with what drinks I had and when but Romain Francois, Karl Broman, Sandy Griffith, Karthik Ram and Hilary Parker can probably fill in the details.) If you want to estimate your current BAC (or a friend’s…) then head over to the drinkr app hosted at ShinyApps.io. If you want to know how how the app estimates BAC read on below. The code for drinkR is available on GitHub, any suggestion on how it can be improved is greatly appreciated.
drinkR estimates the BAC according to the formulas given in The estimation of blood alcohol concentration by Posey and Mozayani (2007). I was also helped by reading through Computer simulation analysis of blood alcohol and the Widmark factor (explained below) was calculated according to The calculation of blood ethanol concentrations in males and females. Unfortunately all these articles are behind paywalls, that is how most how publicly funded research works these days…
The BAC estimates you get out of drinkR will be as good as the formulas in Posey and Mozayani (2007). I don’t know how good they are and I don’t know how well they’ll fit you. Estimating BAC is of course a prediction problem and what you really would want to have is data so that you could build a predictive model and get an idea of how well it predicts BAC. Unfortunately I haven’t found any data on this so the Posey and Mozayani formulas is as good as I can do.
The three steps of estimating BAC
Estimating the BAC (according to Posey and Mozayani, 2007) after you have drunken, say, a beer requires “simulating” three processes:

Alcohol absorption. Just because you drank a beer doesn’t mean it goes directly into your blood stream, it has to be absorbed by your digestive system first and this takes some time.

Alcohol distribution. Your BAC depends on how much of you the absorbed alcohol will be “diluted” by. This depends on, among other things, your weight, height and sex.

Alcohol elimintation. How drunk you get (and how soon you will get sober) depends on how fast your body eliminates the absorbed alcohol.
Alcohol absorption can be approximated by assuming it is first order, that is, assuming there is an alcohol halflife, a time it takes for half of a drink to be absorbed. When measured this halflife tend to be between 6 min to 18 min, depending on how much you have reacently eaten. If you haven’t eaten for a while your halflife might be closer to 6 min while if you just had a big döner kebab it might be closer to 18 min.
Alcohol distribution depends on the amount of water that the alcohol in your body will be diluted in. It can be estimated by the following equation:
$$ C = {A \over rW}$$
where $C$ is the alcohol concentration, $A$ is the mass of the alcohol, $W$ is your body weight and $r$ is the Widmark factor. This factor can be seen as an adjustment that is necessary because your whole body is not made of water, thus the alcohol is not “diluted by” your whole weight. There are many different formulas for estimating $r$ and drinkR uses the one given by Seidl et al. (2000) which estimates $r$ dependent on sex, height and weight:
$r_{\text{female}} = 0.31 – 0.0064 \times \text{weight in kg} + 0.0045 \times \text{height in cm}$$r_{\text{male}} = 0.32 – 0.0048 \times \text{weight in kg} + 0.0046 \times \text{height in cm}$
These linear equations can give really strange values for $r$, for example, if you weight a lot. Therefore I also bound $r$ to be within the limits found by Seidl et al. (2000): 0.44 to 0.80 in women and 0.60 to 0.87 in men.
Finally, alcohol elimination can be reasonably approximated by a constant elimination rate of the BAC. This rate can vary from around 0.009 % per hour to 0.035 % per hour with 0.018 % per hour being a reasonable average.
drinkR puts these three processes together and estimates your BAC over time given a number of drinks with time stamps. Assuming that you are also interested in how drunk you are right now, drinkR shows an estimate of your current BAC by fetching your computers local time (see this stackoverflow question for how this is done). The estimate given by drinkR might be very missleading so don’t use it for any serious purposes! To get a sense of the uncertainty in the BAC estimate play around with the parameters (especially the alcohol elimination rate) and see how much your BAC curve changes.
If you want to see how different levels of BAC could affect you see the Progressive effects of alcohol chart over at Wikipedia
and if you want to try out drinkR live I would recommend one of my favorite drinks: Absinthe mixed with Orange soda (say Fanta orange). It’s better than you think it is! 🙂
References
Posey, D., & Mozayani, A. (2007). The estimation of blood alcohol concentration. Forensic Science, Medicine, and Pathology, 3(1), 3339. Link (Unfortunately behind paywall)
Rockerbie, D. W., & Rockerbie, R. A. (1995). Computer simulation analysis of blood alcohol. Journal of clinical forensic medicine, 2(3), 137141. Link (Unfortunately behind paywall)
Seidl, S., Jensen, U., & Alt, A. (2000). The calculation of blood ethanol concentrations in males and females. International journal of legal medicine, 114(12), 7177. Link (Unfortunately behind paywall)
Rbloggers.com offers daily email updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/datascience job.
Want to share your content on Rbloggers? click here if you have a blog, or here if you don't.