Import wellbeing diary

Author

Johannes Zauner

Preface

This document imports the WHO5 wellbeing diary and shows descriptive statistics for the site.

Setup

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

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

c("labeling",
  "radio_factors",
  "time_summaries",
  "prepare_codebook",
  "filefinder",
  "add_label",
  "who5_scoring",
  "general_parameters",
  "coltype_checker",
  "diarydate",
  "tables"
) |> walk(\(x) source(paste0(remote, x, ".R")))
Warning: package 'rlang' was built under R version 4.5.2

Preparation

#collect codebook
codebook <- prepare_codebook("MeLiDosEveningDiaries_DataDictionary_2024-10-16.csv", 
                             form.filter = c("wellbeing_diary", "form_1"))
#collect files
files <- filefinder("wellbeingdiary", continuous = TRUE, individual = TRUE)
#import files
data <- 
  read_csv(files, show_col_types = FALSE) |> 
  drop_na(redcap_repeat_instance) |> 
  rename_with(\(x) str_remove(x, "_v2$")) 
#check column types
coltype_check <- coltype_checker(codebook, data)
coltype_check$details |> gt()
col expected present actual type_ok issue expected_example
introduction_wellbeing numeric TRUE numeric TRUE ok as.numeric(...)
who5_1998_1 numeric TRUE numeric TRUE ok as.numeric(...)
who5_1998_2 numeric TRUE numeric TRUE ok as.numeric(...)
who5_1998_3 numeric TRUE numeric TRUE ok as.numeric(...)
who5_1998_4 numeric TRUE numeric TRUE ok as.numeric(...)
who5_1998_5 numeric TRUE numeric TRUE ok as.numeric(...)
status_2 numeric TRUE numeric TRUE ok as.numeric(...)
startdate_2 Date TRUE POSIXct FALSE wrong_type as.Date(...)
enddate_2 Date TRUE POSIXct FALSE wrong_type as.Date(...)
scheduledate_2 Date TRUE logical FALSE wrong_type as.Date(...)
record_id character TRUE character TRUE ok as.character(...)
uuid_2 character TRUE character TRUE ok as.character(...)
supplementaldata_2 character TRUE character TRUE ok as.character(...)
serializedresult_2 character TRUE character TRUE ok as.character(...)
#collect relevant columns: POSIXct, Date & numeric
relevant_columns <- 
  coltype_check$details |> 
  pull(col)
#select relevant columns
data <- data |> select(any_of(relevant_columns))
#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`)) |> 
  select(-c(uuid_2, enddate_2:last_col(), introduction_wellbeing))

Set relevant dates

#if data was collected between 14:00 and 24:00, it is assigned to the same day.
#if collected between 00:00 and 13:59, it is assigned to the previous day.

data <- data |> diarydate(startdate_2)
attr(data$Date, "label") <- "Date"

Calculate WHO5 score

data <- data |> who5_scoring() 

Summarize results

table_wellbeingdiary(data)
Wellbeing diary (WHO-5) N N = 1161
I have felt cheerful and in good spirits 116
    At no time
0 (0%)
    Some of the time
2 (1.7%)
    Less than half of the time
3 (2.6%)
    More than half of the time
27 (23%)
    Most of the time
51 (44%)
    All of the time
33 (28%)
I have felt calm and relaxed 116
    At no time
0 (0%)
    Some of the time
4 (3.4%)
    Less than half of the time
11 (9.5%)
    More than half of the time
23 (20%)
    Most of the time
55 (47%)
    All of the time
23 (20%)
I have felt active and vigorous 116
    At no time
0 (0%)
    Some of the time
2 (1.7%)
    Less than half of the time
9 (7.8%)
    More than half of the time
32 (28%)
    Most of the time
59 (51%)
    All of the time
14 (12%)
How would you rate the quality of your sleep last night? 116
    Very poor
3 (2.6%)
    Poor
12 (10%)
    Fair
16 (14%)
    Good
59 (51%)
    Very good
26 (22%)
My daily life has been filled with things that interest me 116
    At no time
0 (0%)
    Some of the time
3 (2.6%)
    Less than half of the time
14 (12%)
    More than half of the time
36 (31%)
    Most of the time
50 (43%)
    All of the time
13 (11%)
WHO-5 percentage score (calculated, 0-100) 116 76 (66, 84)
Items 4 deviates from the WHO-5 questionnaire. Towards the score it is coded as follows. 1: Very poor | 2: Poor | 3: Fair | 4: Good | 5: Very good.
1 n (%); Median (Q1, Q3)
gtsave(table_wellbeingdiary(data), filename = "../output/tables/table_wellbeingdiary.png", vwidth = 800)
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//RtmpJ8WhDM/file12a2652d1f752.html screenshot completed

Export

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