1 Basics

1.1 Load in data

In this case, we’ll simulate data.

demo.model <- '
y ~ .5*f  #strength of regression with external criterion

f =~ .8*x1 + .8*x2 + .8*x3 + .8*x4 + .8*x5  #definition of factor f with loadings on 5 items

x1 ~~ (1-.8^2)*x1 #residual variances. Note that by using 1-squared loading, we achieve a total variability of 1.0 in each indicator (standardized)
x2 ~~ (1-.8^2)*x2
x3 ~~ (1-.8^2)*x3
x4 ~~ (1-.8^2)*x4
x5 ~~ (1-.8^2)*x5
'

# generate data; note, standardized lv is default
simData <- simulateData(demo.model, sample.nobs=200)

#look at the data
describe(simData)[,1:4]
vars n mean sd
x1 1 200 -0.081 0.897
x2 2 200 -0.107 0.959
x3 3 200 -0.115 0.975
x4 4 200 -0.052 0.917
x5 5 200 -0.054 0.937
y 6 200 0.071 1.116
psych::describe(simData)
vars n mean sd median trimmed mad min max range skew kurtosis se
x1 1 200 -0.081 0.897 -0.070 -0.068 0.864 -2.36 2.12 4.48 -0.112 -0.322 0.063
x2 2 200 -0.107 0.959 -0.092 -0.123 0.895 -2.84 3.43 6.27 0.166 0.579 0.068
x3 3 200 -0.115 0.975 -0.168 -0.123 1.062 -2.32 2.64 4.96 0.132 -0.372 0.069
x4 4 200 -0.052 0.917 -0.019 -0.056 1.048 -2.02 2.41 4.43 0.093 -0.458 0.065
x5 5 200 -0.054 0.937 -0.053 -0.057 0.975 -2.70 2.44 5.14 -0.019 -0.120 0.066
y 6 200 0.071 1.116 0.037 0.076 1.072 -3.37 3.21 6.58 -0.029 0.025 0.079

1.2 Specify model

tofit.model <- '
y ~ f # "~ is regressed on"
f =~ x1+ x2 + x3 + x4 + x5 # "=~ is measured by"
x1 ~~ x1 # variance
x2 ~~ x2 #variance
x3~~x3 #variance
x4~~x4 #variance
x5~~x5 #variance
#x4~~x5 would be an example of covariance
'

1.3 Fit Model

tofit.model_m <- sem(tofit.model, simData)
summary(tofit.model_m, fit.measures = TRUE)
## lavaan (0.5-23.1097) converged normally after  25 iterations
## 
##   Number of observations                           200
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               15.764
##   Degrees of freedom                                 9
##   P-value (Chi-square)                           0.072
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic              519.050
##   Degrees of freedom                                15
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.987
##   Tucker-Lewis Index (TLI)                       0.978
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1404.795
##   Loglikelihood unrestricted model (H1)      -1396.913
## 
##   Number of free parameters                         12
##   Akaike (AIC)                                2833.591
##   Bayesian (BIC)                              2873.171
##   Sample-size adjusted Bayesian (BIC)         2835.154
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.061
##   90 Percent Confidence Interval          0.000  0.110
##   P-value RMSEA <= 0.05                          0.310
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.029
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f =~                                                
##     x1                1.000                           
##     x2                1.215    0.113   10.743    0.000
##     x3                1.180    0.115   10.306    0.000
##     x4                1.145    0.108   10.603    0.000
##     x5                1.053    0.110    9.594    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   y ~                                                 
##     f                 0.591    0.130    4.543    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.387    0.045    8.587    0.000
##    .x2                0.304    0.042    7.311    0.000
##    .x3                0.369    0.047    7.917    0.000
##    .x4                0.295    0.039    7.533    0.000
##    .x5                0.415    0.049    8.537    0.000
##    .y                 1.096    0.111    9.828    0.000
##     f                 0.414    0.074    5.605    0.000
inspect(tofit.model_m)
## $lambda
##    f y
## x1 0 0
## x2 2 0
## x3 3 0
## x4 4 0
## x5 5 0
## y  0 0
## 
## $theta
##    x1 x2 x3 x4 x5 y 
## x1  6               
## x2  0  7            
## x3  0  0  8         
## x4  0  0  0  9      
## x5  0  0  0  0 10   
## y   0  0  0  0  0  0
## 
## $psi
##   f  y 
## f 12   
## y  0 11
## 
## $beta
##   f y
## f 0 0
## y 1 0
semPaths(tofit.model_m)
## Warning in qgraph(Edgelist, labels = nLab, bidirectional = Bidir, directed
## = Directed, : The following arguments are not documented and likely not
## arguments of qgraph and thus ignored: loopRotation; residuals; residScale;
## residEdge; CircleEdgeEnd

