# 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
model.ini <- '
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
'

# model fitting
fitted.model <- sem(model = model.ini, 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               76.130
##   Degrees of freedom                                49
##   P-value (Chi-square)                           0.008
## 
## 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.648    0.648
##     vis_match  (b)    0.979    0.145    6.769    0.000    0.634    0.634
##   Fluency =~                                                            
##     VF_letter  (c)    1.000                               0.476    0.476
##     DF_sum     (d)    1.057    0.245    4.320    0.000    0.503    0.503
##   Shifting =~                                                           
##     TMT_switch (e)    1.000                               0.750    0.750
##     VF_switch  (f)    0.624    0.122    5.124    0.000    0.468    0.468
##   WM =~                                                                 
##     num_revrsd (g)    1.000                               0.622    0.622
##     tower      (h)    0.411    0.134    3.062    0.002    0.256    0.256
##   gF =~                                                                 
##     spatial_rl (i)    1.000                               0.576    0.576
##     qunt_rsnng (j)    1.360    0.216    6.307    0.000    0.784    0.784
##   Learning =~                                                           
##     AL_resid   (k)    1.000                               0.762    0.762
##     AL_hints   (l)    0.957    0.162    5.897    0.000    0.729    0.729
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   Fluency ~                                                             
##     Attention  (r)    0.650    0.133    4.881    0.000    0.885    0.885
##   Shifting ~                                                            
##     Attention  (s)    1.036    0.150    6.896    0.000    0.895    0.895
##   WM ~                                                                  
##     Attention  (t)    0.545    0.115    4.735    0.000    0.568    0.568
##   gF ~                                                                  
##     WM         (u)    0.862    0.171    5.029    0.000    0.930    0.930
##   Learning ~                                                            
##     WM         (v)    0.752    0.146    5.158    0.000    0.614    0.614
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .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
##  .gF ~~                                                                 
##    .Learning          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .CWIT              0.580    0.075    7.723    0.000    0.580    0.580
##    .vis_match         0.598    0.076    7.905    0.000    0.598    0.598
##    .VF_letter         0.774    0.097    7.987    0.000    0.774    0.774
##    .DF_sum            0.747    0.100    7.506    0.000    0.747    0.747
##    .TMT_switch        0.437    0.107    4.076    0.000    0.437    0.437
##    .VF_switch         0.781    0.085    9.136    0.000    0.781    0.781
##    .num_reversed      0.613    0.081    7.554    0.000    0.613    0.613
##    .tower             0.935    0.094    9.963    0.000    0.935    0.935
##    .spatial_rel       0.668    0.079    8.404    0.000    0.668    0.668
##    .quant_reasonng    0.386    0.092    4.203    0.000    0.386    0.386
##    .AL_resid          0.419    0.099    4.233    0.000    0.419    0.419
##    .AL_hints          0.469    0.094    4.966    0.000    0.469    0.469
##     Attention         0.420    0.094    4.485    0.000    1.000    1.000
##    .Fluency           0.049    0.059    0.823    0.410    0.216    0.216
##    .Shifting          0.112    0.102    1.100    0.271    0.199    0.199
##    .WM                0.262    0.069    3.784    0.000    0.677    0.677
##    .gF                0.045    0.050    0.899    0.368    0.135    0.135
##    .Learning          0.362    0.095    3.812    0.000    0.623    0.623
## 
## R-Square:
##                    Estimate
##     CWIT              0.420
##     vis_match         0.402
##     VF_letter         0.226
##     DF_sum            0.253
##     TMT_switch        0.563
##     VF_switch         0.219
##     num_reversed      0.387
##     tower             0.065
##     spatial_rel       0.332
##     quant_reasonng    0.614
##     AL_resid          0.581
##     AL_hints          0.531
##     Fluency           0.784
##     Shifting          0.801
##     WM                0.323
##     gF                0.865
##     Learning          0.377
fitMeasures(fitted.model)
##                npar                fmin               chisq 
##              29.000               0.182              76.130 
##                  df              pvalue      baseline.chisq 
##              49.000               0.008             543.661 
##         baseline.df     baseline.pvalue                 cfi 
##              66.000               0.000               0.943 
##                 tli                nnfi                 rfi 
##               0.923               0.923               0.811 
##                 nfi                pnfi                 ifi 
##               0.860               0.638               0.945 
##                 rni                logl   unrestricted.logl 
##               0.943           -3340.841           -3302.594 
##                 aic                 bic              ntotal 
##            6739.681            6836.609             210.000 
##                bic2               rmsea      rmsea.ci.lower 
##            6744.722               0.051               0.027 
##      rmsea.ci.upper        rmsea.pvalue                 rmr 
##               0.073               0.434               0.055 
##          rmr_nomean                srmr        srmr_bentler 
##               0.055               0.055               0.055 
## srmr_bentler_nomean         srmr_bollen  srmr_bollen_nomean 
##               0.055               0.055               0.055 
##          srmr_mplus   srmr_mplus_nomean               cn_05 
##               0.055               0.055             183.121 
##               cn_01                 gfi                agfi 
##             206.678               0.943               0.909 
##                pgfi                 mfi                ecvi 
##               0.592               0.937               0.642
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.089  0.000                                          
## VF_letter       -0.052 -0.026  0.000                                   
## DF_sum           0.000 -0.053  0.000  0.000                            
## TMT_switch      -0.020 -0.028 -0.002  0.036  0.000                     
## VF_switch        0.044 -0.063  0.053  0.003  0.000  0.000              
## num_reversed     0.013  0.076  0.114 -0.034  0.031  0.016  0.000       
## tower            0.100  0.046 -0.092  0.002  0.184  0.116 -0.013  0.000
## spatial_rel     -0.088 -0.019  0.043  0.047 -0.024 -0.064  0.025 -0.087
## quant_reasoning -0.180 -0.035  0.100  0.031  0.047 -0.035 -0.005  0.022
## AL_resid        -0.029  0.017  0.076  0.035  0.042 -0.031 -0.019 -0.049
## AL_hints        -0.035 -0.108  0.040  0.007  0.010  0.040 -0.084 -0.050
##                 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.002  0.012  0.000       
## AL_hints         0.065  0.053  0.000  0.000