Prep

Load packages

library(tidyverse)
library(knitr)
library(reactable)

Load and tidy data

duplicates = read.csv("~/Documents/Penn/MURI_protocol_paper/MURI_Master_Participants - Overview.csv", stringsAsFactors = FALSE) %>%
  filter(duplicate > 0) %>%
  arrange(duplicate) %>%
  group_by(duplicate) %>%
  fill(everything(), .direction = "updown") %>%
  gather(key, value, -c(pID, gID, duplicate, duplicate_id)) %>%
  spread(key, value) %>%
  gather(key, value, ends_with("complete")) %>%
  mutate(value_numeric = recode(value, "complete" = 4, "incomplete" = 3,
                                "invited" = 2, "not_invited" = 1)) %>%
  arrange(duplicate, key) %>%
  group_by(duplicate, key) %>%
  mutate(diff = value_numeric[1] - value_numeric[2],
         value_correct = ifelse(diff > 0, value[1],
                         ifelse(diff < 0, value[2], value)),
         value_correct = ifelse(is.na(value_correct), value, value_correct),
         filter_out = row_number(),
         pID = sprintf("%s_%s", pID, duplicate_id)) %>%
  filter(filter_out == 1) %>%
  ungroup() %>%
  select(pID, gID, key, value_correct) %>%
  rename("value" = value_correct)

data = read.csv("~/Documents/Penn/MURI_protocol_paper/MURI_Master_Participants - Overview.csv", stringsAsFactors = FALSE) %>%
  filter(duplicate == 0 & grepl("^m", pID)) %>%
  gather(key, value, ends_with("complete")) %>%
  select(pID, gID, key, value) %>%
  mutate(value = ifelse(pID == "muri674" & key == "baseline_complete", "complete_short", value)) %>%
  unique() %>%
  bind_rows(., duplicates) %>%
  mutate(value = gsub("incompelte", "incomplete", value),
         value = gsub("\\?", "", value),
         key = gsub("_complete", "", key),
         value = ifelse(value == "invited", "not_started", value))

ema = read.csv("~/Library/CloudStorage/Box-Box/MURI_Data_830189/data/EMA_Round1/SHINE_EMA_Round1_19May2020.csv") %>%
  select(SHINEID, Condition) %>%
  unique() %>%
  rename("pID" = SHINEID) %>%
  mutate(pID = recode(pID, "muri181" = "muri181_muri346",
                      "muric232" = "muric232_muric518",
                        "muric249" = "muric249_muric530",
                        "muric213" = "muric213_muric466",
                        "muric241" = "muric241_muric521",
                        "muric244" = "muric244_muric523")
)

covid_ema = read.csv("~/Library/CloudStorage/Box-Box/MURI_Data_830189/data/EMA_Round2/SHINE_Round2EMA_6Jan2021.csv") %>%
  select(SHINEID, Pak) %>%
  unique() %>%
  rename("pID" = SHINEID) %>%
  mutate(condition = recode(Pak, "A" = "control",
                            "B" = "mindful",
                            "C" = "mindful",
                            "D" = "perspective",
                            "E" = "perspective")) %>%
  select(pID, condition) %>%
  mutate(pID = recode(pID, "muric213" = "muric213_muric466",
                      "muric260" = "muric260_muric476",
                                "muric232" = "muric232_muric518",
                                "muric257" = "muric257_muric519",
                                "muric241" = "muric241_muric521",
                                "muric244" = "muric244_muric523",
                                "muric258" = "muric258_muric526",
                                "muric249" = "muric249_muric530",
                                "muri083" = "muri083_muri411",
                                "muri181" = "muri181_muri346",
                                "muri300" = "muri300_muri558",
                                "muri042" = "muri042_muri164"))

Summarize Ns

Complete = invited and completed

Incomplete = invited and did not complete

Not started = invited but did not start

Not invited = was not invited

Number of unique participants

Started any component

data %>%
  filter(!grepl("incomplete|^complete", value)) %>%
  select(pID) %>%
  unique() %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Completed any component

data %>%
  filter(grepl("^complete", value)) %>%
  select(pID) %>%
  unique() %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Ns (including short baseline surveys)