2 Path Analysis

Same steps as above, but primarily focusing on regression paths. Noteworthy is the utility of this approach for mediation analyses. ##Load in data

set.seed(1234)
X <- rnorm(100)
M <- 0.5*X + rnorm(100)
Y <- 0.7*M + rnorm(100)
Data <- data.frame(X = X, Y = Y, M = M)

2.1 Specify model

medmodel <- ' # direct effect
             Y ~ c*X #use character to name regression path
           # mediator
             M ~ a*X
             Y ~ b*M
           # indirect effect (a*b)
             ab := a*b #define new parameter
           # total effect
             total := c + (a*b) #define new parameter using ":="
         '

2.2 Fit model

medmodel_m <- sem(medmodel, data = Data)
summary(medmodel_m, fit.measures = TRUE)
## lavaan (0.5-23.1097) converged normally after  12 iterations
## 
##   Number of observations                           100
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic               84.319
##   Degrees of freedom                                 3
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -422.892
##   Loglikelihood unrestricted model (H1)       -422.892
## 
##   Number of free parameters                          5
##   Akaike (AIC)                                 855.784
##   Bayesian (BIC)                               868.810
##   Sample-size adjusted Bayesian (BIC)          853.018
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent Confidence Interval          0.000  0.000
##   P-value RMSEA <= 0.05                             NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y ~                                                 
##     X          (c)    0.036    0.104    0.348    0.728
##   M ~                                                 
##     X          (a)    0.474    0.103    4.613    0.000
##   Y ~                                                 
##     M          (b)    0.788    0.092    8.539    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.898    0.127    7.071    0.000
##    .M                 1.054    0.149    7.071    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     ab                0.374    0.092    4.059    0.000
##     total             0.410    0.125    3.287    0.001
semPaths(medmodel_m)
## Warning in qgraph(Edgelist, labels = nLab, bidirectional = Bidir, directed
## = Directed, : The following arguments are not documented and likely not
## arguments of qgraph and thus ignored: loopRotation; residuals; residScale;
## residEdge; CircleEdgeEnd

2.3 Bootstrapping Confidence Interval for Indirect Effects

In addition to specifying that standard errors should be boostrapped for 5000 samples, the following syntax also indicates that the standard errors should be bias corrected (but not accelearted). This approach will yeild similar results to the PROCESS Macro in SPSS with bias-correct standard errors.

medmodel_boostrapped_se <- sem(medmodel, data =Data,se = "bootstrap", boot.ci.type = "bca.simple", bootstrap = 5000)
summary(medmodel_boostrapped_se, fit.measures = TRUE)
## lavaan (0.5-23.1097) converged normally after  12 iterations
## 
##   Number of observations                           100
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic               84.319
##   Degrees of freedom                                 3
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)               -422.892
##   Loglikelihood unrestricted model (H1)       -422.892
## 
##   Number of free parameters                          5
##   Akaike (AIC)                                 855.784
##   Bayesian (BIC)                               868.810
##   Sample-size adjusted Bayesian (BIC)          853.018
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent Confidence Interval          0.000  0.000
##   P-value RMSEA <= 0.05                             NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Observed
##   Standard Errors                            Bootstrap
##   Number of requested bootstrap draws             5000
##   Number of successful bootstrap draws            5000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y ~                                                 
##     X          (c)    0.036    0.113    0.321    0.748
##   M ~                                                 
##     X          (a)    0.474    0.096    4.913    0.000
##   Y ~                                                 
##     M          (b)    0.788    0.090    8.776    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.898    0.148    6.089    0.000
##    .M                 1.054    0.167    6.317    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     ab                0.374    0.085    4.421    0.000
##     total             0.410    0.134    3.051    0.002

3 Confirmatory Factor Analysis

3.1 Load in data

We’ll use the same data from the example

3.2 Specify model

tofit.cfa <- '
f =~ x1 + x2 + x3 +x4 + x5
x1~~x1
x2~~x2
x3~~x3
x4~~x4
x5~~x5
'

3.3 Fit model

