library(lavaan)
## Warning: package 'lavaan' was built under R version 4.0.4
## This is lavaan 0.6-7
## lavaan is BETA software! Please report any bugs.
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Libraries
#library(rlang)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v stringr 1.4.0
## v tidyr   1.1.2     v forcats 0.5.0
## v readr   1.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(dplyr)
library(knitr)
library(lavaan)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:lavaan':
## 
##     cor2cov
library(MBESS)
## Warning: package 'MBESS' was built under R version 4.0.4
## 
## Attaching package: 'MBESS'
## The following object is masked from 'package:psych':
## 
##     cor2cov
## The following object is masked from 'package:lavaan':
## 
##     cor2cov
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
library(mediation)
## Warning: package 'mediation' was built under R version 4.0.4
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Loading required package: mvtnorm
## Loading required package: sandwich
## mediation: Causal Mediation Analysis
## Version: 4.5.0
## 
## Attaching package: 'mediation'
## The following object is masked from 'package:psych':
## 
##     mediate
library(multilevel)
## Warning: package 'multilevel' was built under R version 4.0.4
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(car)
## Loading required package: carData
## Registered S3 methods overwritten by 'car':
##   method                          from
##   influence.merMod                lme4
##   cooks.distance.influence.merMod lme4
##   dfbeta.influence.merMod         lme4
##   dfbetas.influence.merMod        lme4
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:purrr':
## 
##     some
## The following object is masked from 'package:dplyr':
## 
##     recode
library(gtsummary)
## Warning: package 'gtsummary' was built under R version 4.0.4
## 
## Attaching package: 'gtsummary'
## The following object is masked from 'package:MASS':
## 
##     select
library(flextable)
## Warning: package 'flextable' was built under R version 4.0.4
## 
## Attaching package: 'flextable'
## The following object is masked from 'package:gtsummary':
## 
##     as_flextable
## The following object is masked from 'package:purrr':
## 
##     compose
library(skimr)
## Warning: package 'skimr' was built under R version 4.0.5
library(DiagrammeR )
library(processR)
## Warning: package 'processR' was built under R version 4.0.4
## This version of bslib is designed to work with shiny version 1.5.0.9007 or higher.
## 
## Attaching package: 'processR'
## The following objects are masked from 'package:car':
## 
##     densityPlot, qqPlot
## The following object is masked from 'package:psych':
## 
##     corPlot
library(lavaan)
library(lavaan)
library(semPlot)
## Warning: package 'semPlot' was built under R version 4.0.4
library(OpenMx)
## Warning: package 'OpenMx' was built under R version 4.0.4
## 
## Attaching package: 'OpenMx'
## The following objects are masked from 'package:Matrix':
## 
##     %&%, expm
## The following object is masked from 'package:psych':
## 
##     tr
## The following object is masked from 'package:lavaan':
## 
##     vech
library(tidyverse)
library(knitr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following objects are masked from 'package:flextable':
## 
##     as_image, footnote
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2

https://www.rpubs.com/cardiomoon/481347 https://ademos.people.uic.edu/Chapter15.html#12_mediation

LEEP$seizure<-Recode(LEEP$Seizure_Frequency, recodes="'I have not had a seizure in the past 12 months'=0;else=1",as.numeric=T)

LEEP$PHQ_2<-Recode(LEEP$PHQ_2, recodes="'Depression'=1;'No Depression'=0",as.numeric=T)

LEEP$severe<-as.numeric(LEEP$severe)
LEEP<-LEEP %>% 
  rename(QOLIE=means)
# including a contrast in the model
multipleMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 26 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   QOLIE ~                                             
##     severe    (b1)    0.155    0.024    6.335    0.000
##     seizure   (b2)    0.557    0.079    7.017    0.000
##     PHQ_2      (c)    0.341    0.071    4.771    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .QOLIE             0.297    0.026   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.129    0.035    3.676    0.000
##     indirect2         0.145    0.038    3.824    0.000
##     total             0.615    0.084    7.301    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 8)

constrainedMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## noConstrFit  0 1684.5 1716.5 0.0000                              
## constrFit    1 1682.6 1711.1 0.1032    0.10317       1     0.7481
contrastsMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 26 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   QOLIE ~                                             
##     severe    (b1)    0.155    0.024    6.335    0.000
##     seizure   (b2)    0.557    0.079    7.017    0.000
##     PHQ_2      (c)    0.341    0.071    4.771    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .QOLIE             0.297    0.026   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.129    0.035    3.676    0.000
##     indirect2         0.145    0.038    3.824    0.000
##     contrast          0.015    0.048    0.321    0.748
##     total             0.615    0.084    7.301    0.000
# including a contrast in the model
multipleMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   PCS_m ~                                             
##     severe    (b1)   -2.321    0.387   -5.998    0.000
##     seizure   (b2)   -1.582    1.257   -1.258    0.208
##     PHQ_2      (c)   -4.973    1.131   -4.396    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .PCS_m            74.440    6.516   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -1.939    0.538   -3.607    0.000
##     indirect2        -0.411    0.339   -1.213    0.225
##     total            -7.323    1.171   -6.256    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 8)

constrainedMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)  
## noConstrFit  0 3126.2 3158.2 0.0000                                
## constrFit    1 3130.2 3158.7 6.0528     6.0528       1    0.01388 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
contrastsMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   PCS_m ~                                             
##     severe    (b1)   -2.321    0.387   -5.998    0.000
##     seizure   (b2)   -1.582    1.257   -1.258    0.208
##     PHQ_2      (c)   -4.973    1.131   -4.396    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .PCS_m            74.440    6.516   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -1.939    0.538   -3.607    0.000
##     indirect2        -0.411    0.339   -1.213    0.225
##     contrast          1.528    0.674    2.267    0.023
##     total            -7.323    1.171   -6.256    0.000
# including a contrast in the model
multipleMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 44 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   MCS_m ~                                             
##     severe    (b1)   -1.274    0.397   -3.210    0.001
##     seizure   (b2)   -1.180    1.290   -0.915    0.360
##     PHQ_2      (c)   -9.665    1.160   -8.329    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .MCS_m            78.321    6.856   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -1.064    0.407   -2.616    0.009
##     indirect2        -0.306    0.342   -0.897    0.370
##     total           -11.036    1.131   -9.754    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 10)

constrainedMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## noConstrFit  0 3139.4 3171.5 0.0000                              
## constrFit    1 3139.1 3167.6 1.7096     1.7096       1      0.191
contrastsMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * PHQ_2
severe ~ a1 * PHQ_2
seizure ~ a2 * PHQ_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 44 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   MCS_m ~                                             
##     severe    (b1)   -1.274    0.397   -3.210    0.001
##     seizure   (b2)   -1.180    1.290   -0.915    0.360
##     PHQ_2      (c)   -9.665    1.160   -8.329    0.000
##   severe ~                                            
##     PHQ_2     (a1)    0.835    0.185    4.514    0.000
##   seizure ~                                           
##     PHQ_2     (a2)    0.260    0.057    4.560    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.258    0.045    5.699    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .MCS_m            78.321    6.856   11.424    0.000
##    .severe            2.220    0.194   11.424    0.000
##    .seizure           0.210    0.018   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -1.064    0.407   -2.616    0.009
##     indirect2        -0.306    0.342   -0.897    0.370
##     contrast          0.758    0.595    1.274    0.203
##     total           -11.036    1.131   -9.754    0.000
# including a contrast in the model
multipleMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 24 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   QOLIE ~                                             
##     severe    (b1)    0.150    0.025    6.101    0.000
##     seizure   (b2)    0.588    0.078    7.500    0.000
##     GAD2_2     (c)    0.343    0.071    4.844    0.000
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .QOLIE             0.296    0.026   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.129    0.035    3.686    0.000
##     indirect2         0.115    0.037    3.060    0.002
##     total             0.587    0.085    6.874    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 8)

