setwd("C:/Users/SSW/Desktop/Trauma_QoL")
load("QoL.RData")
library(knitr)
library(lavaan)
library(dplyr)
setwd("C:/Users/SSW/Desktop/Trauma_QoL")

library(foreign)

DF <- as.data.frame(read.spss("Trauma article_Bryan.sav"))

DF <- mutate(DF, interactionTerm = TSCTOT_M * PBIM_War)
library(lavaan)

physicalQuality <- '
# outcome model 
PHYS_T2 ~ c*TSCTOT_M + b1*ISELTOT_M_T2 + b2*RSUPTOT_M_T2 + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a1*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D
RSUPTOT_M_T2 ~ a2*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D

# indirect effects (IDE)
med1IDE  := a1*b1
med2IDE  := a2*b2
sumIDE := (a1*b1) + (a2*b2)

# total effect
total := c + (a1*b1) + (a2*b2)
ISELTOT_M_T2 ~~ RSUPTOT_M_T2 # model correlation between mediators
'
physical.fit<-sem(physicalQuality, data=DF, se="boot", bootstrap=5000)

summary(physical.fit)
psychQuality <- '
# outcome model 
PSYCH_T2 ~ c*TSCTOT_M + b1*ISELTOT_M_T2 + b2*RSUPTOT_M_T2 + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a1*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D
RSUPTOT_M_T2 ~ a2*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D

# indirect effects (IDE)
med1IDE  := a1*b1
med2IDE  := a2*b2
sumIDE := (a1*b1) + (a2*b2)

# total effect
total := c + (a1*b1) + (a2*b2)
ISELTOT_M_T2 ~~ RSUPTOT_M_T2 # model correlation between mediators
'
psych.fit<-sem(psychQuality, data=DF, se="boot", bootstrap=5000)

summary(psych.fit)
socialQuality <- '
# outcome model 
SOCIAL_T2 ~ c*TSCTOT_M + b1*ISELTOT_M_T2 + b2*RSUPTOT_M_T2 + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a1*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D
RSUPTOT_M_T2 ~ a2*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D

# indirect effects (IDE)
med1IDE  := a1*b1
med2IDE  := a2*b2
sumIDE := (a1*b1) + (a2*b2)

# total effect
total := c + (a1*b1) + (a2*b2)
ISELTOT_M_T2 ~~ RSUPTOT_M_T2 # model correlation between mediators
'
social.fit<-sem(socialQuality, data=DF, se="boot", bootstrap=5000)

summary(social.fit)
environQuality <- '
# outcome model 
ENVIR_T2 ~ c*TSCTOT_M + b1*ISELTOT_M_T2 + b2*RSUPTOT_M_T2 + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a1*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D
RSUPTOT_M_T2 ~ a2*TSCTOT_M + qa5 + Race_r + EDU_r + DUAL_D

# indirect effects (IDE)
med1IDE  := a1*b1
med2IDE  := a2*b2
sumIDE := (a1*b1) + (a2*b2)

# total effect
total := c + (a1*b1) + (a2*b2)
ISELTOT_M_T2 ~~ RSUPTOT_M_T2 # model correlation between mediators
'
environ.fit<-sem(environQuality, data=DF, se="boot", bootstrap=5000)

summary(environ.fit)
physicalModMed <- '
# outcome model 
PHYS_T2 ~ c*TSCTOT_M + b*ISELTOT_M_T2 + PBIM_War + cw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a*TSCTOT_M + PBIM_War + aw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

  ## physical.indirect and total effects, conditional on W == 0
  physical.indirect0 := a*b            # + 0*aw*b
  total0 := physical.indirect0 + c     # + 0*cw

  ## physical.indirect and total effects, conditional on W == 6
  physical.indirect6 := physical.indirect0 + 6*aw*b
  total6 := physical.indirect6 + c + 6*cw

  ## physical.indirect and total effects, conditional on W == 14
  physical.indirect14 := physical.indirect0 + 14*aw*b
  total14 := physical.indirect14 + c + 14*cw

  ## physical.indirect and total effects, conditional on W == 23
  physical.indirect23 := physical.indirect0 + 23*aw*b
  total23 := physical.indirect23 + c + 23*cw

  ## physical.indirect and total effects, conditional on W == 32
  physical.indirect32 := physical.indirect0 + 32*aw*b
  total32 := physical.indirect32 + c + 32*cw

  ## physical.indirect and total effects, conditional on W == 36
  physical.indirect36 := physical.indirect0 + 36*aw*b
  total36 := physical.indirect36 + c + 36*cw

