Import Sleep environment ASE

Author

Johannes Zauner

Preface

This document imports the ASE (Sleep environment) 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",
  "add_label",
  "prepare_codebook",
  "filefinder",
  "general_parameters",
  "coltype_checker",
  "attention_check",
  "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("MeLiDosDischargeQuestionnaire_DataDictionary_2024-10-16.csv",
                   "your_sleep_environment_ase")
#collect files
files <- filefinder("discharge",individual = FALSE)
files <- files |> 
          subset(grepl("ASE", files))

#import files
data <- 
  read_csv(files, show_col_types = FALSE) |> 
  rename_with(\(x) str_remove_all(x, "_v2")) |> 
  drop_na(redcap_repeat_instance)

  # 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
slypos_ase_001 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_002 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_003 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_004 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_005 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_006 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_007 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_attentioncheck numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_008 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_009 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_010 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_011 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_012 numeric TRUE numeric TRUE ok as.numeric(...)
slypos_ase_0123 numeric TRUE numeric TRUE ok as.numeric(...)
#collect relevant columns
relevant_columns <- 
  coltype_check$details |> 
  pull(col) |> 
  c("record_id")
#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(record_id, any_of(codebook$`Variable / Field Name`))
data$record_id <- add_label(data$record_id, "Record ID")

Attention check

data <- 
  data |> 
  attention_check(slypos_ase_attentioncheck, "Strongly disagree")

ASE score

data <- 
data |> 
  rowwise() |> 
  mutate(
across(starts_with("slypos_ase_0"),
       \(x) {x <- x |> as.numeric()
       x-1
       }, .names = "{.col}_num"),
ASE = sum(c_across(ends_with("_num")), na.rm = TRUE),
.after = record_id
) |> select(-ends_with("_num"))

data <- 
add_labels(data, c(ASE = "ASE score"))

Summarize results

table <- 
table_general(data, "ASE: Sleep environment")
table
ASE: Sleep environment N N = 221
ASE score 22 28.0 (25.0, 30.0)
Has too much light 22
    Strongly agree
4 (18%)
    Agree
4 (18%)
    Disagree
6 (27%)
    Strongly disagree
8 (36%)
Is too dark 22
    Strongly agree
8 (36%)
    Agree
7 (32%)
    Disagree
4 (18%)
    Strongly disagree
3 (14%)
Is too noisy 22
    Strongly agree
0 (0%)
    Agree
3 (14%)
    Disagree
8 (36%)
    Strongly disagree
11 (50%)
Is too quiet 22
    Strongly agree
5 (23%)
    Agree
9 (41%)
    Disagree
3 (14%)
    Strongly disagree
5 (23%)
Is too warm 22
    Strongly agree
0 (0%)
    Agree
14 (64%)
    Disagree
7 (32%)
    Strongly disagree
1 (4.5%)
Is too cool 22
    Strongly agree
0 (0%)
    Agree
2 (9.1%)
    Disagree
15 (68%)
    Strongly disagree
5 (23%)
Is too humid 22
    Strongly agree
1 (4.5%)
    Agree
1 (4.5%)
    Disagree
4 (18%)
    Strongly disagree
16 (73%)
Has an uncomfortable smell 21
    Strongly agree
0 (0%)
    Agree
0 (0%)
    Disagree
3 (14%)
    Strongly disagree
18 (86%)
    missing
1
Has uncomfortable pillows or blankets 22
    Strongly agree
0 (0%)
    Agree
1 (4.5%)
    Disagree
4 (18%)
    Strongly disagree
17 (77%)
Has a mattress or other sleeping surface that is too firm 21
    Strongly agree
0 (0%)
    Agree
3 (14%)
    Disagree
8 (38%)
    Strongly disagree
10 (48%)
    missing
1
Has a mattress or other sleeping surface that is too soft 21
    Strongly agree
0 (0%)
    Agree
2 (9.5%)
    Disagree
10 (48%)
    Strongly disagree
9 (43%)
    missing
1
Has a mattress or other sleeping surface that is uncomfortable for another reason 21
    Strongly agree
0 (0%)
    Agree
1 (4.8%)
    Disagree
5 (24%)
    Strongly disagree
15 (71%)
    missing
1
Does not feel safe or secure 21
    Strongly agree
0 (0%)
    Agree
0 (0%)
    Disagree
5 (24%)
    Strongly disagree
16 (76%)
    missing
1
Attention check successful 22 18 (82%)
1 Median (Q1, Q3); n (%)
gtsave(table |> as_gt(), filename = "../output/tables/table_ASE.png", vwidth = 800)
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//RtmpUpN6bl/file5c3a7b5867da.html screenshot completed

Export

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