# loading the required libraries
library(knitr)
library(foreign)
library(lavaan)
## This is lavaan 0.5-22
## lavaan is BETA software! Please report any bugs.
# loading the data
setwd(dir = "/Users/ivanropovik/OneDrive/Projects/APVV IK/DATA")
data <- read.csv("APVV_Analogies_SEmodel.csv", sep = ";")

# Pearson's correlations
round(cor(data[,1:12], method = "pearson"), 3)
##                 vis_match   CWIT VF_letter DF_sum TMT_switch VF_switch
## vis_match           1.000 -0.500     0.241  0.230     -0.398     0.202
## CWIT               -0.500  1.000    -0.221 -0.289      0.415    -0.315
## VF_letter           0.241 -0.221     1.000  0.239     -0.280     0.230
## DF_sum              0.230 -0.289     0.239  1.000     -0.335     0.190
## TMT_switch         -0.398  0.415    -0.280 -0.335      1.000    -0.351
## VF_switch           0.202 -0.315     0.230  0.190     -0.351     1.000
## num_reversed        0.300 -0.243     0.263  0.123     -0.269     0.165
## tower               0.138 -0.195    -0.031  0.067     -0.282     0.177
## spatial_rel         0.174 -0.109     0.172  0.182     -0.181     0.064
## quant_reasoning     0.228 -0.088     0.274  0.215     -0.325     0.139
## AL_resid            0.185 -0.144     0.188  0.154     -0.220     0.080
## AL_hints           -0.053  0.129    -0.147 -0.120      0.180    -0.146
##                 num_reversed  tower spatial_rel quant_reasoning AL_resid
## vis_match              0.300  0.138       0.174           0.228    0.185
## CWIT                  -0.243 -0.195      -0.109          -0.088   -0.144
## VF_letter              0.263 -0.031       0.172           0.274    0.188
## DF_sum                 0.123  0.067       0.182           0.215    0.154
## TMT_switch            -0.269 -0.282      -0.181          -0.325   -0.220
## VF_switch              0.165  0.177       0.064           0.139    0.080
## num_reversed           1.000  0.146       0.359           0.449    0.272
## tower                  0.146  1.000       0.050           0.209    0.071
## spatial_rel            0.359  0.050       1.000           0.452    0.249
## quant_reasoning        0.449  0.209       0.452           1.000    0.354
## AL_resid               0.272  0.071       0.249           0.354    1.000
## AL_hints              -0.194 -0.065      -0.305          -0.380   -0.555
##                 AL_hints
## vis_match         -0.053
## CWIT               0.129
## VF_letter         -0.147
## DF_sum            -0.120
## TMT_switch         0.180
## VF_switch         -0.146
## num_reversed      -0.194
## tower             -0.065
## spatial_rel       -0.305
## quant_reasoning   -0.380
## AL_resid          -0.555
## AL_hints           1.000
# SD's
lapply(data[,1:12], sd)
## $vis_match
## [1] 5.976004
## 
## $CWIT
## [1] 26.59799
## 
## $VF_letter
## [1] 7.054964
## 
## $DF_sum
## [1] 5.12479
## 
## $TMT_switch
## [1] 45.42468
## 
## $VF_switch
## [1] 3.069963
## 
## $num_reversed
## [1] 3.325602
## 
## $tower
## [1] 3.385887
## 
## $spatial_rel
## [1] 3.767991
## 
## $quant_reasoning
## [1] 3.726717
## 
## $AL_resid
## [1] 0.9976048
## 
## $AL_hints
## [1] 3.415043
#attach(data)

# rescaling the variables
data$CWIT <- with(data, ((max(CWIT) + 1) - CWIT))
data$TMT_switch <- with(data, ((max(TMT_switch) + 1) - TMT_switch))
data$AL_hints <- with(data, ((max(AL_hints) + 1) - AL_hints))