tofit.cfa_lavaan <- lavaan(tofit.cfa, simData, int.ov.free = TRUE, int.lv.free = FALSE, auto.fix.first = TRUE, auto.fix.single = TRUE, auto.var = TRUE, auto.cov.lv.x = TRUE, auto.th = TRUE, auto.delta = TRUE, auto.cov.y = TRUE)
tofit.sem_lavaan <- lavaan(tofit.cfa, simData,int.ov.free = TRUE, int.lv.free = FALSE, auto.fix.first = TRUE, auto.fix.single = TRUE, auto.var = TRUE, auto.cov.lv.x = TRUE, auto.th = TRUE, auto.delta = TRUE, auto.cov.y = TRUE)
tofit.cfa_m <- sem(tofit.cfa, simData)
summary(tofit.cfa_m)
## lavaan (0.5-23.1097) converged normally after  22 iterations
## 
##   Number of observations                           200
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               11.509
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.042
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f =~                                                
##     x1                1.000                           
##     x2                1.216    0.113   10.746    0.000
##     x3                1.186    0.115   10.356    0.000
##     x4                1.141    0.108   10.569    0.000
##     x5                1.044    0.110    9.517    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.386    0.045    8.556    0.000
##    .x2                0.302    0.042    7.237    0.000
##    .x3                0.362    0.046    7.811    0.000
##    .x4                0.297    0.040    7.526    0.000
##    .x5                0.422    0.049    8.560    0.000
##     f                 0.415    0.074    5.607    0.000
tofit.sem_m <- sem(tofit.cfa, simData)
semPaths(tofit.cfa_m)
## Warning in qgraph(Edgelist, labels = nLab, bidirectional = Bidir, directed
## = Directed, : The following arguments are not documented and likely not
## arguments of qgraph and thus ignored: loopRotation; residuals; residScale;
## residEdge; CircleEdgeEnd

anova(tofit.cfa_m, tofit.cfa_lavaan, tofit.sem_lavaan, tofit.sem_m)
Df AIC BIC Chisq Chisq diff Df diff Pr(>Chisq)
tofit.cfa_m 5 2241 2274 11.5 NA NA NA
tofit.cfa_lavaan 5 2241 2274 11.5 0 0 1
tofit.sem_lavaan 5 2241 2274 11.5 0 0 1
tofit.sem_m 5 2241 2274 11.5 0 0 1

As indicated by the LRT across the models, lavaan::sem() and lavaan::cfa() are wrappers that have the same defaults. Additionally, CFA can easily be done using either cfa() or sem() # Structural Equation Model

3.4 Load in data

In this case, I will simulate data.

tosim <- '

#structural component
y ~ .5*f1 + .7*f2  #strength of regression with external criterion


#measurement component
f1 =~ .8*x1 + .6*x2 + .7*x3 + .8*x4 + .75*x5  #definition of factor f with loadings on 5 items

x1 ~~ (1-.8^2)*x1 #residual variances. Note that by using 1-squared loading, we achieve a total variability of 1.0 in each indicator (standardized)
x2 ~~ (1-.6^2)*x2
x3 ~~ (1-.7^2)*x3
x4 ~~ (1-.8^2)*x4
x5 ~~ (1-.75^2)*x5

f2 =~ .8*x6 + .9*x7
x6~~(1-.8^2)*x6
x7~~(1-.9^2)*x7

f1~~.2*f2
'

# generate data; note, standardized lv is default
sim_df <- simulateData(tosim, sample.nobs=800)

#look at the data
psych::describe(sim_df)[,1:4]
vars n mean sd
x1 1 800 0.052 0.994
x2 2 800 -0.017 1.017
x3 3 800 0.037 1.002
x4 4 800 0.039 0.996
x5 5 800 0.013 0.973
x6 6 800 -0.011 0.980
x7 7 800 -0.024 0.986
y 8 800 -0.030 1.313

3.5 Specify model

Test correct model

correctmodel <- "
#structural
y ~ f1+ f2
#measurement
f1 =~ x1 + x2 + x3 + x4 + x5 
f2 =~ x6 + x7
"

Test incorrect model. Let’s say we incorrectly believe that x4 and x5 load onto factor 2.

incorrectmodel <- "
#structural
y ~ f1+ f2
#measurement
f1 =~ x1 + x2 + x3 
f2 =~ x6 + x7 + x4 + x5
"

3.6 Fit Models

Correct model

