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)
#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
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 = 261
ASE score 26 27.5 (26.0, 31.0)
Has too much light 26
    Strongly agree
1 (3.8%)
    Agree
3 (12%)
    Disagree
10 (38%)
    Strongly disagree
12 (46%)
Is too dark 26
    Strongly agree
0 (0%)
    Agree
3 (12%)
    Disagree
11 (42%)
    Strongly disagree
12 (46%)
Is too noisy 26
    Strongly agree
1 (3.8%)
    Agree
3 (12%)
    Disagree
8 (31%)
    Strongly disagree
14 (54%)
Is too quiet 26
    Strongly agree
2 (7.7%)
    Agree
7 (27%)
    Disagree
6 (23%)
    Strongly disagree
11 (42%)
Is too warm 26
    Strongly agree
4 (15%)
    Agree
4 (15%)
    Disagree
15 (58%)
    Strongly disagree
3 (12%)
Is too cool 26
    Strongly agree
0 (0%)
    Agree
6 (23%)
    Disagree
11 (42%)
    Strongly disagree
9 (35%)
Is too humid 26
    Strongly agree
2 (7.7%)
    Agree
2 (7.7%)
    Disagree
11 (42%)
    Strongly disagree
11 (42%)
Has an uncomfortable smell 26
    Strongly agree
0 (0%)
    Agree
0 (0%)
    Disagree
9 (35%)
    Strongly disagree
17 (65%)
Has uncomfortable pillows or blankets 26
    Strongly agree
0 (0%)
    Agree
4 (15%)
    Disagree
14 (54%)
    Strongly disagree
8 (31%)
Has a mattress or other sleeping surface that is too firm 26
    Strongly agree
1 (3.8%)
    Agree
2 (7.7%)
    Disagree
11 (42%)
    Strongly disagree
12 (46%)
Has a mattress or other sleeping surface that is too soft 26
    Strongly agree
2 (7.7%)
    Agree
4 (15%)
    Disagree
14 (54%)
    Strongly disagree
6 (23%)
Has a mattress or other sleeping surface that is uncomfortable for another reason 26
    Strongly agree
1 (3.8%)
    Agree
3 (12%)
    Disagree
12 (46%)
    Strongly disagree
10 (38%)
Does not feel safe or secure 26
    Strongly agree
0 (0%)
    Agree
1 (3.8%)
    Disagree
4 (15%)
    Strongly disagree
21 (81%)
Attention check successful 26 25 (96%)
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//RtmpcBd2va/file5d44426943de.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")