library(pacman)
p_load(psych,lavaan,semTools,semPlot)
dta <-'
 1
 0.473   1
 0.480   0.620   1    
 0.272   0.304   0.323   1      
 0.199   0.229   0.175   0.291   1        
 0.291   0.440   0.407   0.449   0.450   1        
 0.272   0.378   0.283   0.384   0.391   0.600   1      
 0.246   0.397   0.345   0.491   0.474   0.659   0.605   1      
 0.172   0.313   0.315   0.312   0.316   0.472   0.432   0.482   1 '

samplecov <- 
    getCov(dta, names = c("X1", "X2", "X3", "X4",
                            "Y1", "Y2", "Y3", "Y4", "Y5"))
n <- 300

str(samplecov)
##  num [1:9, 1:9] 1 0.473 0.48 0.272 0.199 0.291 0.272 0.246 0.172 0.473 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:9] "X1" "X2" "X3" "X4" ...
##   ..$ : chr [1:9] "X1" "X2" "X3" "X4" ...
Model1 = '
    factor1 =~ X1+X2+X3+X4
    factor2 =~ Y1+Y2+Y3+Y4+Y5
'
fit1 <- sem(Model1, sample.cov=samplecov, sample.nobs=n,std.lv=TRUE)
summary(fit1, fit.measures=TRUE)
## lavaan 0.6.14 ended normally after 17 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        19
## 
##   Number of observations                           300
## 
## Model Test User Model:
##                                                       
##   Test statistic                                64.458
##   Degrees of freedom                                26
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               987.420
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.960
##   Tucker-Lewis Index (TLI)                       0.944
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3365.146
##   Loglikelihood unrestricted model (H1)      -3332.916
##                                                       
##   Akaike (AIC)                                6768.291
##   Bayesian (BIC)                              6838.663
##   Sample-size adjusted Bayesian (SABIC)       6778.406
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.070
##   90 Percent confidence interval - lower         0.049
##   90 Percent confidence interval - upper         0.092
##   P-value H_0: RMSEA <= 0.050                    0.059
##   P-value H_0: RMSEA >= 0.080                    0.244
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.066
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   factor1 =~                                          
##     X1                0.596    0.058   10.308    0.000
##     X2                0.783    0.054   14.379    0.000
##     X3                0.760    0.055   13.842    0.000
##     X4                0.474    0.060    7.890    0.000
##   factor2 =~                                          
##     Y1                0.548    0.057    9.657    0.000
##     Y2                0.817    0.050   16.269    0.000
##     Y3                0.732    0.052   13.936    0.000
##     Y4                0.814    0.050   16.174    0.000
##     Y5                0.587    0.056   10.496    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   factor1 ~~                                          
##     factor2           0.652    0.046   14.027    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .X1                0.641    0.060   10.654    0.000
##    .X2                0.383    0.051    7.476    0.000
##    .X3                0.420    0.052    8.110    0.000
##    .X4                0.772    0.068   11.425    0.000
##    .Y1                0.697    0.061   11.445    0.000
##    .Y2                0.329    0.040    8.312    0.000
##    .Y3                0.462    0.046   10.036    0.000
##    .Y4                0.334    0.040    8.404    0.000
##    .Y5                0.652    0.058   11.260    0.000
##     factor1           1.000                           
##     factor2           1.000
Model2 = '
    factor1 =~ X1+X2+X3+X4+Y1+Y2+Y3+Y4+Y5
'
fit2 <- sem(Model2, sample.cov=samplecov, sample.nobs=n)
summary(fit2, fit.measures=TRUE)
## lavaan 0.6.14 ended normally after 30 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        18
## 
##   Number of observations                           300
## 
## Model Test User Model:
##                                                       
##   Test statistic                               159.488
##   Degrees of freedom                                27
##   P-value (Chi-square)                           0.000
## 
## Model Test Baseline Model:
## 
##   Test statistic                               987.420
##   Degrees of freedom                                36
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.861
##   Tucker-Lewis Index (TLI)                       0.814
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -3412.660
##   Loglikelihood unrestricted model (H1)      -3332.916
##                                                       
##   Akaike (AIC)                                6861.321
##   Bayesian (BIC)                              6927.989
##   Sample-size adjusted Bayesian (SABIC)       6870.904
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.128
##   90 Percent confidence interval - lower         0.109
##   90 Percent confidence interval - upper         0.147
##   P-value H_0: RMSEA <= 0.050                    0.000
##   P-value H_0: RMSEA >= 0.080                    1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.080
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   factor1 =~                                          
##     X1                1.000                           
##     X2                1.381    0.223    6.196    0.000
##     X3                1.264    0.213    5.946    0.000
##     X4                1.368    0.222    6.170    0.000
##     Y1                1.281    0.214    5.984    0.000
##     Y2                1.943    0.278    6.989    0.000
##     Y3                1.723    0.255    6.745    0.000
##     Y4                1.921    0.276    6.967    0.000
##     Y5                1.398    0.224    6.227    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .X1                0.824    0.069   11.890    0.000
##    .X2                0.667    0.058   11.400    0.000
##    .X3                0.721    0.062   11.593    0.000
##    .X4                0.673    0.059   11.424    0.000
##    .Y1                0.713    0.062   11.568    0.000
##    .Y2                0.344    0.038    8.938    0.000
##    .Y3                0.484    0.046   10.410    0.000
##    .Y4                0.359    0.039    9.151    0.000
##    .Y5                0.659    0.058   11.370    0.000
##     factor1           0.173    0.049    3.547    0.000
anova(fit1, fit2)
## 
## Chi-Squared Difference Test
## 
##      Df    AIC    BIC   Chisq Chisq diff   RMSEA Df diff Pr(>Chisq)    
## fit1 26 6768.3 6838.7  64.458                                          
## fit2 27 6861.3 6928.0 159.488      95.03 0.55985       1  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1