Skip to content

jpiippon/sleep-diary-analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sleep diary analysis

This repository contains an analysis of my long-term sleep diary. The data cover about 3,000 nights and include both daily notes and bedroom sensor data.

The main goal is simple: I want to understand which things are linked to better or worse sleep. Examples include bedtime, weekday, coffee, exercise, stress, illness, bedroom temperature, CO2, and humidity.

The project is also a coding and data-visualization portfolio. The aim is to build clear, reproducible R code and figures that can later be used in reports, presentations, or public posts.

What the data include

The project uses two main sources of data:

  • my sleep diary, where I record sleep duration and daily factors such as coffee, exercise, stress, illness, bedtime, and insomnia
  • bedroom sensor data, where temperature, CO2, and humidity are measured during the night

The focus is on changes from night to night within the same person. This means the project does not compare different people. It asks, for example, whether my sleep is different on nights after coffee, late bedtime, exercise, stress, or high bedroom temperature.

Important date convention

The diary date means the day before the sleep period ends. For example, if I wake up on Wednesday morning, the sleep is recorded under Tuesday when Tuesday was the day with the relevant bedtime, coffee, exercise, and stress.

This is intentional. It keeps daily factors and the following night of sleep on the same diary date.

The sensor data use the same idea. Sensor readings after midnight, for example 00:00-07:59, are assigned to the previous calendar day because they still belong to the same night.

Repository structure

  • data/raw/: original input files
  • scripts/: main R scripts for loading, cleaning, joining, plotting, and modeling the data
  • scripts/variable_specific/: focused scripts that study one variable at a time
  • figures/: generated figures
  • figures/variable_specific/: figures from the one-variable scripts
  • outputs/: optional outputs, used only when reusable tables are needed
  • archive/: older exploratory work
  • projects/: additional project-specific work

Main analysis workflow

The main scripts are designed to run in numeric order.

  1. scripts/01_load_main_data.R
    Loads and cleans the sleep diary.

  2. scripts/02_load_co2_temp_data.R
    Loads the bedroom sensor data and creates night-level summaries.

  3. scripts/03_join_relevant_data.R
    Combines the sleep diary with the sensor data.

  4. scripts/04_descriptives_and_plots.R
    Creates general summary figures and descriptive results.

  5. scripts/05_models.R
    Runs baseline models for sleep duration and insomnia.

  6. scripts/06_fixed_effects_models.R
    Runs models that compare nights within the same time period.

  7. scripts/99_smoke_test.R
    Checks that the main data pipeline still runs after code changes.

The main pipeline can also be run with:

source("scripts/run_core_pipeline.R")

One-variable analyses

The most important part of the project is the set of focused scripts in scripts/variable_specific/. Each script studies one variable or one closely related theme.

Current examples include:

  • bedtime.R: bedtime and sleep
  • weekday.R: weekday differences in sleep
  • insomnia.R: insomnia patterns
  • exercise.R: exercise and sleep
  • temperature.R: bedroom temperature and sleep
  • coffee.R: coffee, previous-night sleep, and same-night sleep
  • coffee_relationships.R: coffee/no coffee, bedtime, exercise, and context checks

Each one-variable script usually creates:

  • one main figure
  • a small set of supporting figures
  • simple model results printed to the console
  • sensitivity checks when they are useful

The main figure is named like this:

<variable>_figure1_main.png

Supporting figures are named like this:

<variable>_figureS*.png

For example:

coffee_figure1_main.png
coffee_figureS20_context_bedtime.png

The one-variable scripts can be run with:

source("scripts/run_variable_specific_reports.R")

Or one script at a time, for example:

Rscript scripts/variable_specific/coffee.R
Rscript scripts/variable_specific/temperature.R

Modeling approach

The models are meant to support interpretation, not to prove cause and effect.

The project uses three common model layers:

  • raw models, which show the simple association
  • adjusted models, which include other diary variables
  • month fixed-effect models, which compare nights within the same year-month period

Some scripts also include sensitivity checks. For example, the coffee analysis checks whether coffee is related to sleep after controlling for previous-night sleep. This is important because coffee may be a response to poor sleep, not only a possible cause of poor sleep.

The wording in the project should therefore stay careful. It is better to write:

Coffee is associated with shorter sleep on some nights.

than:

Coffee causes shorter sleep.

Author

Johannes Piipponen

R-based sleep diary and bedroom sensor data analysis.

About

Personal sleep diary analysis — tracking and modelling daily sleep patterns with R.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages