library(tidyverse)
library(knitr)
library(stargazer)
library(dplyr)
library(tibble)
library(Hmisc)
library(psych)
library(lavaan)
library(mediation)
#setwd
setwd("C:/Users/C00252837/OneDrive - University of Louisiana Lafayette/ACE TAST FORCE_RESEARCH COMMITTEE/Manuscipt_ACEs and SSE via Att Insecurity/DATA")

#------------Read in Data
data<-read.csv("SCALE SCORES_2018-10-7.csv", header = T)
#count(data)
#names(select(data, 1:10))
# create a male dummy variable
data$male <- ifelse(data$SEX == "male", 1, 0)

# create a female dummy variable
data$female <- ifelse(data$SEX == "female", 1, 0)
# create a White dummy variable
data$White <- ifelse(data$Ethnic.Heritage == "White", 1, 0)

# create a Black dummy variable
data$Black <- ifelse(data$Ethnic.Heritage == "Black", 1, 0)

# create an Asian dummy variable
data$Asian <- ifelse(data$Ethnic.Heritage == "Asian", 1, 0)

# create a Hispanic dummy variable
data$Hispanic <- ifelse(data$Ethnic.Heritage == "Hispanic", 1, 0)

# create a Native American dummy variable
data$Native.American <- ifelse(data$Ethnic.Heritage == "Native American", 1, 0)

# create a Multicultural/Other dummy variable
data$Multicultural.Other <- ifelse(data$Ethnic.Heritage == "Multicultural/Other", 1, 0)
# create a single dummy variable
data$single <- ifelse(data$Relationship.status == "single", 1, 0)