estimatesTable(fit)
##   Variables Predictors label    B   SE    z       p    ß
## 1     QOLIE     severe    b1 0.15 0.02 6.10 < 0.001 0.31
## 2     QOLIE    seizure    b2 0.59 0.08 7.50 < 0.001 0.38
## 3     QOLIE     GAD2_2     c 0.34 0.07 4.84 < 0.001 0.23
## 4    severe     GAD2_2    a1 0.86 0.19 4.63 < 0.001 0.28
## 5   seizure     GAD2_2    a2 0.19 0.06 3.35   0.001 0.20
constrainedMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## noConstrFit  0 1689.0 1721.0 0.0000                              
## constrFit    1 1687.1 1715.6 0.0921   0.092055       1     0.7616
contrastsMediation <- '
QOLIE ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 24 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   QOLIE ~                                             
##     severe    (b1)    0.150    0.025    6.101    0.000
##     seizure   (b2)    0.588    0.078    7.500    0.000
##     GAD2_2     (c)    0.343    0.071    4.844    0.000
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .QOLIE             0.296    0.026   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.129    0.035    3.686    0.000
##     indirect2         0.115    0.037    3.060    0.002
##     contrast         -0.014    0.046   -0.304    0.761
##     total             0.587    0.085    6.874    0.000
# including a contrast in the model
multipleMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   PCS_m ~                                             
##     severe    (b1)   -2.337    0.396   -5.910    0.000
##     seizure   (b2)   -2.158    1.261   -1.712    0.087
##     GAD2_2     (c)   -3.849    1.140   -3.377    0.001
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .PCS_m            76.603    6.706   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -2.003    0.550   -3.642    0.000
##     indirect2        -0.420    0.276   -1.524    0.127
##     total            -6.272    1.198   -5.238    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 8)

constrainedMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)   
## noConstrFit  0 3138.8 3170.9 0.0000                                 
## constrFit    1 3144.2 3172.7 7.4527     7.4527       1   0.006334 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
contrastsMediation <- '
PCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   PCS_m ~                                             
##     severe    (b1)   -2.337    0.396   -5.910    0.000
##     seizure   (b2)   -2.158    1.261   -1.712    0.087
##     GAD2_2     (c)   -3.849    1.140   -3.377    0.001
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .PCS_m            76.603    6.706   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -2.003    0.550   -3.642    0.000
##     indirect2        -0.420    0.276   -1.524    0.127
##     contrast          1.582    0.633    2.499    0.012
##     total            -6.272    1.198   -5.238    0.000
# including a contrast in the model
multipleMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'
fit <- sem(model = multipleMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 44 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   MCS_m ~                                             
##     severe    (b1)   -1.114    0.395   -2.822    0.005
##     seizure   (b2)   -2.013    1.258   -1.601    0.109
##     GAD2_2     (c)  -10.068    1.137   -8.854    0.000
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .MCS_m            76.238    6.674   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -0.954    0.396   -2.409    0.016
##     indirect2        -0.392    0.272   -1.445    0.149
##     total           -11.414    1.122  -10.176    0.000
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")

semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'tree')

ly<-matrix(c(1, 1,
    0.33, 2,
    0.33, 0,
   -1,    1,
   -1,   -1), ncol=2, byrow=TRUE)

semPaths(fit, whatLabels = 'std', nCharNodes = 0, layout = ly, 
   edge.label.cex = 1.2, residuals=FALSE, sizeMan=8)

semPaths(fit, what = "std",layout=ly, residuals = FALSE, 
   nCharNodes = 0, edge.label.cex = 1.2, sizeMan = 10)

constrainedMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
# covariances
severe ~~ seizure
# constrain
indirect1 == indirect2
'
noConstrFit <- sem(model = multipleMediation, data = LEEP)
constrFit <- sem(model = constrainedMediation, data = LEEP)
anova(noConstrFit, constrFit)
## Chi-Squared Difference Test
## 
##             Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## noConstrFit  0 3137.5 3169.6 0.0000                              
## constrFit    1 3136.7 3165.2 1.1665     1.1665       1     0.2801
contrastsMediation <- '
MCS_m ~ b1 * severe + b2 * seizure + c * GAD2_2
severe ~ a1 * GAD2_2
seizure ~ a2 * GAD2_2
indirect1 := a1 * b1
indirect2 := a2 * b2
contrast := indirect2-indirect1
total := c + (a1 * b1) + (a2 * b2)
severe ~~ seizure
'