Note: short baseline surveys were sent to participants who did not complete the original baseline survey

data %>%
  group_by(key, value) %>%
  summarize(n = n()) %>%
  spread(value, n) %>%
  mutate_if(is.numeric, ~ifelse(is.na(.), 0, .)) %>%
  mutate(complete = complete + complete_short, 
         incomplete = incomplete + incomplete_short,
         total = complete + incomplete + not_started + not_invited,
         total_invited = complete + incomplete + not_started,
         total_started = complete + incomplete) %>%
  replace(is.na(.), 0) %>%
  select(key, total, total_invited, not_started, total_started, incomplete, complete, not_invited) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Ns (separating short baseline surveys)

data %>%
  group_by(key, value) %>%
  summarize(n = n()) %>%
  spread(value, n) %>%
  mutate_if(is.numeric, ~ifelse(is.na(.), 0, .)) %>%
  mutate(total = complete + complete_short + incomplete + incomplete_short + not_started + not_invited,
         total_invited = complete + complete_short + incomplete + incomplete_short + not_started,
         total_started = complete + complete_short + incomplete + incomplete_short) %>%
  replace(is.na(.), 0) %>%
  select(key, total, total_invited, not_started, total_started, incomplete, incomplete_short, complete, complete_short, not_invited) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

EMA

Overlap between cohorts

data %>%
  filter(grepl("ema", key)) %>%
  filter(!value == "not_invited") %>%
  spread(key, value) %>%
  filter(grepl("incomplete|^complete", covid_ema) & !is.na(ema)) %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

COVID EMA conditions

Completed

data %>%
  filter(key == "covid_ema") %>%
  filter(value == "complete") %>%
  left_join(., covid_ema) %>%
  mutate(condition = ifelse(pID == "muri016", "perspective",
                     ifelse(pID == "muri101", "mindful", condition))) %>%
  group_by(condition) %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Started

covid_ema %>%
  group_by(condition) %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

EMA conditions

Completed

data %>%
  filter(key == "ema") %>%
  filter(value == "complete") %>%
  left_join(., ema) %>%
  mutate(Condition = ifelse(pID == "muric212_muricnon009", "mindful", Condition)) %>%
  group_by(Condition) %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Started

ema %>%
  group_by(Condition) %>%
  summarize(n = n()) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Percents

data %>%
  group_by(key, value) %>%
  summarize(n = n()) %>%
  filter(grepl("complete|^invited|incomplete|not_started", value)) %>%
  group_by(key) %>%
  mutate(total_invited = sum(n)) %>%
  mutate(percent = round((n / total_invited) * 100, 1)) %>%
  select(key, value, percent) %>%
  spread(value, percent) %>%
  replace(is.na(.), 0)  %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Data coverage

Summarize unique combinations of study component completions

Main study

data %>%
  arrange(pID, key) %>%
  filter(grepl("baseline|scan|^ema", key)) %>%
  filter(grepl("^complete", value)) %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

COVID study

data %>%
  arrange(pID, key) %>%
  filter(grepl("covid", key)) %>%
  filter(grepl("^complete", value)) %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Main surveys

data %>%
  arrange(pID, key) %>%
  filter(grepl("baseline|follow", key)) %>%
  filter(grepl("^complete", value)) %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

Baseline & at least one follow-up

data %>%
  arrange(pID, key) %>%
  filter(grepl("baseline|follow", key)) %>%
  mutate(key = gsub("1|2", "", key)) %>%
  filter(grepl("^complete", value)) %>%
  unique() %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

All surveys

data %>%
  arrange(pID, key) %>%
  filter(grepl("baseline|covid$|follow", key)) %>%
  filter(grepl("^complete", value)) %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)

All components

data %>%
  arrange(pID, key) %>%
  filter(grepl("^complete", value)) %>%
  arrange(pID, key) %>%
  group_by(pID) %>%
  summarize(coverage = paste(key, collapse = ", ")) %>%
  group_by(coverage) %>%
  summarize(n = n()) %>%
  arrange(desc(n)) %>%
  reactable::reactable(striped = TRUE,
            resizable = TRUE)