# create an "in a relationship" dummy variable
data$in.a.relationship <- ifelse(data$Relationship.status == "in a relationship", 1, 0)
names(data)
##  [1] "ID"                                                                                                                                                                                                                
##  [2] "What.is.your.age."                                                                                                                                                                                                 
##  [3] "SEX"                                                                                                                                                                                                               
##  [4] "Ethnic.Heritage.2"                                                                                                                                                                                                 
##  [5] "Relationship.status"                                                                                                                                                                                               
##  [6] "Ethnic.Heritage"                                                                                                                                                                                                   
##  [7] "ACEs"                                                                                                                                                                                                              
##  [8] "ACE.Item.3.Sexual.Abuse"                                                                                                                                                                                           
##  [9] "PCL.5.SUM"                                                                                                                                                                                                         
## [10] "LEC.5.Happened.to.me"                                                                                                                                                                                              
## [11] "LEC.5.Witnessed.it"                                                                                                                                                                                                
## [12] "LEC.5.Learned.about.it"                                                                                                                                                                                            
## [13] "RSQ.Attachment.Anxiety"                                                                                                                                                                                            
## [14] "RSQ.Attachment.Avoidance"                                                                                                                                                                                          
## [15] "CHIPS.SUM"                                                                                                                                                                                                         
## [16] "BHS1.Respond.to.the.following.questions.using.this.scale...1...daily.or.near.daily..2...3.4.days.a.week..3...1.2.days.a.week..4...1.3.days.a.month..5...Once.a.month.to.once.a.year..6...Almost.never...........1."
## [17] "Migraine.Diagnosis"                                                                                                                                                                                                
## [18] "DERS.SUM"                                                                                                                                                                                                          
## [19] "DERS_Nonaccept"                                                                                                                                                                                                    
## [20] "DERS_Goals"                                                                                                                                                                                                        
## [21] "DERS_Impuls"                                                                                                                                                                                                       
## [22] "DERS_Aware"                                                                                                                                                                                                        
## [23] "DERS_Strategies"                                                                                                                                                                                                   
## [24] "DERS_Clarity"                                                                                                                                                                                                      
## [25] "DASS.Depression"                                                                                                                                                                                                   
## [26] "DASS.Anxiety"                                                                                                                                                                                                      
## [27] "DASS.Stress"                                                                                                                                                                                                       
## [28] "SSEI.SF.SUM"                                                                                                                                                                                                       
## [29] "SSEI.SF.Skill...Experience"                                                                                                                                                                                        
## [30] "SSEI.SF.Attractiveness"                                                                                                                                                                                            
## [31] "SSEI.SF.Control"                                                                                                                                                                                                   
## [32] "SSEI.SF.Adaptiveness"                                                                                                                                                                                              
## [33] "SSEI.SF.Morality"                                                                                                                                                                                                  
## [34] "SEX_Nominal"                                                                                                                                                                                                       
## [35] "Criterion.A.Trauma_Nominal"                                                                                                                                                                                        
## [36] "Criterion.A.Trauma"                                                                                                                                                                                                
## [37] "Std.ACEs"                                                                                                                                                                                                          
## [38] "Std.PCL.5.SUM"                                                                                                                                                                                                     
## [39] "Std.LEC.5.Happened.to.me"                                                                                                                                                                                          
## [40] "Std.LEC.5.Witnessed.it"                                                                                                                                                                                            
## [41] "Std.LEC.5.Learned.about.it"                                                                                                                                                                                        
## [42] "Std.RSQ.Attachment.Anxiety"                                                                                                                                                                                        
## [43] "Std.RSQ.Attachment.Avoidance"                                                                                                                                                                                      
## [44] "Std.CHIPS.SUM"                                                                                                                                                                                                     
## [45] "Std.DERS.SUM"                                                                                                                                                                                                      
## [46] "Std.DERS_Nonaccept"                                                                                                                                                                                                
## [47] "Std.DERS_Goals"                                                                                                                                                                                                    
## [48] "Std.DERS_Impuls"                                                                                                                                                                                                   
## [49] "Std.DERS_Aware"                                                                                                                                                                                                    
## [50] "Std.DERS_Strategies"                                                                                                                                                                                               
## [51] "Std.DERS_Clarity"                                                                                                                                                                                                  
## [52] "Std.DASS.Depression"                                                                                                                                                                                               
## [53] "Std.DASS.Anxiety"                                                                                                                                                                                                  
## [54] "Std.DASS.Stress"                                                                                                                                                                                                   
## [55] "Std.SSEI.SF.SUM"                                                                                                                                                                                                   
## [56] "Std.SSEI.SF.Skill...Experience"                                                                                                                                                                                    
## [57] "Std.SSEI.SF.Attractiveness"                                                                                                                                                                                        
## [58] "Std.SSEI.SF.Control"                                                                                                                                                                                               
## [59] "Std.SSEI.SF.Adaptiveness"                                                                                                                                                                                          
## [60] "Std.SSEI.SF.Morality"                                                                                                                                                                                              
## [61] "male"                                                                                                                                                                                                              
## [62] "female"                                                                                                                                                                                                            
## [63] "White"                                                                                                                                                                                                             
## [64] "Black"                                                                                                                                                                                                             
## [65] "Asian"                                                                                                                                                                                                             
## [66] "Hispanic"                                                                                                                                                                                                          
## [67] "Native.American"                                                                                                                                                                                                   
## [68] "Multicultural.Other"                                                                                                                                                                                               
## [69] "single"                                                                                                                                                                                                            
## [70] "in.a.relationship"
# Fit a multiple regression model
model1 <- lm(Std.SSEI.SF.SUM ~ Std.ACEs + What.is.your.age. + male + female + White + Black + Asian + Hispanic + Native.American + Multicultural.Other + single + in.a.relationship, data=data)

# Fit a multiple regression model including the mediator
model2 <- lm(Std.SSEI.SF.SUM ~ Std.ACEs + Std.RSQ.Attachment.Anxiety + What.is.your.age. + male + female + White + Black + Asian + Hispanic + Native.American + Multicultural.Other + single + in.a.relationship, data=data)

# Test the difference in R^2 between the two models
summary(model1)$r.squared - summary(model2)$r.squared
## [1] -0.08779963
#This code will fit two multiple regression models, one without the mediator and one with the mediator. The difference in the R^2 values between the two models can be interpreted as the proportion of the total effect of Std.ACEs on Std.SSEI.SF.SUM that is explained by Std.RSQ.Attachment.Anxiety after controlling for the effects of SEX, Ethnic.Heritage, and Relationship.status.

