library(lavaan)
setwd(mypath)  # set working directory to where the data file is stored

data1 <- read.table("causacian.csv", sep=",", header = F)
data2 <- read.table("african-american.csv", sep = ",", header = F)

data1$race <- 1
data2$race <- 2
data <- rbind(data1, data2)

colnames(data) <- c('hand', 'number', 'word',
                    'gestalt', 'triangle', 'matrix', 'spatial', 'photo','race')

cfa.constrained <- '

SEQUENT =~ hand + number + word
SIMULTAN =~ triangle + matrix + spatial

hand ~ 1
number ~ 1
word ~ 1
triangle ~ 1
matrix ~ 1
spatial ~ 1


'

fit.constrained <- sem(cfa.constrained, data, group = "race", group.equal = c("loadings"))
summary(fit.constrained, fit.measures = T, standardized = T)
## lavaan 0.6.15 ended normally after 78 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        38
##   Number of equality constraints                     4
## 
##   Number of observations per group:                   
##     1                                               86
##     2                                               86
## 
## Model Test User Model:
##                                                       
##   Test statistic                                31.643
##   Degrees of freedom                                20
##   P-value (Chi-square)                           0.047
##   Test statistic for each group:
##     1                                           20.207
##     2                                           11.436
## 
## Model Test Baseline Model:
## 
##   Test statistic                               200.885
##   Degrees of freedom                                30
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.932
##   Tucker-Lewis Index (TLI)                       0.898
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2436.281
##   Loglikelihood unrestricted model (H1)      -2420.459
##                                                       
##   Akaike (AIC)                                4940.561
##   Bayesian (BIC)                              5047.576
##   Sample-size adjusted Bayesian (SABIC)       4939.915
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.082
##   90 Percent confidence interval - lower         0.009
##   90 Percent confidence interval - upper         0.134
##   P-value H_0: RMSEA <= 0.050                    0.162
##   P-value H_0: RMSEA >= 0.080                    0.564
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.082
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT =~                                                            
##     hand              1.000                               1.526    0.552
##     number  (.p2.)    0.960    0.212    4.531    0.000    1.465    0.718
##     word    (.p3.)    1.004    0.223    4.508    0.000    1.532    0.761
##   SIMULTAN =~                                                           
##     triangl           1.000                               1.589    0.700
##     matrix  (.p5.)    1.459    0.297    4.918    0.000    2.318    0.678
##     spatial (.p6.)    1.434    0.336    4.266    0.000    2.278    0.186
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT ~~                                                            
##     SIMULTAN          1.412    0.491    2.874    0.004    0.582    0.582
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             -0.001    0.298   -0.003    0.998   -0.001   -0.000
##    .number           -0.001    0.220   -0.003    0.998   -0.001   -0.000
##    .word              0.001    0.217    0.004    0.997    0.001    0.000
##    .triangle         -0.000    0.245   -0.000    1.000   -0.000   -0.000
##    .matrix            0.001    0.368    0.004    0.997    0.001    0.000
##    .spatial          -0.003    1.318   -0.002    0.998   -0.003   -0.000
##     SEQUENT           0.000                               0.000    0.000
##     SIMULTAN          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand              5.306    0.942    5.635    0.000    5.306    0.695
##    .number            2.020    0.485    4.162    0.000    2.020    0.485
##    .word              1.701    0.481    3.536    0.000    1.701    0.420
##    .triangle          2.627    0.712    3.693    0.000    2.627    0.510
##    .matrix            6.300    1.581    3.984    0.000    6.300    0.540
##    .spatial         144.165   22.248    6.480    0.000  144.165    0.965
##     SEQUENT           2.329    0.916    2.544    0.011    1.000    1.000
##     SIMULTAN          2.524    0.804    3.137    0.002    1.000    1.000
## 
## 
## Group 2 [2]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT =~                                                            
##     hand              1.000                               1.119    0.263
##     number  (.p2.)    0.960    0.212    4.531    0.000    1.074    0.566
##     word    (.p3.)    1.004    0.223    4.508    0.000    1.123    0.727
##   SIMULTAN =~                                                           
##     triangl           1.000                               1.249    0.624
##     matrix  (.p5.)    1.459    0.297    4.918    0.000    1.823    0.543
##     spatial (.p6.)    1.434    0.336    4.266    0.000    1.791    0.769
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT ~~                                                            
##     SIMULTAN          0.752    0.303    2.477    0.013    0.538    0.538
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             -0.002    0.459   -0.004    0.997   -0.002   -0.000
##    .number           -0.001    0.205   -0.005    0.996   -0.001   -0.001
##    .word              0.000    0.167    0.001    0.999    0.000    0.000
##    .triangle         -0.000    0.216   -0.002    0.998   -0.000   -0.000
##    .matrix            0.000    0.362    0.001    0.999    0.000    0.000
##    .spatial          -0.001    0.251   -0.003    0.998   -0.001   -0.000
##     SEQUENT           0.000                               0.000    0.000
##     SIMULTAN          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             16.881    2.648    6.375    0.000   16.881    0.931
##    .number            2.449    0.479    5.115    0.000    2.449    0.680
##    .word              1.123    0.360    3.120    0.002    1.123    0.471
##    .triangle          2.451    0.507    4.832    0.000    2.451    0.611
##    .matrix            7.934    1.437    5.520    0.000    7.934    0.705
##    .spatial           2.214    0.771    2.872    0.004    2.214    0.408
##     SEQUENT           1.252    0.579    2.164    0.030    1.000    1.000
##     SIMULTAN          1.561    0.547    2.853    0.004    1.000    1.000
mod.constrained <- modindices(fit.constrained, sort = T)
mod.constrained[mod.constrained$mi >= 3.841, ]   
##         lhs op      rhs block group level    mi    epc sepc.lv sepc.all
## 62   number ~~     word     1     1     1 8.130  2.023   2.023    1.091
## 87     word ~~ triangle     2     2     1 5.747 -0.649  -0.649   -0.391
## 69 triangle ~~   matrix     1     1     1 4.679 -5.443  -5.443   -1.338
## 73  SEQUENT =~   matrix     2     2     1 4.173  0.933   1.043    0.311
## 60     hand ~~   matrix     1     1     1 4.108  1.603   1.603    0.277
##    sepc.nox
## 62    1.091
## 87   -0.391
## 69   -1.338
## 73    0.311
## 60    0.277
### different from the Mplus output


