####Preparation for analyses#### library(foreign) library(lme4) library(parallel) library(vctrs) library(car) library(dplyr) library(qgraph) library(Matrix) library(glmnet) library(igraph) library(devtools) library(NetworkComparisonTest) library(mgm) library(bootnet) library(ggplot2) library(cowplot) library(broom.mixed) #read in data and correct data type if necessary data_post_tx <- read.spss("C:/Users/fritz/OneDrive/Desktop/sides/IPD-MA DAS/Data/CBT-ADM DAS IPDMA - total database - v20-1 - Final wide.sav", use.value.label=TRUE, to.data.frame=TRUE) str(data_post_tx) data_post_tx$unique_pt_id <- as.factor(data_post_tx$unique_pt_id) #subset data set so that participants who received placebo are excluded data_post_tx <- subset(data_post_tx, Tx_group != "placebo") #cross-walk BDI-I total scores (Elkin et al., 1989; Murphy et al., 1984) #into BDI-II total scores according to algorithm by Furukawa et al., 2019 convert_bdi_to_bdi_ii <- function(bdi_scores) { conversion_table_bdi_i <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 17, 19, 21, 23, 25, 27, 28, 30, 31, 33, 34, 36, 38, 39, 41, 42) conversion_table_bdi_ii <- c(0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 15, 16, 18, 20, 21, 23, 25, 27, 30, 32, 34, 36, 38, 40, 42, 44, 45, 47, 49, 50) bdi_ii_scores <- numeric(length(bdi_scores)) for (i in seq_along(bdi_scores)) { if (bdi_scores[i] %in% conversion_table_bdi_i) { bdi_ii_scores[i] <- conversion_table_bdi_ii[conversion_table_bdi_i == bdi_scores[i]] } else { # Find the nearest available scores for linear interpolation lower_index <- max(which(conversion_table_bdi_i < bdi_scores[i])) upper_index <- min(which(conversion_table_bdi_i > bdi_scores[i])) # Perform linear interpolation if both lower and upper scores are available if (!is.na(lower_index) && !is.na(upper_index)) { lower_score <- conversion_table_bdi_i[lower_index] upper_score <- conversion_table_bdi_i[upper_index] interpolation_factor <- (bdi_scores[i] - lower_score) / (upper_score - lower_score) lower_ii_score <- conversion_table_bdi_ii[lower_index] upper_ii_score <- conversion_table_bdi_ii[upper_index] bdi_ii_scores[i] <- lower_ii_score + interpolation_factor * (upper_ii_score - lower_ii_score) bdi_ii_scores[i] <- round(bdi_ii_scores[i]) # Round to the nearest integer } else { bdi_ii_scores[i] <- NA # Unable to interpolate, handle as missing } } } return(bdi_ii_scores) } data_post_tx <- data_post_tx %>% mutate(BDI_II = ifelse(studyid %in% c("Elkin", "Murphy"), convert_bdi_to_bdi_ii(BDI), BDI)) #create preBDI_II variable with the cross-walked BDI-II values data_post_tx <- data_post_tx %>% arrange(unique_pt_id, Time) %>% group_by(unique_pt_id) %>% mutate(preBDI_II = if_else(Time == "Baseline", BDI_II, lag(BDI_II, default = NA))) #create interaction term variable #Note: It is not possible to drop a main effect in R if one includes the #variable also in the interaction (here the treatment variable), therefore, #the interaction term has to be created (here called Interaction_term). #The Interaction term coding is as following: matrix(c(0,0,0,1), nrow = 2, dimnames = list(c("CBT","ADM"), c("Baseline","Post-treatment"))) data_post_tx$Interaction_term <- ifelse(data_post_tx$Time == "Post-treatment" & data_post_tx$Tx_group == "PHA", 1, 0) #create variables for moderator models data_post_tx$preDAS_c <- scale(data_post_tx$preDAS, scale = F) data_post_tx <- data_post_tx %>% group_by(studyid) %>% mutate( zpreDAS = as.numeric(scale(preDAS, center = TRUE, scale = TRUE)), zHAMD = as.numeric(scale(HAMD, center = TRUE, scale = TRUE)), zBDI_II = as.numeric(scale(BDI_II, center = TRUE, scale = TRUE)) ) %>% ungroup() #subset database per study for study-specific results # Get the unique study IDs unique_study_ids <- unique(data_post_tx$studyid) # Loop through each unique study ID and create a subset dataset for (study in unique_study_ids) { # Create the subset dataset subset_data <- data_post_tx %>% filter(studyid == study) # Create a dynamic name for the dataset dataset_name <- paste0("data_post_tx_", study) # Assign the subset data to a variable with the dynamic name assign(dataset_name, subset_data) } ####1.Dysfunctional attitude change in CBT and ADM#### DAS_post <- lmer(DAS ~ Time + Interaction_term + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(DAS_post) DAS_post_CIs <- confint(DAS_post, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) DAS_post_CIs p_values_DAS_post <- Anova(DAS_post, test="F", type = 3) p_values_DAS_post #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(DAS_post) std_residuals <- resid(DAS_post, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #RoB items added DAS_post_RoB <- lmer(DAS ~ Time + Interaction_term + AlloConceal + blinding + ITT + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(DAS_post_RoB) DAS_post_RoB_CIs <- confint(DAS_post_RoB, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) DAS_post_RoB_CIs p_values_DAS_post_RoB <- Anova(DAS_post_RoB, test="F", type = 3) p_values_DAS_post_RoB #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(DAS_post_RoB) std_residuals <- resid(DAS_post_RoB, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #Pre- to post-tx change of DAS across conditions DAS_post_PP <- lmer(DAS ~ Time + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(DAS_post_PP) DAS_post_PP_CIs <- confint(DAS_post_PP, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) DAS_post_PP_CIs p_values_DAS_post_PP <- Anova(DAS_post_PP, test="F", type = 3) p_values_DAS_post_PP #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(DAS_post_PP) std_residuals <- resid(DAS_post_PP, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") ####2.Dysfunctional attitudes and associations with depression symptomatology#### #import dataset with item-level HAMD data_post_tx_HAMD <- read.spss("/home/fritzw/IPD-MA_CBT_vs_ADM/Project Lynn 20221124.sav", use.value.label=TRUE, to.data.frame=TRUE) str(data_post_tx_HAMD) data_post_tx_HAMD$id <- as.factor(data_post_tx_HAMD$id) # Create a new variable HAMD_sumscore_post_tx data_post_tx_HAMD$HAMD_sumscore_post_tx <- rowSums(data_post_tx_HAMD[, c("H1t1", "H2t1", "H3t1", "H4t1", "H5t1", "H6t1", "H7t1", "H8t1", "H9t1", "H10t1", "H11t1", "H12t1", "H13t1", "H14t1", "H15t1", "H16t1", "H17t1")], na.rm = T) # Replace sumscore with NA if any of the columns have missing values data_post_tx_HAMD$HAMD_sumscore_post_tx[apply(data_post_tx_HAMD[, c("H1t1", "H2t1", "H3t1", "H4t1", "H5t1", "H6t1", "H7t1", "H8t1", "H9t1", "H10t1", "H11t1", "H12t1", "H13t1", "H14t1", "H15t1", "H16t1", "H17t1")], 1, anyNA)] <- NA #run model relation of dysfunctional attitudes with depression symptomatology #at the post-treatment, test whether this association differed across the CBT and ADM model2 <- lm(HAMD_sumscore_post_tx ~ PHACBT*DASt1, data = data_post_tx_HAMD) summary(model2) model2_CIs <- confint(model2, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) model2_CIs p_values_model2 <- Anova(model2, test="F", type = 3) p_values_model2 ### READING RELEVANT DATASETS # Combined sample of both the CBT and the ADM condition # var 1-16: Individual items of the Hamilton-depression (HAM-D) scale (excluding the item on Lack of insight) at the post-treatment assessment # var 17: Total score on the Dysfunction Attitude Scale (DAS) at the post-treatment assessment ALLt1A=read.delim("ALLt1A.dat", header=TRUE) ALLt1Amatrix<-as.matrix(ALLt1A) ## Combined sample of both the CBT and the ADM condition # var 1: Treatment condition (0=ADM, 1=CBT) # var 2-17: Individual items of the Hamilton-depression (HAM-D) scale (excluding the item on Lack of insight) at the post-treatment assessment # var 18: Total score on the Dysfunction Attitude Scale (DAS) at the post-treatment assessment PHACBTt1A=read.delim("PHACBTt1A.dat", header=TRUE) PHACBTt1Amatrix<-as.matrix(PHACBTt1A) ## Sample of participants in the CBT condition condition # var 1-16: Individual items of the Hamilton-depression (HAM-D) scale (excluding the item on Lack of insight) at the post-treatment assessment # var 17: Total score on the Dysfunction Attitude Scale (DAS) at the post-treatment assessment CBTt1A=read.delim("CBTt1A.dat", header=TRUE) CBTt1Amatrix<-as.matrix(CBTt1A) ## Sample of participants in the ADM condition condition # var 1-16: Individual items of the Hamilton-depression (HAM-D) scale (excluding the item on Lack of insight) at the post-treatment assessment # var 17: Total score on the Dysfunction Attitude Scale (DAS) at the post-treatment assessment PHAt1A=read.delim("PHAt1A.dat", header=TRUE) PHAt1Amatrix<-as.matrix(PHAt1A) #### ESTIMATING NETWORKS ## FIGURE 1: Interrelations of individual HAM-D items and DAS total score at the post-treatment assessment in the combined sample of both the CBT and the ADM condition ALLt1Anetwork <- estimateNetwork(ALLt1Amatrix, default = "EBICglasso",tuning=0.25) ALLt1Agraph=qgraph(ALLt1Anetwork$graph, layout='spring', aspect=TRUE, width=9, height=7, esize=4, shape=c(rep("circle",17)), color=c("firebrick1",rep("steelblue2",2),rep("yellow",3),rep("steelblue2",2),"green3","firebrick1",rep("green3",6),"violet"), label.color=c(rep("black",17)), labels=c("dep","gui","sui","eni","mni","emi","w/a","ret","agi","pan","san","gas","gso","gen","hyp","wei","dys"), label.prop=1.0, vsize=3.5) plot(NULL ,xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=0:1, ylim=0:1) ALLt1Alegend=legend("center", c("Dysfunctional attitudes", "Affective symptoms", "Cognitive symptoms","Arousal/somatic symptoms","Sleep-related symptoms"), border="black", fill = c("violet","firebrick1","steelblue2","green3","yellow"),pt.bg="black", bty = "n", pt.cex = 1.5, cex = 0.7 , text.col = "black", horiz = FALSE, inset = c(0, 0.1)) ## FIGURE S1: Interrelations of treatment condition as well as individual HAM-D items and DAS total score at the post-treatment assessment in the combined sample of both the CBT and the ADM condition PHACBTt1Amgm <- mgm(data = PHACBTt1Amatrix, type = c("c",rep("g",17)), level = c(2,rep(1,17)), lambdaSel = "EBIC", lambdaGam = 0.25, ruleReg = "OR", pbar = TRUE) PHACBTt1Amgmwadj=PHACBTt1Amgm$pairwise$wadj PHACBTt1AmgmEC=PHACBTt1Amgm$pairwise$edgecolor PHACBTt1Amgmsigns=PHACBTt1Amgm$pairwise$signs PHACBTt1Agraph=qgraph(PHACBTt1Amgmwadj, layout='spring', aspect=TRUE, width=9, height=7, esize=4, edge.color=PHACBTt1AmgmEC, shape=c("rectangle",rep("circle",17)), color=c("black","firebrick1",rep("steelblue2",2),rep("yellow",3),rep("steelblue2",2),"green3","firebrick1",rep("green3",6),"violet"), label.color=c("white",rep("black",17)), labels=c("CBT","dep","gui","sui","eni","mni","emi","w/a","ret","agi","pan","san","gas","gso","gen","hyp","wei","dys"), label.prop=1.0, vsize=c(3.5,rep(3.5,17))) plot(NULL ,xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=0:1, ylim=0:1) PHACBTt1Alegend=legend("center", c("CBT versus ADM", "Dysfunctional attitudes", "Affective symptoms", "Cognitive symptoms","Arousal/somatic symptoms","Sleep-related symptoms"), border="black", fill = c("black","violet","firebrick1","steelblue2","green3","yellow"),pt.bg="black", bty = "n", pt.cex = 1.5, cex = 0.7 , text.col = "black", horiz = FALSE, inset = c(0, 0.1)) ## FIGURE S2A: Interrelations of individual HAM-D items and DAS total score at the post-treatment assessment in the CBT condition CBTt1Anetwork <- estimateNetwork(CBTt1Amatrix, default = "EBICglasso",tuning=0.25) CBTt1Agraph=qgraph(CBTt1Anetwork$graph, layout='spring', aspect=TRUE, width=9, height=7, esize=4, shape=c(rep("circle",17)), color=c("firebrick1",rep("steelblue2",2),rep("yellow",3),rep("steelblue2",2),"green3","firebrick1",rep("green3",6),"violet"), label.color=c(rep("black",17)), labels=c("dep","gui","sui","eni","mni","emi","w/a","ret","agi","pan","san","gas","gso","gen","hyp","wei","dys"), label.prop=1.0, vsize=3.5) ## FIGURE S2B: Interrelations of individual HAM-D items and DAS total score at the post-treatment assessment in the ADM condition PHAt1Anetwork <- estimateNetwork(PHAt1Amatrix, default = "EBICglasso",tuning=0.25) PHAt1Agraph=qgraph(PHAt1Anetwork$graph, layout='spring', aspect=TRUE, width=9, height=7, esize=4, shape=c(rep("circle",17)), color=c("firebrick1",rep("steelblue2",2),rep("yellow",3),rep("steelblue2",2),"green3","firebrick1",rep("green3",6),"violet"), label.color=c(rep("black",17)), labels=c("dep","gui","sui","eni","mni","emi","w/a","ret","agi","pan","san","gas","gso","gen","hyp","wei","dys"), label.prop=1.0, vsize=3.5) #### COMPARING THE NETWORK OF INDIVIDUAL HAM-D ITEMS AND THE DAS TOTAL SCORE AT THE POST-TREATMENT ASSESSMENT IN THE ADM CONDITION (FIGURE S2B) VERSUS THE CBT CONDITION (FIGURE S2A) NCT_t1A<- NCT(PHAt1Anetwork, CBTt1Anetwork, it=1000, test.edges=TRUE, edges='all') NCT_t1A ## OUTCOMES: Network invariance test #Test statistic M: 0.1927056, p-value 0.8 ## OUTCOMES: Global strength invariance test #Global strength per group: 4.389295 2.599239, test statistic S: 1.790056, p-value 0.393 ## OUTCOMES: Edge invariance test # Of all connections of the DAS total score to HAM-D items, p=.050 (DAS-emi) is the smallest #####3.Dysfunctional attitudes as a moderator of CBT versus ADM efficacy#### #HAMD as outcome HAMD_post <- lmer(HAMD ~ Time*preDAS_c + Interaction_term*preDAS_c + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(HAMD_post) HAMD_post_CIs <- confint(HAMD_post, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) HAMD_post_CIs p_values_HAMD_post <- Anova(HAMD_post, test="F", type = 3) p_values_HAMD_post #model for pseudo standardized beta zHAMD_post <- lmer(zHAMD ~ Time*zpreDAS + Interaction_term*zpreDAS + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(zHAMD_post) #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(HAMD_post) std_residuals <- resid(HAMD_post, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #Sensitivity analysis HAMD as outcome data_post_tx$preHAMD_c <- scale(data_post_tx$preHAMD, scale = F) HAMD_post_sens <- lmer(HAMD ~ Time*preDAS_c + Interaction_term*preDAS_c + Time*preHAMD_c + Interaction_term*preHAMD_c + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(HAMD_post_sens) HAMD_post_sens_CIs <- confint(HAMD_post_sens, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) HAMD_post_sens_CIs p_values_HAMD_post_sens <- Anova(HAMD_post_sens, test="F", type = 3) p_values_HAMD_post_sens #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(HAMD_post_sens) std_residuals <- resid(HAMD_post_sens, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #BDI_II as outcome BDI_II_post <- lmer(BDI_II ~ Time*preDAS_c + Interaction_term*preDAS_c + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(BDI_II_post) BDI_II_post_CIs <- confint(BDI_II_post, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) BDI_II_post_CIs p_values_BDI_II_post <- Anova(BDI_II_post, test="F", type = 3) p_values_BDI_II_post #model for pseudo standardized beta zBDI_II_post <- lmer(zBDI_II ~ Time*zpreDAS + Interaction_term*zpreDAS + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(zBDI_II_post) #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(BDI_II_post) std_residuals <- resid(BDI_II_post, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #Sensitivity analysis BDI_II as outcome data_post_tx$preBDI_II_c <- scale(data_post_tx$preBDI_II, scale = F) BDI_II_post_sens <- lmer(BDI_II ~ Time*preDAS_c + Interaction_term*preDAS_c + Time*preBDI_II_c + Interaction_term*preBDI_II_c + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(BDI_II_post_sens) BDI_II_post_sens_CIs <- confint(BDI_II_post_sens, oldNames=F, method="boot", nsim=5000, parallel = "multicore", ncpus = 32) BDI_II_post_sens_CIs p_values_BDI_II_post_sens <- Anova(BDI_II_post_sens, test="F", type = 3) p_values_BDI_II_post_sens #testing normal distribution: #plot the residuals # Get the residuals and standardized residuals residuals_raw <- residuals(BDI_II_post_sens) std_residuals <- resid(BDI_II_post_sens, type = "pearson") # Combine residuals and standardized residuals into a data frame residual_data <- data.frame(Raw_Residuals = residuals_raw, Standardized_Residuals = std_residuals) # Create side-by-side histograms par(mfrow = c(1, 2)) # Set up a 1x2 plotting grid hist(residual_data$Raw_Residuals, main = "Histogram of Raw Residuals", xlab = "Raw Residuals", col = "lightblue", border = "black") hist(residual_data$Standardized_Residuals, main = "Histogram of Standardized Residuals", xlab = "Standardized Residuals", col = "lightgreen", border = "black") #####Probing of the HAMD - DAS moderator finding #sig model HAMD_post <- lmer(HAMD ~ Time*preDAS_c + Interaction_term*preDAS_c + (1| unique_pt_id)+(1| studyid), data = data_post_tx) summary(HAMD_post) #plotting/probing #extract the coefficients from the model manually b0 = HAMD_post@beta[1] # intercept b1 = HAMD_post@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = HAMD_post@beta[3] # moderator (baseline preDAS) b4 = HAMD_post@beta[4] # Interaction_term b5 = HAMD_post@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = HAMD_post@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(HAMD_post) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(726)) # p-value for slope 726 is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(726)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(726)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(726)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #preparation for plotting mytheme <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(size=0.5, colour = "black"), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"), text = element_text(size=12, family="TT Arial"), strip.background = element_blank(), legend.position = c(0.1, 0.2), legend.title = element_blank(), legend.box = "horizontal", legend.key= element_blank()) #Plots for baseline preDAS +/-1SD p <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("High baseline DAS (+1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme p r <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("Low baseline DAS (-1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme r preDAS_baseline_fig <- plot_grid(r, p, labels = c("A", "B"), ncol = 1, nrow = 2) preDAS_baseline_fig #### observed post-treatment M and SD per tx condition (HAMD & BDI) #### # List of study datasets study_datasets <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Zegal", "data_post_tx_Murphy","data_post_tx_Quilty") # Initialize a list to store results results <- list() # Loop through each study dataset for (dataset in study_datasets) { # Get the dataset data <- get(dataset) # Filter the data for Post-treatment post_treatment_data <- data %>% filter(Time == "Post-treatment") # Calculate means and standard deviations for ADM group ADM_data <- post_treatment_data %>% filter(Tx_group == "PHA") mean_HAMD_ADM <- mean(ADM_data$HAMD, na.rm = TRUE) sd_HAMD_ADM <- sd(ADM_data$HAMD, na.rm = TRUE) mean_BDI_ADM <- mean(ADM_data$BDI, na.rm = TRUE) sd_BDI_ADM <- sd(ADM_data$BDI, na.rm = TRUE) # Calculate means and standard deviations for CBT group CBT_data <- post_treatment_data %>% filter(Tx_group == "CBT") mean_HAMD_CBT <- mean(CBT_data$HAMD, na.rm = TRUE) sd_HAMD_CBT <- sd(CBT_data$HAMD, na.rm = TRUE) mean_BDI_CBT <- mean(CBT_data$BDI, na.rm = TRUE) sd_BDI_CBT <- sd(CBT_data$BDI, na.rm = TRUE) # Store the results in the list results[[dataset]] <- data.frame( Study = dataset, Group = c("ADM", "CBT"), Mean_HAMD = c(mean_HAMD_ADM, mean_HAMD_CBT), SD_HAMD = c(sd_HAMD_ADM, sd_HAMD_CBT), Mean_BDI = c(mean_BDI_ADM, mean_BDI_CBT), SD_BDI = c(sd_BDI_ADM, sd_BDI_CBT) ) } # Combine the results into a single dataframe results_df <- do.call(rbind, results) # Print the results print(results_df) ##### observed M and SD per time point and tx condition (DAS scores) #### # List of study datasets study_datasets <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Zegal", "data_post_tx_Murphy","data_post_tx_Quilty") # Initialize a list to store results results <- list() # Function to calculate mean and sd for DAS scores calc_das_stats <- function(data, time_point, tx_group) { filtered_data <- data %>% filter(Time == time_point, Tx_group == tx_group) mean_DAS <- mean(filtered_data$DAS, na.rm = TRUE) sd_DAS <- sd(filtered_data$DAS, na.rm = TRUE) return(data.frame( Time = time_point, Group = tx_group, Mean_DAS = mean_DAS, SD_DAS = sd_DAS )) } # Loop through each study dataset for (dataset in study_datasets) { # Get the dataset data <- get(dataset) # Calculate stats for ADM group at Baseline stats_ADM_Baseline <- calc_das_stats(data, "Baseline", "PHA") # Calculate stats for CBT group at Baseline stats_CBT_Baseline <- calc_das_stats(data, "Baseline", "CBT") # Calculate stats for ADM group at Post-treatment stats_ADM_Post <- calc_das_stats(data, "Post-treatment", "PHA") # Calculate stats for CBT group at Post-treatment stats_CBT_Post <- calc_das_stats(data, "Post-treatment", "CBT") # Combine the results for each dataset combined_stats <- rbind(stats_ADM_Baseline, stats_CBT_Baseline, stats_ADM_Post, stats_CBT_Post) combined_stats$Study <- dataset # Store the results in the list results[[dataset]] <- combined_stats } # Combine the results into a single dataframe results_df <- do.call(rbind, results) # Print the results print(results_df) ##### Estimated mean difference DAS score per study #### # List of study datasets study_datasets <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store results model_summaries <- list() # Function to fit the model and extract summary fit_model_and_summarize <- function(data) { # Fit the model model <- lmer(DAS ~ Time + Interaction_term + (1 | unique_pt_id), data = data) # Get the tidy summary of the model model_summary <- tidy(model, effects = "fixed") # Extract fixed effects # Get random effects random_effects <- ranef(model) return(list(model = model, summary = model_summary, random_effects = random_effects)) } # Loop through each study dataset for (dataset in study_datasets) { # Get the dataset data <- get(dataset) # Check if the dataset is valid if (nrow(data) > 0 && length(unique(data$unique_pt_id)) > 1) { # Fit the model and get the summary result <- fit_model_and_summarize(data) # Store the summary and random effects in the list model_summaries[[dataset]] <- result # Save the model summary to an individual object assign(paste0("model_summary_", dataset), result$summary) } else { cat("\nDataset is empty or does not have sufficient levels for 'unique_pt_id':", dataset, "\n") } } # Print each model_summary object for (dataset in study_datasets) { model_summary_object <- get(paste0("model_summary_", dataset), envir = .GlobalEnv) cat("\nModel Summary for study:", dataset, "\n") print(model_summary_object) } #get p values # List of model summary object names model_summary_names <- grep("^model_summary_", ls(), value = TRUE) # Define the dataset names used to fit the models dataset_names <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store p-values p_values_list <- list() # Loop through each model summary object name and dataset for (i in seq_along(model_summary_names)) { model_name <- model_summary_names[i] dataset_name <- dataset_names[i] # Get the dataset data <- get(dataset_name) # Fit the model again with the original dataset model <- lmer(DAS ~ Time + Interaction_term + (1 | unique_pt_id), data = data) # Run Anova on the fitted model p_values_DAS_post <- Anova(model, test = "F", type = 3) # Store p-values in the list p_values_list[[dataset_name]] <- p_values_DAS_post # Print p-values for each model cat("\nP-values for model:", dataset_name, "\n") print(p_values_DAS_post) } ##### Estimated DAS moderator effect per study (HAMD as outcome)#### # List of study datasets study_datasets <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store results model_summaries <- list() fit_model <- function(data) { lmer(HAMD ~ Time*preDAS_c + Interaction_term*preDAS_c + (1 | unique_pt_id), data = data) } model_list <- list() for (dataset in study_datasets) { data <- get(dataset) if (nrow(data) > 0 && length(unique(data$unique_pt_id)) > 1) { model <- fit_model(data) # store lmer object in a list model_list[[dataset]] <- model # optionally also create separate objects in .GlobalEnv: assign(paste0("model_", dataset), model) } else { cat("\nDataset is empty or does not have sufficient levels for 'unique_pt_id':", dataset, "\n") } } # Print summaries for (dataset in study_datasets) { m <- get(paste0("model_", dataset), envir = .GlobalEnv) cat("\nModel Summary for study:", dataset, "\n") print(summary(m)) } #models for pseudo standardized betas fit_model <- function(data) { lmer(zHAMD ~ Time*zpreDAS + Interaction_term*zpreDAS + (1 | unique_pt_id), data = data) } model_list <- list() for (dataset in study_datasets) { data <- get(dataset) if (nrow(data) > 0 && length(unique(data$unique_pt_id)) > 1) { model <- fit_model(data) # store lmer object in a list model_list[[dataset]] <- model # optionally also create separate objects in .GlobalEnv: assign(paste0("model_pseudo_", dataset), model) } else { cat("\nDataset is empty or does not have sufficient levels for 'unique_pt_id':", dataset, "\n") } } # Print summaries for (dataset in study_datasets) { m <- get(paste0("model_pseudo_", dataset), envir = .GlobalEnv) cat("\nModel Summary for study:", dataset, "\n") print(summary(m)) } #get p values # List of model summary object names model_summary_names <- grep("^model_summary_", ls(), value = TRUE) # Define the dataset names used to fit the models dataset_names <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store p-values p_values_list <- list() # Loop through each model summary object name and dataset for (i in seq_along(model_summary_names)) { model_name <- model_summary_names[i] dataset_name <- dataset_names[i] # Get the dataset data <- get(dataset_name) # Fit the model again with the original dataset model <- lmer(HAMD ~ Time*preDAS_c + Interaction_term*preDAS_c + (1 | unique_pt_id), data = data) # Run Anova on the fitted model p_values_HAMD_post <- Anova(model, test = "F", type = 3) # Store p-values in the list p_values_list[[dataset_name]] <- p_values_HAMD_post # Print p-values for each model cat("\nP-values for model:", dataset_name, "\n") print(p_values_HAMD_post) } ##### Estimated DAS moderator effect per study (BDI_II as outcome)#### # List of study datasets study_datasets <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store results model_summaries <- list() # Function to fit the model and extract summary fit_model_and_summarize <- function(data) { # Fit the model model <- lmer(BDI_II ~ Time*preDAS_c + Interaction_term*preDAS_c + (1 | unique_pt_id), data = data) # Get the tidy summary of the model model_summary <- tidy(model, effects = "fixed") # Extract fixed effects # Get random effects random_effects <- ranef(model) return(list(summary = model_summary, random_effects = random_effects)) } # Loop through each study dataset for (dataset in study_datasets) { # Get the dataset data <- get(dataset) # Check if the dataset is valid if (nrow(data) > 0 && length(unique(data$unique_pt_id)) > 1) { # Fit the model and get the summary result <- fit_model_and_summarize(data) # Store the summary and random effects in the list model_summaries[[dataset]] <- result # Save the model summary to an individual object assign(paste0("model_summary_", dataset), result$summary) } else { cat("\nDataset is empty or does not have sufficient levels for 'unique_pt_id':", dataset, "\n") } } # Print each model_summary object for (dataset in study_datasets) { model_summary_object <- get(paste0("model_summary_", dataset), envir = .GlobalEnv) cat("\nModel Summary for study:", dataset, "\n") print(model_summary_object) } #models for pseudo standardized betas fit_model <- function(data) { lmer(zBDI_II ~ Time*zpreDAS + Interaction_term*zpreDAS + (1 | unique_pt_id), data = data) } model_list <- list() for (dataset in study_datasets) { data <- get(dataset) if (nrow(data) > 0 && length(unique(data$unique_pt_id)) > 1) { model <- fit_model(data) # store lmer object in a list model_list[[dataset]] <- model # optionally also create separate objects in .GlobalEnv: assign(paste0("model_pseudo_", dataset), model) } else { cat("\nDataset is empty or does not have sufficient levels for 'unique_pt_id':", dataset, "\n") } } # Print summaries for (dataset in study_datasets) { m <- get(paste0("model_pseudo_", dataset), envir = .GlobalEnv) cat("\nModel Summary for study:", dataset, "\n") print(summary(m)) } #get p values # List of model summary object names model_summary_names <- grep("^model_summary_", ls(), value = TRUE) # Define the dataset names used to fit the models dataset_names <- c("data_post_tx_DeRubeis", "data_post_tx_Elkin", "data_post_tx_Murphy", "data_post_tx_Quilty", "data_post_tx_Zegal") # Initialize a list to store p-values p_values_list <- list() # Loop through each model summary object name and dataset for (i in seq_along(model_summary_names)) { model_name <- model_summary_names[i] dataset_name <- dataset_names[i] # Get the dataset data <- get(dataset_name) # Fit the model again with the original dataset model <- lmer(BDI_II ~ Time*preDAS_c + Interaction_term*preDAS_c + (1 | unique_pt_id), data = data) # Run Anova on the fitted model p_values_BDI_II_post <- Anova(model, test = "F", type = 3) # Store p-values in the list p_values_list[[dataset_name]] <- p_values_BDI_II_post # Print p-values for each model cat("\nP-values for model:", dataset_name, "\n") print(p_values_BDI_II_post) } #### plotting of moderator effect DAS HAMD per study #### # common y-axis for all panels common_y <- scale_y_continuous( limits = c(0, 24), breaks = seq(0, 24, by = 4) ) # common theme for all panels mytheme <- theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(size = 0.5, colour = "black"), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"), text = element_text(size = 12, family = "Arial"), strip.background = element_blank(), legend.position = c(0.1, 0.2), legend.title = element_blank(), legend.box = "horizontal", legend.key = element_blank() ) #DeRubeis et al., 2005 #plotting/probing #extract the coefficients from the model manually b0 = model_data_post_tx_DeRubeis@beta[1] # intercept b1 = model_data_post_tx_DeRubeis@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = model_data_post_tx_DeRubeis@beta[3] # moderator (baseline preDAS) b4 = model_data_post_tx_DeRubeis@beta[4] # Interaction_term b5 = model_data_post_tx_DeRubeis@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = model_data_post_tx_DeRubeis@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx_DeRubeis$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx_DeRubeis$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(model_data_post_tx_DeRubeis) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(175)) # p-value for slope is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(175)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(175)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(175)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #Plots for baseline preDAS +/-1SD p_DeRubeis <- ggplot(IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx)) + geom_point(size=1.5) + ggtitle("High baseline DAS (+1SD)") + theme(plot.title = element_text(hjust = 0.5)) + labs(x="", y="HAMD scores") + common_y + scale_x_discrete(expand=c(0,0.15)) + mytheme r_DeRubeis <- ggplot(IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx)) + geom_point(size=1.5) + ggtitle("Low baseline DAS (-1SD)") + theme(plot.title = element_text(hjust = 0.5)) + labs(x="", y="HAMD scores") + common_y + scale_x_discrete(expand=c(0,0.15)) + mytheme row_DeRubeis <- plot_grid( r_DeRubeis, p_DeRubeis, ncol = 2, align = "hv" ) row_DeRubeis_titled <- plot_grid( ggdraw() + draw_label("DeRubeis et al. (2005)", fontface = "bold", size = 14, hjust = 0.5, vjust = 0.5), row_DeRubeis, ncol = 1, rel_heights = c(0.1, 1) # small space for title, rest for plots ) row_DeRubeis_titled #Elkin et al., 1989 #plotting/probing #extract the coefficients from the model manually b0 = model_data_post_tx_Elkin@beta[1] # intercept b1 = model_data_post_tx_Elkin@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = model_data_post_tx_Elkin@beta[3] # moderator (baseline preDAS) b4 = model_data_post_tx_Elkin@beta[4] # Interaction_term b5 = model_data_post_tx_Elkin@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = model_data_post_tx_Elkin@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx_Elkin$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx_Elkin$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(model_data_post_tx_Elkin) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(111)) # p-value for slope is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(111)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(111)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(111)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #Plots for baseline preDAS +/-1SD p_Elkin <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("High baseline DAS (+1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme r_Elkin <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("Low baseline DAS (-1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme row_Elkin <- plot_grid( r_Elkin, p_Elkin, ncol = 2, align = "hv" ) row_Elkin_titled <- plot_grid( ggdraw() + draw_label("Elkin et al. (1989)", fontface = "bold", size = 14, hjust = 0.5, vjust = 0.5), row_Elkin, ncol = 1, rel_heights = c(0.1, 1) # small space for title, rest for plots ) row_Elkin_titled #Segal et al., 2006 #plotting/probing #extract the coefficients from the model manually b0 = model_data_post_tx_Zegal@beta[1] # intercept b1 = model_data_post_tx_Zegal@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = model_data_post_tx_Zegal@beta[3] # moderator (baseline preDAS) b4 = model_data_post_tx_Zegal@beta[4] # Interaction_term b5 = model_data_post_tx_Zegal@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = model_data_post_tx_Zegal@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx_Zegal$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx_Zegal$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(model_data_post_tx_Zegal) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(224)) # p-value for slope is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(224)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(224)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(224)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #Plots for baseline preDAS +/-1SD p_Segal <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("High baseline DAS (+1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme r_Segal <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("Low baseline DAS (-1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme row_Segal <- plot_grid( r_Segal, p_Segal, ncol = 2, align = "hv" ) row_Segal_titled <- plot_grid( ggdraw() + draw_label("Segal et al. (2006)", fontface = "bold", size = 14, hjust = 0.5, vjust = 0.5), row_Segal, ncol = 1, rel_heights = c(0.1, 1) # small space for title, rest for plots ) row_Segal_titled #Murphy et al., 1984 #plotting/probing #extract the coefficients from the model manually b0 = model_data_post_tx_Murphy@beta[1] # intercept b1 = model_data_post_tx_Murphy@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = model_data_post_tx_Murphy@beta[3] # moderator (baseline preDAS) b4 = model_data_post_tx_Murphy@beta[4] # Interaction_term b5 = model_data_post_tx_Murphy@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = model_data_post_tx_Murphy@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx_Murphy$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx_Murphy$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(model_data_post_tx_Murphy) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(33)) # p-value for slope is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(33)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(33)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(33)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #Plots for baseline preDAS +/-1SD p_Murphy <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("High baseline DAS (+1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme r_Murphy <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("Low baseline DAS (-1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme row_Murphy <- plot_grid( r_Murphy, p_Murphy, ncol = 2, align = "hv" ) row_Murphy_titled <- plot_grid( ggdraw() + draw_label("Murphy et al. (1984)", fontface = "bold", size = 14, hjust = 0.5, vjust = 0.5), row_Murphy, ncol = 1, rel_heights = c(0.1, 1) # small space for title, rest for plots ) row_Murphy_titled #Quilty et al., 2008 #plotting/probing #extract the coefficients from the model manually b0 = model_data_post_tx_Quilty@beta[1] # intercept b1 = model_data_post_tx_Quilty@beta[2] # predictor (time) b2 = 0 # moderator (group) b3 = model_data_post_tx_Quilty@beta[3] # moderator (baseline preDAS) b4 = model_data_post_tx_Quilty@beta[4] # Interaction_term b5 = model_data_post_tx_Quilty@beta[5] # Time x baseline preDAS b6 = 0 # Group x baseline preDAS b7 = model_data_post_tx_Quilty@beta[6] # 3-way # identify meaningful values of moderator (Group) CBT <- 1 # CBT ADM <- 2 # ADM High.preDAS <- sd(data_post_tx_Quilty$preDAS, na.rm = T) #Higher preDAS baseline Low.preDAS <- sd(data_post_tx_Quilty$preDAS, na.rm = T)*-1 #Lower preDAS baseline # now calculate simple intercept and slope (for each level of the moderator) ### CBT ### ### High preDAS baseline ## CBT_intL <- b0 + (b2*CBT) + (b3*High.preDAS) + (b6*CBT*High.preDAS) CBT_slpL <- b1 + (b4*CBT) + (b5*High.preDAS) + (b7*CBT*High.preDAS) c(CBT_intL,CBT_slpL) # simple intercept and slope for CBT High preDAS baseline ### Low preDAS baseline ### CBT_intS <- b0 + (b2*CBT) + (b3*Low.preDAS) + (b6*CBT*Low.preDAS) CBT_slpS <- b1 + (b4*CBT) + (b5*Low.preDAS) + (b7*CBT*Low.preDAS) c(CBT_intS,CBT_slpS) # simple intercept and slope for CBT Low preDAS baseline ### ADM ### ### High preDAS baseline ### ADM_intL <- b0 + (b2*ADM) + (b3*High.preDAS) + (b6*ADM*High.preDAS) ADM_slpL <- b1 + (b4*ADM) + (b5*High.preDAS) + (b7*ADM*High.preDAS) c(ADM_intL,ADM_slpL) # simple intercept and slope for ADM High preDAS baseline ### Low preDAS baseline ### ADM_intS <- b0 + (b2*ADM) + (b3*Low.preDAS) + (b6*ADM*Low.preDAS) ADM_slpS <- b1 + (b4*ADM) + (b5*Low.preDAS) + (b7*ADM*Low.preDAS) c(ADM_intS,ADM_slpS) # simple intercept and slope for ADM Low preDAS baseline # to calculate the SEs of the slopes, then we need the (co)variances from the model covM <-vcov(model_data_post_tx_Quilty) var00 = covM[1,1] var11 = covM[2,2] var22 = 0 var33 = covM[3,3] var44 = covM[4,4] var55 = covM[5,5] var66 = 0 var77 = covM[6,6] covar14 = covM[2,4] covar15 = covM[2,5] covar17 = covM[2,6] covar45 = covM[4,5] covar47 = covM[4,6] covar57 = covM[5,6] covar02 = 0 covar03 = covM[1,3] covar06 = 0 covar23 = 0 covar26 = 0 covar36 = 0 # calculate SE, t- and p-value for simple slope for CBT CBT_slpS_se <- sqrt(var11 + ((CBT*CBT)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((CBT*CBT)*(Low.preDAS*Low.preDAS)*var77) + (2*CBT*covar14) + (2*Low.preDAS*covar15) + (2*CBT*Low.preDAS*covar17) + (2*CBT*Low.preDAS*covar45) + (2*Low.preDAS*CBT*CBT*covar47) + (2*CBT*Low.preDAS*Low.preDAS*covar57)) # SE for simple slope for CBT Low preDAS baseline CBT_slpS_t <- CBT_slpS / CBT_slpS_se # t-value for slope CBT_slpS_p <- 2*pt(-abs(CBT_slpS_t),(104)) # p-value for slope is manually calulcated N - k - 1 # CBT Low preDAS baseline c(CBT_intS, CBT_slpS) c(CBT_slpS_se,CBT_slpS_t, CBT_slpS_p) CBT_slpL_se <- sqrt(var11 + (CBT*CBT*var44) + (High.preDAS*High.preDAS*var55) + ((CBT*CBT)*(High.preDAS*High.preDAS)*var77) + (2*CBT*covar14) + (2*High.preDAS*covar15) + ((2*CBT)*High.preDAS*covar17) + ((2*CBT)*High.preDAS*covar45) + (2*High.preDAS*(CBT*CBT)*covar47) + ((2*CBT)*High.preDAS*High.preDAS*covar57)) # SE for simple slope for CBT High preDAS baseline CBT_slpL_t <- CBT_slpL / CBT_slpL_se # t-value for slope CBT_slpL_p <- 2*pt(-abs(CBT_slpL_t),(104)) # p-value for slope # CBT High preDAS baseline c(CBT_intL, CBT_slpL) c(CBT_slpL_se,CBT_slpL_t, CBT_slpL_p) # calculate SE, t- and p-value for simple slope for ADM ADM_slpS_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((Low.preDAS*Low.preDAS)*var55) + ((ADM*ADM)*(Low.preDAS*Low.preDAS)*var77) + ((2*ADM)*covar14) + ((2*Low.preDAS)*covar15) + ((2*ADM)*Low.preDAS*covar17) + ((2*ADM)*Low.preDAS*covar45) + (2*Low.preDAS*(ADM*ADM)*covar47) + (2*(ADM*Low.preDAS)*Low.preDAS*covar57)) # SE for simple slope for ADM Low preDAS baseline ADM_slpS_t <- ADM_slpS / ADM_slpS_se # t-value for slope ADM_slpS_p <- 2*pt(-abs(ADM_slpS_t),(104)) # p-value for slope # ADM Low preDAS baseline c(ADM_intS, ADM_slpS) c(ADM_slpS_se,ADM_slpS_t, ADM_slpS_p) ADM_slpL_se <- sqrt(var11 + ((ADM*ADM)*var44) + ((High.preDAS*High.preDAS)*var55) + ((ADM*ADM)*(High.preDAS*High.preDAS)*var77) + ((2*ADM)*covar14) + ((2*High.preDAS)*covar15) + ((2*ADM)*High.preDAS*covar17) + ((2*ADM)*High.preDAS*covar45) + ((2*High.preDAS)*ADM*ADM*covar47) + ((2*ADM)*(High.preDAS*High.preDAS)*covar57)) # SE for simple slope for ADM High preDAS baseline ADM_slpL_t <- ADM_slpL / ADM_slpL_se # t-value for slope ADM_slpL_p <- 2*pt(-abs(ADM_slpL_t),(104)) # p-value for slope # ADM High preDAS baseline c(ADM_intL, ADM_slpL) c(ADM_slpL_se,ADM_slpL_t, ADM_slpL_p) #to identify the points for low and high levels of preDAS baseline # pick points to plot #CBT High preDAS baseline CBT_post_High <- CBT_intL + CBT_slpL * 1 CBT_base_High <- CBT_intL + CBT_slpL * 0 c(CBT_base_High,CBT_post_High) #CBT Low preDAS baseline CBT_post_Low <- CBT_intS + CBT_slpS * 1 CBT_base_Low <- CBT_intS + CBT_slpS * 0 c(CBT_base_Low,CBT_post_Low) #ADM High preDAS baseline ADM_post_High <- ADM_intL + ADM_slpL * 1 ADM_base_High <- ADM_intL + ADM_slpL * 0 c(ADM_base_High,ADM_post_High) #ADM Low preDAS baseline ADM_post_Low <- ADM_intS + ADM_slpS * 1 ADM_base_Low <- ADM_intS + ADM_slpS * 0 c(ADM_base_Low,ADM_post_Low) IntPoints2Plot <- as.data.frame(matrix(data=NA,nrow=8,ncol=3)) # add value labels to 1st and 2nd columns IntPoints2Plot[,1] <- c("High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT", "High preDAS baseline ADM", "High preDAS baseline CBT","Low preDAS baseline ADM","Low preDAS baseline CBT") IntPoints2Plot[,2] <- c("Baseline","Baseline","Baseline","Baseline", "Post-treatment","Post-treatment","Post-treatment","Post-treatment") # add the values to the 3rd column...be sure the values correspond to the value labels IntPoints2Plot[1,3] <- ADM_base_High IntPoints2Plot[2,3] <- CBT_base_High IntPoints2Plot[3,3] <- ADM_base_Low IntPoints2Plot[4,3] <- CBT_base_Low IntPoints2Plot[5,3] <- ADM_post_High IntPoints2Plot[6,3] <- CBT_post_High IntPoints2Plot[7,3] <- ADM_post_Low IntPoints2Plot[8,3] <- CBT_post_Low # add column names to correspond to variable names names(IntPoints2Plot) <- c("Mod","Time","Outcome") # change first two columns to factors IntPoints2Plot$Mod <- as.factor(IntPoints2Plot$Mod) IntPoints2Plot$Time <- as.factor(IntPoints2Plot$Time) #add factor labels to first two columns IntPoints2Plot$Mod <- with(IntPoints2Plot, factor(Mod, levels = rev(levels(Mod)))) IntPoints2Plot$Time <- with(IntPoints2Plot, factor(Time, levels = rev(levels(Time)))) #check matrix IntPoints2Plot #reorder estimates from matrix into dataframe for plotting tx <- factor(c("ADM","CBT","ADM","CBT")) time_point <- factor(c("Baseline","Baseline","Post-treatment","Post-treatment")) High_preDAS_baseline <- c(IntPoints2Plot$Outcome[1:2],IntPoints2Plot$Outcome[5:6]) Low_preDAS_baseline <- c(IntPoints2Plot$Outcome[3:4],IntPoints2Plot$Outcome[7:8]) IntPoints_preDAS_baseline_2Plot <- data.frame(tx, time_point, High_preDAS_baseline, Low_preDAS_baseline) #Plots for baseline preDAS +/-1SD p_Quilty <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=High_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("High baseline DAS (+1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme r_Quilty <- ggplot(data=IntPoints_preDAS_baseline_2Plot, aes(x=time_point, y=Low_preDAS_baseline, group=tx)) + geom_line(aes(linetype=tx))+ geom_point(size = 1.5) + ggtitle("Low baseline DAS (-1SD)")+ theme(plot.title = element_text(hjust = 0.5))+ labs(x = "", y = "HAMD scores")+ common_y+ expand_limits(y=c(-1.07, 1.06)) + scale_x_discrete(expand = c(0, 0.15)) + mytheme row_Quilty <- plot_grid( r_Quilty, p_Quilty, ncol = 2, align = "hv" ) row_Quilty_titled <- plot_grid( ggdraw() + draw_label("Quilty et al. (2008)", fontface = "bold", size = 14, hjust = 0.5, vjust = 0.5), row_Quilty, ncol = 1, rel_heights = c(0.1, 1) # small space for title, rest for plots ) row_Quilty_titled #combined plot final_moderation_plot <- cowplot::plot_grid( row_DeRubeis_titled, row_Elkin_titled, row_Segal_titled, row_Murphy_titled, row_Quilty_titled, ncol = 1, align = "v" ) final_moderation_plot ggsave("Figure_moderation_by_study.tiff", final_moderation_plot, width = 8, height = 14, dpi = 300) ##### #Explorative leave-one-out moderator sensitivity analysis ##### # List of studies to loop over studies <- unique(data_post_tx$studyid) # Create a list to store all results loso_results <- list() # Loop through each study for (study in studies) { cat("\n\n--- Excluding study:", study, "---\n") # Exclude current study data_loso <- filter(data_post_tx, studyid != study) # Fit the model (exact template) HAMD_post <- lmer(HAMD ~ Time * preDAS_c + Interaction_term * preDAS_c + (1 | unique_pt_id) + (1 | studyid), data = data_loso) # Summary summary_HAMD_post <- summary(HAMD_post) print(summary_HAMD_post) # Bootstrapped 95% CIs HAMD_post_CIs <- confint(HAMD_post, oldNames = FALSE, method = "boot", nsim = 5000, parallel = "multicore", ncpus = 32) print(HAMD_post_CIs) # Type III F-tests p_values_HAMD_post <- Anova(HAMD_post, test = "F", type = 3) print(p_values_HAMD_post) # Store all results loso_results[[as.character(study)]] <- list( summary = summary_HAMD_post, CIs = HAMD_post_CIs, p_values = p_values_HAMD_post ) } # pseudo standardized betas for (study in studies) { cat("\n\n--- Excluding study:", study, "---\n") # Exclude current study data_loso <- filter(data_post_tx, studyid != study) # Fit the model (exact template) HAMD_post <- lmer(zHAMD ~ Time * zpreDAS + Interaction_term * zpreDAS + (1 | unique_pt_id) + (1 | studyid), data = data_loso) # Summary summary_HAMD_post <- summary(HAMD_post) print(summary_HAMD_post) }