Import experience log

Author

Johannes Zauner

Preface

This document imports the experience log 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 <- "MPI"

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

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

Preparation

#collect codebook
codebook <- prepare_codebook("MeLiDosExperienceLog_DataDictionary_2024-10-16.csv")
#collect files
files <- filefinder("experiencelog", continuous = TRUE, individual = TRUE)
#import files
data <- read_csv2(files, show_col_types = FALSE) |> drop_na(redcap_repeat_instance)
ℹ Using "','" as decimal and "'.'" as grouping mark. Use `read_delim()` for more control.
#combine itentity and social columns
data <- 
data |> 
  mutate(
    .before = identity___1,
    identity = case_when(identity___1 == 1 ~ 1,
                         identity___2 == 1 ~ 2,
                         identity___3 == 1 ~ 3,
                         identity___4 == 1 ~ 4,
                         identity___5 == 1 ~ 5) |> as.character(),
    social_type = case_when(social_type___1 == 1 ~ 1,
                         social_type___2 == 1 ~ 2,
                         social_type___3 == 1 ~ 3,
                         social_type___4 == 1 ~ 4) |> as.character()
  ) |> 
  select(-c(contains("identity__"), contains("social_type__"))) |> 
  mutate(feedback = as.logical(feedback),
         removal = as.logical(removal),
      across(c(current, past), 
           \(x) parse_date_time(x, c("ymdHM", "dmyHM")))
    ) |> 
      mutate(record_id = paste0("MPI_S", record_id))


#check column types
coltype_check <- coltype_checker(codebook, data)
coltype_check$details |> gt()
col expected present actual type_ok issue expected_example
current POSIXct TRUE POSIXct TRUE ok as.POSIXct(..., tz = 'UTC')
past POSIXct TRUE POSIXct TRUE ok as.POSIXct(..., tz = 'UTC')
exp_time numeric TRUE numeric TRUE ok as.numeric(...)
type numeric TRUE numeric TRUE ok as.numeric(...)
location numeric TRUE numeric TRUE ok as.numeric(...)
status_2 numeric FALSE NA FALSE missing as.numeric(...)
feedback logical TRUE logical TRUE ok as.logical(...)
removal logical TRUE logical TRUE ok as.logical(...)
startdate_2 Date TRUE character FALSE wrong_type as.Date(...)
enddate_2 Date TRUE character FALSE wrong_type as.Date(...)
scheduledate_2 Date FALSE NA FALSE missing as.Date(...)
record_id character TRUE character TRUE ok as.character(...)
explog_instr character FALSE NA FALSE missing as.character(...)
loc_spec character TRUE character TRUE ok as.character(...)
descr character TRUE character TRUE ok as.character(...)
identity character TRUE character TRUE ok as.character(...)
identity_spec character TRUE character TRUE ok as.character(...)
social_type character TRUE character TRUE ok as.character(...)
future_use character TRUE character TRUE ok as.character(...)
uuid_2 character FALSE NA FALSE missing as.character(...)
supplementaldata_2 character FALSE NA FALSE missing as.character(...)
serializedresult_2 character FALSE NA FALSE missing as.character(...)
#collect relevant columns: POSIXct, Date & numeric
relevant_columns <- 
  coltype_check$details |> 
  pull(col)
