library(readr)
library(lavaan)
## This is lavaan 0.6-8
## lavaan is FREE software! Please report any bugs.
library(rmarkdown)
library(knitr)
library(ggplot2)

wlps2 <- read_csv("~/Google Drive/Research/WLU/Master's thesis/Data/updated may 2020/wlps.2.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_double()
## )
## ℹ Use `spec()` for the full column specifications.
# Error term covariances constrained across waves
errorstructure <- 'swl1.t1 ~~ swl1cov*swl1.t2
swl2.t1 ~~ swl2cov*swl2.t2
swl3.t1 ~~ swl3cov*swl3.t2
swl4.t1 ~~ swl4cov*swl4.t2
swl5.t1 ~~ swl5cov*swl5.t2'

#configural CFA
configural.v1 <-
    
'
# SWL factor loadings defined

SWL.1 =~ NA*swl1.t1 + swl1.t2 + swl3.t1 + swl4.t1 + swl5.t1
SWL.2 =~ NA*swl1.t2 + swl2.t2 + swl3.t2 + swl4.t2 + swl5.t2

# SWL variance constrained to 1

SWL.1 ~~ 1*SWL.1
SWL.2 ~~ 1*SWL.2
'

configural.model <- paste(configural.v1, errorstructure, sep = ' ', collapse = NULL)

configural.fit <- cfa(configural.model,
                            data = wlps2,
                            estimator = "MLR",
                            se = "robust",
                            missing = "ML",
                            std.lv = TRUE)

summary(configural.fit, fit.measures = TRUE, standardized = TRUE, ci = TRUE,rsquare = TRUE)
## lavaan 0.6-8 ended normally after 52 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        36
##                                                       
##   Number of observations                           235
##   Number of missing patterns                         2
##                                                       
## Model Test User Model:
##                                                Standard      Robust
##   Test Statistic                                410.690     296.883
##   Degrees of freedom                                 29          29
##   P-value (Chi-square)                            0.000       0.000
##   Scaling correction factor                                   1.383
##        Yuan-Bentler correction (Mplus variant)                     
## 
## Model Test Baseline Model:
## 
##   Test statistic                              1526.466     965.549
##   Degrees of freedom                                45          45
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.581
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.742       0.709
##   Tucker-Lewis Index (TLI)                       0.600       0.548
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.745
##   Robust Tucker-Lewis Index (TLI)                            0.605
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2604.503   -2604.503
##   Scaling correction factor                                  1.638
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -2399.158   -2399.158
##   Scaling correction factor                                  1.525
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                5281.006    5281.006
##   Bayesian (BIC)                              5405.551    5405.551
##   Sample-size adjusted Bayesian (BIC)         5291.446    5291.446
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.237       0.198
##   90 Percent confidence interval - lower         0.217       0.181
##   90 Percent confidence interval - upper         0.257       0.216
##   P-value RMSEA <= 0.05                          0.000       0.000
##                                                                   
##   Robust RMSEA                                               0.233
##   90 Percent confidence interval - lower                     0.210
##   90 Percent confidence interval - upper                     0.258
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.253       0.253
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   SWL.1 =~                                                              
##     swl1.t1           1.322    0.087   15.124    0.000    1.150    1.493
##     swl1.t2          -0.549    0.388   -1.415    0.157   -1.308    0.211
##     swl3.t1           1.267    0.105   12.032    0.000    1.061    1.473
##     swl4.t1           1.034    0.109    9.454    0.000    0.820    1.249
##     swl5.t1           1.355    0.101   13.407    0.000    1.157    1.554
##   SWL.2 =~                                                              
##     swl1.t2           1.812    0.380    4.763    0.000    1.066    2.557
##     swl2.t2           1.267    0.197    6.423    0.000    0.880    1.653
##     swl3.t2           1.056    0.144    7.347    0.000    0.774    1.337
##     swl4.t2           0.646    0.122    5.299    0.000    0.407    0.885
##     swl5.t2           1.126    0.157    7.181    0.000    0.818    1.433
##    Std.lv  Std.all
##                   
##     1.322    0.885
##    -0.549   -0.366
##     1.267    0.915
##     1.034    0.803
##     1.355    0.767
##                   
##     1.812    1.209
##     1.267    0.832
##     1.056    0.844
##     0.646    0.702
##     1.126    0.679
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .swl1.t1 ~~                                                            
##    .swl1.t2 (swl1)    0.109    0.072    1.510    0.131   -0.032    0.249
##  .swl2.t2 ~~                                                            
##     swl2.t1 (swl2)   -0.170    0.272   -0.627    0.531   -0.702    0.362
##  .swl3.t1 ~~                                                            
##    .swl3.t2 (swl3)   -0.040    0.074   -0.536    0.592   -0.185    0.105
##  .swl4.t1 ~~                                                            
##    .swl4.t2 (swl4)    0.055    0.084    0.659    0.510   -0.109    0.219
##  .swl5.t1 ~~                                                            
##    .swl5.t2 (swl5)    0.685    0.188    3.644    0.000    0.317    1.054
##   SWL.1 ~~                                                              
##     SWL.2             0.840    0.057   14.709    0.000    0.728    0.952
##    Std.lv  Std.all
##                   
##     0.109    0.270
##                   
##    -0.170   -0.139
##                   
##    -0.040   -0.106
##                   
##     0.055    0.109
##                   
##     0.685    0.496
##                   
##     0.840    0.840
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .swl1.t1           5.304    0.097   54.428    0.000    5.113    5.495
##    .swl1.t2           5.251    0.122   42.906    0.000    5.011    5.491
##    .swl3.t1           5.709    0.090   63.149    0.000    5.531    5.886
##    .swl4.t1           5.862    0.084   69.507    0.000    5.696    6.027
##    .swl5.t1           4.853    0.118   41.123    0.000    4.622    5.085
##    .swl2.t2           5.546    0.128   43.378    0.000    5.295    5.796
##    .swl3.t2           5.751    0.108   53.425    0.000    5.540    5.962
##    .swl4.t2           6.133    0.079   77.982    0.000    5.979    6.287
##    .swl5.t2           5.131    0.133   38.640    0.000    4.871    5.391
##     swl2.t1           5.474    0.094   58.038    0.000    5.290    5.659
##     SWL.1             0.000                               0.000    0.000
##     SWL.2             0.000                               0.000    0.000
##    Std.lv  Std.all
##     5.304    3.550
##     5.251    3.503
##     5.709    4.124
##     5.862    4.550
##     4.853    2.746
##     5.546    3.640
##     5.751    4.596
##     6.133    6.665
##     5.131    3.093
##     5.474    3.786
##     0.000    0.000
##     0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     SWL.1             1.000                               1.000    1.000
##     SWL.2             1.000                               1.000    1.000
##    .swl1.t1           0.485    0.085    5.696    0.000    0.318    0.652
##    .swl1.t2           0.333    0.226    1.473    0.141   -0.110    0.777
##    .swl3.t1           0.311    0.075    4.135    0.000    0.163    0.458
##    .swl4.t1           0.590    0.097    6.063    0.000    0.400    0.781
##    .swl5.t1           1.287    0.161    7.997    0.000    0.972    1.602
##    .swl2.t2           0.716    0.186    3.856    0.000    0.352    1.080
##    .swl3.t2           0.451    0.154    2.937    0.003    0.150    0.753
##    .swl4.t2           0.429    0.070    6.138    0.000    0.292    0.566
##    .swl5.t2           1.485    0.295    5.033    0.000    0.907    2.064
##     swl2.t1           2.091    0.250    8.365    0.000    1.601    2.581
##    Std.lv  Std.all
##     1.000    1.000
##     1.000    1.000
##     0.485    0.217
##     0.333    0.148
##     0.311    0.162
##     0.590    0.356
##     1.287    0.412
##     0.716    0.309
##     0.451    0.288
##     0.429    0.507
##     1.485    0.540
##     2.091    1.000
## 
## R-Square:
##                    Estimate
##     swl1.t1           0.783
##     swl1.t2           0.852
##     swl3.t1           0.838
##     swl4.t1           0.644
##     swl5.t1           0.588
##     swl2.t2           0.691
##     swl3.t2           0.712
##     swl4.t2           0.493
##     swl5.t2           0.460
#metric

metric.v1 <-

'
# SWL factor loadings defined, and constrained across waves

SWL.1 =~ SWL1f*swl1.t1 + SWL2f*swl2.t1 + SWL3f*swl3.t1 + SWL4f*swl4.t1 + SWL5f*swl5.t1
SWL.2 =~ SWL1f*swl1.t2 + SWL2f*swl2.t2 + SWL3f*swl3.t2 + SWL4f*swl4.t2 + SWL5f*swl5.t2

# SWL variance constrained to 1

SWL.1 ~~ 1*SWL.1
SWL.2 ~~ 1*SWL.2

'

metric.model <- paste(metric.v1, errorstructure, sep = ' ', collapse = NULL)

metric.fit <- cfa(metric.model,
                       data = wlps2,
                       estimator = "MLR",
                       se = "robust",
                       missing = "ML",
                       std.lv = TRUE)

summary(metric.fit, fit.measures = TRUE,
        standardized = TRUE,
        ci = TRUE,
        rsquare = TRUE)
## lavaan 0.6-8 ended normally after 51 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        36
##   Number of equality constraints                     5
##                                                       
##   Number of observations                           235
##   Number of missing patterns                         2
##                                                       
## Model Test User Model:
##                                                Standard      Robust
##   Test Statistic                                147.502      99.258
##   Degrees of freedom                                 34          34
##   P-value (Chi-square)                            0.000       0.000
##   Scaling correction factor                                   1.486
##        Yuan-Bentler correction (Mplus variant)                     
## 
## Model Test Baseline Model:
## 
##   Test statistic                              1526.466     965.549
##   Degrees of freedom                                45          45
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.581
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.923       0.929
##   Tucker-Lewis Index (TLI)                       0.899       0.906
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.933
##   Robust Tucker-Lewis Index (TLI)                            0.912
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2472.909   -2472.909
##   Scaling correction factor                                  1.349
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -2399.158   -2399.158
##   Scaling correction factor                                  1.525
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                5007.817    5007.817
##   Bayesian (BIC)                              5115.064    5115.064
##   Sample-size adjusted Bayesian (BIC)         5016.807    5016.807
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.119       0.090
##   90 Percent confidence interval - lower         0.100       0.074
##   90 Percent confidence interval - upper         0.139       0.108
##   P-value RMSEA <= 0.05                          0.000       0.000
##                                                                   
##   Robust RMSEA                                               0.110
##   90 Percent confidence interval - lower                     0.085
##   90 Percent confidence interval - upper                     0.136
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.150       0.150
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   SWL.1 =~                                                              
##     swl1.t1 (SWL1)    1.320    0.081   16.335    0.000    1.162    1.479
##     swl2.t1 (SWL2)    1.255    0.096   13.051    0.000    1.067    1.444
##     swl3.t1 (SWL3)    1.235    0.093   13.238    0.000    1.052    1.418
##     swl4.t1 (SWL4)    0.891    0.094    9.482    0.000    0.707    1.075
##     swl5.t1 (SWL5)    1.256    0.104   12.088    0.000    1.052    1.460
##   SWL.2 =~                                                              
##     swl1.t2 (SWL1)    1.320    0.081   16.335    0.000    1.162    1.479
##     swl2.t2 (SWL2)    1.255    0.096   13.051    0.000    1.067    1.444
##     swl3.t2 (SWL3)    1.235    0.093   13.238    0.000    1.052    1.418
##     swl4.t2 (SWL4)    0.891    0.094    9.482    0.000    0.707    1.075
##     swl5.t2 (SWL5)    1.256    0.104   12.088    0.000    1.052    1.460
##    Std.lv  Std.all
##                   
##     1.320    0.886
##     1.255    0.873
##     1.235    0.920
##     0.891    0.742
##     1.256    0.731
##                   
##     1.320    0.867
##     1.255    0.836
##     1.235    0.896
##     0.891    0.798
##     1.256    0.711
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .swl1.t1 ~~                                                            
##    .swl1.t2 (swl1)    0.063    0.074    0.851    0.395   -0.082    0.208
##  .swl2.t1 ~~                                                            
##    .swl2.t2 (swl2)    0.076    0.079    0.969    0.333   -0.078    0.230
##  .swl3.t1 ~~                                                            
##    .swl3.t2 (swl3)   -0.067    0.051   -1.314    0.189   -0.167    0.033
##  .swl4.t1 ~~                                                            
##    .swl4.t2 (swl4)    0.141    0.086    1.630    0.103   -0.028    0.310
##  .swl5.t1 ~~                                                            
##    .swl5.t2 (swl5)    0.750    0.187    4.018    0.000    0.384    1.116
##   SWL.1 ~~                                                              
##     SWL.2             0.810    0.053   15.429    0.000    0.707    0.913
##    Std.lv  Std.all
##                   
##     0.063    0.121
##                   
##     0.076    0.132
##                   
##    -0.067   -0.208
##                   
##     0.141    0.260
##                   
##     0.750    0.515
##                   
##     0.810    0.810
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .swl1.t1           5.304    0.097   54.428    0.000    5.113    5.495
##    .swl2.t1           5.474    0.094   58.038    0.000    5.290    5.659
##    .swl3.t1           5.709    0.090   63.149    0.000    5.531    5.886
##    .swl4.t1           5.862    0.084   69.507    0.000    5.696    6.027
##    .swl5.t1           4.853    0.118   41.123    0.000    4.622    5.085
##    .swl1.t2           5.232    0.122   42.853    0.000    4.993    5.471
##    .swl2.t2           5.511    0.125   44.028    0.000    5.266    5.756
##    .swl3.t2           5.724    0.105   54.311    0.000    5.517    5.930
##    .swl4.t2           6.098    0.080   76.220    0.000    5.941    6.254
##    .swl5.t2           5.117    0.131   39.148    0.000    4.861    5.373
##     SWL.1             0.000                               0.000    0.000
##     SWL.2             0.000                               0.000    0.000
##    Std.lv  Std.all
##     5.304    3.559
##     5.474    3.805
##     5.709    4.251
##     5.862    4.880
##     4.853    2.825
##     5.232    3.437
##     5.511    3.671
##     5.724    4.154
##     6.098    5.463
##     5.117    2.897
##     0.000    0.000
##     0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     SWL.1             1.000                               1.000    1.000
##     SWL.2             1.000                               1.000    1.000
##    .swl1.t1           0.478    0.072    6.662    0.000    0.337    0.618
##    .swl2.t1           0.494    0.079    6.249    0.000    0.339    0.649
##    .swl3.t1           0.278    0.061    4.558    0.000    0.158    0.398
##    .swl4.t1           0.649    0.102    6.361    0.000    0.449    0.849
##    .swl5.t1           1.373    0.159    8.633    0.000    1.061    1.684
##    .swl1.t2           0.573    0.196    2.932    0.003    0.190    0.957
##    .swl2.t2           0.677    0.158    4.274    0.000    0.367    0.988
##    .swl3.t2           0.373    0.109    3.408    0.001    0.158    0.587
##    .swl4.t2           0.452    0.078    5.776    0.000    0.299    0.605
##    .swl5.t2           1.543    0.290    5.330    0.000    0.976    2.110
##    Std.lv  Std.all
##     1.000    1.000
##     1.000    1.000
##     0.478    0.215
##     0.494    0.239
##     0.278    0.154
##     0.649    0.450
##     1.373    0.465
##     0.573    0.247
##     0.677    0.301
##     0.373    0.196
##     0.452    0.363
##     1.543    0.494
## 
## R-Square:
##                    Estimate
##     swl1.t1           0.785
##     swl2.t1           0.761
##     swl3.t1           0.846
##     swl4.t1           0.550
##     swl5.t1           0.535
##     swl1.t2           0.753
##     swl2.t2           0.699
##     swl3.t2           0.804
##     swl4.t2           0.637
##     swl5.t2           0.506
#scalar CFA

scalar.v1 <-

'
# Intercepts constrained

swl1.t1 ~ swl1i*1
swl1.t2 ~ swl1i*1

swl2.t1 ~ swl2i*1
swl2.t2 ~ swl2i*1

swl3.t1 ~ swl3i*1
swl3.t2 ~ swl3i*1

swl4.t1 ~ swl4i*1
swl4.t2 ~ swl4i*1

swl5.t1 ~ swl5i*1
swl5.t2 ~ swl5i*1

'

scalar.model <- paste(metric.v1, errorstructure, scalar.v1, sep = ' ', collapse = NULL)

scalar.fit <- cfa(scalar.model,
                       data = wlps2,
                       estimator = "MLR",
                       se = "robust",
                       missing = "ML",
                       std.lv = TRUE)

summary(scalar.fit, fit.measures = TRUE,
        standardized = TRUE,
        ci = TRUE,
        rsquare = TRUE)
## lavaan 0.6-8 ended normally after 43 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        36
##   Number of equality constraints                    10
##                                                       
##   Number of observations                           235
##   Number of missing patterns                         2
##                                                       
## Model Test User Model:
##                                                Standard      Robust
##   Test Statistic                                162.707     115.609
##   Degrees of freedom                                 39          39
##   P-value (Chi-square)                            0.000       0.000
##   Scaling correction factor                                   1.407
##        Yuan-Bentler correction (Mplus variant)                     
## 
## Model Test Baseline Model:
## 
##   Test statistic                              1526.466     965.549
##   Degrees of freedom                                45          45
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.581
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.916       0.917
##   Tucker-Lewis Index (TLI)                       0.904       0.904
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.926
##   Robust Tucker-Lewis Index (TLI)                            0.915
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2480.511   -2480.511
##   Scaling correction factor                                  1.228
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -2399.158   -2399.158
##   Scaling correction factor                                  1.525
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                5013.023    5013.023
##   Bayesian (BIC)                              5102.972    5102.972
##   Sample-size adjusted Bayesian (BIC)         5020.563    5020.563
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.116       0.091
##   90 Percent confidence interval - lower         0.098       0.075
##   90 Percent confidence interval - upper         0.135       0.108
##   P-value RMSEA <= 0.05                          0.000       0.000
##                                                                   
##   Robust RMSEA                                               0.108
##   90 Percent confidence interval - lower                     0.086
##   90 Percent confidence interval - upper                     0.132
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.158       0.158
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   SWL.1 =~                                                              
##     swl1.t1 (SWL1)    1.317    0.081   16.249    0.000    1.159    1.476
##     swl2.t1 (SWL2)    1.257    0.096   13.072    0.000    1.068    1.445
##     swl3.t1 (SWL3)    1.236    0.093   13.243    0.000    1.053    1.419
##     swl4.t1 (SWL4)    0.902    0.097    9.311    0.000    0.712    1.092
##     swl5.t1 (SWL5)    1.264    0.104   12.103    0.000    1.060    1.469
##   SWL.2 =~                                                              
##     swl1.t2 (SWL1)    1.317    0.081   16.249    0.000    1.159    1.476
##     swl2.t2 (SWL2)    1.257    0.096   13.072    0.000    1.068    1.445
##     swl3.t2 (SWL3)    1.236    0.093   13.243    0.000    1.053    1.419
##     swl4.t2 (SWL4)    0.902    0.097    9.311    0.000    0.712    1.092
##     swl5.t2 (SWL5)    1.264    0.104   12.103    0.000    1.060    1.469
##    Std.lv  Std.all
##                   
##     1.317    0.885
##     1.257    0.873
##     1.236    0.920
##     0.902    0.745
##     1.264    0.733
##                   
##     1.317    0.865
##     1.257    0.838
##     1.236    0.898
##     0.902    0.793
##     1.264    0.708
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .swl1.t1 ~~                                                            
##    .swl1.t2 (swl1)    0.061    0.076    0.806    0.421   -0.087    0.210
##  .swl2.t1 ~~                                                            
##    .swl2.t2 (swl2)    0.074    0.077    0.967    0.334   -0.076    0.225
##  .swl3.t1 ~~                                                            
##    .swl3.t2 (swl3)   -0.064    0.051   -1.250    0.211   -0.165    0.037
##  .swl4.t1 ~~                                                            
##    .swl4.t2 (swl4)    0.151    0.089    1.684    0.092   -0.025    0.326
##  .swl5.t1 ~~                                                            
##    .swl5.t2 (swl5)    0.747    0.189    3.945    0.000    0.376    1.117
##   SWL.1 ~~                                                              
##     SWL.2             0.809    0.053   15.287    0.000    0.706    0.913
##    Std.lv  Std.all
##                   
##     0.061    0.115
##                   
##     0.074    0.129
##                   
##    -0.064   -0.201
##                   
##     0.151    0.269
##                   
##     0.747    0.506
##                   
##     0.809    0.809
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .swl1.t1 (swl1)    5.283    0.092   57.337    0.000    5.103    5.464
##    .swl1.t2 (swl1)    5.283    0.092   57.337    0.000    5.103    5.464
##    .swl2.t1 (swl2)    5.485    0.089   61.486    0.000    5.310    5.660
##    .swl2.t2 (swl2)    5.485    0.089   61.486    0.000    5.310    5.660
##    .swl3.t1 (swl3)    5.712    0.084   68.305    0.000    5.548    5.876
##    .swl3.t2 (swl3)    5.712    0.084   68.305    0.000    5.548    5.876
##    .swl4.t1 (swl4)    5.951    0.074   80.020    0.000    5.806    6.097
##    .swl4.t2 (swl4)    5.951    0.074   80.020    0.000    5.806    6.097
##    .swl5.t1 (swl5)    4.920    0.115   42.750    0.000    4.694    5.146
##    .swl5.t2 (swl5)    4.920    0.115   42.750    0.000    4.694    5.146
##     SWL.1             0.000                               0.000    0.000
##     SWL.2             0.000                               0.000    0.000
##    Std.lv  Std.all
##     5.283    3.550
##     5.283    3.468
##     5.485    3.809
##     5.485    3.656
##     5.712    4.251
##     5.712    4.149
##     5.951    4.916
##     5.951    5.231
##     4.920    2.854
##     4.920    2.757
##     0.000    0.000
##     0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     SWL.1             1.000                               1.000    1.000
##     SWL.2             1.000                               1.000    1.000
##    .swl1.t1           0.480    0.071    6.772    0.000    0.341    0.619
##    .swl2.t1           0.494    0.079    6.233    0.000    0.339    0.650
##    .swl3.t1           0.278    0.061    4.561    0.000    0.159    0.397
##    .swl4.t1           0.652    0.104    6.298    0.000    0.449    0.855
##    .swl5.t1           1.373    0.159    8.621    0.000    1.061    1.686
##    .swl1.t2           0.586    0.206    2.848    0.004    0.183    0.989
##    .swl2.t2           0.672    0.157    4.291    0.000    0.365    0.979
##    .swl3.t2           0.369    0.109    3.384    0.001    0.155    0.582
##    .swl4.t2           0.481    0.087    5.543    0.000    0.311    0.651
##    .swl5.t2           1.587    0.293    5.409    0.000    1.012    2.162
##    Std.lv  Std.all
##     1.000    1.000
##     1.000    1.000
##     0.480    0.217
##     0.494    0.238
##     0.278    0.154
##     0.652    0.445
##     1.373    0.462
##     0.586    0.252
##     0.672    0.299
##     0.369    0.194
##     0.481    0.372
##     1.587    0.498
## 
## R-Square:
##                    Estimate
##     swl1.t1           0.783
##     swl2.t1           0.762
##     swl3.t1           0.846
##     swl4.t1           0.555
##     swl5.t1           0.538
##     swl1.t2           0.748
##     swl2.t2           0.701
##     swl3.t2           0.806
##     swl4.t2           0.628
##     swl5.t2           0.502
residual.v1 <-

'
# Residual error constrained across waves


swl1.t1 ~~ swl1u*swl1.t1
swl1.t2 ~~ swl1u*swl1.t2

swl2.t1 ~~ swl1u*swl1.t1
swl2.t2 ~~ swl1u*swl2.t2

swl3.t1 ~~ swl1u*swl3.t1
swl3.t2 ~~ swl1u*swl3.t2

swl4.t1 ~~ swl1u*swl4.t1
swl4.t2 ~~ swl1u*swl4.t2

swl5.t1 ~~ swl1u*swl5.t1
swl5.t2 ~~ swl1u*swl5.t2

'

residual.model <- paste(metric.v1, errorstructure, scalar.v1, residual.v1, sep = ' ', collapse = NULL)


residual.fit <- cfa(residual.model,
                       data = wlps2,
                       estimator = "MLR",
                       se = "robust",
                       missing = "ML",
                       std.lv = TRUE)

summary(residual.fit, fit.measures = TRUE,
        standardized = TRUE,
        ci = TRUE,
        rsquare = TRUE)
## lavaan 0.6-8 ended normally after 39 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        37
##   Number of equality constraints                    19
##                                                       
##   Number of observations                           235
##   Number of missing patterns                         2
##                                                       
## Model Test User Model:
##                                                Standard      Robust
##   Test Statistic                                362.521     249.345
##   Degrees of freedom                                 47          47
##   P-value (Chi-square)                            0.000       0.000
##   Scaling correction factor                                   1.454
##        Yuan-Bentler correction (Mplus variant)                     
## 
## Model Test Baseline Model:
## 
##   Test statistic                              1526.466     965.549
##   Degrees of freedom                                45          45
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.581
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.787       0.780
##   Tucker-Lewis Index (TLI)                       0.796       0.790
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.798
##   Robust Tucker-Lewis Index (TLI)                            0.806
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2580.418   -2580.418
##   Scaling correction factor                                  0.832
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -2399.158   -2399.158
##   Scaling correction factor                                  1.525
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                5196.836    5196.836
##   Bayesian (BIC)                              5259.109    5259.109
##   Sample-size adjusted Bayesian (BIC)         5202.056    5202.056
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.169       0.135
##   90 Percent confidence interval - lower         0.153       0.122
##   90 Percent confidence interval - upper         0.185       0.149
##   P-value RMSEA <= 0.05                          0.000       0.000
##                                                                   
##   Robust RMSEA                                               0.163
##   90 Percent confidence interval - lower                     0.144
##   90 Percent confidence interval - upper                     0.183
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.200       0.200
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   SWL.1 =~                                                              
##     swl1.t1 (SWL1)    1.315    0.086   15.272    0.000    1.146    1.484
##     swl2.t1 (SWL2)    1.170    0.111   10.549    0.000    0.953    1.388
##     swl3.t1 (SWL3)    1.139    0.095   12.032    0.000    0.954    1.325
##     swl4.t1 (SWL4)    0.942    0.099    9.479    0.000    0.747    1.136
##     swl5.t1 (SWL5)    1.472    0.079   18.546    0.000    1.316    1.628
##   SWL.2 =~                                                              
##     swl1.t2 (SWL1)    1.315    0.086   15.272    0.000    1.146    1.484
##     swl2.t2 (SWL2)    1.170    0.111   10.549    0.000    0.953    1.388
##     swl3.t2 (SWL3)    1.139    0.095   12.032    0.000    0.954    1.325
##     swl4.t2 (SWL4)    0.942    0.099    9.479    0.000    0.747    1.136
##     swl5.t2 (SWL5)    1.472    0.079   18.546    0.000    1.316    1.628
##    Std.lv  Std.all
##                   
##     1.315    0.846
##     1.170    0.678
##     1.139    0.809
##     0.942    0.751
##     1.472    0.872
##                   
##     1.315    0.846
##     1.170    0.816
##     1.139    0.809
##     0.942    0.751
##     1.472    0.872
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .swl1.t1 ~~                                                            
##    .swl1.2 (swl1c)    0.004    0.070    0.062    0.950   -0.132    0.141
##  .swl2.t1 ~~                                                            
##    .swl2.2  (swl2)    0.153    0.077    1.974    0.048    0.001    0.304
##  .swl3.t1 ~~                                                            
##    .swl3.2  (swl3)   -0.047    0.150   -0.310    0.756   -0.341    0.248
##  .swl4.t1 ~~                                                            
##    .swl4.2  (swl4)    0.242    0.136    1.773    0.076   -0.025    0.509
##  .swl5.t1 ~~                                                            
##    .swl5.2  (swl5)    0.282    0.075    3.758    0.000    0.135    0.429
##  .swl1.t1 ~~                                                            
##    .swl2.1 (swl1u)    0.685    0.058   11.737    0.000    0.570    0.799
##   SWL.1 ~~                                                              
##     SWL.2             0.859    0.037   22.902    0.000    0.785    0.932
##    Std.lv  Std.all
##                   
##     0.004    0.006
##                   
##     0.153    0.145
##                   
##    -0.047   -0.068
##                   
##     0.242    0.353
##                   
##     0.282    0.412
##                   
##     0.685    0.653
##                   
##     0.859    0.859
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .swl1.t1 (swl1)    5.273    0.096   55.082    0.000    5.086    5.461
##    .swl1.t2 (swl1)    5.273    0.096   55.082    0.000    5.086    5.461
##    .swl2.t1 (swl2)    5.444    0.098   55.382    0.000    5.252    5.637
##    .swl2.t2 (swl2)    5.444    0.098   55.382    0.000    5.252    5.637
##    .swl3.t1 (swl3)    5.712    0.084   67.942    0.000    5.547    5.877
##    .swl3.t2 (swl3)    5.712    0.084   67.942    0.000    5.547    5.877
##    .swl4.t1 (swl4)    5.926    0.076   78.115    0.000    5.778    6.075
##    .swl4.t2 (swl4)    5.926    0.076   78.115    0.000    5.778    6.075
##    .swl5.t1 (swl5)    4.928    0.113   43.588    0.000    4.706    5.149
##    .swl5.t2 (swl5)    4.928    0.113   43.588    0.000    4.706    5.149
##     SWL.1             0.000                               0.000    0.000
##     SWL.2             0.000                               0.000    0.000
##    Std.lv  Std.all
##     5.273    3.394
##     5.273    3.394
##     5.444    3.157
##     5.444    3.799
##     5.712    4.057
##     5.712    4.057
##     5.926    4.728
##     5.926    4.728
##     4.928    2.918
##     4.928    2.918
##     0.000    0.000
##     0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     SWL.1             1.000                               1.000    1.000
##     SWL.2             1.000                               1.000    1.000
##    .swl1.t1 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl1.t2 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl2.t2 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl3.t1 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl3.t2 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl4.t1 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl4.t2 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl5.t1 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl5.t2 (swl1)    0.685    0.058   11.737    0.000    0.570    0.799
##    .swl2.t1           1.605    0.139   11.533    0.000    1.332    1.878
##    Std.lv  Std.all
##     1.000    1.000
##     1.000    1.000
##     0.685    0.284
##     0.685    0.284
##     0.685    0.333
##     0.685    0.345
##     0.685    0.345
##     0.685    0.436
##     0.685    0.436
##     0.685    0.240
##     0.685    0.240
##     1.605    0.540
## 
## R-Square:
##                    Estimate
##     swl1.t1           0.716
##     swl1.t2           0.716
##     swl2.t2           0.667
##     swl3.t1           0.655
##     swl3.t2           0.655
##     swl4.t1           0.564
##     swl4.t2           0.564
##     swl5.t1           0.760
##     swl5.t2           0.760
##     swl2.t1           0.460
round(cbind(configural.error=inspect(configural.fit, 'fit.measures'),
            metric=inspect(metric.fit, 'fit.measures'),
            scalar=inspect(scalar.fit, 'fit.measures'),
            residual=inspect(residual.fit, 'fit.measures')),3)
##                               configural.error    metric    scalar  residual
## npar                                    36.000    31.000    26.000    18.000
## fmin                                     0.874     0.314     0.346     0.771
## chisq                                  410.690   147.502   162.707   362.521
## df                                      29.000    34.000    39.000    47.000
## pvalue                                   0.000     0.000     0.000     0.000
## chisq.scaled                           296.883    99.258   115.609   249.345
## df.scaled                               29.000    34.000    39.000    47.000
## pvalue.scaled                            0.000     0.000     0.000     0.000
## chisq.scaling.factor                     1.383     1.486     1.407     1.454
## baseline.chisq                        1526.466  1526.466  1526.466  1526.466
## baseline.df                             45.000    45.000    45.000    45.000
## baseline.pvalue                          0.000     0.000     0.000     0.000
## baseline.chisq.scaled                  965.549   965.549   965.549   965.549
## baseline.df.scaled                      45.000    45.000    45.000    45.000
## baseline.pvalue.scaled                   0.000     0.000     0.000     0.000
## baseline.chisq.scaling.factor            1.581     1.581     1.581     1.581
## cfi                                      0.742     0.923     0.916     0.787
## tli                                      0.600     0.899     0.904     0.796
## nnfi                                     0.600     0.899     0.904     0.796
## rfi                                      0.583     0.872     0.877        NA
## nfi                                      0.731     0.903     0.893        NA
## pnfi                                     0.471     0.683     0.774     0.796
## ifi                                      0.745     0.924     0.917     0.787
## rni                                      0.742     0.923     0.916     0.787
## cfi.scaled                               0.709     0.929     0.917     0.780
## tli.scaled                               0.548     0.906     0.904     0.790
## cfi.robust                               0.745     0.933     0.926     0.798
## tli.robust                               0.605     0.912     0.915     0.806
## nnfi.scaled                              0.548     0.906     0.904     0.790
## nnfi.robust                              0.605     0.912     0.915     0.806
## rfi.scaled                               0.523     0.864     0.862        NA
## nfi.scaled                               0.693     0.897     0.880        NA
## ifi.scaled                               0.714     0.930     0.917     0.780
## rni.scaled                               0.709     0.929     0.917     0.780
## rni.robust                               0.745     0.933     0.926     0.798
## logl                                 -2604.503 -2472.909 -2480.511 -2580.418
## unrestricted.logl                    -2399.158 -2399.158 -2399.158 -2399.158
## aic                                   5281.006  5007.817  5013.023  5196.836
## bic                                   5405.551  5115.064  5102.972  5259.109
## ntotal                                 235.000   235.000   235.000   235.000
## bic2                                  5291.446  5016.807  5020.563  5202.056
## scaling.factor.h1                        1.525     1.525     1.525     1.525
## scaling.factor.h0                        1.638     1.349     1.228     0.832
## rmsea                                    0.237     0.119     0.116     0.169
## rmsea.ci.lower                           0.217     0.100     0.098     0.153
## rmsea.ci.upper                           0.257     0.139     0.135     0.185
## rmsea.pvalue                             0.000     0.000     0.000     0.000
## rmsea.scaled                             0.198     0.090     0.091     0.135
## rmsea.ci.lower.scaled                    0.181     0.074     0.075     0.122
## rmsea.ci.upper.scaled                    0.216     0.108     0.108     0.149
## rmsea.pvalue.scaled                      0.000     0.000     0.000     0.000
## rmsea.robust                             0.233     0.110     0.108     0.163
## rmsea.ci.lower.robust                    0.210     0.085     0.086     0.144
## rmsea.ci.upper.robust                    0.258     0.136     0.132     0.183
## rmsea.pvalue.robust                         NA        NA        NA        NA
## rmr                                      0.526     0.245     0.251     0.308
## rmr_nomean                               0.572     0.266     0.269     0.331
## srmr                                     0.253     0.150     0.158     0.200
## srmr_bentler                             0.253     0.150     0.158     0.200
## srmr_bentler_nomean                      0.275     0.162     0.168     0.214
## crmr                                     0.275     0.221     0.252     0.324
## crmr_nomean                              0.300     0.094     0.093     0.115
## srmr_mplus                               0.254     0.218     0.246     0.327
## srmr_mplus_nomean                        0.272     0.120     0.125     0.178
## cn_05                                   25.351    78.433    79.819    42.488
## cn_01                                   29.375    90.316    91.166    47.961
## gfi                                      0.978     0.981     0.978     0.957
## agfi                                     0.950     0.963     0.964     0.940
## pgfi                                     0.436     0.513     0.587     0.692
## mfi                                      0.444     0.785     0.769     0.511
## ecvi                                     2.054     0.891     0.914     1.696
anova(configural.fit, metric.fit)
## Warning in lavTestLRT(object = new("lavaan", version = "0.6.8", call = lavaan::lavaan(model = configural.model, : lavaan WARNING: some restricted models fit better than less 
##   restricted models; either these models are not nested, or
##   the less restricted model failed to reach a global optimum.
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan NOTE:
##     The "Chisq" column contains standard test statistics, not the
##     robust test that should be reported per model. A robust difference
##     test is a function of two standard (not robust) statistics.
##  
##                Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)
## configural.fit 29 5281.0 5405.6 410.69                              
## metric.fit     34 5007.8 5115.1 147.50    -126.43       5          1
anova(metric.fit, scalar.fit)
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan NOTE:
##     The "Chisq" column contains standard test statistics, not the
##     robust test that should be reported per model. A robust difference
##     test is a function of two standard (not robust) statistics.
##  
##            Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)   
## metric.fit 34 5007.8 5115.1 147.50                                 
## scalar.fit 39 5013.0 5103.0 162.71     17.427       5   0.003758 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(scalar.fit, residual.fit)
## Scaled Chi-Squared Difference Test (method = "satorra.bentler.2001")
## 
## lavaan NOTE:
##     The "Chisq" column contains standard test statistics, not the
##     robust test that should be reported per model. A robust difference
##     test is a function of two standard (not robust) statistics.
##  
##              Df    AIC    BIC  Chisq Chisq diff Df diff Pr(>Chisq)    
## scalar.fit   39 5013.0 5103.0 162.71                                  
## residual.fit 47 5196.8 5259.1 362.52      118.9       8  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1