Import current conditions

Author

Johannes Zauner

Preface

This document imports the current conditions 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)

site <- "FUSPCEU"

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

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

Preparation

#collect codebook
codebook <- prepare_codebook("MeLiDosCurrentConditions4xday_DataDictionary_2024-10-16.csv")
#collect files
files <- filefinder("currentconditions", continuous = TRUE, individual = TRUE)
#import files
data <- 
  read_csv(files, show_col_types = FALSE) |> 
  rename_with(\(x) str_remove_all(x, "_v2")) |>  
  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
ema_light numeric TRUE numeric TRUE ok as.numeric(...)
currentight numeric TRUE character FALSE wrong_type as.numeric(...)
additional_light numeric TRUE character FALSE wrong_type as.numeric(...)
currentmood numeric TRUE numeric TRUE ok as.numeric(...)
anxious numeric TRUE numeric TRUE ok as.numeric(...)
elated numeric TRUE numeric TRUE ok as.numeric(...)
sad numeric TRUE numeric TRUE ok as.numeric(...)
angry numeric TRUE numeric TRUE ok as.numeric(...)
irritable numeric TRUE numeric TRUE ok as.numeric(...)
energetic numeric TRUE numeric TRUE ok as.numeric(...)
kss_instr numeric TRUE numeric TRUE ok as.numeric(...)
kss numeric TRUE numeric TRUE ok as.numeric(...)
status_4 numeric TRUE numeric TRUE ok as.numeric(...)
check_mixedlight logical TRUE numeric FALSE wrong_type as.logical(...)
startdate_4 Date TRUE POSIXct FALSE wrong_type as.Date(...)
enddate_4 Date TRUE POSIXct FALSE wrong_type as.Date(...)
scheduledate_4 Date TRUE logical FALSE wrong_type as.Date(...)
record_id character TRUE character TRUE ok as.character(...)
uuid_4 character TRUE character TRUE ok as.character(...)
supplementaldata_4 character TRUE character TRUE ok as.character(...)
serializedresult_4 character TRUE character TRUE ok as.character(...)
#collect relevant columns: POSIXct, Date & numeric
relevant_columns <- 
  coltype_check$details |> 
  pull(col)
relevant_columns <- 
  c("record_id", relevant_columns)
#select relevant columns
data <- 
  data |> 
  select(any_of(relevant_columns)) |> 
  select(-c(scheduledate_4:serializedresult_4, 
            ema_light, currentmood, kss_instr, status_4)) |> 
  mutate(check_mixedlight = as.logical(check_mixedlight))
#label variables
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`) |> 
  relocate(record_id, any_of(codebook$`Variable / Field Name`)) |> 
  mutate(across(where(is.POSIXct), \(x) force_tz(x, tzs[[site]]))) |> 
  rename(Datetime = startdate_4, 
         currentlight = currentight) |> 
  relocate(Datetime, .after = record_id)
Warning in add_radio_factors(data, codebook, var_col = `Variable / Field Name`,
: Radio variables provided but not in `data`: ema_light, currentmood, kss_instr
Warning in add_col_labels(add_radio_factors(data, codebook, var_col = `Variable
/ Field Name`, : Labels provided for variables not in `data`: ema_light,
currentmood, kss_instr, uuid_4, scheduledate_4, status_4, supplementaldata_4,
serializedresult_4
attr(data$Datetime, "label") <- "EMA time (start)"
attr(data$enddate_4, "label") <- "EMA time (finish)"

Summarize results

table_currentconditions(data)
Warning in add_col_labels(mutate(data, across(anxious:kss, as.numeric)), :
Labels provided for variables not in `data`: ema_light, currentight,
currentmood, kss_instr, uuid_4, startdate_4, scheduledate_4, status_4,
supplementaldata_4, serializedresult_4
Current conditions (EMA) N N = 5261
Select the main light source you are currently exposed to. If you are unsure which one this is, think about it as the biggest and brightest source from which you are receiving light 526
    Electric light source indoors (e.g.: LED/halogen/fluorescent lamps)
285 (54%)
    Electric light source outdoors (e.g.: street lights)
13 (2.5%)
    Daylight indoors (through windows)
149 (28%)
    Daylight outdoors (including being in the shade)
57 (11%)
    Emissive displays (e.g.: smartphone, laptop etc.)
17 (3.2%)
    Darkness (outdoors and/or indoors)
4 (0.8%)
    Light entering from outside during sleep (e.g.: skyglow, street lights etc.)
1 (0.2%)
Are you also being exposed to an additional, secondary light source? 526 283 (54%)
Please select the additional light source you are currently exposed to 283
    Electric light source indoors (e.g.: LED/halogen/fluorescent lamps)
60 (21%)
    Electric light source outdoors (e.g.: street lights)
4 (1.4%)
    Daylight indoors (through windows)
81 (29%)
    Daylight outdoors (including being in the shade)
12 (4.2%)
    Emissive displays (e.g.: smartphone, laptop etc.)
122 (43%)
    Darkness (outdoors and/or indoors)
0 (0%)
    Light entering from outside during sleep (e.g.: skyglow, street lights etc.)
4 (1.4%)
    missing
243
Anxious 526 1.00 (1.00, 2.00)
Elated 526 1.00 (1.00, 2.00)
Sad 526 1.00 (1.00, 1.00)
Angry 526 1.00 (1.00, 1.00)
Irritable 526 1.00 (1.00, 1.00)
Energetic 526 4.00 (3.00, 5.00)
Please rate your sleepiness in the last 5 minutes 526 3.00 (3.00, 5.00)
1 n (%); Median (Q1, Q3)
gtsave(table_currentconditions(data) |> as_gt(), filename = "../output/tables/table_currentconditions.png", vwidth = 800)
Warning in add_col_labels(mutate(data, across(anxious:kss, as.numeric)), :
Labels provided for variables not in `data`: ema_light, currentight,
currentmood, kss_instr, uuid_4, startdate_4, scheduledate_4, status_4,
supplementaldata_4, serializedresult_4
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//RtmpPwVHII/file20a7670c364e.html screenshot completed

Export

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