# Setting up environment
library(lavaan)
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
# Creating var/cov matrix for males (N = 322)

lower_m <- '
102.0100    
36.7781   104.0400    
16.3423    24.4999   101.0025    
11.5271    14.4228    39.6885   102.0100    
8.8597    17.1666    45.1044    39.7657   104.0400    
14.6167    21.2196    32.4218    39.2824    32.4957   101.0025
'

males.cov <-
  getCov(lower_m, names = c("Blocks", "Geo", "Info", "Arith",
                            "Colors", "SimDiff"))
# Creating var/cov matrix for females (N = 262)

lower_f <- '
98.0100    
42.1087   104.0400    
22.2305    23.5149    99.6004    
13.6986    12.0533    23.9899   102.0100    
15.7608    17.3548    32.9679    32.1584    99.0025    
14.5703    24.1225    38.4929    37.7255    27.3690   103.0225
'

females.cov <- getCov(lower_f, names = c("Blocks", "Geo", "Info", "Arith",
                            "Colors", "SimDiff"))
# Fitting one-factor model for males
Model1 <- '
F1 =~ Blocks + Geo + Info + Arith + Colors + SimDiff
'
fit1 <- cfa(Model1, sample.cov = males.cov, sample.nobs = 322)
fit1
## lavaan 0.6.15 ended normally after 60 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                           322
## 
## Model Test User Model:
##                                                       
##   Test statistic                                41.247
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.000
summary(fit1, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 60 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                           322
## 
## Model Test User Model:
##                                                       
##   Test statistic                                41.247
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               290.312
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.883
##   Tucker-Lewis Index (TLI)                       0.805
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -7084.819
##   Loglikelihood unrestricted model (H1)      -7064.196
##                                                       
##   Akaike (AIC)                               14193.639
##   Bayesian (BIC)                             14238.934
##   Sample-size adjusted Bayesian (SABIC)      14200.871
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.105
##   90 Percent confidence interval - lower         0.074
##   90 Percent confidence interval - upper         0.139
##   P-value H_0: RMSEA <= 0.050                    0.003
##   P-value H_0: RMSEA >= 0.080                    0.913
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.067
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 =~                                                                 
##     Blocks            1.000                               2.519    0.250
##     Geo               1.368    0.433    3.163    0.002    3.447    0.338
##     Info              2.618    0.709    3.695    0.000    6.594    0.657
##     Arith             2.452    0.669    3.665    0.000    6.177    0.613
##     Colors            2.490    0.679    3.668    0.000    6.272    0.616
##     SimDiff           2.174    0.604    3.600    0.000    5.475    0.546
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Blocks           95.349    7.713   12.361    0.000   95.349    0.938
##    .Geo              91.835    7.621   12.050    0.000   91.835    0.885
##    .Info             57.203    6.492    8.811    0.000   57.203    0.568
##    .Arith            63.539    6.597    9.631    0.000   63.539    0.625
##    .Colors           64.383    6.723    9.577    0.000   64.383    0.621
##    .SimDiff          70.712    6.701   10.553    0.000   70.712    0.702
##     F1                6.345    3.285    1.931    0.053    1.000    1.000
# Fitting two-factor model (block design and geometric on F1) for males - provide sample size when covariance/variance 
Model3 <-'
F1 =~ Blocks + Geo 
F2 =~ Info + Arith + Colors + SimDiff
'
fit3 <- cfa(Model3, sample.cov = males.cov, sample.nobs = 322)
fit3
## lavaan 0.6.15 ended normally after 97 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           322
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 9.104
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.334
summary(fit3, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 97 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           322
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 9.104
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.334
## 
## Model Test Baseline Model:
## 
##   Test statistic                               290.312
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.996
##   Tucker-Lewis Index (TLI)                       0.992
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -7068.748
##   Loglikelihood unrestricted model (H1)      -7064.196
##                                                       
##   Akaike (AIC)                               14163.496
##   Bayesian (BIC)                             14212.565
##   Sample-size adjusted Bayesian (SABIC)      14171.331
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.021
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.071
##   P-value H_0: RMSEA <= 0.050                    0.787
##   P-value H_0: RMSEA >= 0.080                    0.020
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.027
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 =~                                                                 
##     Blocks            1.000                               4.928    0.489
##     Geo               1.510    0.482    3.133    0.002    7.440    0.731
##   F2 =~                                                                 
##     Info              1.000                               6.588    0.657
##     Arith             0.950    0.123    7.738    0.000    6.260    0.621
##     Colors            0.968    0.125    7.767    0.000    6.374    0.626
##     SimDiff           0.829    0.116    7.155    0.000    5.459    0.544
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 ~~                                                                 
##     F2               13.554    4.566    2.969    0.003    0.417    0.417
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Blocks           77.406    9.746    7.942    0.000   77.406    0.761
##    .Geo              48.368   17.736    2.727    0.006   48.368    0.466
##    .Info             57.288    6.537    8.764    0.000   57.288    0.569
##    .Arith            62.502    6.617    9.446    0.000   62.502    0.615
##    .Colors           63.083    6.743    9.355    0.000   63.083    0.608
##    .SimDiff          70.887    6.723   10.544    0.000   70.887    0.704
##     F1               24.287    9.208    2.638    0.008    1.000    1.000
##     F2               43.401    8.058    5.386    0.000    1.000    1.000
# Fitting one-factor model for females
Model4 <- '
F1 =~ Blocks + Geo + Info + Arith + Colors + SimDiff
'
fit4 <- cfa(Model4, sample.cov = females.cov, sample.nobs = 262)
fit4
## lavaan 0.6.15 ended normally after 60 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                           262
## 
## Model Test User Model:
##                                                       
##   Test statistic                                42.586
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.000
summary(fit4, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 60 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                           262
## 
## Model Test User Model:
##                                                       
##   Test statistic                                42.586
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               214.381
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.832
##   Tucker-Lewis Index (TLI)                       0.719
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5768.557
##   Loglikelihood unrestricted model (H1)      -5747.264
##                                                       
##   Akaike (AIC)                               11561.114
##   Bayesian (BIC)                             11603.934
##   Sample-size adjusted Bayesian (SABIC)      11565.889
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.119
##   90 Percent confidence interval - lower         0.085
##   90 Percent confidence interval - upper         0.157
##   P-value H_0: RMSEA <= 0.050                    0.001
##   P-value H_0: RMSEA >= 0.080                    0.968
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.070
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 =~                                                                 
##     Blocks            1.000                               3.731    0.378
##     Geo               1.133    0.287    3.939    0.000    4.225    0.415
##     Info              1.583    0.350    4.520    0.000    5.907    0.593
##     Arith             1.331    0.313    4.252    0.000    4.966    0.493
##     Colors            1.364    0.316    4.316    0.000    5.088    0.512
##     SimDiff           1.629    0.359    4.533    0.000    6.079    0.600
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Blocks           83.718    7.933   10.553    0.000   83.718    0.857
##    .Geo              85.792    8.309   10.325    0.000   85.792    0.828
##    .Info             64.332    7.592    8.474    0.000   64.332    0.648
##    .Arith            76.962    7.928    9.707    0.000   76.962    0.757
##    .Colors           72.735    7.648    9.510    0.000   72.735    0.737
##    .SimDiff          65.679    7.853    8.363    0.000   65.679    0.640
##     F1               13.917    5.357    2.598    0.009    1.000    1.000
# Fitting two-factor model for females (blocks and geo on f1)
model5 <-'
F1 =~ Blocks + Geo 
F2 =~ Info + Arith + Colors + SimDiff
'
fit5 <- cfa(model5, sample.cov = females.cov, sample.nobs = 262)
fit5
## lavaan 0.6.15 ended normally after 96 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           262
## 
## Model Test User Model:
##                                                       
##   Test statistic                                11.669
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.167
summary(fit5, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.15 ended normally after 96 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        13
## 
##   Number of observations                           262
## 
## Model Test User Model:
##                                                       
##   Test statistic                                11.669
##   Degrees of freedom                                 8
##   P-value (Chi-square)                           0.167
## 
## Model Test Baseline Model:
## 
##   Test statistic                               214.381
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.982
##   Tucker-Lewis Index (TLI)                       0.965
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -5753.099
##   Loglikelihood unrestricted model (H1)      -5747.264
##                                                       
##   Akaike (AIC)                               11532.197
##   Bayesian (BIC)                             11578.586
##   Sample-size adjusted Bayesian (SABIC)      11537.370
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.042
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.090
##   P-value H_0: RMSEA <= 0.050                    0.549
##   P-value H_0: RMSEA >= 0.080                    0.106
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.032
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 =~                                                                 
##     Blocks            1.000                               5.928    0.600
##     Geo               1.194    0.313    3.818    0.000    7.076    0.695
##   F2 =~                                                                 
##     Info              1.000                               5.892    0.592
##     Arith             0.883    0.159    5.566    0.000    5.205    0.516
##     Colors            0.879    0.157    5.599    0.000    5.180    0.522
##     SimDiff           1.074    0.178    6.039    0.000    6.328    0.625
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   F1 ~~                                                                 
##     F2               17.156    4.878    3.517    0.000    0.491    0.491
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .Blocks           62.489   10.493    5.955    0.000   62.489    0.640
##    .Geo              53.578   13.595    3.941    0.000   53.578    0.517
##    .Info             64.500    7.719    8.356    0.000   64.500    0.650
##    .Arith            74.532    7.941    9.385    0.000   74.532    0.733
##    .Colors           71.788    7.699    9.324    0.000   71.788    0.728
##    .SimDiff          62.588    8.046    7.779    0.000   62.588    0.610
##     F1               35.147   11.102    3.166    0.002    1.000    1.000
##     F2               34.721    8.439    4.114    0.000    1.000    1.000
#expected var/voc matrix for 2 factor model females 
#perfectly to fit the 
SMat_females <- fitted (fit5)
SMat_females
## $cov
##          Blocks     Geo    Info   Arith  Colors  SimDff
## Blocks   97.636                                        
## Geo      41.948 103.643                                
## Info     17.156  20.475  99.220                        
## Arith    15.153  18.086  30.668 101.621                
## Colors   15.083  18.002  30.525  26.963  98.625        
## SimDiff  18.423  21.988  37.286  32.934  32.781 102.629
#residual for females 2 factor model 
#negative over predicted and positive is under predicted 
#any above 3 will be problematic 

ResMat_females <- resid(fit5)
ResMat_females
## $type
## [1] "raw"
## 
## $cov
##         Blocks    Geo   Info  Arith Colors SimDff
## Blocks   0.000                                   
## Geo      0.000  0.000                            
## Info     4.990  2.950  0.000                     
## Arith   -1.507 -6.078 -6.770  0.000              
## Colors   0.618 -0.713  2.317  5.073  0.000       
## SimDiff -3.909  2.042  1.060  4.647 -5.516  0.000
#covMat <- cov(cfadata, use = "complete.obs")
#covMat
#corMat <- cor(cfadata, use ="complete.obs", method = "pearson")
#corMat