correctmodel_m <- sem(correctmodel, sim_df)
parTable(correctmodel_m)
id lhs op rhs user block group free ustart exo label plabel start est se
1 y ~ f1 1 1 1 1 NA 0 .p1. 0.000 0.583 0.053
2 y ~ f2 1 1 1 2 NA 0 .p2. 0.000 0.917 0.057
3 f1 =~ x1 1 1 1 0 1 0 .p3. 1.000 1.000 0.000
4 f1 =~ x2 1 1 1 3 NA 0 .p4. 0.781 0.778 0.047
5 f1 =~ x3 1 1 1 4 NA 0 .p5. 0.873 0.860 0.046
6 f1 =~ x4 1 1 1 5 NA 0 .p6. 0.993 0.985 0.045
7 f1 =~ x5 1 1 1 6 NA 0 .p7. 0.906 0.891 0.044
8 f2 =~ x6 1 1 1 0 1 0 .p8. 1.000 1.000 0.000
9 f2 =~ x7 1 1 1 7 NA 0 .p9. 0.703 1.108 0.059
10 x1 ~~ x1 0 1 1 8 NA 0 .p10. 0.493 0.365 0.026
11 x2 ~~ x2 0 1 1 9 NA 0 .p11. 0.516 0.657 0.036
12 x3 ~~ x3 0 1 1 10 NA 0 .p12. 0.502 0.544 0.032
13 x4 ~~ x4 0 1 1 11 NA 0 .p13. 0.495 0.387 0.026
14 x5 ~~ x5 0 1 1 12 NA 0 .p14. 0.472 0.451 0.028
15 x6 ~~ x6 0 1 1 13 NA 0 .p15. 0.480 0.351 0.032
16 x7 ~~ x7 0 1 1 14 NA 0 .p16. 0.486 0.224 0.035
17 y ~~ y 0 1 1 15 NA 0 .p17. 0.861 0.861 0.051
18 f1 ~~ f1 0 1 1 16 NA 0 .p18. 0.050 0.621 0.049
19 f2 ~~ f2 0 1 1 17 NA 0 .p19. 0.050 0.609 0.052
20 f1 ~~ f2 0 1 1 18 NA 0 .p20. 0.000 0.129 0.027
summary(correctmodel_m, fit.measures = TRUE)
## lavaan (0.5-23.1097) converged normally after  25 iterations
## 
##   Number of observations                           800
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic               13.420
##   Degrees of freedom                                18
##   P-value (Chi-square)                           0.766
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             2485.813
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.003
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -8016.566
##   Loglikelihood unrestricted model (H1)      -8009.856
## 
##   Number of free parameters                         18
##   Akaike (AIC)                               16069.133
##   Bayesian (BIC)                             16153.456
##   Sample-size adjusted Bayesian (BIC)        16096.296
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent Confidence Interval          0.000  0.022
##   P-value RMSEA <= 0.05                          1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.016
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     x1                1.000                           
##     x2                0.778    0.047   16.606    0.000
##     x3                0.860    0.046   18.832    0.000
##     x4                0.985    0.045   21.896    0.000
##     x5                0.891    0.044   20.236    0.000
##   f2 =~                                               
##     x6                1.000                           
##     x7                1.108    0.059   18.849    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   y ~                                                 
##     f1                0.583    0.053   11.074    0.000
##     f2                0.917    0.057   15.956    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~~                                               
##     f2                0.129    0.027    4.819    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.365    0.026   14.178    0.000
##    .x2                0.657    0.036   18.083    0.000
##    .x3                0.544    0.032   17.148    0.000
##    .x4                0.387    0.026   14.667    0.000
##    .x5                0.451    0.028   16.281    0.000
##    .x6                0.351    0.032   10.955    0.000
##    .x7                0.224    0.035    6.452    0.000
##    .y                 0.861    0.051   16.985    0.000
##     f1                0.621    0.049   12.606    0.000
##     f2                0.609    0.052   11.691    0.000

Incorrect Model