fit <- sem(model = contrastsMediation, data = LEEP)
summary(fit)
## lavaan 0.6-7 ended normally after 44 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                          9
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   MCS_m ~                                             
##     severe    (b1)   -1.114    0.395   -2.822    0.005
##     seizure   (b2)   -2.013    1.258   -1.601    0.109
##     GAD2_2     (c)  -10.068    1.137   -8.854    0.000
##   severe ~                                            
##     GAD2_2    (a1)    0.857    0.185    4.626    0.000
##   seizure ~                                           
##     GAD2_2    (a2)    0.195    0.058    3.352    0.001
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.270    0.046    5.863    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .MCS_m            76.238    6.674   11.424    0.000
##    .severe            2.212    0.194   11.424    0.000
##    .seizure           0.218    0.019   11.424    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -0.954    0.396   -2.409    0.016
##     indirect2        -0.392    0.272   -1.445    0.149
##     contrast          0.562    0.526    1.069    0.285
##     total           -11.414    1.122  -10.176    0.000
LEEP$seizure<-Recode(LEEP$Seizure_Frequency, recodes="'I have not had a seizure in the past 12 months'=0;else=1",as.numeric=T)

myModel <- '
PCS_m ~ b1 * severe + b2 * seizure + c1 * PHQ_2 + c2 * GAD2_2 + c3 * RSES
severe ~ a1 * PHQ_2 + a2 * GAD2_2 + a5 * RSES
seizure ~ a3 * PHQ_2 + a4 * GAD2_2 + a6 * RSES
#indirect effects
indirect1 := a1 * b1
indirect2 := a3 * b2
indirect3 := a2 * b1
indirect4 := a4 * b2
# contrasts
con1 := a1 * b1 - a3 * b2
con2 := a2 * b1 - a4 * b2
con3 := (a1-a2) * b1
con4 := (a3-a4) * b2
# total effect
total1 := c1 + (a1 * b1) + (a3 * b2)
total2 := c2 + (a2 * b1) + (a4 * b2)
total3 := c3 + (a5 * b1) + (a6 * b2)
# covariates
severe ~~ seizure
'

fit <- sem(myModel, 
    data=LEEP, 
    se = "bootstrap", 
    bootstrap = 50)

summary(fit)
## lavaan 0.6-7 ended normally after 61 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         15
##                                                       
##   Number of observations                           261
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws               50
##   Number of successful bootstrap draws              50
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   PCS_m ~                                             
##     severe    (b1)   -2.224    0.392   -5.680    0.000
##     seizure   (b2)   -1.574    1.394   -1.129    0.259
##     PHQ_2     (c1)   -4.064    1.057   -3.844    0.000
##     GAD2_2    (c2)   -1.990    1.286   -1.548    0.122
##     RSES      (c3)   -0.024    0.197   -0.120    0.905
##   severe ~                                            
##     PHQ_2     (a1)    0.577    0.222    2.600    0.009
##     GAD2_2    (a2)    0.622    0.226    2.755    0.006
##     RSES      (a5)    0.045    0.030    1.520    0.129
##   seizure ~                                           
##     PHQ_2     (a3)    0.207    0.065    3.178    0.001
##     GAD2_2    (a4)    0.072    0.079    0.907    0.365
##     RSES      (a6)   -0.011    0.009   -1.249    0.212
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .severe ~~                                           
##    .seizure           0.254    0.039    6.499    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .PCS_m            73.754    7.837    9.411    0.000
##    .severe            2.138    0.141   15.132    0.000
##    .seizure           0.208    0.011   18.989    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1        -1.283    0.569   -2.255    0.024
##     indirect2        -0.327    0.341   -0.959    0.338
##     indirect3        -1.383    0.551   -2.510    0.012
##     indirect4        -0.113    0.158   -0.714    0.475
##     con1             -0.956    0.678   -1.410    0.159
##     con2             -1.270    0.559   -2.271    0.023
##     con3              0.100    0.836    0.119    0.905
##     con4             -0.214    0.373   -0.574    0.566
##     total1           -5.674    1.217   -4.662    0.000
##     total2           -3.485    1.407   -2.478    0.013
##     total3           -0.106    0.180   -0.587    0.557
boot.fit <- parameterEstimates(fit, boot.ci.type="bca.simple")
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints

## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
semPaths(fit, what='std', nCharNodes=6, sizeMan=10,
         edge.label.cex=1.25, curvePivot = F, fade=FALSE,  layout = 'circle')