#add specific character columns
relevant_columns <- c("record_id", relevant_columns)
#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(startdate_2:enddate_2))
Warning in add_col_labels(add_radio_factors(data, codebook, var_col = `Variable
/ Field Name`, : Labels provided for variables not in `data`: explog_instr,
uuid_2, scheduledate_2, status_2, supplementaldata_2, serializedresult_2
data <-
data |> 
  pivot_longer(c(past, current),
               names_to = c("is_past"),
               values_to = "Datetime"
               ) |> 
  drop_na(Datetime) |> 
  relocate(.after = record_id,
           Datetime, is_past) |> 
  mutate(across(where(is.POSIXct), \(x) force_tz(x, tzs[[site]])))
data <- data |> arrange(record_id, Datetime)

Translate comments into english

Native language is translated into English with AI and later checked by a site researcher.

# library(ellmer)
# 
# #Providing the relevant codebook portions
# chat <- chat_openai(paste0("Clean the dataset according to the instructions in the output structure."))
# 
# #Providing the input
# data_red <-
# data|>
#   select(record_id, loc_spec, descr, identity_spec, future_use)
# data_red <-
#   data_red |>
#   pmap(~ paste(paste(names(data_red), c(...), sep = ": "), collapse = ", "))
# 
# #creating an output structure
# type_data <- type_object(
#   record_id = type_string("use the record_id information"),
#   loc_spec_english = type_string("Translate (or if in english already, copy) the 'loc_spec' column here. Question: Please specify the location which describes the location where you made the experience?", required = FALSE),
#   descr_english = type_string("Translate (or if in english already, copy) the 'descr' column here. Question: Please provide a brief description of your experience, including the location, the activity you were performing and what feelings it triggered for you.", required = FALSE),
#   identity_spec_english = type_string("Translate (or if in english already, copy) the 'identity_spec' column here. Question: Who provided this feedback?", required = FALSE),
#   future_use_english = type_string("Translate (or if in english already, copy) the 'future_use' column here. Question: Explain how this experience will influence your future use of the light glasses, if at all.", required = FALSE)
# )
# 
# data_llm <-
# parallel_chat_structured(
#   chat,
#   data_red,
#   type = type_data
# )
# 
# #Ensure that no NA is caught as string
# data_llm <-
#   data_llm |>
#   mutate(across(everything(), \(x) case_when(x == "NA" ~ NA, .default = x)))
# 
# #check that input and output are identical
# stopifnot("Input must by identical to output check" =
#             all(data_llm$record_id == data$record_id, na.rm = TRUE))
# 
# data_llm <-
#   data |>
#   select(record_id, Datetime, loc_spec, descr, identity_spec, future_use) |>
#   bind_cols(data_llm |> select(-record_id))
# 
# path <- "../data/AI_translations/"
# if(!dir.exists(path)) dir.create(path, recursive = TRUE)
# write_csv(data_llm, "../data/AI_translations/experiencelog.csv")
data_llm <- 
  read_csv("../data/AI_translations/experiencelog.csv") |> 
  select(-c(record_id, Datetime, loc_spec, descr, identity_spec, future_use))
Rows: 67 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (9): record_id, loc_spec, descr, identity_spec, future_use, loc_spec_en...
dttm (1): Datetime

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#add output to original
data <- data |> bind_cols(data_llm)
data <-
  data |> 
  mutate(across(ends_with("english"), \(x) add_label(x, "English translation")
                ))

Summarize results

table <- 
table_general(
  data |> 
    select(
      -c(Datetime, is_past, loc_spec, descr, 
         identity_spec, future_use, ends_with("english"))
      ), 
  header = "Experience log")
table
Experience log N N = 671
The experience you would like to report is 67
    Current (happened in the last 10 minutes)
34 (51%)
    Past (happened in the past and I forgot to report it)
33 (49%)
The experience I would like to report is 67
    Positive
15 (22%)
    Neutral
25 (37%)
    Negative
27 (40%)
Please select the option which describes the location where you made the experience 67
    Home
20 (30%)
    Office or work-related space (e.g.: canteen)
19 (28%)
    Public spaces (e.g.: supermarket, restaurant, shop, park etc.)
26 (39%)
    Other
2 (3.0%)
Did this experience involve verbal or non-verbal feedback from others? Example: comments or questions from people 67 42 (63%)
Who provided this feedback? 42
    Friend or family member
8 (19%)
    Colleague
11 (26%)
    Acquaintance
2 (4.8%)
    Stranger
18 (43%)
    Other
3 (7.1%)
    missing
25
Please select the options that best describes the type feedback you received about the light glasses 42
    Comments about the light glasses
11 (26%)
    Questions about the light glasses
22 (52%)
    Looks due to the light glasses
5 (12%)
    Concerns about the light glasses
4 (9.5%)
    missing
25
Did this experience prompt you to remove the light glasses? 67 11 (16%)
1 n (%)
gtsave(table |> as_gt(), filename = "../output/tables/table_experiencelog.png", vwidth = 800)
file:////var/folders/9p/326_k3kx43qbn_cyl1rqfhb00000gn/T//Rtmp5Ru1KX/file8f053a5780ea.html screenshot completed

Export

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