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)
This vignette aims to replicate results published in @Wichers2016.
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 lots of missing values ---- csd$nna <- csd %>% select(matches("mood_")) %>% is.na(.) %>% rowSums csd <- csd %>% filter(nna == 0)
# 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)
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))
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] }
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))
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))
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.