incorrectmodel_m <- sem(incorrectmodel, sim_df)
parTable(incorrectmodel_m)
id lhs op rhs user block group free ustart exo label plabel start est se
1 y ~ f1 1 1 1 1 NA 0 .p1. 0.000 0.491 0.057
2 y ~ f2 1 1 1 2 NA 0 .p2. 0.000 0.941 0.060
3 f1 =~ x1 1 1 1 0 1 0 .p3. 1.000 1.000 0.000
4 f1 =~ x2 1 1 1 3 NA 0 .p4. 0.773 0.735 0.054
5 f1 =~ x3 1 1 1 4 NA 0 .p5. 0.840 0.795 0.056
6 f2 =~ x6 1 1 1 0 1 0 .p6. 1.000 1.000 0.000
7 f2 =~ x7 1 1 1 5 NA 0 .p7. 1.086 1.075 0.055
8 f2 =~ x4 1 1 1 6 NA 0 .p8. 0.251 0.329 0.049
9 f2 =~ x5 1 1 1 7 NA 0 .p9. 0.188 0.242 0.048
10 x1 ~~ x1 0 1 1 8 NA 0 .p10. 0.493 0.314 0.041
11 x2 ~~ x2 0 1 1 9 NA 0 .p11. 0.516 0.669 0.040
12 x3 ~~ x3 0 1 1 10 NA 0 .p12. 0.502 0.579 0.038
13 x6 ~~ x6 0 1 1 11 NA 0 .p13. 0.480 0.348 0.030
14 x7 ~~ x7 0 1 1 12 NA 0 .p14. 0.486 0.266 0.032
15 x4 ~~ x4 0 1 1 13 NA 0 .p15. 0.495 0.924 0.047
16 x5 ~~ x5 0 1 1 14 NA 0 .p16. 0.472 0.909 0.046
17 y ~~ y 0 1 1 15 NA 0 .p17. 0.861 0.846 0.051
18 f1 ~~ f1 0 1 1 16 NA 0 .p18. 0.050 0.672 0.060
19 f2 ~~ f2 0 1 1 17 NA 0 .p19. 0.050 0.611 0.051
20 f1 ~~ f2 0 1 1 18 NA 0 .p20. 0.000 0.187 0.030
summary(incorrectmodel_m, fit.measures = TRUE)
## lavaan (0.5-23.1097) converged normally after  25 iterations
## 
##   Number of observations                           800
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic              914.703
##   Degrees of freedom                                18
##   P-value (Chi-square)                           0.000
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic             2485.813
##   Degrees of freedom                                28
##   P-value                                        0.000
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    0.635
##   Tucker-Lewis Index (TLI)                       0.432
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -8467.208
##   Loglikelihood unrestricted model (H1)      -8009.856
## 
##   Number of free parameters                         18
##   Akaike (AIC)                               16970.416
##   Bayesian (BIC)                             17054.739
##   Sample-size adjusted Bayesian (BIC)        16997.579
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.250
##   90 Percent Confidence Interval          0.236  0.263
##   P-value RMSEA <= 0.05                          0.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.220
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     x1                1.000                           
##     x2                0.735    0.054   13.554    0.000
##     x3                0.795    0.056   14.293    0.000
##   f2 =~                                               
##     x6                1.000                           
##     x7                1.075    0.055   19.672    0.000
##     x4                0.329    0.049    6.738    0.000
##     x5                0.242    0.048    5.057    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   y ~                                                 
##     f1                0.491    0.057    8.659    0.000
##     f2                0.941    0.060   15.590    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~~                                               
##     f2                0.187    0.030    6.226    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .x1                0.314    0.041    7.673    0.000
##    .x2                0.669    0.040   16.571    0.000
##    .x3                0.579    0.038   15.069    0.000
##    .x6                0.348    0.030   11.443    0.000
##    .x7                0.266    0.032    8.406    0.000
##    .x4                0.924    0.047   19.726    0.000
##    .x5                0.909    0.046   19.850    0.000
##    .y                 0.846    0.051   16.489    0.000
##     f1                0.672    0.060   11.175    0.000
##     f2                0.611    0.051   11.930    0.000

Compare models

semPaths(correctmodel_m)
## Warning in qgraph(Edgelist, labels = nLab, bidirectional = Bidir, directed
## = Directed, : The following arguments are not documented and likely not
## arguments of qgraph and thus ignored: loopRotation; residuals; residScale;
## residEdge; CircleEdgeEnd

semPaths(incorrectmodel_m)
## Warning in qgraph(Edgelist, labels = nLab, bidirectional = Bidir, directed
## = Directed, : The following arguments are not documented and likely not
## arguments of qgraph and thus ignored: loopRotation; residuals; residScale;
## residEdge; CircleEdgeEnd

anova(correctmodel_m, incorrectmodel_m)
Df AIC BIC Chisq Chisq diff Df diff Pr(>Chisq)
correctmodel_m 18 16069 16153 13.4 NA NA NA
incorrectmodel_m 18 16970 17055 914.7 901 0 0

In addition to poor global fit indices in the incorrect model–as inidciated by CFI < .95, RMSEA > .06, SRMR > .08, and Chi-square test <.05, the corect model also beats out the incorrectmodel, as inidicated by much lower AIC and BIC for the correct model.