# Use the 'anova' function to perform the model comparison
anova(model1, model1)
## Analysis of Variance Table
## 
## Model 1: Std.SSEI.SF.SUM ~ Std.ACEs + What.is.your.age. + male + female + 
##     White + Black + Asian + Hispanic + Native.American + Multicultural.Other + 
##     single + in.a.relationship
## Model 2: Std.SSEI.SF.SUM ~ Std.ACEs + What.is.your.age. + male + female + 
##     White + Black + Asian + Hispanic + Native.American + Multicultural.Other + 
##     single + in.a.relationship
##   Res.Df    RSS Df Sum of Sq F Pr(>F)
## 1    390 365.31                      
## 2    390 365.31  0         0
#testing mediating role of att anxiety, controlling Std.RSQ.Attachment.Avoidance
model <- '
Std.RSQ.Attachment.Anxiety ~ a*Std.ACEs + What.is.your.age. + SEX + Ethnic.Heritage + Relationship.status +Std.RSQ.Attachment.Avoidance
Std.SSEI.SF.SUM ~ c*Std.ACEs + b*Std.RSQ.Attachment.Anxiety + What.is.your.age. + SEX + Ethnic.Heritage + Relationship.status +Std.RSQ.Attachment.Avoidance

indirect  := a*b
direct    := c
total     := c + (a*b)'

# fit the SEM model to the data
fit <- sem(model, data = data, estimator= "MLM")
fit <- sem(model, data = data, se = "bootstrap", bootstrap = 5000)
summary(fit, estimates = TRUE, standardized = TRUE, fit.measures = TRUE, rsquare=TRUE, ci = TRUE)
## lavaan 0.6-19 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        15
## 
##                                                   Used       Total
##   Number of observations                           371         482
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               118.287
##   Degrees of freedom                                13
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -992.834
##   Loglikelihood unrestricted model (H1)             NA
##                                                       
##   Akaike (AIC)                                2015.668
##   Bayesian (BIC)                              2074.411
##   Sample-size adjusted Bayesian (SABIC)       2026.821
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             5000
##   Number of successful bootstrap draws            5000
## 
## Regressions:
##                                Estimate  Std.Err  z-value  P(>|z|) ci.lower
##   Std.RSQ.Attachment.Anxiety ~                                             
##     Std.ACEs   (a)                0.152    0.049    3.098    0.002    0.054
##     Wht.s.yr..                   -0.030    0.011   -2.707    0.007   -0.054
##     SEX                          -0.186    0.123   -1.509    0.131   -0.426
##     Ethnc.Hrtg                    0.039    0.061    0.649    0.516   -0.077
##     Rltnshp.st                    0.224    0.105    2.127    0.033    0.018
##     St.RSQ.A.A                    0.131    0.058    2.238    0.025    0.014
##   Std.SSEI.SF.SUM ~                                                        
##     Std.ACEs   (c)               -0.028    0.053   -0.518    0.605   -0.138
##     St.RSQ.A.A (b)               -0.259    0.045   -5.822    0.000   -0.346
##     Wht.s.yr..                    0.010    0.014    0.721    0.471   -0.013
##     SEX                           0.003    0.106    0.031    0.975   -0.212
##     Ethnc.Hrtg                   -0.017    0.068   -0.257    0.797   -0.148
##     Rltnshp.st                   -0.361    0.102   -3.540    0.000   -0.561
##     St.RSQ.A.A                   -0.184    0.052   -3.531    0.000   -0.282
##  ci.upper   Std.lv  Std.all
##                            
##     0.247    0.152    0.149
##    -0.010   -0.030   -0.126
##     0.061   -0.186   -0.078
##     0.163    0.039    0.032
##     0.436    0.224    0.110
##     0.246    0.131    0.125
##                            
##     0.071   -0.028   -0.028
##    -0.172   -0.259   -0.265
##     0.040    0.010    0.042
##     0.208    0.003    0.001
##     0.120   -0.017   -0.014
##    -0.163   -0.361   -0.182
##    -0.079   -0.184   -0.180
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Std.RSQ.Attc.A    0.926    0.068   13.670    0.000    0.781    1.043
##    .St.SSEI.SF.SUM    0.781    0.054   14.479    0.000    0.661    0.873
##    Std.lv  Std.all
##     0.926    0.906
##     0.781    0.803
## 
## R-Square:
##                    Estimate
##     Std.RSQ.Attc.A    0.094
##     St.SSEI.SF.SUM    0.197
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     indirect         -0.039    0.014   -2.733    0.006   -0.070   -0.013
##     direct           -0.028    0.053   -0.518    0.605   -0.138    0.071
##     total            -0.067    0.052   -1.290    0.197   -0.173    0.029
##    Std.lv  Std.all
##    -0.039   -0.040
##    -0.028   -0.028
##    -0.067   -0.067
#testing mediating role of att avoidance, controlling Std.RSQ.Attachment.anxiety
model <- '
Std.RSQ.Attachment.Avoidance ~ a*Std.ACEs + What.is.your.age. + SEX + Ethnic.Heritage + Relationship.status +Std.RSQ.Attachment.Anxiety
Std.SSEI.SF.SUM ~ c*Std.ACEs + b*Std.RSQ.Attachment.Avoidance + What.is.your.age. + SEX + Ethnic.Heritage + Relationship.status +Std.RSQ.Attachment.Anxiety