# specification of the model
respc.model <- '
Attention =~ a*CWIT + b*vis_match
Fluency =~ c*VF_letter + d*DF_sum
Shifting =~ e*TMT_switch + f*VF_switch
WM =~ g*num_reversed + h*tower
gF =~ i*spatial_rel + j*quant_reasoning
Learning =~ k*AL_resid + l*AL_hints
Fluency ~ r*Attention
Shifting ~ s*Attention
WM ~ t*Attention
gF ~ u*WM
Learning ~ v*WM
vis_match ~~ CWIT
gF ~~ Learning
'

# model fitting
fitted.model <- sem(model = respc.model, data = data, meanstructure = FALSE,
                    std.lv = FALSE, estimator = "ML", test = "standard",
                    orthogonal = TRUE, std.ov = TRUE, likelihood = "wishart", bootstrap = 2000)

# results
summary(fitted.model, standardized = TRUE, rsquare = TRUE)
## lavaan (0.5-22) converged normally after  39 iterations
## 
##   Number of observations                           210
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               61.485
##   Degrees of freedom                                47
##   P-value (Chi-square)                           0.076
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   Attention =~                                                          
##     CWIT       (a)    1.000                               0.567    0.567
##     vis_match  (b)    0.968    0.146    6.632    0.000    0.549    0.549
##   Fluency =~                                                            
##     VF_letter  (c)    1.000                               0.481    0.481
##     DF_sum     (d)    1.034    0.235    4.393    0.000    0.497    0.497
##   Shifting =~                                                           
##     TMT_switch (e)    1.000                               0.750    0.750
##     VF_switch  (f)    0.624    0.121    5.168    0.000    0.468    0.468
##   WM =~                                                                 
##     num_revrsd (g)    1.000                               0.665    0.665
##     tower      (h)    0.424    0.131    3.235    0.001    0.282    0.282
##   gF =~                                                                 
##     spatial_rl (i)    1.000                               0.575    0.575
##     qunt_rsnng (j)    1.365    0.211    6.461    0.000    0.785    0.785
##   Learning =~                                                           
##     AL_resid   (k)    1.000                               0.732    0.732
##     AL_hints   (l)    1.037    0.171    6.080    0.000    0.759    0.759
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   Fluency ~                                                             
##     Attention  (r)    0.778    0.167    4.662    0.000    0.917    0.917
##   Shifting ~                                                            
##     Attention  (s)    1.264    0.215    5.878    0.000    0.955    0.955
##   WM ~                                                                  
##     Attention  (t)    0.752    0.161    4.658    0.000    0.640    0.640
##   gF ~                                                                  
##     WM         (u)    0.730    0.159    4.603    0.000    0.844    0.844
##   Learning ~                                                            
##     WM         (v)    0.538    0.145    3.696    0.000    0.489    0.489
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .CWIT ~~                                                               
##    .vis_match         0.189    0.065    2.934    0.003    0.189    0.275
##  .gF ~~                                                                 
##    .Learning          0.093    0.046    2.001    0.045    0.470    0.470
##  .Fluency ~~                                                            
##    .Shifting          0.000                               0.000    0.000
##    .gF                0.000                               0.000    0.000
##    .Learning          0.000                               0.000    0.000
##  .Shifting ~~                                                           
##    .gF                0.000                               0.000    0.000
##    .Learning          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .CWIT              0.679    0.083    8.169    0.000    0.679    0.679
##    .vis_match         0.699    0.084    8.332    0.000    0.699    0.699
##    .VF_letter         0.769    0.097    7.934    0.000    0.769    0.769
##    .DF_sum            0.753    0.098    7.648    0.000    0.753    0.753
##    .TMT_switch        0.437    0.106    4.108    0.000    0.437    0.437
##    .VF_switch         0.781    0.085    9.158    0.000    0.781    0.781
##    .num_reversed      0.557    0.092    6.070    0.000    0.557    0.557
##    .tower             0.920    0.094    9.830    0.000    0.920    0.920
##    .spatial_rel       0.669    0.079    8.512    0.000    0.669    0.669
##    .quant_reasonng    0.383    0.089    4.286    0.000    0.383    0.383
##    .AL_resid          0.464    0.092    5.037    0.000    0.464    0.464
##    .AL_hints          0.424    0.096    4.431    0.000    0.424    0.424
##     Attention         0.321    0.087    3.671    0.000    1.000    1.000
##    .Fluency           0.037    0.061    0.607    0.544    0.160    0.160
##    .Shifting          0.050    0.106    0.471    0.638    0.089    0.089
##    .WM                0.261    0.077    3.376    0.001    0.590    0.590
##    .gF                0.095    0.054    1.769    0.077    0.287    0.287
##    .Learning          0.408    0.100    4.064    0.000    0.761    0.761
## 
## R-Square:
##                    Estimate
##     CWIT              0.321
##     vis_match         0.301
##     VF_letter         0.231
##     DF_sum            0.247
##     TMT_switch        0.563
##     VF_switch         0.219
##     num_reversed      0.443
##     tower             0.080
##     spatial_rel       0.331
##     quant_reasonng    0.617
##     AL_resid          0.536
##     AL_hints          0.576
##     Fluency           0.840
##     Shifting          0.911
##     WM                0.410
##     gF                0.713
##     Learning          0.239
fitMeasures(fitted.model)
##                npar                fmin               chisq 
##              31.000               0.147              61.485 
##                  df              pvalue      baseline.chisq 
##              47.000               0.076             543.661 
##         baseline.df     baseline.pvalue                 cfi 
##              66.000               0.000               0.970 
##                 tli                nnfi                 rfi 
##               0.957               0.957               0.841 
##                 nfi                pnfi                 ifi 
##               0.887               0.632               0.971 
##                 rni                logl   unrestricted.logl 
##               0.970           -3333.484           -3302.594 
##                 aic                 bic              ntotal 
##            6728.967            6832.580             210.000 
##                bic2               rmsea      rmsea.ci.lower 
##            6734.355               0.038               0.000 
##      rmsea.ci.upper        rmsea.pvalue                 rmr 
##               0.063               0.758               0.048 
##          rmr_nomean                srmr        srmr_bentler 
##               0.048               0.048               0.048 
## srmr_bentler_nomean         srmr_bollen  srmr_bollen_nomean 
##               0.048               0.048               0.048 
##          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.048               0.048             218.551 
##               cn_01                 gfi                agfi 
##             247.248               0.953               0.922 
##                pgfi                 mfi                ecvi 
##               0.574               0.966               0.591
residuals(fitted.model, type = "cor")$cor
##                 CWIT   vs_mtc VF_ltt DF_sum TMT_sw VF_swt nm_rvr tower 
## CWIT             0.000                                                 
## vis_match        0.000  0.000                                          
## VF_letter       -0.029 -0.001  0.000                                   
## DF_sum           0.030 -0.020  0.000  0.000                            
## TMT_switch       0.009  0.005 -0.036  0.008  0.000                     
## VF_switch        0.062 -0.043  0.033 -0.014  0.000  0.000              
## num_reversed     0.001  0.066  0.075 -0.071 -0.037 -0.026  0.000       
## tower            0.092  0.039 -0.111 -0.015  0.152  0.096 -0.041  0.000
## spatial_rel     -0.067  0.004  0.035  0.041 -0.042 -0.075  0.036 -0.087
## quant_reasoning -0.152 -0.005  0.087  0.022  0.021 -0.051  0.008  0.022
## AL_resid         0.014  0.060  0.087  0.049  0.056 -0.022  0.034 -0.030
## AL_hints        -0.005 -0.077  0.043  0.012  0.010  0.040 -0.052 -0.040
##                 sptl_r qnt_rs AL_rsd AL_hnt
## CWIT                                       
## vis_match                                  
## VF_letter                                  
## DF_sum                                     
## TMT_switch                                 
## VF_switch                                  
## num_reversed                               
## tower                                      
## spatial_rel      0.000                     
## quant_reasoning  0.000  0.000              
## AL_resid        -0.017 -0.010  0.000       
## AL_hints         0.029  0.003  0.000  0.000