### Release the constraints
## free the loading based on the modification indices obtained in Mplus (to be consistent with your practice in class)

cfa.unconstrained <- '

SEQUENT =~ hand + number + word
SIMULTAN =~ triangle + matrix + c(a,b)*spatial

hand~1
number~1
word~1

triangle~1
matrix ~ 1
spatial~1

'

fit.unconstrained <- sem(cfa.unconstrained, data, group="race", group.equal=c("loadings"))
summary(fit.unconstrained, fit.measures = T, standardized = T)
## lavaan 0.6.15 ended normally after 79 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        38
##   Number of equality constraints                     3
## 
##   Number of observations per group:                   
##     1                                               86
##     2                                               86
## 
## Model Test User Model:
##                                                       
##   Test statistic                                23.515
##   Degrees of freedom                                19
##   P-value (Chi-square)                           0.215
##   Test statistic for each group:
##     1                                           12.784
##     2                                           10.731
## 
## Model Test Baseline Model:
## 
##   Test statistic                               200.885
##   Degrees of freedom                                30
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.974
##   Tucker-Lewis Index (TLI)                       0.958
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -2432.216
##   Loglikelihood unrestricted model (H1)      -2420.459
##                                                       
##   Akaike (AIC)                                4934.433
##   Bayesian (BIC)                              5044.595
##   Sample-size adjusted Bayesian (SABIC)       4933.768
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.053
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.113
##   P-value H_0: RMSEA <= 0.050                    0.436
##   P-value H_0: RMSEA >= 0.080                    0.270
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.062
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT =~                                                            
##     hand              1.000                               1.536    0.556
##     number  (.p2.)    0.949    0.209    4.549    0.000    1.458    0.713
##     word    (.p3.)    0.998    0.220    4.530    0.000    1.533    0.763
##   SIMULTAN =~                                                           
##     triangl           1.000                               1.526    0.684
##     matrix  (.p5.)    1.449    0.267    5.428    0.000    2.212    0.650
##     spatial    (a)    4.264    1.186    3.595    0.000    6.508    0.512
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT ~~                                                            
##     SIMULTAN          1.371    0.473    2.901    0.004    0.585    0.585
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             -0.001    0.298   -0.003    0.998   -0.001   -0.000
##    .number           -0.001    0.221   -0.003    0.998   -0.001   -0.000
##    .word              0.001    0.217    0.004    0.997    0.001    0.000
##    .triangle         -0.000    0.241   -0.000    1.000   -0.000   -0.000
##    .matrix            0.001    0.367    0.004    0.997    0.001    0.000
##    .spatial          -0.003    1.372   -0.002    0.998   -0.003   -0.000
##     SEQUENT           0.000                               0.000    0.000
##     SIMULTAN          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand              5.278    0.939    5.618    0.000    5.278    0.691
##    .number            2.060    0.486    4.238    0.000    2.060    0.492
##    .word              1.683    0.478    3.517    0.000    1.683    0.417
##    .triangle          2.649    0.635    4.170    0.000    2.649    0.532
##    .matrix            6.670    1.457    4.577    0.000    6.670    0.577
##    .spatial         119.507   21.702    5.507    0.000  119.507    0.738
##     SEQUENT           2.359    0.921    2.560    0.010    1.000    1.000
##     SIMULTAN          2.329    0.734    3.175    0.002    1.000    1.000
## 
## 
## Group 2 [2]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT =~                                                            
##     hand              1.000                               1.125    0.264
##     number  (.p2.)    0.949    0.209    4.549    0.000    1.068    0.564
##     word    (.p3.)    0.998    0.220    4.530    0.000    1.123    0.725
##   SIMULTAN =~                                                           
##     triangl           1.000                               1.369    0.671
##     matrix  (.p5.)    1.449    0.267    5.428    0.000    1.984    0.586
##     spatial    (b)    1.190    0.274    4.345    0.000    1.629    0.712
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   SEQUENT ~~                                                            
##     SIMULTAN          0.833    0.328    2.543    0.011    0.541    0.541
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             -0.002    0.459   -0.004    0.997   -0.002   -0.000
##    .number           -0.001    0.204   -0.005    0.996   -0.001   -0.001
##    .word              0.000    0.167    0.001    0.999    0.000    0.000
##    .triangle         -0.000    0.220   -0.002    0.998   -0.000   -0.000
##    .matrix            0.000    0.365    0.001    0.999    0.000    0.000
##    .spatial          -0.001    0.247   -0.003    0.998   -0.001   -0.000
##     SEQUENT           0.000                               0.000    0.000
##     SIMULTAN          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .hand             16.860    2.646    6.372    0.000   16.860    0.930
##    .number            2.439    0.476    5.120    0.000    2.439    0.681
##    .word              1.135    0.362    3.138    0.002    1.135    0.474
##    .triangle          2.289    0.518    4.416    0.000    2.289    0.550
##    .matrix            7.515    1.424    5.277    0.000    7.515    0.656
##    .spatial           2.578    0.691    3.733    0.000    2.578    0.493
##     SEQUENT           1.266    0.583    2.171    0.030    1.000    1.000
##     SIMULTAN          1.874    0.606    3.094    0.002    1.000    1.000
mod.constrained <- modindices(fit.constrained, sort = T)
mod.constrained[mod.constrained$mi >= 3.841, ]
##         lhs op      rhs block group level    mi    epc sepc.lv sepc.all
## 62   number ~~     word     1     1     1 8.130  2.023   2.023    1.091
## 87     word ~~ triangle     2     2     1 5.747 -0.649  -0.649   -0.391
## 69 triangle ~~   matrix     1     1     1 4.679 -5.443  -5.443   -1.338
## 73  SEQUENT =~   matrix     2     2     1 4.173  0.933   1.043    0.311
## 60     hand ~~   matrix     1     1     1 4.108  1.603   1.603    0.277
##    sepc.nox
## 62    1.091
## 87   -0.391
## 69   -1.338
## 73    0.311
## 60    0.277

© Copyright 2024 @Yi Feng and @Gregory R. Hancock.