indirect  := a*b
direct    := c
total     := c + (a*b)'

# fit the SEM model to the data
fit <- sem(model, data = data, estimator= "MLM")
fit <- sem(model, data = data, se = "bootstrap", bootstrap = 5000)
summary(fit, estimates = TRUE, standardized = TRUE, fit.measures = TRUE, rsquare=TRUE, ci = TRUE)
## lavaan 0.6-19 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        15
## 
##                                                   Used       Total
##   Number of observations                           371         482
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               140.436
##   Degrees of freedom                                13
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -964.451
##   Loglikelihood unrestricted model (H1)             NA
##                                                       
##   Akaike (AIC)                                1958.903
##   Bayesian (BIC)                              2017.646
##   Sample-size adjusted Bayesian (SABIC)       1970.056
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             5000
##   Number of successful bootstrap draws            5000
## 
## Regressions:
##                                  Estimate  Std.Err  z-value  P(>|z|) ci.lower
##   Std.RSQ.Attachment.Avoidance ~                                             
##     Std.ACEs   (a)                  0.242    0.051    4.774    0.000    0.141
##     Wht.s.yr..                     -0.005    0.011   -0.445    0.656   -0.024
##     SEX                            -0.103    0.115   -0.896    0.370   -0.328
##     Ethnc.Hrtg                      0.003    0.074    0.047    0.963   -0.142
##     Rltnshp.st                      0.425    0.096    4.414    0.000    0.232
##     St.RSQ.A.A                      0.112    0.049    2.278    0.023    0.012
##   Std.SSEI.SF.SUM ~                                                          
##     Std.ACEs   (c)                 -0.028    0.053   -0.518    0.605   -0.138
##     St.RSQ.A.A (b)                 -0.184    0.052   -3.531    0.000   -0.282
##     Wht.s.yr..                      0.010    0.014    0.721    0.471   -0.013
##     SEX                             0.003    0.106    0.031    0.975   -0.212
##     Ethnc.Hrtg                     -0.017    0.068   -0.257    0.797   -0.148
##     Rltnshp.st                     -0.361    0.102   -3.540    0.000   -0.561
##     St.RSQ.A.A                     -0.259    0.045   -5.822    0.000   -0.346
##  ci.upper   Std.lv  Std.all
##                            
##     0.341    0.242    0.248
##     0.017   -0.005   -0.020
##     0.122   -0.103   -0.045
##     0.144    0.003    0.003
##     0.612    0.425    0.219
##     0.209    0.112    0.117
##                            
##     0.071   -0.028   -0.028
##    -0.079   -0.184   -0.180
##     0.040    0.010    0.042
##     0.208    0.003    0.001
##     0.120   -0.017   -0.014
##    -0.163   -0.361   -0.182
##    -0.172   -0.259   -0.265
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Std.RSQ.Attc.A    0.795    0.064   12.362    0.000    0.657    0.910
##    .St.SSEI.SF.SUM    0.781    0.054   14.479    0.000    0.661    0.873
##    Std.lv  Std.all
##     0.795    0.853
##     0.781    0.803
## 
## R-Square:
##                    Estimate
##     Std.RSQ.Attc.A    0.147
##     St.SSEI.SF.SUM    0.197
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     indirect         -0.044    0.017   -2.658    0.008   -0.081   -0.016
##     direct           -0.028    0.053   -0.518    0.605   -0.138    0.071
##     total            -0.072    0.054   -1.337    0.181   -0.185    0.028
##    Std.lv  Std.all
##    -0.044   -0.045
##    -0.028   -0.028
##    -0.072   -0.072