#Load packages

#read in tidyverse
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyr)
library(lavaan) #why do you need this package for this analysis?
## This is lavaan 0.6-20
## lavaan is FREE software! Please report any bugs.
library(tidySEM) #same as above
library(knitr)

library(ggplot2)
library(ggdist)
library(viridis)
## Loading required package: viridisLite
library(paletteer)

#load psych
library(psych)
## 
## Attaching package: 'psych'
## 
## The following object is masked from 'package:lavaan':
## 
##     cor2cov
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
#install.packages(c("tidytext", "wordcloud", "RColorBrewer"))
library(tidytext)
library(wordcloud)
## Loading required package: RColorBrewer
library(RColorBrewer)
library(modsem)
## This is modsem (1.0.22). Please report any bugs!
## OpenMP is not available! Multi-threading will not work properly!
options(
  scipen = 999,
  digits = 6
)

#Read in data

#read in data

CESSD_wide <- read.csv("CESSD.W1andW2_Wide.csv")

#some light data wrangling/mean centering

CESSD_wide <- CESSD_wide %>%
  mutate(c_soc_w2_c   = c_soc_w2 - mean(c_soc_w2, na.rm = TRUE),
         pcc_soc_w2_c = pcc_soc_w2 - mean(pcc_soc_w2, na.rm = TRUE),
         c_soc_x_pcc  = c_soc_w2_c * pcc_soc_w2_c
        )

#Longitudinal Models ##Longitudinal model 1 – c_soc and pcc_soc predicting ERI-R W2 controlling for W1 Model

#specify the model
path_model_1 <- 'eri_r_w2 ~ eri_r_w1 + c_soc_w2_c + pcc_soc_w2_c'

#now estimate the model
path_fit_1 <- sem(path_model_1, CESSD_wide, missing = "fiml", estimator = "MLR") 
## Warning: lavaan->lav_data_full():  
##    186 cases were deleted due to missing values in exogenous variable(s), 
##    while fixed.x = TRUE.
summary(path_fit_1, fit.measures = T, standardized = T)
## lavaan 0.6-20 ended normally after 9 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##                                                   Used       Total
##   Number of observations                           311         497
##   Number of missing patterns                         2            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               107.532      92.609
##   Degrees of freedom                                 3           3
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.161
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -290.890    -290.890
##   Loglikelihood unrestricted model (H1)       -290.890    -290.890
##                                                                   
##   Akaike (AIC)                                 591.780     591.780
##   Bayesian (BIC)                               610.479     610.479
##   Sample-size adjusted Bayesian (SABIC)        594.621     594.621
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   eri_r_w2 ~                                                            
##     eri_r_w1          0.441    0.054    8.175    0.000    0.441    0.455
##     c_soc_w2_c        0.072    0.050    1.443    0.149    0.072    0.090
##     pcc_soc_w2_c      0.103    0.059    1.760    0.078    0.103    0.119
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_r_w2          1.566    0.155   10.094    0.000    1.566    2.114
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_r_w2          0.387    0.032   11.942    0.000    0.387    0.706

##Longitudinal model 2 – c_soc and pcc_soc + interaction predicting ERI-R Model

#specify the model with itneration
path_model_2 <- 'eri_r_w2 ~ eri_r_w1 + c_soc_w2_c + pcc_soc_w2_c + c_soc_x_pcc'

#now estimate the model
path_fit_2 <- sem(path_model_2, CESSD_wide, missing = "fiml", estimator = "MLR") 
## Warning: lavaan->lav_data_full():  
##    186 cases were deleted due to missing values in exogenous variable(s), 
##    while fixed.x = TRUE.
summary(path_fit_2, fit.measures = T, standardized = T)
## lavaan 0.6-20 ended normally after 11 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         6
## 
##                                                   Used       Total
##   Number of observations                           311         497
##   Number of missing patterns                         2            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               108.897      90.193
##   Degrees of freedom                                 4           4
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.207
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -290.207    -290.207
##   Loglikelihood unrestricted model (H1)       -290.207    -290.207
##                                                                   
##   Akaike (AIC)                                 592.415     592.415
##   Bayesian (BIC)                               614.854     614.854
##   Sample-size adjusted Bayesian (SABIC)        595.824     595.824
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   eri_r_w2 ~                                                            
##     eri_r_w1          0.443    0.054    8.219    0.000    0.443    0.457
##     c_soc_w2_c        0.078    0.051    1.538    0.124    0.078    0.097
##     pcc_soc_w2_c      0.110    0.060    1.826    0.068    0.110    0.127
##     c_soc_x_pcc       0.043    0.042    1.015    0.310    0.043    0.058
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_r_w2          1.535    0.153   10.014    0.000    1.535    2.072
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_r_w2          0.385    0.032   11.994    0.000    0.385    0.702