'
physicalModMed.fit<-sem(physicalModMed, data=DF, se="boot", bootstrap=5000)

summary(physicalModMed.fit)

physicalParam <- parameterEstimates(physicalModMed.fit)
physicalParam.reduced <- physicalParam %>%
  filter(grepl("indirect", lhs))

physicalIndirects <- select(physicalParam.reduced, label, est, se, pvalue, ci.lower, ci.upper)
psychModMed <- '
# outcome model 
PSYCH_T2 ~ c*TSCTOT_M + b*ISELTOT_M_T2 + PBIM_War + cw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a*TSCTOT_M + PBIM_War + aw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

  ## psych.indirect and total effects, conditional on W == 0
  psych.indirect0 := a*b            # + 0*aw*b
  total0 := psych.indirect0 + c     # + 0*cw

  ## psych.indirect and total effects, conditional on W == 6
  psych.indirect6 := psych.indirect0 + 6*aw*b
  total6 := psych.indirect6 + c + 6*cw

  ## psych.indirect and total effects, conditional on W == 14
  psych.indirect14 := psych.indirect0 + 14*aw*b
  total14 := psych.indirect14 + c + 14*cw

  ## psych.indirect and total effects, conditional on W == 23
  psych.indirect23 := psych.indirect0 + 23*aw*b
  total23 := psych.indirect23 + c + 23*cw

  ## psych.indirect and total effects, conditional on W == 32
  psych.indirect32 := psych.indirect0 + 32*aw*b
  total32 := psych.indirect32 + c + 32*cw

  ## psych.indirect and total effects, conditional on W == 36
  psych.indirect36 := psych.indirect0 + 36*aw*b
  total36 := psych.indirect36 + c + 36*cw

'
psychModMed.fit<-sem(psychModMed, data=DF, se="boot", bootstrap=5000)

summary(psychModMed.fit)

psychParam <- parameterEstimates(psychModMed.fit)
psychParam.reduced <- psychParam %>%
  filter(grepl("indirect", lhs))

psychIndirects <- select(psychParam.reduced, label, est, se, pvalue, ci.lower, ci.upper)
socialModMed <- '
# outcome model 
SOCIAL_T2 ~ c*TSCTOT_M + b*ISELTOT_M_T2 + PBIM_War + cw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a*TSCTOT_M + PBIM_War + aw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

  ## social.indirect and total effects, conditional on W == 0
  social.indirect0 := a*b            # + 0*aw*b
  total0 := social.indirect0 + c     # + 0*cw

  ## social.indirect and total effects, conditional on W == 6
  social.indirect6 := social.indirect0 + 6*aw*b
  total6 := social.indirect6 + c + 6*cw

  ## social.indirect and total effects, conditional on W == 14
  social.indirect14 := social.indirect0 + 14*aw*b
  total14 := social.indirect14 + c + 14*cw

  ## social.indirect and total effects, conditional on W == 23
  social.indirect23 := social.indirect0 + 23*aw*b
  total23 := social.indirect23 + c + 23*cw

  ## social.indirect and total effects, conditional on W == 32
  social.indirect32 := social.indirect0 + 32*aw*b
  total32 := social.indirect32 + c + 32*cw

  ## social.indirect and total effects, conditional on W == 36
  social.indirect36 := social.indirect0 + 36*aw*b
  total36 := social.indirect36 + c + 36*cw

'
socialModMed.fit<-sem(socialModMed, data=DF, se="boot", bootstrap=5000)

summary(socialModMed.fit)

socialParam <- parameterEstimates(socialModMed.fit)
socialParam.reduced <- socialParam %>%
  filter(grepl("indirect", lhs))

socialIndirects <- select(socialParam.reduced, label, est, se, pvalue, ci.lower, ci.upper)
environModMed <- '
# outcome model 
ENVIR_T2 ~ c*TSCTOT_M + b*ISELTOT_M_T2 + PBIM_War + cw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

