Last active
October 28, 2018 15:36
-
-
Save ericpgreen/ac733e9e82f1175a2290c7a67cdabb95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load packages | |
| library(tidyverse) | |
| library(RCurl) | |
| library(magrittr) | |
| # get the data | |
| csv <- getURL("https://raw.githubusercontent.com/ericpgreen/JCPP2018/master/data%20and%20replication%20files/input/dat.csv") | |
| dat <- read.csv(text = csv) | |
| # data wrangling | |
| datL <- | |
| dat %>% | |
| # select the key variables for analysis | |
| select(ID, w1group, | |
| w1_54a, w1_54b, w1_54d, w1_54e, w1_54f, | |
| w2_54a, w2_54b, w2_54d, w2_54e, w2_54f, | |
| w3_54a, w3_54b, w3_54d, w3_54e, w3_54f, | |
| w4_54a, w4_54b, w4_54d, w4_54e, w4_54f) %>% | |
| # replace any NaN with NA | |
| mutate_at(vars(-ID, -w1group), funs(ifelse(is.nan(.), NA, .))) %>% | |
| # rescale data range (1-4) to (0-3) by subtracting 1 | |
| mutate_at(vars(-ID, -w1group), funs(. - 1)) %>% | |
| # reverse code variable _54a | |
| mutate_at(vars(w1_54a, w2_54a, w3_54a, w4_54a), | |
| funs(3 - .)) %>% | |
| # construct depression severity | |
| rowwise() %>% | |
| mutate(depress.1 = mean(c(w1_54a, w1_54b, w1_54d, w1_54e, w1_54f), | |
| na.rm=TRUE), | |
| depress.2 = mean(c(w2_54a, w2_54b, w2_54d, w2_54e, w2_54f), | |
| na.rm=TRUE), | |
| depress.3 = mean(c(w3_54a, w3_54b, w3_54d, w3_54e, w3_54f), | |
| na.rm=TRUE), | |
| depress.4 = mean(c(w4_54a, w4_54b, w4_54d, w4_54e, w4_54f), | |
| na.rm=TRUE) | |
| ) %>% | |
| # convert from wide to long | |
| select(ID, w1group, depress.1, depress.2, depress.3, depress.4) %>% | |
| gather(key = time, value = depress, depress.1:depress.4) %>% | |
| mutate(time = sub("depress.", "", time)) %>% | |
| # construct indicator of depression | |
| mutate(dep16 = ifelse(depress >= 0.8, 1, 0)) %>% | |
| # construct indicator of depression at baseline for every obs using dep16 | |
| group_by(ID) %>% | |
| mutate(dep16Base = dep16[time == 1L]) %>% | |
| ungroup() %>% | |
| # categorize participants relative to baseline (time 1) | |
| mutate(depStatus = ifelse(dep16Base==1 & dep16==1, "still depressed", | |
| ifelse(dep16Base==1 & dep16==0, "remission", | |
| ifelse(dep16Base==0 & dep16==1, "became depressed", | |
| ifelse(dep16Base==0 & dep16==0, "never depressed", | |
| "missing")))) | |
| ) %>% | |
| # labels | |
| mutate(w1group = factor(w1group, | |
| levels=c(0, 1), | |
| labels=c("control", "intervention"))) %>% | |
| # highlight two participants | |
| mutate(highlight = ifelse(ID==2, 1, | |
| ifelse(ID==3, 2, | |
| 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment