Import LEBA

Author

Johannes Zauner

Preface

This document imports the LEBA and shows descriptive statistics for the site.

Setup

library(tidyverse)
library(LightLogR)
Warning: package 'LightLogR' was built under R version 4.5.2
library(glue)
library(readxl)
library(gt)
library(gtsummary)

remote <- 
  "https://raw.githubusercontent.com/MeLiDosProject/Data_Metadata_Conventions/main/scripts/"

c("labeling",
  "radio_factors",
  "add_label",
  "prepare_codebook",
  "filefinder",
  "general_parameters",
  "coltype_checker",
  "tables"
) |> walk(\(x) source(paste0(remote, x, ".R")))

Preparation

#collect codebook
codebook <- 
  prepare_codebook("MeLiDosDischargeQuestionnaire_DataDictionary_2024-10-16.csv",
                   "your_light_behaviour_leba") |> 
  filter(!`Variable / Field Name` %in% c("email", "email_2"))
#collect files
files <- filefinder("discharge",individual = FALSE)
#import files
data <- 
  read_csv2(files, show_col_types = FALSE) |> 
  mutate(record_id = paste0("MPI_S", record_id)) |> 
  filter(!redcap_survey_identifier %in% c(200, 217))
ℹ Using "','" as decimal and "'.'" as grouping mark. Use `read_delim()` for more control.
  # drop_na(redcap_repeat_instance)
#check column types
coltype_check <- coltype_checker(codebook, data)
coltype_check$details |> gt()
col expected present actual type_ok issue expected_example
leba_f2_04 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f2_05 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f2_06 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f2_07 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f2_08 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f2_09 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f3_10 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f3_11 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f3_12 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f3_13 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f3_14 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f4_15 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f4_16 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f4_17 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f4_18 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f5_19 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f5_20 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f5_21 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f5_22 numeric TRUE numeric TRUE ok as.numeric(...)
leba_f5_23 numeric TRUE numeric TRUE ok as.numeric(...)
record_id character TRUE character TRUE ok as.character(...)
#collect relevant columns
relevant_columns <- 
  coltype_check$details |> 
  pull(col)
#select relevant columns
data <- data |> select(any_of(relevant_columns))
#label
data <-
data |> 
  add_radio_factors(codebook, 
                    var_col = `Variable / Field Name`, 
                    type_col = `Field Type`,
                    levels_col = `Choices, Calculations, OR Slider Labels`
                    ) |> 
  add_col_labels(codebook, var_col = `Variable / Field Name`, label_col = `Field Label`)

#reorder variables
data <- 
  data |> 
  relocate(any_of(codebook$`Variable / Field Name`))

Calculate LEBA factors

#Factor calculation
factors_leba <- list(
  F2 = paste0("leba_f2_0", 4:9),
  F3 = paste0("leba_f3_1", 0:4),
  F4 = paste0("leba_f4_1", 5:8),
  F5 = paste0("leba_f5_", 19:23)
)
Fs <- paste0("F", 2:5)

data <-
data |> 
  rowwise() |> 
  mutate(leba_f2_04 = fct_rev(leba_f2_04), 
         leba_f2 = rowSums(across(contains(factors_leba$F2), as.numeric)),
         leba_f3 = rowSums(across(contains(factors_leba$F3), as.numeric)),
         leba_f4 = rowSums(across(contains(factors_leba$F4), as.numeric)),
         leba_f5 = rowSums(across(contains(factors_leba$F5), as.numeric)),
         ) |> 
  mutate(leba_f2_04 = fct_rev(leba_f2_04)) |> 
  relocate(leba_f2:leba_f5, .after = record_id)

leba_factors <- c(
  leba_f2 = "F2: Spending time outdoors",
  leba_f3 = "F3: Using phones and smartwatches in bed before sleep",
  leba_f4 = "F4: Controlling and using ambient light before bedtime",
  leba_f5 = "F5: Using light in the morning and during daytime"
)


#label variables
data <- 
  data |> 
  add_labels(leba_factors)

data <-
data |> 
  add_col_labels(codebook, var_col = `Variable / Field Name`, label_col = `Field Label`)

Summarize results

table_leba(data)
Light exposure behaviour (LEBA) N N = 261
F2: Spending time outdoors 26 16.5 (14.0, 19.0)
F3: Using phones and smartwatches in bed before sleep 26 14.00 (13.00, 16.00)
F4: Controlling and using ambient light before bedtime 26 15.0 (11.0, 17.0)
F5: Using light in the morning and during daytime 26 11.5 (9.0, 15.0)
I spend 30 minutes or less per day (in total) outside. 26 2.50 (2.00, 4.00)
I spend between 30 minutes and 1 hour per day (in total) outside. 26 3.00 (3.00, 4.00)
I spend between 1 and 3 hours per day (in total) outside. 26 3.00 (2.00, 4.00)
I spend more than 3 hours per day (in total) outside. 26 2.50 (1.00, 3.00)
I spend as much time outside as possible. 26 2.00 (1.00, 4.00)
I go for a walk or exercise outside within 2 hours after waking up. 26 2.00 (1.00, 3.00)
I use my mobile phone within 1 hour before attempting to fall asleep.Note: this does not include the time spent filling in the questionnaires. 26 5.00 (4.00, 5.00)
I look at my mobile phone screen immediately after waking up.Note: this does not include the time spent filling in the questionnaires. 26 4.00 (4.00, 5.00)
I check my phone when I wake up at night. 26 2.00 (1.00, 3.00)
I look at my smartwatch within 1 hour before attempting to fall asleep.Note: please answer with regard to your personal smartwatch (if you own one), not the one actimeter given to you for the study 26 1.00 (1.00, 4.00)
I look at my smartwatch when I wake up at night.Note: please answer with regard to your personal smartwatch (if you own one), not the one actimeter given to you for the study 26 1.00 (1.00, 1.00)
I dim my mobile phone screen within 1 hour before attempting to fall asleep. 26 4.00 (2.00, 5.00)
I use a blue-filter app on my computer screen within 1 hour before attempting to fall asleep. 26 3.50 (1.00, 5.00)
I use as little light as possible when I get up during the night. 26 5.00 (4.00, 5.00)
I dim my computer screen within 1 hour before attempting to fall asleep. 26 3.00 (1.00, 4.00)
I use tunable lights to create a healthy light environment. 26 1.00 (1.00, 3.00)
I use LEDs to create a healthy light environment. 26 3.00 (1.00, 5.00)
I use a desk lamp when I do focused work. 26 2.00 (1.00, 4.00)
I use an alarm with a dawn simulation light. 26 1.00 (1.00, 1.00)
I turn on the lights immediately after waking up. 26 2.00 (2.00, 4.00)
Items are coded as follows. 1: Never | 2: Rarely | 3: Sometimes | 4: Often | 5: Always. Factors ('F') are numerical summations
1 Median (Q1, Q3)
gtsave(table_leba(data), filename = "../output/tables/table_leba.png", vwidth = 800)
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//RtmpXsqnQe/file166307c43f019.html screenshot completed

Export

data <- data |> rename(Id = record_id)
leba <- data
path <- "../data/imported/"
if(!dir.exists(path)) dir.create(path, recursive = TRUE)
save(leba, file = "../data/imported/leba.RData")