# mediator models
ISELTOT_M_T2 ~ a*TSCTOT_M + PBIM_War + aw*interactionTerm + qa5 + Race_r + EDU_r + DUAL_D

  ## environ.indirect and total effects, conditional on W == 0
  environ.indirect0 := a*b            # + 0*aw*b
  total0 := environ.indirect0 + c     # + 0*cw

  ## environ.indirect and total effects, conditional on W == 6
  environ.indirect6 := environ.indirect0 + 6*aw*b
  total6 := environ.indirect6 + c + 6*cw

  ## environ.indirect and total effects, conditional on W == 14
  environ.indirect14 := environ.indirect0 + 14*aw*b
  total14 := environ.indirect14 + c + 14*cw

  ## environ.indirect and total effects, conditional on W == 23
  environ.indirect23 := environ.indirect0 + 23*aw*b
  total23 := environ.indirect23 + c + 23*cw

  ## environ.indirect and total effects, conditional on W == 32
  environ.indirect32 := environ.indirect0 + 32*aw*b
  total32 := environ.indirect32 + c + 32*cw

  ## environ.indirect and total effects, conditional on W == 36
  environ.indirect36 := environ.indirect0 + 36*aw*b
  total36 := environ.indirect36 + c + 36*cw

'
environModMed.fit<-sem(environModMed, data=DF, se="boot", bootstrap=5000)

summary(environModMed.fit)

environParam <- parameterEstimates(environModMed.fit)
environParam.reduced <- environParam %>%
  filter(grepl("indirect", lhs))

environIndirects <- select(environParam.reduced, label, est, se, pvalue, ci.lower, ci.upper)
modMedValues <- rbind(physicalIndirects, psychIndirects, socialIndirects, environIndirects)

modMedValues[,-1] <-round(modMedValues[,-1],3)

#setwd("C:/Users/SSW/Desktop/Trauma_QoL")
#save.image(file="QoL.RData")

Indirect effects of trauma symptoms on QoL measures via interpersonal & recovery support

Physical

label est se z pvalue ci.lower ci.upper
Indirect via interpersonal support -0.091 0.025 -3.620 0.000 -0.144 -0.047
Indirect via recovery support 0.010 0.010 1.012 0.312 -0.006 0.032
Total effect -0.081 0.022 -3.726 0.000 -0.127 -0.042

Psychological

label est se z pvalue ci.lower ci.upper
Indirect via interpersonal support -0.105 0.027 -3.918 0.00 -0.162 -0.056
Indirect via recovery support -0.015 0.011 -1.371 0.17 -0.040 0.001
Total effect -0.119 0.028 -4.197 0.00 -0.178 -0.067

Social

label est se z pvalue ci.lower ci.upper
Indirect via interpersonal support -0.087 0.027 -3.276 0.001 -0.146 -0.041
Indirect via recovery support -0.033 0.015 -2.142 0.032 -0.066 -0.006
Total effect -0.120 0.031 -3.876 0.000 -0.185 -0.063

Environmental

label est se z pvalue ci.lower ci.upper
Indirect via interpersonal support -0.092 0.023 -3.994 0.000 -0.141 -0.051
Indirect via recovery support -0.009 0.009 -1.038 0.299 -0.030 0.005
Total effect -0.101 0.024 -4.231 0.000 -0.151 -0.057

Conditional indirect effects of trauma symptoms on interpersonal support at low and high levels of maternal warmth.

label est se pvalue ci.lower ci.upper
Physical at W=6 -0.030 0.030 0.320 -0.094 0.024
Physical at W=14 -0.047 0.023 0.046 -0.100 -0.008
Physical at W=23 -0.066 0.021 0.002 -0.113 -0.031
Physical at W=32 -0.085 0.025 0.001 -0.139 -0.042
Physical at W=36 -0.094 0.028 0.001 -0.155 -0.046
Psychological at W=6 -0.050 0.048 0.292 -0.145 0.040
Psychological at W=14 -0.079 0.035 0.024 -0.151 -0.014
Psychological at W=23 -0.112 0.028 0.000 -0.170 -0.062
Psychological at W=32 -0.145 0.033 0.000 -0.214 -0.087
Psychological at W=36 -0.159 0.038 0.000 -0.240 -0.091
Social at W=6 -0.060 0.048 0.212 -0.164 0.029
Social at W=14 -0.086 0.036 0.017 -0.163 -0.021
Social at W=23 -0.115 0.029 0.000 -0.175 -0.065
Social at W=32 -0.144 0.033 0.000 -0.213 -0.084
Social at W=36 -0.156 0.038 0.000 -0.236 -0.088
Environmental at W=6 -0.037 0.035 0.290 -0.113 0.029
Environmental at W=14 -0.059 0.027 0.026 -0.116 -0.011
Environmental at W=23 -0.084 0.022 0.000 -0.129 -0.044
Environmental at W=32 -0.108 0.026 0.000 -0.163 -0.062
Environmental at W=36 -0.119 0.030 0.000 -0.181 -0.066