Quantified Self Experiments / Bayesian analysis - How caffeine and weekdays affect sleep time (EEG)

Introduction

In my previous analysis [1] [2] on caffeine versus sleep i've used classical frequentists approach to model caffeine - sleep relationship. Here i'm going to use Bayesian data analysis and better sleep data derived from multichannel EEG.

Bayesian approach will offer a posterior distribution which can be used to calculate expected value / loss for making a decision.

Methods

During each night a multichannel EEG were recorded to sd card by using OpenBCI Cyton with 500Hz sampling rate with Gold cup electrodes and Ten20 paste, placed on Frontal, Temporal and Occipital regions. YASA were used to score hypnograms for each channel and then consensus hypnogram were built. Total sleep time, TST = N3 + N2 + N1 + REM were used as outcome parameter.

Caffeine intake for each day were recorded and days were split into 2 groups: days with caffeinated coffee (caff) and with decaffeinated (decaff) by "swiss water" method. Both groups were split into 2 subgroups: weekend and weekdays. I drink coffee once a day in the morning with standard portion, which justify just grouping days and doing BEST for groups.

In a total 4 groups were analysed: caff-weekday, caff-weekend, decaff-weekday and decaff-weekend.

Data analysis

Doing bayesian modeling start from describing parameters of interest and their priors.

TST is a time, which always take positive values and healthy sleep is around 5 sleep cycles 90 minutes each, so about 450 minutes. What's the easiest distribution to start with? If you ask ChatGPT the answer will be a Poisson. For sure there might be a better distributions where we can controls variation, but Poisson is a simpliest one. Here is how data looks like against poisson with lambda=450

This doesnt fit well, but for a prior we arent not looking for best fit, it should generally catch our prior knowledge: TST is positive number, concentrated around 450. This will be a hyperprior which will serve as a population prior. Group priors will also be Poisson with lambda drawn from hyperprior and will be passed into likelyhood.

This simple model will look like that:

"h_mu" is a population hyperprior, "mu" is group level prior (4 groups) and "like" is a likelihood function into which we will pass data (n=63)

Prior predictive check seems to be a hairy, which is expected for not so big dataset. Prior predictive mean seems to be somewhere near 450 which is expected.

Lets look at trace to see how our sampling from posterior gone:

Traces on the right looks good enough. Also for mu, which is a group level priors we can see 3 distinct groups (not colored for some reason, but can be clearly seen from right plot). Hyperprior h_mu is around 438 minutes.

Here is short summary for our groups, differences can be clearly seen:

Lets plot posterior distributions:

94% HDI stands for High Density Interval - interval where 94% of probability density is concentrated. Also we can see group means.

Lets look how prior h_mu changed after we pass some data:

Here we can see that from wide less informative prior it shifted to more concentrated posterior, as expected.

Lets do posterior predictive checks to see how our posterior distribution fits data:

It seem to be a hairy, but black line (observed data) somehow agree with posterior predictive mean.

Lets get to the final stage and look for difference between groups:

Here we can see that during caff days there is difference in TST for weekday vs weekend with mean -14 minutes. The probability is 68% for effect being at least -10 minutes. Not a huge one but we can see a direction and there is 97% probability that effect is less than zero.

Interestingly same happens for decaff days. 76% probability there is at least 10 minutes less sleep during weekdays. So independently to caffeine, during weekdays (when i work mostly) is sleep less. 

Lets look what caffeine do to my sleep:

We can see that during weekdays caffeine results in -19 minutes of sleep on average and with 93% probability effect is at least 10 minutes.

Same stand for weekends. During weekend i lose 22 minutes of sleep on average due to caffeine. 88% probability effect is at least 10 minutes.

So from here i can conclude that weekdays and caffeine independently affect my sleep. If we go back to group means we can see that decaf weekend mean is 456 minutes and caff weekday mean is 421 minutes and thats a 35 minute difference due to both effects.

Post is a work in progress and i'm going to improve it soon. Code will be published on github.