############################################################################## # # # Script for the "Measuring resilience prospectively # # as the speed of affect recovery in daily life: # # A complex systems perspective on mental health" # # paper # # # ############################################################################## # Function to install a package or load if it exists. # Credits to: https://stackoverflow.com/a/9341833/5252007 load.package <- function(name) { if(!require(name, character.only = TRUE)) { install.packages(name, dep = TRUE) if(!require(name, character.only = TRUE)) stop("Package not found.") } } # Load the packages we will need. load.package("readstata13") #For importing datasets load.package("plyr") #For data transformation, function "count" load.package("dplyr") #For data description, function "group_by" etc. load.package("statar") #For selecting groups load.package("nlme") #For multilevel models load.package("DataCombine") #For laggind variables load.package("qgraph") #For network analysis load.package("tidyr") #To complete dataset load.package("lattice") #To plot missings load.package("moments") #For skewness and kurtosis load.package("robustHD") #For standartization ##Uploading the databases my_data_main <- read.dta13("X:/My Desktop/Documents/twinssCan2.3.dta") ###Creating the subset with cross-sectional data data_des <- subset(my_data_main, select = c("subjno","idtw", "_1_J1", "_1_J2", "_1_J3", "_1_J4", "_1_scl90_tot", "_2_scl90_tot", "gender", "age_wave1", "_2_le_neg")) ###Deleting parents and other unrelated participants (they do not have the age data): data_des<-data_des[!is.na(data_des$age_wave1), ] ##creating the variable representing the number of beeps for each individual: n_beeps <- plyr::count(data_des,c('subjno')) data_des <- merge(n_beeps, data_des, by = "subjno") rm(n_beeps) ####Sample selection process#### ###Now the dataset is collapsed: data_des <- aggregate(.~subjno, data_des, mean, na.action=NULL) ###Selection of the subsample with the lower level of happy childhood experiences: #checking the missings in the JTV items first: summary(data_des[c("_1_J1", "_1_J2", "_1_J3", "_1_J4")]) ## all items have the same number of missings ## see the descriptive statistics part below for more details data_des$subjno[is.na(data_des$`_1_J1`)] data_des$subjno[is.na(data_des$`_1_J2`)] data_des$subjno[is.na(data_des$`_1_J3`)] data_des$subjno[is.na(data_des$`_1_J4`)] ##and they belong to the same people #Calculating then the JTV sum score: data_des$chi_exp <- data_des$`_1_J1` + data_des$`_1_J2` + data_des$`_1_J3` + data_des$`_1_J4` length(data_des$subjno[is.na(data_des$chi_exp)]) ## 25 people do not have values data_des<-data_des[!is.na(data_des$chi_exp), ] ## they are omitted from the sample #Splitting the sample to select the subsample with the lowest level of JTV sumscore (becasue items are formulating positively): data_des$chi_exp_2 <- xtile(data_des$chi_exp, n = 2) table(data_des$chi_exp_2) ## resulting in 451 people with lower level of JTV sumscore data_des<-data_des[data_des$chi_exp_2 == 1, ] # Deleting people with high level of happy childhood ### SCL-90 scores: #Checking missings: summary(data_des[c("_1_scl90_tot", "_2_scl90_tot")]) ## see the descriptive statistics part below for more details #Calculating the change score: data_des$scl_change <- data_des$`_2_scl90_tot` - data_des$`_1_scl90_tot` # thus people with missings either on T0 or T1 will have NAs length(data_des$subjno[!is.na(data_des$scl_change)]) # 249 participants have data availible # Deleting the participants without SCL scores: data_des<-data_des[!is.na(data_des$scl_change), ] ### Having more than 30% of ESM data available: length(data_des$subjno[(data_des$freq <= 18)]) #the study period consisted of 6 days with 10 beeps per day; #60 is considered max (some people have more, but 60 was the aim) #and therefore 30% of 60 is 18. We exclude all people (10) with #18 or less beeps: data_des <- data_des[data_des$freq > 18, ] # resulting in 239 participants ###Having at least one unpleasant experience events <- subset(my_data_main, select = c("subjno", "eve_pleasant")) events <- events[events$subjno %in% data_des$subjno, ] #deleting extra people events$stress <- events$eve_pleasant <= 0 events <- aggregate(.~subjno, events, sum, na.action = na.omit) View(events) #"TWS0308" "TWS0609" - are these people; rm(events) data_des <- data_des[!data_des$subjno == "TWS0308", ] data_des <- data_des[!data_des$subjno == "TWS0609", ] ### Creating groups data_des$group <- xtile(data_des$scl_change, 3) data_des$group <- factor(data_des$group, labels = c("Decrease group", "Stable group", "Increase group")) table(data_des$group) data_des <- data_des[!data_des$group == "Decrease group", ] #excluding people from the group with the decrease of symptoms ####Preparing variables for the analysis#### my_data <- subset(my_data_main, select = c("subjno", "dayno", "beepno", "idtw", "mood_cheerful", "mood_relaxed", "mood_insecur", "mood_lonely", "mood_anxious", "mood_irritat", "mood_satisfi", "mood_listles", "pat_suspic", "mood_down", "mood_guilty", "mood_enthus", "phy_energy", "phy_tired", "eve_pleasant", "gender", "age_wave1")) my_data <- my_data[my_data$subjno %in% data_des$subjno, ] #deleting extra people my_data <- merge(my_data, data_des[c("subjno", "group")], by = "subjno") ## NA factor and PA factor my_data$NA_factor <- rowMeans(my_data[ , c("mood_insecur", "mood_lonely", "mood_anxious", "mood_irritat", "mood_listles", "pat_suspic", "mood_down", "mood_guilty")], na.rm=TRUE) my_data$PA_factor <- rowMeans(my_data[ , c("mood_cheerful", "mood_relaxed","mood_satisfi", "mood_enthus")], na.rm=TRUE) ###Person-mean center these factors personmeans <- subset(my_data, select = c("subjno", "NA_factor", "PA_factor")) personmeans <- aggregate(.~subjno, personmeans, mean, na.rm=TRUE) names(personmeans)<-c("subjno", "NA_factor_mean", "PA_factor_mean") my_data <-merge(my_data, personmeans, by="subjno") my_data$NA_permean <- my_data$NA_factor - my_data$NA_factor_mean my_data$PA_permean <- my_data$PA_factor - my_data$PA_factor_mean rm(personmeans) ### Stressful event my_data$stress <- my_data$eve_pleasant * -1 my_data$stress[my_data$stress < 0] <- NA ### We have a lot of missing time points, so need to fill in NAs there my_data <- my_data[order(my_data$subjno, my_data$dayno, my_data$beepno),] my_data_complete <- complete(my_data, nesting(group, idtw, subjno), dayno = 1:42, beepno=1:15) ### 42 is the maximum day number #Time variables, one for all days ## my_data_complete$id_time <- rep(seq(1:630), 157) ##### Creating the lagged variable for unpleasant event #### my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l1_stress", TimeVar = "beepno", slideBy =-1) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l2_stress", TimeVar = "beepno", slideBy =-2) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l3_stress", TimeVar = "beepno", slideBy =-3) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l4_stress", TimeVar = "beepno", slideBy =-4) ######Main analysis######### ###INVESTIGATING QUANTITATIVE DIFFERENCES BETWEEN GROUPS IN POSITIVE AND NEGATIVE AFFECT RECOVERY ## Negative affect #Lag 0 recovery_lag0_all_na <- lme(NA_permean ~ stress + id_time + group + stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag0_all_na) #Assumptions: ###Homogeniety of variance plot(recovery_lag0_all_na) # Normality of residuals qqnorm(resid(recovery_lag0_all_na)) hist(resid(recovery_lag0_all_na)) #Lag 1 recovery_lag1_all_na <- lme(NA_permean ~ id_time + l1_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag1_all_na) ###Homogeniety of variance plot(jitter(fitted(recovery_lag1_all_na), amount=0.2), jitter(resid(recovery_lag1_all_na, type="p"), amount=0.3), pch=19, cex=.5, xlab="Fitted Values", ylab="Standardized Residuals") # Normality of residuals qqnorm(resid(recovery_lag1_all_na)) hist(resid(recovery_lag1_all_na)) #Lag 2 recovery_lag2_all_na <- lme(NA_permean ~ id_time + l2_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag2_all_na) ###Homogeniety of variance plot(recovery_lag2_all_na) # Normality of residuals qqnorm(resid(recovery_lag2_all_na)) hist(resid(recovery_lag2_all_na)) #Lag 3 recovery_lag3_all_na <- lme(NA_permean ~ id_time + l3_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag3_all_na) ###Homogeniety of variance plot(recovery_lag3_all_na) # Normality of residuals qqnorm(resid(recovery_lag3_all_na)) hist(resid(recovery_lag3_all_na)) #Lag 4 recovery_lag4_all_na <- lme(NA_permean ~ id_time + l4_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag4_all_na) ###Homogeniety of variance plot(recovery_lag4_all_na) # Normality of residuals qqnorm(resid(recovery_lag4_all_na)) hist(resid(recovery_lag4_all_na)) ## Positive affect #Lag 0 recovery_lag0_all_pa <- lme(PA_permean ~ stress + id_time + group + stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag0_all_pa) ###Homogeniety of variance plot(recovery_lag0_all_pa) # Normality of residuals qqnorm(resid(recovery_lag0_all_pa)) hist(resid(recovery_lag0_all_pa)) ###Homogeniety of variance plot(recovery_lag0_all_pa_rhythm) # Normality of residuals qqnorm(resid(recovery_lag0_all_pa_rhythm)) hist(resid(recovery_lag0_all_pa_rhythm)) #Lag 1 recovery_lag1_all_pa <- lme(PA_permean ~ id_time + l1_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag1_all_pa) ###Homogeniety of variance plot(recovery_lag1_all_pa) # Normality of residuals qqnorm(resid(recovery_lag1_all_pa)) hist(resid(recovery_lag1_all_pa)) ###Homogeniety of variance plot(recovery_lag1_all_pa_rhythm) # Normality of residuals qqnorm(resid(recovery_lag1_all_pa_rhythm)) hist(resid(recovery_lag1_all_pa_rhythm)) #Lag 2 recovery_lag2_all_pa <- lme(PA_permean ~ l2_stress + id_time + group + l2_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag2_all_pa) ###Homogeniety of variance plot(recovery_lag2_all_pa) # Normality of residuals qqnorm(resid(recovery_lag2_all_pa)) hist(resid(recovery_lag2_all_pa)) #Lag 3 recovery_lag3_all_pa <- lme(PA_permean ~ l3_stress + id_time + group + l3_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag3_all_pa) ###Homogeniety of variance plot(recovery_lag3_all_pa) # Normality of residuals qqnorm(resid(recovery_lag3_all_pa)) hist(resid(recovery_lag3_all_pa)) #Lag 4 recovery_lag4_all_pa <- lme(PA_permean ~ id_time +l4_stress*group + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) summary(recovery_lag4_all_pa) ###Homogeniety of variance plot(recovery_lag4_all_pa) # Normality of residuals qqnorm(resid(recovery_lag4_all_pa)) hist(resid(recovery_lag4_all_pa)) ##IS THERE AN EFFECT FROM THE MEAN ON AN INDIVIDUAL GROUP LEVEL?#### ### Lag 0 #NA #Stable group recovery_na_lag0_model_group1 <- lme(NA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag0_model_group1) ###Homogeniety of variance plot(recovery_na_lag0_model_group1) # Normality of residuals qqnorm(resid(recovery_na_lag0_model_group1)) hist(resid(recovery_na_lag0_model_group1)) #Increase group ### recovery_na_lag0_model_group2 <- lme(NA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_na_lag0_model_group2) ###Homogeniety of variance plot(recovery_na_lag0_model_group2) # Normality of residuals qqnorm(resid(recovery_na_lag0_model_group2)) hist(resid(recovery_na_lag0_model_group2)) ##PA #Stable group ### recovery_na_lag0_model_group1 <- lme(PA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag0_model_group1) ###Homogeniety of variance plot(recovery_na_lag0_model_group1) # Normality of residuals qqnorm(resid(recovery_na_lag0_model_group1)) hist(resid(recovery_na_lag0_model_group1)) #Increase group ### recovery_pa_lag0_model_group2 <- lme(PA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_pa_lag0_model_group2) ###Homogeniety of variance plot(recovery_pa_lag0_model_group2) # Normality of residuals qqnorm(resid(recovery_pa_lag0_model_group2)) hist(resid(recovery_pa_lag0_model_group2)) ###Lag 1 ##NA #Stable group # recovery_na_lag1_model_group1 <- lme(NA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag1_model_group1) ###Homogeniety of variance plot(recovery_na_lag1_model_group1) # Normality of residuals qqnorm(resid(recovery_na_lag1_model_group1)) hist(resid(recovery_na_lag1_model_group1)) #Increase group # recovery_na_lag1_model_group2 <- lme(NA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_na_lag1_model_group2) ###Homogeniety of variance plot(recovery_na_lag1_model_group2) # Normality of residuals qqnorm(resid(recovery_na_lag1_model_group2)) hist(resid(recovery_na_lag1_model_group2)) ##PA #Stable group # recovery_pa_lag1_model_group1 <- lme(PA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_pa_lag1_model_group1) ###Homogeniety of variance plot(recovery_pa_lag1_model_group1) # Normality of residuals qqnorm(resid(recovery_pa_lag1_model_group1)) hist(resid(recovery_pa_lag1_model_group1)) #Increase group recovery_pa_lag1_model_group2 <- lme(PA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_pa_lag1_model_group2) ###Homogeniety of variance plot(recovery_pa_lag1_model_group2) # Normality of residuals qqnorm(resid(recovery_pa_lag1_model_group2)) hist(resid(recovery_pa_lag1_model_group2)) #Lag 2 #NA #Stable group recovery_na_lag2_model_group1 <- lme(NA_permean ~ l2_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag2_model_group1) #Increase group recovery_na_lag2_model_group2 <- lme(NA_permean ~ l2_stress+ id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_na_lag2_model_group2) ##PA #Stable group recovery_pa_lag2_model_group1 <- lme(PA_permean ~ l2_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_pa_lag2_model_group1) #Increase group recovery_pa_lag2_model_group2 <- lme(PA_permean ~ l2_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_pa_lag2_model_group2) #Lag 3 #NA #table group recovery_na_lag3_model_group1 <- lme(NA_permean ~ l3_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag3_model_group1) #Increase group recovery_na_lag3_model_group2 <- lme(NA_permean ~ l3_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_na_lag3_model_group2) ##PA #Stable group recovery_pa_lag3_model_group1 <- lme(PA_permean ~ l3_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_pa_lag3_model_group1) #Increase group recovery_pa_lag3_model_group2 <- lme(PA_permean ~ l3_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l3_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_pa_lag3_model_group2) #Lag 4 #NA #table group recovery_na_lag4_model_group1 <- lme(NA_permean ~ l4_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_na_lag4_model_group1) #Increase group recovery_na_lag4_model_group2 <- lme(NA_permean ~ l4_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_na_lag4_model_group2) ##PA #Stable group recovery_pa_lag4_model_group1 <- lme(PA_permean ~ l4_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Stable group") summary(recovery_pa_lag4_model_group1) #Increase group recovery_pa_lag4_model_group2 <- lme(PA_permean ~ l4_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l4_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset=group=="Increase group") summary(recovery_pa_lag4_model_group2) #####Investigating the effects of delayed recovery on the course of psychopathology #### ### NEGATIVE AFFECT ### Delayed recovery is operationalised as effect of random effect variances of the slope for stressor= BLUP ## # We'll calculate AUC for T0, T1 and T2 ### At T0 recovery_na_all_t0 <- lme(NA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_na_all_t0) ###Homogeniety of variance #plot(recovery_na_all_t1) # Normality of residuals #qqnorm(resid(recovery_na_all_t1)) #hist(resid(recovery_na_all_t1)) ### At T1 recovery_na_all_t1 <- lme(NA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_na_all_t1) ###Homogeniety of variance #plot(recovery_na_all_t1) # Normality of residuals #qqnorm(resid(recovery_na_all_t1)) #hist(resid(recovery_na_all_t1)) ### At T2 recovery_na_all_t2 <- lme(NA_permean ~ l2_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_na_all_t2) ###Homogeniety of variance plot(recovery_na_all_t2) # Normality of residuals #qqnorm(resid(recovery_na_all_t2)) #hist(resid(recovery_na_all_t2)) ### Blups for T0 ## na_blups_t0 <- as.data.frame(random.effects(recovery_na_all_t0, level = 2)) na_blups_t0$subjno <- rownames(random.effects(recovery_na_all_t0, level = 2)) na_blups_t0$subjno <- gsub('.*\\/', '', na_blups_t0$subjno) na_blups_t0$idtw <- rownames(random.effects(recovery_na_all_t0, level = 2)) na_blups_t0$idtw <- sapply(strsplit(na_blups_t0$idtw, split='/', fixed=TRUE), function(x) (x[1])) na_blups_t0$idtw <- as.numeric(na_blups_t0$idtw) names(na_blups_t0)[3] <- "T0" na_blups_t0$id_time <- NULL na_blups_t0$`(Intercept)` <- NULL ### Blups for T1 na_blups_t1 <- as.data.frame(random.effects(recovery_na_all_t1, level = 2)) na_blups_t1$subjno <- rownames(random.effects(recovery_na_all_t1, level = 2)) na_blups_t1$subjno <- gsub('.*\\/', '', na_blups_t1$subjno) na_blups_t1$idtw <- rownames(random.effects(recovery_na_all_t1, level = 2)) na_blups_t1$idtw <- sapply(strsplit(na_blups_t1$idtw, split='/', fixed=TRUE), function(x) (x[1])) na_blups_t1$idtw <- as.numeric(na_blups_t1$idtw) names(na_blups_t1)[3] <- "T1" na_blups_t1$id_time <- NULL na_blups_t1$`(Intercept)` <- NULL ### Blups for T2 na_blups_t2 <- as.data.frame(random.effects(recovery_na_all_t2, level = 2)) na_blups_t2$subjno <- rownames(random.effects(recovery_na_all_t2, level = 2)) na_blups_t2$subjno <- gsub('.*\\/', '', na_blups_t2$subjno) na_blups_t2$idtw <- rownames(random.effects(recovery_na_all_t2, level = 2)) na_blups_t2$idtw <- sapply(strsplit(na_blups_t2$idtw, split='/', fixed=TRUE), function(x) (x[1])) na_blups_t2$idtw <- as.numeric(na_blups_t2$idtw) names(na_blups_t2)[3] <- "T2" na_blups_t2$id_time <- NULL na_blups_t2$`(Intercept)` <- NULL ### At this point there should be different number of people in Blups at lag 2 ## let's check ### we lost two people; who are they? #setdiff(recovery_na_all_t1[["groups"]][["subjno"]], recovery_na_all_t2[["groups"]][["subjno"]]) # "4238/TWS0534" "4637/TWS0546" na_blups_t0 <- na_blups_t0[!na_blups_t0$subjno == "TWS0534", ] na_blups_t0 <- na_blups_t0[!na_blups_t0$subjno == "TWS0546", ] na_blups_t1 <- na_blups_t1[!na_blups_t1$subjno == "TWS0534", ] na_blups_t1 <- na_blups_t1[!na_blups_t1$subjno == "TWS0546", ] ###Now it should be possible to merge datasets; data_na_blups <- merge(na_blups_t0, na_blups_t1, by = c("subjno", "idtw")) data_na_blups <- merge(data_na_blups, na_blups_t2, by = c("subjno", "idtw")) ###Calculating the AUC data_na_blups$T0 <- data_na_blups$T0 + as.numeric(fixed.effects(recovery_na_all_t0)[2]) data_na_blups$T1 <- data_na_blups$T1 + as.numeric(fixed.effects(recovery_na_all_t1)[2]) data_na_blups$T2 <- data_na_blups$T2 + as.numeric(fixed.effects(recovery_na_all_t2)[2]) data_na_blups$auc <- ((data_na_blups$T2 + data_na_blups$T1)/2 + (data_na_blups$T1 + data_na_blups$T0)/2) - 2*data_na_blups$T2 hist(data_na_blups$auc) ### Merge with dataset with other variables data_na_blups <- merge(data_na_blups, data_des, by= c("subjno", "idtw")) names(data_na_blups)[names(data_na_blups) == '_1_scl90_tot'] <- 'SCL_T0' names(data_na_blups)[names(data_na_blups) == '_2_scl90_tot'] <- 'SCL_T1' names(data_na_blups)[names(data_na_blups) == '_2_le_neg'] <- 'neg_events' ###Standardisation: data_na_blups$SCL_T1_std <- standardize(data_na_blups$SCL_T1) data_na_blups$auc_std <- standardize(data_na_blups$auc) data_na_blups$scl_change_std <- standardize(data_na_blups$scl_change) #### with covariances blups_on_scl_na_std <- lme(SCL_T1_std ~ auc_std + gender + age_wave1 + neg_events + SCL_T0, random = list(idtw = ~ 1), data = data_na_blups, na.action=na.omit) blups_on_scl_na_std <- lm(SCL_T1_std ~ auc_std + gender + age_wave1 + neg_events + SCL_T0, data = data_na_blups, na.action=na.omit) summary(blups_on_scl_na_std) ### without covariances blups_on_scl_na_nocov <- lme(SCL_T1 ~ auc + SCL_T0, random = list(idtw = ~ 1), data = data_na_blups, na.action=na.omit, control=list(opt="optim")) summary(blups_on_scl_na_nocov) ##### Ploting #ggplot(data_na_blups, aes(x=auc, y=scl_change, colour= as.factor(group))) + geom_point() + geom_smooth() #ggplot(data_na_blups, aes(x=auc, y=scl_change)) + geom_point() + geom_smooth() #ggplot(data_na_blups, aes(x=auc, y=scl_change)) + geom_point() + geom_smooth(method="lm", se=FALSE, colour="red") ggplot(data_na_blups, aes(x=auc_std, y=scl_change_std)) + geom_point() + geom_smooth(method="lm", se=FALSE, colour="red") + labs(x = "AUC, standardized", y = "Change in SCL-90 scores, standardized", title = "Figure 3. Speed of negative affect recovery as a predictor of individual symptom trajectories") ####POSITIVE AFFECT## ### Delayed recovery is operationalised as effect of random effect variances of the slope for stressor= BLUP ## # We'll calculate AUC for T0, T1 and T2 # ### At T0 recovery_pa_all_t0 <- lme(PA_permean ~ stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_pa_all_t0) ###Homogeniety of variance #plot(recovery_pa_all_t1) # Normality of residuals #qqnorm(resid(recovery_pa_all_t1)) #hist(resid(recovery_pa_all_t1)) ### At T1 recovery_pa_all_t1 <- lme(PA_permean ~ l1_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_pa_all_t1) ###Homogeniety of variance #plot(recovery_pa_all_t1) # Normality of residuals #qqnorm(resid(recovery_pa_all_t1)) #hist(resid(recovery_pa_all_t1)) ### At T2 recovery_pa_all_t2 <- lme(PA_permean ~ l2_stress + id_time + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l2_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) #summary(recovery_pa_all_t2) ###Homogeniety of variance #plot(recovery_pa_all_t2) # Normality of residuals #qqnorm(resid(recovery_pa_all_t2)) #hist(resid(recovery_pa_all_t2)) ### Blups for T0 pa_blups_t0 <- as.data.frame(random.effects(recovery_pa_all_t0, level = 2)) pa_blups_t0$subjno <- rownames(random.effects(recovery_pa_all_t0, level = 2)) pa_blups_t0$subjno <- gsub('.*\\/', '', pa_blups_t0$subjno) pa_blups_t0$idtw <- rownames(random.effects(recovery_pa_all_t0, level = 2)) pa_blups_t0$idtw <- sapply(strsplit(pa_blups_t0$idtw, split='/', fixed=TRUE), function(x) (x[1])) pa_blups_t0$idtw <- as.numeric(pa_blups_t0$idtw) names(pa_blups_t0)[3] <- "T0" pa_blups_t0$id_time <- NULL pa_blups_t0$`(Intercept)` <- NULL ### Blups for T1 pa_blups_t1 <- as.data.frame(random.effects(recovery_pa_all_t1, level = 2)) pa_blups_t1$subjno <- rownames(random.effects(recovery_pa_all_t1, level = 2)) pa_blups_t1$subjno <- gsub('.*\\/', '', pa_blups_t1$subjno) pa_blups_t1$idtw <- rownames(random.effects(recovery_pa_all_t1, level = 2)) pa_blups_t1$idtw <- sapply(strsplit(pa_blups_t1$idtw, split='/', fixed=TRUE), function(x) (x[1])) pa_blups_t1$idtw <- as.numeric(pa_blups_t1$idtw) names(pa_blups_t1)[3] <- "T1" pa_blups_t1$id_time <- NULL pa_blups_t1$`(Intercept)` <- NULL ### Blups for T2 pa_blups_t2 <- as.data.frame(random.effects(recovery_pa_all_t2, level = 2)) pa_blups_t2$subjno <- rownames(random.effects(recovery_pa_all_t2, level = 2)) pa_blups_t2$subjno <- gsub('.*\\/', '', pa_blups_t2$subjno) pa_blups_t2$idtw <- rownames(random.effects(recovery_pa_all_t2, level = 2)) pa_blups_t2$idtw <- sapply(strsplit(pa_blups_t2$idtw, split='/', fixed=TRUE), function(x) (x[1])) pa_blups_t2$idtw <- as.numeric(pa_blups_t2$idtw) names(pa_blups_t2)[3] <- "T2" pa_blups_t2$id_time <- NULL pa_blups_t2$`(Intercept)` <- NULL ### At this point there should be different number of people in Blups at lag 2 pa_blups_t0 <- pa_blups_t0[!pa_blups_t0$subjno == "TWS0534", ] pa_blups_t0 <- pa_blups_t0[!pa_blups_t0$subjno == "TWS0546", ] pa_blups_t1 <- pa_blups_t1[!pa_blups_t1$subjno == "TWS0534", ] pa_blups_t1 <- pa_blups_t1[!pa_blups_t1$subjno == "TWS0546", ] ###Now it should be possible to merge datasets; data_pa_blups <- merge(pa_blups_t0, pa_blups_t1, by = c("subjno", "idtw")) data_pa_blups <- merge(data_pa_blups, pa_blups_t2, by = c("subjno", "idtw")) ###Calculating the AUC data_pa_blups$T0 <- data_pa_blups$T0 + as.numeric(fixed.effects(recovery_pa_all_t0)[2]) data_pa_blups$T1 <- data_pa_blups$T1 + as.numeric(fixed.effects(recovery_pa_all_t0)[2]) data_pa_blups$T2 <- data_pa_blups$T2 + as.numeric(fixed.effects(recovery_pa_all_t0)[2]) data_pa_blups$auc <- ((data_pa_blups$T2 + data_pa_blups$T1)/2 + (data_pa_blups$T1 + data_pa_blups$T0)/2) - 2*data_pa_blups$T2 hist(data_pa_blups$auc) ### Merge with dataset with other variables data_pa_blups <- merge(data_pa_blups, data_des, by= c("subjno", "idtw")) names(data_pa_blups)[names(data_pa_blups) == '_1_scl90_tot'] <- 'SCL_T0' names(data_pa_blups)[names(data_pa_blups) == '_2_scl90_tot'] <- 'SCL_T1' names(data_pa_blups)[names(data_pa_blups) == '_2_le_neg'] <- 'neg_events' ###Standardisation: data_pa_blups$SCL_T1_std <- standardize(data_pa_blups$SCL_T1) data_pa_blups$auc_std <- standardize(data_pa_blups$auc) data_pa_blups$scl_change_std <- standardize(data_pa_blups$scl_change) #### with covariances blups_on_scl_pa_std <- lme(SCL_T1_std ~ auc_std + gender + age_wave1 + neg_events + SCL_T0, random = list(idtw = ~ 1), data = data_pa_blups, na.action=na.omit) blups_on_scl_pa_std <- lm(SCL_T1_std ~ auc_std + gender + age_wave1 + neg_events + SCL_T0, data = data_pa_blups, na.action=na.omit) summary(blups_on_scl_pa_std) ### without covariances blups_on_scl_pa_nocov <- lme(SCL_T1 ~ auc + SCL_T0, random = list(idtw = ~ 1), data = data_pa_blups, na.action=na.omit, control=list(opt="optim")) summary(blups_on_scl_pa_nocov) ##### Ploting #ggplot(data_pa_blups, aes(x=auc, y=scl_change, colour= as.factor(group))) + geom_point() + geom_smooth() #ggplot(data_pa_blups, aes(x=auc, y=scl_change)) + geom_point() + geom_smooth() ggplot(data_pa_blups, aes(x=auc_std, y=scl_change_std)) + geom_point() + geom_smooth(method="lm", se=FALSE, colour="red") ##### Descriptive analysis #### #Number of people# table(data_des$group) #Gender# prop.table(table(data_des$gender[data_des$group == "Stable group"], exclude = NULL)) prop.table(table(data_des$gender[data_des$group == "Increase group"], exclude = NULL)) prop.table(table(data_des$gender[data_des$group == "Decrease group"], exclude = NULL)) #And is it different between groups? chisq.test(data_des$gender, data_des$group) #No ##Eductation and Ethnicity ##These data come from additional dataset with demographic variables: my_data_demo <- read.dta13("X:/My Desktop/Documents/demografie.dta") names(my_data_demo) data_demo <- subset(my_data_demo, select = c("st_subjid", "educ_cb3", "etn_b","etn_z", "etn_a", "etn_r", "etn2")) data_demo$subjno <- data_demo$st_subjid # because subjno has a different name in this dataset data_demo$st_subjid <- NULL data_des <- merge(data_demo, data_des, by = "subjno") rm(data_demo) # Education: table(data_des$educ_cb3[data_des$group == "Stable group"], exclude = NULL) table(data_des$educ_cb3[data_des$group == "Increase group"], exclude = NULL) #difference between groups: chisq.test(data_des$educ_cb3, data_des$group) #No #Ethnicity: table(data_des$etn2, data_des$group) table(data_des$etn_b, data_des$group) table(data_des$etn_z, data_des$group) table(data_des$etn_a, data_des$group) table(data_des$etn_r, data_des$group) #Age options(pillar.sigfig=4) ## Becuase dplyr "ëats" digits, apparently group_by(data_des, group) %>% summarise( count = n(), mean = mean(age_wave1, na.rm = TRUE), min = min(age_wave1, na.rm = TRUE), max = max(age_wave1, na.rm = TRUE), sd = sd(age_wave1, na.rm = TRUE) ) #Is it different? var.test(data_des$age_wave1 ~ data_des$group) # variances are similar t.test(data_des$age_wave1 ~ data_des$group, var.equal = T) ## No differences in age #JTV scores group_by(data_des, group) %>% summarise( count = n(), mean = mean(chi_exp, na.rm = TRUE), min = min(chi_exp, na.rm = TRUE), max = max(chi_exp, na.rm = TRUE), sd = sd(chi_exp, na.rm = TRUE) ) var.test(data_des$chi_exp ~ data_des$group) # variances are different t.test(data_des$chi_exp ~ data_des$group, var.equal = F) ## Different level, smaller in the Increase group #SCL-90 at baseline group_by(data_des, group) %>% summarise( count = n(), mean = mean(`_1_scl90_tot` + 90, na.rm = TRUE), min = min(`_1_scl90_tot` + 90, na.rm = TRUE), max = max(`_1_scl90_tot` + 90, na.rm = TRUE), sd = sd(`_1_scl90_tot`, na.rm = TRUE) ) var.test(data_des$`_1_scl90_tot` ~ data_des$group) # variances are different t.test(data_des$`_1_scl90_tot` ~ data_des$group, var.equal = F) ## Not different ##comparison with the Decrease group #TukeyHSD(aov(data_des$`_1_scl90_tot` ~ data_des$group)) #SCL-90 at follow-up group_by(data_des, group) %>% summarise( count = n(), mean = mean(`_2_scl90_tot` + 90, na.rm = TRUE), min = min(`_2_scl90_tot` + 90, na.rm = TRUE), max = max(`_2_scl90_tot` + 90, na.rm = TRUE), sd = sd(`_2_scl90_tot` + 90, na.rm = TRUE) ) var.test(data_des$`_2_scl90_tot` ~ data_des$group) # variances are different t.test(data_des$`_2_scl90_tot` ~ data_des$group, var.equal = F) ## Different #SCL change group_by(data_des, group) %>% summarise( count = n(), mean = mean(scl_change, na.rm = TRUE), min = min(scl_change, na.rm = TRUE), max = max(scl_change, na.rm = TRUE), sd = sd(scl_change, na.rm = TRUE) ) ##Negative life events between T0 and T1 names(data_des)[names(data_des) == '_2_le_neg'] <- 'neg_events' group_by(data_des, group) %>% summarise( count = n(), mean = mean(neg_events, na.rm = TRUE), min = min(neg_events, na.rm = TRUE), max = max(neg_events, na.rm = TRUE), sd = sd(neg_events, na.rm = TRUE) ) var.test(data_des$neg_events ~ data_des$group) # variances are different t.test(data_des$neg_events ~ data_des$group, var.equal = F) ## same ### ESM variables ### ###Number of stressful events: events <- merge(events, data_des[c("subjno", "group")], by = "subjno") group_by(events, group) %>% summarise( count = n(), mean = mean(stress, na.rm = TRUE), min = min(stress, na.rm = TRUE), max = max(stress, na.rm = TRUE), sd = sd(stress, na.rm = TRUE) ) var.test(events$stress ~ data_des$group) # variances are equal t.test(events$stress ~ data_des$group, var.equal = T) ## same ##Get ESM variabels means and SDs; # Because we need within-person SD, we use this function (written by Robert Walker, found on stackoverflow.com) : XTSUM <- function(data, varname, unit) { varname <- enquo(varname) loc.unit <- enquo(unit) ores <- data %>% summarise(ovr.mean=mean(!! varname, na.rm=TRUE), ovr.sd=sd(!! varname, na.rm=TRUE), ovr.min = min(!! varname, na.rm=TRUE), ovr.max=max(!! varname, na.rm=TRUE), ovr.N=sum(as.numeric((!is.na(!! varname))))) bmeans <- data %>% group_by(!! loc.unit) %>% summarise(meanx=mean(!! varname, na.rm=T), t.count=sum(as.numeric(!is.na(!! varname)))) bres <- bmeans %>% ungroup() %>% summarise(between.sd = sd(meanx, na.rm=TRUE), between.min = min(meanx, na.rm=TRUE), between.max=max(meanx, na.rm=TRUE), Units=sum(as.numeric(!is.na(t.count))), t.bar=mean(t.count, na.rm=TRUE)) wdat <- data %>% group_by(!! loc.unit) %>% mutate(W.x = scale(!! varname, scale=FALSE)) wres <- wdat %>% ungroup() %>% summarise(within.sd=sd(W.x, na.rm=TRUE), within.min=min(W.x, na.rm=TRUE), within.max=max(W.x, na.rm=TRUE)) return(list(ores=ores,bres=bres,wres=wres)) } XTSUM(subset(my_data_complete, group == 'Stable group'), varname=NA_factor, unit=subjno) XTSUM(subset(my_data_complete, group == 'Stable group'), varname=PA_factor, unit=subjno) XTSUM(subset(my_data_complete, group == 'Stable group'), varname=stress, unit=subjno) XTSUM(subset(my_data_complete, group == 'Increase group'), varname=NA_factor, unit=subjno) XTSUM(subset(my_data_complete, group == 'Increase group'), varname=PA_factor, unit=subjno) XTSUM(subset(my_data_complete, group == 'Increase group'), varname=stress, unit=subjno) ###ESM variables; for them to get means and SDs dataset must be aggregated, but to compare groups, # multilevel models must be used (not nessesary, but in this way more data is saved) ## comparing NA factor between groups Na_factor_T0_difference <- lme(NA_factor ~ group, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) summary(Na_factor_T0_difference) ##NO rm(Na_factor_T0_difference) ##PA affect score PA_factor_T0_difference <- lme(PA_factor ~ group, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) summary(PA_factor_T0_difference) ##NO rm(PA_factor_T0_difference) ##Daily stressors mean # Are they different between groups? stress_group_difference <- lme(stress ~ group, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) summary(stress_group_difference)### No difference rm(stress_group_difference) ###Daily stressors sequences: are they same across groups? my_data_complete$cons_stress <- my_data_complete$stress & my_data_complete$l1_stress >= 0 table(my_data_complete$cons_stress) summary(lme(cons_stress ~ group12, data = my_data_complete, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) my_data_complete$cons_stress2 <- my_data_complete$stress & my_data_complete$l1_stress & my_data_complete$l2_stress >= 0 summary(lme(cons_stress2 ~ group12, data = my_data_complete, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) ### how many missings there are? ### it is impossible to work with complete dataset, because 42 mostly empty rows add too much noise. Therefore, up to 8 days for this analysis my_data_missings <- complete(my_data, nesting(group, idtw, subjno), dayno = 1:8, beepno=1:10) ### #Time variables, one for all days ## my_data_missings$morning <- my_data_missings$beepno < 4 my_data_missings$afternoon <- my_data_missings$beepno >= 4 & my_data_missings$beepno <= 7 my_data_missings$evening <- my_data_missings$beepno > 7 my_data_missings$count <- is.na(my_data_missings$NA_factor) table(my_data_missings$count, my_data_missings$group) chisq.test(my_data_missings$count, my_data_missings$group, correct=FALSE) ###Easier alterpative my_data_missings$time_of_the_day <- factor(cut(my_data_missings$beepno, breaks=c(0,3,7,11), ordered=TRUE, labels = c("Morning", "Afternoon", "Evening"))) chisq.test(my_data_missings$count, my_data_missings$group, my_data_missings$time_of_the_day, correct=FALSE) ## Something is different: prop.table(table(my_data_missings$count[my_data_missings$morning == T], my_data_missings$group[my_data_missings$morning == T])) chisq.test(my_data_missings$count[my_data_missings$morning == T], my_data_missings$group[my_data_missings$morning == T], correct = F) prop.table(table(my_data_missings$count[my_data_missings$afternoon == T], my_data_missings$group[my_data_missings$afternoon == T])) chisq.test(my_data_missings$count[my_data_missings$afternoon == T], my_data_missings$group[my_data_missings$afternoon == T], correct = F) prop.table(table(my_data_missings$count[my_data_missings$evening == T], my_data_missings$group[my_data_missings$evening == T])) chisq.test(my_data_missings$count[my_data_missings$evening == T], my_data_missings$group[my_data_missings$evening == T], correct = F) ### Apperantly differences are in the evening table(my_data_missings$count, my_data_missings$group, my_data_missings$dayno) barchart(table(my_data_missings$dayno, my_data_missings$group, my_data_missings$count), origin = 0, auto.key=T, ylab = "Day number") barchart(table(my_data_missings$beepno, my_data_missings$group, my_data_missings$count), origin = 0, auto.key=T) barchart(table(my_data_missings$time_of_the_day, my_data_missings$group, my_data_missings$count), origin = 0, auto.key=T, ylab = "Time of the day") chisq.test(my_data_missings$time_of_the_day, my_data_missings$group, my_data_missings$count) #### Check whether SDs of NA and PA and stressors are the same between groups test_NA <- lme(NA_factor ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) test_NA2 <- lme(NA_factor ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, weights=varIdent(form=~1|group), na.action=na.omit, control=list(opt="optim")) anova(test_NA, test_NA2) test_PA <- lme(PA_factor ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) test_PA2 <- lme(PA_factor ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, weights=varIdent(form=~1|group), na.action=na.omit, control=list(opt="optim")) anova(test_PA, test_PA2) test_stress <- lme(stress ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim")) test_stress2 <- lme(stress ~ group + id_time, data = my_data_complete, random=~1|idtw/subjno, weights=varIdent(form=~1|group), na.action=na.omit, control=list(opt="optim")) anova(test_stress, test_stress2) ### this one IS the same, good ###Multiverse analysis##### # Assumptions: maximum power # Same level of symptoms on baseline, different at T1 # Same level of affect and stressors rm(data_des) ###Creating the subset with cross-sectional data data_des <- subset(my_data_main, select = c("subjno","idtw", "_1_J1", "_1_J2", "_1_J3", "_1_J4", "_1_scl90_tot", "_2_scl90_tot", "gender", "age_wave1", "_2_le_neg")) ###Deleting parents and other unrelated participants (they do not have the age data): data_des<-data_des[!is.na(data_des$age_wave1), ] ##creating the variable representing the number of beeps for each individual: n_beeps <- plyr::count(data_des,c('subjno')) data_des <- merge(n_beeps, data_des, by = "subjno") rm(n_beeps) ###Now the dataset is collapsed: data_des <- aggregate(.~subjno, data_des, mean, na.action=NULL) ###Selection of the subsample with the lower level of happy childhood experiences: #checking the missings in the JTV items first: summary(data_des[c("_1_J1", "_1_J2", "_1_J3", "_1_J4")]) ## all items have the same number of missings ## see the descriptive statistics part below for more details data_des$subjno[is.na(data_des$`_1_J1`)] data_des$subjno[is.na(data_des$`_1_J2`)] data_des$subjno[is.na(data_des$`_1_J3`)] data_des$subjno[is.na(data_des$`_1_J4`)] ##and they belong to the same people #Calculating then the JTV sum score: data_des$chi_exp <- data_des$`_1_J1` + data_des$`_1_J2` + data_des$`_1_J3` + data_des$`_1_J4` length(data_des$subjno[is.na(data_des$chi_exp)]) ## 25 people do not have values data_des<-data_des[!is.na(data_des$chi_exp), ] ## they are omitted from the sample #Splitting the sample to select the subsample with the lowest level of JTV sumscore (becasue items are formulating positively): data_des$chi_exp_2 <- xtile(data_des$chi_exp, n = 2) table(data_des$chi_exp_2) ## resulting in 451 people with lower level of JTV sumscore data_des<-data_des[data_des$chi_exp_2 == 1, ] # Deleting people with high level of happy childhood ### SCL-90 scores: #Checking missings: summary(data_des[c("_1_scl90_tot", "_2_scl90_tot")]) ## see the descriptive statistics part below for more details #Calculating the change score: data_des$scl_change <- data_des$`_2_scl90_tot` - data_des$`_1_scl90_tot` # thus people with missings either on T0 or T1 will have NAs length(data_des$subjno[!is.na(data_des$scl_change)]) # 249 participants have data availible # Deleting the participants without SCL scores: data_des<-data_des[!is.na(data_des$scl_change), ] ### Having more than 30% of ESM data available: length(data_des$subjno[(data_des$freq <= 18)]) #the study period consisted of 6 days with 10 beeps per day; #60 is considered max (some people have more, but 60 was the aim) #and therefore 30% of 60 is 18. We exclude all people (10) with #18 or less beeps: data_des <- data_des[data_des$freq > 18, ] # resulting in 239 participants ###Having at least one unpleasant experience events <- subset(my_data_main, select = c("subjno", "eve_pleasant")) events <- events[events$subjno %in% data_des$subjno, ] #deleting extra people events$stress <- events$eve_pleasant <= 0 events <- aggregate(.~subjno, events, sum, na.action = na.omit) View(events) #"TWS0308" "TWS0609" - are these people; rm(events) data_des <- data_des[!data_des$subjno == "TWS0308", ] data_des <- data_des[!data_des$subjno == "TWS0609", ] ####Group allocations: # To make it reproducible, we shall exclude the same Decrease group, so: data_des$group <- xtile(data_des$scl_change, 3) data_des$group <- factor(data_des$group, labels = c("Decrease group", "Stable group", "Increase group")) data_des <- data_des[!data_des$group == "Decrease group", ] #excluding people from the group with the decrease of symptoms data_des$group = NULL ## # we need at least 70 people in each group. data_des$group1 <-xtile(data_des$scl_change, cutpoints = c(1, 6)) # these are limits upper and lower xtile(data_des$scl_change, cutpoints = -11) # -13, -12, -11 - only versions "from the bottom" table(xtile(data_des$scl_change, cutpoints = 7)) #1, 2, 3, 4 (the actual), 5, 6 table(xtile(data_des$scl_change, cutpoints = c(1, 2))) #two - 1 and 6, 2 and 6, 3 and 6, 4 and 6, 5 and 6, 1 and 5, 2 and 5, 3 and 5 # 4 and 5, 1 and 4, 2 and 4, 3 and 4, 1 and 3, 2 and 3, 1 and 2 table(xtile(data_des$scl_change, cutpoints = c(-13, 2)))# -13 $ 2, 3, 4, 5, 6 table(xtile(data_des$scl_change, cutpoints = c(-12, 7)))# -12 $ 5, 6 table(xtile(data_des$scl_change, cutpoints = c(-11, 6)))# -11 6 #creating all possible groupings # without negative "tail" # 8 groups data_des$group1 <- xtile(data_des$scl_change, cutpoints = c(-13, 2)) # we need group 2 and 3 data_des$group2 <- xtile(data_des$scl_change, cutpoints = c(-13, 3)) data_des$group3 <- xtile(data_des$scl_change, cutpoints = c(-13, 4)) data_des$group4 <- xtile(data_des$scl_change, cutpoints = c(-13, 5)) data_des$group5 <- xtile(data_des$scl_change, cutpoints = c(-13, 6)) data_des$group6 <- xtile(data_des$scl_change, cutpoints = c(-12, 5)) data_des$group7 <- xtile(data_des$scl_change, cutpoints = c(-12, 6)) data_des$group8 <- xtile(data_des$scl_change, cutpoints = c(-11, 6)) #With only one split (maximum power!) data_des$group9 <- xtile(data_des$scl_change, cutpoints = 1) data_des$group10 <- xtile(data_des$scl_change, cutpoints = 2) data_des$group11 <- xtile(data_des$scl_change, cutpoints = 3) data_des$group12 <- xtile(data_des$scl_change, cutpoints = 4) #the actual grouping data_des$group13 <- xtile(data_des$scl_change, cutpoints = 5) data_des$group14 <- xtile(data_des$scl_change, cutpoints = 6) ## With potential group in between data_des$group15 <- xtile(data_des$scl_change, cutpoints = c(1, 6)) data_des$group16 <- xtile(data_des$scl_change, cutpoints = c(2, 6)) data_des$group17 <- xtile(data_des$scl_change, cutpoints = c(3, 6)) data_des$group18 <- xtile(data_des$scl_change, cutpoints = c(4, 6)) data_des$group19 <- xtile(data_des$scl_change, cutpoints = c(5, 6)) data_des$group20 <- xtile(data_des$scl_change, cutpoints = c(1, 5)) data_des$group21 <- xtile(data_des$scl_change, cutpoints = c(2, 5)) data_des$group22 <- xtile(data_des$scl_change, cutpoints = c(3, 5)) data_des$group23 <- xtile(data_des$scl_change, cutpoints = c(4, 5)) data_des$group24 <- xtile(data_des$scl_change, cutpoints = c(1, 4)) data_des$group25 <- xtile(data_des$scl_change, cutpoints = c(2, 4)) data_des$group26 <- xtile(data_des$scl_change, cutpoints = c(3, 4)) data_des$group27 <- xtile(data_des$scl_change, cutpoints = c(1, 3)) data_des$group28 <- xtile(data_des$scl_change, cutpoints = c(2, 3)) data_des$group29 <- xtile(data_des$scl_change, cutpoints = c(1, 2)) ###Checking SCL-90 at baseline: summary(lm(data_des$`_1_scl90_tot` ~ data_des$group1, subset = data_des$group1 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group2, subset = data_des$group2 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group3, subset = data_des$group3 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group4, subset = data_des$group4 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group5, subset = data_des$group5 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group6, subset = data_des$group6 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group7, subset = data_des$group7 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group8, subset = data_des$group8 != 1)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group9)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group10)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group11)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group12)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group13)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group14)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group15, subset = data_des$group15 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group16, subset = data_des$group16 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group17, subset = data_des$group17 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group18, subset = data_des$group18 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group19, subset = data_des$group19 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group20, subset = data_des$group20 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group21, subset = data_des$group21 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group22, subset = data_des$group22 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group23, subset = data_des$group23 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group24, subset = data_des$group24 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group25, subset = data_des$group25 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group26, subset = data_des$group26 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group27, subset = data_des$group27 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group28, subset = data_des$group28 != 2)) summary(lm(data_des$`_1_scl90_tot` ~ data_des$group28, subset = data_des$group29 != 2)) ###Checking SCL-90 at T1: summary(lm(data_des$`_2_scl90_tot` ~ data_des$group1, subset = data_des$group1 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group2, subset = data_des$group2 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group3, subset = data_des$group3 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group4, subset = data_des$group4 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group5, subset = data_des$group5 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group6, subset = data_des$group6 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group7, subset = data_des$group7 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group8, subset = data_des$group8 != 1)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group9)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group10)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group11)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group12)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group13)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group14)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group15, subset = data_des$group15 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group16, subset = data_des$group16 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group17, subset = data_des$group17 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group18, subset = data_des$group18 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group19, subset = data_des$group19 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group20, subset = data_des$group20 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group21, subset = data_des$group21 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group22, subset = data_des$group22 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group23, subset = data_des$group23 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group24, subset = data_des$group24 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group25, subset = data_des$group25 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group26, subset = data_des$group26 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group27, subset = data_des$group27 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group28, subset = data_des$group28 != 2)) summary(lm(data_des$`_2_scl90_tot` ~ data_des$group28, subset = data_des$group29 != 2)) ####Preparing variables for the analysis my_data <- subset(my_data_main, select = c("subjno", "dayno", "beepno", "mood_cheerful", "mood_relaxed", "mood_insecur", "mood_lonely", "mood_anxious", "mood_irritat", "mood_satisfi", "mood_listles", "pat_suspic", "mood_down", "mood_guilty", "mood_enthus", "phy_energy", "phy_tired", "eve_pleasant")) my_data <- my_data[my_data$subjno %in% data_des$subjno, ] #deleting extra people my_data <- merge(my_data, data_des, by = "subjno") ## NA factor and PA factor my_data$NA_factor <- rowMeans(my_data[ , c("mood_insecur", "mood_lonely", "mood_anxious", "mood_irritat", "mood_listles", "pat_suspic", "mood_down", "mood_guilty")], na.rm=TRUE) my_data$PA_factor <- rowMeans(my_data[ , c("mood_cheerful", "mood_relaxed","mood_satisfi", "mood_enthus")], na.rm=TRUE) ###Person-mean center these factors personmeans <- subset(my_data, select = c("subjno", "NA_factor", "PA_factor")) personmeans <- aggregate(.~subjno, personmeans, mean, na.rm=TRUE) names(personmeans)<-c("subjno", "NA_factor_mean", "PA_factor_mean") my_data <-merge(my_data, personmeans, by="subjno") my_data$NA_permean <- my_data$NA_factor - my_data$NA_factor_mean my_data$PA_permean <- my_data$PA_factor - my_data$PA_factor_mean rm(personmeans) ### Stressful event my_data$stress <- my_data$eve_pleasant * -1 my_data$stress[my_data$stress < 0] <- NA ### We have a lot of missing time points, so need to fill in NAs there my_data <- my_data[order(my_data$subjno, my_data$dayno, my_data$beepno),] my_data_complete <- complete(my_data, nesting(idtw, subjno), dayno = 1:42, beepno=1:15) ### 42 is the maximum day number #Time variables, one for all days ## my_data_complete$id_time <- rep(seq(1:630), 157) ##### Creating the lagged variable for unpleasant event my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l1_stress", TimeVar = "beepno", slideBy =-1) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l2_stress", TimeVar = "beepno", slideBy =-2) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l3_stress", TimeVar = "beepno", slideBy =-3) my_data_complete = slide(data = my_data_complete, Var = "stress", GroupVar = "subjno", NewVar = "l4_stress", TimeVar = "beepno", slideBy =-4) #### Checking the difference in NA and stressors: summary(lme(NA_factor ~ group1, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group2, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group3, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group4, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group5, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group6, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group7, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group8, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(NA_factor ~ group9, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group10, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group11, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group12, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group13, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group14, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(NA_factor ~ group15, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group16, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group17, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group18, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group19, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group20, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group21, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group22, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group23, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group24, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group25, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group26, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group27, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group28, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(NA_factor ~ group29, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) #Daily stressor: summary(lme(stress ~ group1, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group2, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group3, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group4, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group5, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group6, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group7, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group8, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 1)) summary(lme(stress ~ group9, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group10, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group11, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group12, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group13, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group14, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"))) summary(lme(stress ~ group15, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group16, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group17, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group18, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group19, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group20, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group21, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group22, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group23, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group24, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group25, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group26, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group27, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group28, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) summary(lme(stress ~ group29, data = my_data, random=~1|idtw/subjno, na.action=na.omit, control=list(opt="optim"), subset = group1 != 2)) ###And now multiverse analysis test1 <- lme(NA_permean ~ l1_stress + id_time + group1 + l1_stress*group1 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group1 != 1) test2 <- lme(NA_permean ~ l1_stress + id_time + group2 + l1_stress*group2 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group2 != 1) test3 <- lme(NA_permean ~ l1_stress + id_time + group3 + l1_stress*group3 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group3 != 1) test4 <- lme(NA_permean ~ l1_stress + id_time + group4 + l1_stress*group4 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group4 != 1) test5 <- lme(NA_permean ~ l1_stress + id_time + group5 + l1_stress*group5 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group5 != 1) test6 <- lme(NA_permean ~ l1_stress + id_time + group6 + l1_stress*group6 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group6 != 1) test7 <- lme(NA_permean ~ l1_stress + id_time + group7 + l1_stress*group7 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group7 != 1) test8 <- lme(NA_permean ~ l1_stress + id_time + group8 + l1_stress*group8 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group8 != 1) test9 <- lme(NA_permean ~ l1_stress + id_time + group9 + l1_stress*group9 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test10 <- lme(NA_permean ~ l1_stress + id_time + group10 + l1_stress*group10 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test11 <- lme(NA_permean ~ l1_stress + id_time + group11 + l1_stress*group11 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test12 <- lme(NA_permean ~ l1_stress + id_time + group12 + l1_stress*group12 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test13 <- lme(NA_permean ~ l1_stress + id_time + group13 + l1_stress*group13 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test14 <- lme(NA_permean ~ l1_stress + id_time + group14 + l1_stress*group14 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno)) test15 <- lme(NA_permean ~ l1_stress + id_time + group15 + l1_stress*group15 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group15 != 2) test16 <- lme(NA_permean ~ l1_stress + id_time + group16 + l1_stress*group16 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group16 != 2) test17 <- lme(NA_permean ~ l1_stress + id_time + group17 + l1_stress*group17 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group17 != 2) test18 <- lme(NA_permean ~ l1_stress + id_time + group18 + l1_stress*group18 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group18 != 2) test19 <- lme(NA_permean ~ l1_stress + id_time + group19 + l1_stress*group19 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group19 != 2) test20 <- lme(NA_permean ~ l1_stress + id_time + group20 + l1_stress*group20 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group20 != 2) test21 <- lme(NA_permean ~ l1_stress + id_time + group21 + l1_stress*group21 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group21 != 2) test22 <- lme(NA_permean ~ l1_stress + id_time + group22 + l1_stress*group22 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group22 != 2) test23 <- lme(NA_permean ~ l1_stress + id_time + group23 + l1_stress*group23 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group23 != 2) test24 <- lme(NA_permean ~ l1_stress + id_time + group24 + l1_stress*group24 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group24 != 2) test25 <- lme(NA_permean ~ l1_stress + id_time + group25 + l1_stress*group25 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group25 != 2) test26 <- lme(NA_permean ~ l1_stress + id_time + group26 + l1_stress*group26 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group26 != 2) test27 <- lme(NA_permean ~ l1_stress + id_time + group27 + l1_stress*group27 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group27 != 2) test28 <- lme(NA_permean ~ l1_stress + id_time + group28 + l1_stress*group28 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group28 != 2) test29 <- lme(NA_permean ~ l1_stress + id_time + group28 + l1_stress*group29 + gender + age_wave1, random=list(idtw = ~1, subjno = pdDiag(~ 1 + id_time + l1_stress)), data=my_data_complete, na.action=na.omit, control=list(opt="optim"), correlation = corCAR1(form = ~ id_time|idtw/subjno), subset = group29 != 2) vector_of_p <- c(summary(test1)$tTable[7, 5], summary(test2)$tTable[7, 5], summary(test3)$tTable[7, 5], summary(test4)$tTable[7, 5], summary(test5)$tTable[7, 5], summary(test6)$tTable[7, 5], summary(test7)$tTable[7, 5], summary(test8)$tTable[7, 5], summary(test9)$tTable[7, 5], summary(test10)$tTable[7, 5], summary(test11)$tTable[7, 5], summary(test12)$tTable[7, 5], summary(test13)$tTable[7, 5], summary(test14)$tTable[7, 5], summary(test15)$tTable[7, 5], summary(test16)$tTable[7, 5], summary(test17)$tTable[7, 5], summary(test18)$tTable[7, 5], summary(test19)$tTable[7, 5], summary(test20)$tTable[7, 5], summary(test21)$tTable[7, 5], summary(test22)$tTable[7, 5], summary(test23)$tTable[7, 5], summary(test24)$tTable[7, 5], summary(test25)$tTable[7, 5], summary(test26)$tTable[7, 5], summary(test27)$tTable[7, 5], summary(test28)$tTable[7, 5], summary(test29)$tTable[7, 5]) hist(vector_of_p, breaks = 10, col = "grey", main = "Distribution of p-values", xlab = "p-value") abline(v=0.05, col="red")