##Longitudinal model 3 – c_soc and pcc_soc predicting ERI-R W2 controlling for W1 Model

#specify the model
path_model_3 <- 'eri_e_w2 ~ eri_e_w1 + c_soc_w2_c + pcc_soc_w2_c'

#now estimate the model
path_fit_3 <- sem(path_model_3, CESSD_wide, missing = "fiml", estimator = "MLR") 
## Warning: lavaan->lav_data_full():  
##    188 cases were deleted due to missing values in exogenous variable(s), 
##    while fixed.x = TRUE.
summary(path_fit_3, fit.measures = T, standardized = T)
## lavaan 0.6-20 ended normally after 11 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##                                                   Used       Total
##   Number of observations                           309         497
##   Number of missing patterns                         2            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               106.385      90.664
##   Degrees of freedom                                 3           3
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.173
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -302.496    -302.496
##   Loglikelihood unrestricted model (H1)       -302.496    -302.496
##                                                                   
##   Akaike (AIC)                                 614.992     614.992
##   Bayesian (BIC)                               633.659     633.659
##   Sample-size adjusted Bayesian (SABIC)        617.801     617.801
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.000
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   eri_e_w2 ~                                                            
##     eri_e_w1          0.430    0.050    8.573    0.000    0.430    0.428
##     c_soc_w2_c        0.147    0.061    2.396    0.017    0.147    0.174
##     pcc_soc_w2_c      0.080    0.061    1.298    0.194    0.080    0.088
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_e_w2          1.438    0.119   12.093    0.000    1.438    1.859
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_e_w2          0.423    0.033   12.851    0.000    0.423    0.707

##Longitudinal model 4 – c_soc and pcc_soc + interaction predicting ERI-R Model

#specify the model with itneration
path_model_4 <- 'eri_e_w2 ~ eri_e_w1 + c_soc_w2_c + pcc_soc_w2_c + c_soc_x_pcc'

#now estimate the model
path_fit_4 <- sem(path_model_4, CESSD_wide, missing = "fiml", estimator = "MLR") 
## Warning: lavaan->lav_data_full():  
##    188 cases were deleted due to missing values in exogenous variable(s), 
##    while fixed.x = TRUE.
summary(path_fit_4, fit.measures = T, standardized = T)
## Warning: lavaan->lav_fit_cfi_lavobject():  
##    computation of robust CFI resulted in NA values.
## Warning: lavaan->lav_fit_rmsea_lavobject():  
##    computation of robust RMSEA resulted in NA values.
## lavaan 0.6-20 ended normally after 12 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         6
## 
##                                                   Used       Total
##   Number of observations                           309         497
##   Number of missing patterns                         2            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 0.000       0.000
##   Degrees of freedom                                 0           0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               108.825      89.287
##   Degrees of freedom                                 4           4
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.219
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000       1.000
##   Tucker-Lewis Index (TLI)                       1.000       1.000
##                                                                   
##   Robust Comparative Fit Index (CFI)                            NA
##   Robust Tucker-Lewis Index (TLI)                               NA
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -301.276    -301.276
##   Loglikelihood unrestricted model (H1)       -301.276    -301.276
##                                                                   
##   Akaike (AIC)                                 614.552     614.552
##   Bayesian (BIC)                               636.952     636.952
##   Sample-size adjusted Bayesian (SABIC)        617.922     617.922
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000          NA
##   90 Percent confidence interval - lower         0.000          NA
##   90 Percent confidence interval - upper         0.000          NA
##   P-value H_0: RMSEA <= 0.050                       NA          NA
##   P-value H_0: RMSEA >= 0.080                       NA          NA
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                        NA
##   90 Percent confidence interval - upper                        NA
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000       0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   eri_e_w2 ~                                                            
##     eri_e_w1          0.429    0.050    8.572    0.000    0.429    0.427
##     c_soc_w2_c        0.153    0.062    2.463    0.014    0.153    0.181
##     pcc_soc_w2_c      0.089    0.061    1.453    0.146    0.089    0.099
##     c_soc_x_pcc       0.060    0.045    1.335    0.182    0.060    0.076
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_e_w2          1.406    0.119   11.772    0.000    1.406    1.818
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .eri_e_w2          0.419    0.033   12.861    0.000    0.419    0.701