Import lifestyle and health

Author

Johannes Zauner

Preface

This document imports the health questionnaire 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("MeLiDosScreeningQuestionnaire_DataDictionary_2024-10-16.csv",
                   c("online_screening_consent_form", "your_lifestyle_and_health")) |> 
  filter(!`Variable / Field Name` %in% c("consent", "explanation"))
#collect files
files <- filefinder("screening",individual = FALSE)
#import files
data <- 
  read_csv(files, show_col_types = FALSE) |> 
  drop_na(redcap_repeat_instance) |> 
  rename_with(\(x) str_remove(x, "_v2$")) |> 
  mutate(across(medication:disorder, as.logical),
         medication_type = as.character(medication_type),
         drugs = na_lgl)
#check column types
coltype_check <- coltype_checker(codebook, data)
coltype_check$details |> gt()
col expected present actual type_ok issue expected_example
gen_health numeric TRUE numeric TRUE ok as.numeric(...)
visual_acuity numeric TRUE numeric TRUE ok as.numeric(...)
medication logical TRUE logical TRUE ok as.logical(...)
tobacco logical TRUE logical TRUE ok as.logical(...)
drugs logical TRUE logical TRUE ok as.logical(...)
disorder logical TRUE logical TRUE ok as.logical(...)
record_id character TRUE character TRUE ok as.character(...)
medication_type character TRUE character TRUE ok as.character(...)
availability character FALSE NA FALSE missing as.character(...)
#collect relevant columns
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`)
Warning in add_col_labels(add_radio_factors(data, codebook, var_col = `Variable
/ Field Name`, : Labels provided for variables not in `data`: availability
#reorder variables
data <- 
  data |> 
  relocate(any_of(codebook$`Variable / Field Name`))

Summarize results

table_health(data)
Lifestyle and health N N = 231
Are you currently taking any medications? This includes non-prescription medication such as antihistamines, painkillers, etc. 23 7 (30%)
Do you regularly (once per week or more) smoke cigarettes or other tobacco products? 23 1 (4.3%)
Are you regularly (once a week or more) using recreational drugs? 0 0 (NA%)
    missing
23
Are you currently diagnosed with a sleep and/or psychiatric disorder? 23 0 (0%)
How is your health in general? 23
    Excellent
10 (43%)
    Very good
8 (35%)
    Good
4 (17%)
    Fair
1 (4.3%)
    Poor
0 (0%)
    Very poor
0 (0%)
This experiment will require you to wear non-prescription glasses for a week. To ensure this is possible, please select the option that best describes your visual acuity. 23
    I do not require prescription glasses and/or contact lenses
19 (83%)
    I require prescription glasses and/or lenses but I am willing to only wear contact lenses during the experimental week
3 (13%)
    I require prescription glasses and/or lenses and I will need to wear glasses during the experimental week
1 (4.3%)
1 n (%)
gtsave(table_health(data) |> as_gt(), filename = "../output/tables/table_health.png", vwidth = 800)
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//RtmpRYjRpo/file1755d22effeea.html screenshot completed

Export

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