knitr::opts_chunk$set(
  collapse = TRUE, 
  fig.dpi = 96, 
  fig.align = 'center', 
  fig.width = 7,
  out.width = "99%",
  echo = TRUE, 
  warning = FALSE, 
  message = FALSE,
  comment = "#>"
)

# standard emaph libraries
library(emaph)
library(tidyverse)

Summary

This vignette aims to replicate results published in @Wichers2016.

Preliminaries

Define mental state 'scales'

mental states: positive and negative affect, mental unrest

# mental states: positive and negative affect, mental unrest ----
pa <- c("mood_enthus", "mood_cheerf", 
        "mood_strong", "mood_satisfi")
na <- c("mood_lonely", "mood_anxious", 
        "mood_guilty", "mood_doubt")
mu <- c("mood_irritat", "pat_restl", 
        "pat_agitate")

Remove rows with many missing values.

# remove rows with lots of missing values ----
csd$nna <- csd %>%
  select(matches("mood_")) %>%
  is.na(.) %>% rowSums
csd <- csd %>% filter(nna == 0)

Calculate MoodState sum score

# Mood state scores are (standardized) mean of scale items
csd$pa <- csd %>%
  select(pa) %>%
  #scale(.) %>%
  rowMeans(., na.rm = TRUE)

csd$na <- csd %>%
  select(na) %>%
  #scale(.) %>%
  rowMeans(., na.rm = TRUE)

csd$mu <- csd %>%
  select(mu) %>%
  scale(.) %>%
  rowMeans(., na.rm = TRUE)

csd$su <- scale(csd$mood_suspic)
csd$wo <- scale(csd$pat_worry)

csd$ms = select(csd, pa, na, mu, su, wo) %>%
  rowSums(na.rm = TRUE)

De-trend

ggplot(data = csd, 
       aes(x = dayno, 
           y = ms)) +
  geom_smooth() + 
  geom_line() + 
  scale_x_continuous(
    breaks = seq(from = 1, 
                 to = 239, 
                 by = 9)) +
  theme(
    axis.text.x = element_text(angle = 90, 
                               hjust = 1))
fm <- mgcv::gam(formula = ms ~ s(dayno, bs = "cs"), data = csd)
csd$ms_dt <- residuals(fm)
ggplot(data = csd, 
       aes(x = dayno, 
           y = ms_dt)) +
  geom_smooth() + 
  geom_line() + 
  scale_x_continuous(
    breaks = seq(from = 1, 
                 to = 239, 
                 by = 9)) +
  theme(
    axis.text.x = element_text(angle = 90, 
                               hjust = 1))

MoodState Variance & Autocorrelation (30-day window)

csd$ms_rv = NA
csd$ms_rac = NA
for(i in 1:nrow(csd)){

  if(csd$dayno[i] < 30) next

  d <- subset(csd[1:i, ], 
              dayno > (csd$dayno[i] - 30) & 
              dayno <= csd$dayno[i])

  csd[i, ]$ms_rv <- var(d$ms_dt, na.rm = TRUE)
  csd[i, ]$ms_rac <- acf(d$ms_dt,
                         lag.max = 1,
                         type = "correlation",
                         plot = FALSE)$acf[2]
}

Results

Depression

d <- csd %>% filter(!is.na(scl90r_dep)) %>% select(dayno, scl90r_dep, phase)
d <- unique(d)
ggplot(data = d, 
       aes(x = dayno, 
           y = scl90r_dep, 
           group = 1)) +
  geom_point(aes(color = phase), 
             show.legend = FALSE) + 
  geom_step(aes(color = phase), 
            show.legend = FALSE) + 
  scale_x_continuous(
    breaks = seq(from = 1, 
                 to = 239, 
                 by = 9)) +
  theme(
    axis.text.x = element_text(angle = 90, 
                               hjust = 1))

MoodState Variance

d <- csd %>%
  group_by(dayno) %>%
  summarise(
    ms_rac = mean(ms_rac),
    ms_rv = mean(ms_rv), 
    phase = phase[1])
ggplot(data = d, 
       aes(x = dayno, 
           y = ms_rv)) +
  geom_line(aes(color = phase, 
                group = 1), 
            show.legend = FALSE) +
  scale_x_continuous(
    breaks = seq(from = 1, 
                 to = 239, 
                 by = 9)) +
  theme(
    axis.text.x = element_text(angle = 90, 
                               hjust = 1))

MoodState Autocorrelation

ggplot(data = d, 
       aes(x = dayno, 
           y = ms_rac)) +
  geom_line(aes(color = phase, 
                group = 1), 
            show.legend = FALSE) +
  scale_x_continuous(
    breaks = seq(from = 1, 
                 to = 239, 
                 by = 9)) +
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1))

References



jruwaard/emaph documentation built on May 3, 2019, 8:04 p.m.