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.
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.
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.
data/raw/: original input filesscripts/: main R scripts for loading, cleaning, joining, plotting, and modeling the datascripts/variable_specific/: focused scripts that study one variable at a timefigures/: generated figuresfigures/variable_specific/: figures from the one-variable scriptsoutputs/: optional outputs, used only when reusable tables are neededarchive/: older exploratory workprojects/: additional project-specific work
The main scripts are designed to run in numeric order.
-
scripts/01_load_main_data.R
Loads and cleans the sleep diary. -
scripts/02_load_co2_temp_data.R
Loads the bedroom sensor data and creates night-level summaries. -
scripts/03_join_relevant_data.R
Combines the sleep diary with the sensor data. -
scripts/04_descriptives_and_plots.R
Creates general summary figures and descriptive results. -
scripts/05_models.R
Runs baseline models for sleep duration and insomnia. -
scripts/06_fixed_effects_models.R
Runs models that compare nights within the same time period. -
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")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 sleepweekday.R: weekday differences in sleepinsomnia.R: insomnia patternsexercise.R: exercise and sleeptemperature.R: bedroom temperature and sleepcoffee.R: coffee, previous-night sleep, and same-night sleepcoffee_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.RThe 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.
Johannes Piipponen
R-based sleep diary and bedroom sensor data analysis.