This week, we tackle estimating a mediation model in an SEM framework, both with observed variables and with latent factors.

Estimating A mediation model with observed variables (no latent)

Load in necessary packages

library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
  method         from
  print.tbl_lazy     
  print.tbl_sql      
── Attaching packages ─────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3     ✓ purrr   0.3.4
✓ tibble  3.0.6     ✓ dplyr   1.0.4
✓ tidyr   1.1.2     ✓ stringr 1.4.0
✓ readr   1.4.0     ✓ forcats 0.5.1
── Conflicts ────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(psych)

Attaching package: ‘psych’

The following objects are masked from ‘package:ggplot2’:

    %+%, alpha
library(lavaan)
This is lavaan 0.6-7
lavaan is BETA software! Please report any bugs.

Attaching package: ‘lavaan’

The following object is masked from ‘package:psych’:

    cor2cov

Load in the health data

glimpse(health_old)
Rows: 500
Columns: 4
$ id     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ consc  <dbl> -0.534766, 0.522444, 1.581126, -0.458229, 0.275520, -0.8932…
$ behave <dbl> 0.099598, 0.958975, 2.484509, 0.885141, -0.840232, -1.46591…
$ health <dbl> -0.886676, 2.618775, 3.084666, 0.249537, -2.079687, -0.0254…

Basic Command for a Mediation Model

Note here - we use the * convention to define specific paths a, b, and c that represent the a, b, and c paths from our proposed mediation model. Then, we use the := operator to define some nonlinear parameters that we then can estimate. These are the indirect effect ab and the total effect c + ab. We also add the missing = "ML" option to the sem call. This is the option to use full information maximum likelihood (FIML) to handle missing data.

model <- ' # direct effect
             health ~ c * consc
           # mediator
             behave ~ a * consc
             health ~ b * behave
           # indirect effect (a*b)
             ab := a * b
           # total effect
             total := c + (a*b)
         '
fit <- sem(model, data = health_old, missing = "ML")
lavaan WARNING: 5 cases were deleted due to missing values in 
          exogenous variable(s), while fixed.x = TRUE.
summary(fit)
lavaan 0.6-7 ended normally after 15 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                          7
                                                      
                                                  Used       Total
  Number of observations                           495         500
  Number of missing patterns                         3            
                                                                  
Model Test User Model:
                                                      
  Test statistic                                 0.000
  Degrees of freedom                                 0

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Observed
  Observed information based on                Hessian

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)
  health ~                                            
    consc      (c)    0.268    0.043    6.219    0.000
  behave ~                                            
    consc      (a)    0.528    0.038   13.912    0.000
  health ~                                            
    behave     (b)    0.390    0.043    9.013    0.000

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)
   .health            0.029    0.051    0.563    0.574
   .behave            0.027    0.053    0.499    0.617

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .health            1.291    0.082   15.684    0.000
   .behave            1.404    0.089   15.693    0.000

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|)
    ab                0.206    0.027    7.553    0.000
    total             0.474    0.039   12.060    0.000

Same as above, with standardized results

summary(fit, standardized=TRUE, fit.measures = TRUE)
lavaan 0.6-7 ended normally after 15 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                          7
                                                      
                                                  Used       Total
  Number of observations                           495         500
  Number of missing patterns                         3            
                                                                  
Model Test User Model:
                                                      
  Test statistic                                 0.000
  Degrees of freedom                                 0

Model Test Baseline Model:

  Test statistic                               364.966
  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)              -1545.855
  Loglikelihood unrestricted model (H1)      -1545.855
                                                      
  Akaike (AIC)                                3105.710
  Bayesian (BIC)                              3135.142
  Sample-size adjusted Bayesian (BIC)         3112.924

Root Mean Square Error of Approximation:

  RMSEA                                          0.000
  90 Percent confidence interval - lower         0.000
  90 Percent confidence interval - upper         0.000
  P-value RMSEA <= 0.05                             NA

Standardized Root Mean Square Residual:

  SRMR                                           0.000

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Observed
  Observed information based on                Hessian

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  health ~                                                              
    consc      (c)    0.268    0.043    6.219    0.000    0.268    0.269
  behave ~                                                              
    consc      (a)    0.528    0.038   13.912    0.000    0.528    0.531
  health ~                                                              
    behave     (b)    0.390    0.043    9.013    0.000    0.390    0.391

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .health            0.029    0.051    0.563    0.574    0.029    0.021
   .behave            0.027    0.053    0.499    0.617    0.027    0.019

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .health            1.291    0.082   15.684    0.000    1.291    0.663
   .behave            1.404    0.089   15.693    0.000    1.404    0.718

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
    ab                0.206    0.027    7.553    0.000    0.206    0.208
    total             0.474    0.039   12.060    0.000    0.474    0.477

To obtain R Squared Values

lavInspect(fit, "r2")
health behave 
 0.337  0.282 

Same model, but with bootstrapped standard errors and 95% confidence intervals

fit2 <- sem(model, data = health_old, se = "bootstrap", bootstrap = 10)
summary(fit2, standardized=TRUE, ci = .95)
lavaan 0.6-7 ended normally after 15 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                          5
                                                      
                                                  Used       Total
  Number of observations                           491         500
                                                                  
Model Test User Model:
                                                      
  Test statistic                                 0.000
  Degrees of freedom                                 0
extreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpoints

Parameter Estimates:

  Standard errors                            Bootstrap
  Number of requested bootstrap draws               10
  Number of successful bootstrap draws              10

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
  health ~                                                                                
    consc      (c)    0.265    0.043    6.203    0.000    0.191    0.340    0.265    0.268
  behave ~                                                                                
    consc      (a)    0.527    0.041   12.905    0.000    0.413    0.554    0.527    0.531
  health ~                                                                                
    behave     (b)    0.389    0.057    6.798    0.000    0.274    0.472    0.389    0.391

Variances:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
   .health            1.286    0.062   20.726    0.000    1.160    1.346    1.286    0.665
   .behave            1.402    0.099   14.112    0.000    1.225    1.584    1.402    0.718

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
    ab                0.205    0.037    5.575    0.000    0.146    0.249    0.205    0.207
    total             0.470    0.019   25.078    0.000    0.421    0.486    0.470    0.475

Visualize it with a path diagram

semPlot::semPaths(fit, whatLabels = "standardized")

Estimating A Mediation Model With Latent Vars

glimpse(writing)
Rows: 1,509
Columns: 27
$ W1_FBP1 <dbl> 3, 4, 4, 3, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 1, 4, 3, 3, 2, 4, 3, 4, 3, 4, 4,…
$ W1_FBP2 <dbl> 3, 4, 2, 3, 2, 3, 3, 2, 3, 4, 2, 4, 1, 4, 1, 4, 3, 2, 2, 4, 3, 3, 4, 2, 4,…
$ W1_FBP3 <dbl> 4, 4, 4, 3, 4, 4, 2, 1, 3, 4, 3, 4, 3, 4, 1, 4, 2, 2, 3, 4, 3, 4, 4, 3, 4,…
$ W1_FBP4 <dbl> 3, 4, 4, 3, 3, 4, 3, 2, 3, 4, 3, 4, 3, 4, 1, 4, 2, 2, 2, 4, 2, 4, 3, 4, 4,…
$ W1_FBP5 <dbl> 2, 4, 3, 3, 2, 2, 2, 2, 2, 4, 1, 4, 1, 4, 1, 3, 2, 3, 3, 4, 2, 4, 3, 2, 4,…
$ W1_SE1  <dbl> 4, 4, 3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 4, 3, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 4,…
$ W1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2, 4, 4, 2, 3, 4, 2, 3, 2, 2, 1, 4, 3, 3, 4, 4, 2, 4, 2, 1, 4,…
$ W1_SR5  <dbl> 4, 3, 4, 2, 2, 2, 3, 2, 2, 1, 4, 2, 2, 4, 2, 3, 2, 4, 2, 2, 3, 2, 1, 2, 2,…
$ W1_SR6  <dbl> 2, 4, 1, 2, 3, 2, 2, 2, 2, 3, 3, 4, 1, 3, 2, 3, 2, 2, 1, 3, 1, 2, 4, 3, 3,…
$ W1_SR7  <dbl> 4, 3, 4, 3, 4, 3, 3, 3, 2, 4, 3, 4, 4, 2, 2, 4, 2, 2, 4, 3, 2, 4, 3, 4, 4,…
$ W1_SR8  <dbl> 2, 2, 3, 2, 3, 2, 3, 3, 3, 1, 3, 3, 2, 3, 1, 2, 3, 2, 2, 4, 4, 3, 3, 4, 4,…
$ W1_SR9  <dbl> 3, 3, 4, 3, 4, 2, 3, 3, 2, 4, 2, 3, 1, 3, 1, 3, 1, 4, 3, 4, 3, 2, 4, 4, 2,…
$ W1_SR10 <dbl> 4, 4, 4, 2, 3, 2, 2, 1, 3, 1, 3, 4, 1, 4, 1, 3, 2, 3, 2, 4, 2, 2, 3, 3, 4,…
$ W1_SR11 <dbl> 4, 3, 4, 4, 3, 2, 4, 4, 4, 3, 2, 3, 2, 3, 3, 4, 3, 2, 4, 3, 3, 2, 1, 4, 4,…
$ W1_SR12 <dbl> 4, 4, 4, 2, 4, 3, 3, 2, 3, 4, 4, 4, 1, 3, 1, 2, 2, 4, 2, 4, 2, 4, 2, 4, 3,…
$ W1_TNum <dbl> 71, 34, 63, 52, 81, 71, 71, 71, 83, 70, 84, 83, 76, 60, 85, 80, 39, 8, 9, …

Start with CFAs for all three latent measures

cfa.model <- ' FBP  =~ W1_FBP1 + W1_FBP2 + W1_FBP3 + W1_FBP4 + W1_FBP5
              SE =~ W1_SE1 + W1_SE2 + W1_SE3 + W1_SE4 + W1_SE5 + W1_SE6 + W1_SE7 + W1_SE8 + W1_SE9
              SR   =~ W1_SR1 + W1_SR2 + W1_SR3 + W1_SR4 + W1_SR5 + W1_SR6 + W1_SR7 + W1_SR8 + W1_SR9 + W1_SR10 + W1_SR11 + W1_SR12'
cfa_fit <- cfa(cfa.model, data=writing, missing = "ML")
summary(cfa_fit, fit.measures=TRUE, standardized=TRUE)
lavaan 0.6-7 ended normally after 96 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                         81
                                                      
  Number of observations                          1509
  Number of missing patterns                        27
                                                      
Model Test User Model:
                                                      
  Test statistic                              1988.299
  Degrees of freedom                               296
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                             13338.258
  Degrees of freedom                               325
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.870
  Tucker-Lewis Index (TLI)                       0.857

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -48264.380
  Loglikelihood unrestricted model (H1)     -47270.231
                                                      
  Akaike (AIC)                               96690.761
  Bayesian (BIC)                             97121.616
  Sample-size adjusted Bayesian (BIC)        96864.301

Root Mean Square Error of Approximation:

  RMSEA                                          0.062
  90 Percent confidence interval - lower         0.059
  90 Percent confidence interval - upper         0.064
  P-value RMSEA <= 0.05                          0.000

Standardized Root Mean Square Residual:

  SRMR                                           0.050

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Observed
  Observed information based on                Hessian

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  FBP =~                                                                
    W1_FBP1           1.000                               0.633    0.707
    W1_FBP2           1.276    0.047   27.173    0.000    0.807    0.779
    W1_FBP3           1.117    0.044   25.105    0.000    0.707    0.705
    W1_FBP4           1.194    0.040   29.778    0.000    0.755    0.814
    W1_FBP5           1.416    0.050   28.118    0.000    0.896    0.811
  SE =~                                                                 
    W1_SE1            1.000                               0.243    0.343
    W1_SE2            2.274    0.190   11.941    0.000    0.552    0.625
    W1_SE3            1.293    0.126   10.291    0.000    0.314    0.396
    W1_SE4            2.873    0.236   12.166    0.000    0.697    0.695
    W1_SE5            0.852    0.111    7.679    0.000    0.207    0.247
    W1_SE6            2.654    0.214   12.390    0.000    0.644    0.729
    W1_SE7            2.552    0.207   12.308    0.000    0.619    0.718
    W1_SE8            2.559    0.217   11.767    0.000    0.621    0.609
    W1_SE9            2.657    0.220   12.106    0.000    0.645    0.670
  SR =~                                                                 
    W1_SR1            1.000                               0.490    0.550
    W1_SR2            1.201    0.070   17.251    0.000    0.588    0.588
    W1_SR3            1.026    0.070   14.625    0.000    0.503    0.471
    W1_SR4            0.738    0.065   11.329    0.000    0.361    0.342
    W1_SR5            0.729    0.066   11.120    0.000    0.357    0.335
    W1_SR6            1.344    0.074   18.180    0.000    0.658    0.643
    W1_SR7            0.931    0.062   15.106    0.000    0.456    0.489
    W1_SR8            1.068    0.068   15.638    0.000    0.523    0.511
    W1_SR9            1.036    0.070   14.878    0.000    0.507    0.480
    W1_SR10           1.228    0.073   16.846    0.000    0.601    0.570
    W1_SR11           0.842    0.057   14.847    0.000    0.412    0.471
    W1_SR12           1.320    0.077   17.073    0.000    0.646    0.586

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  FBP ~~                                                                
    SE                0.096    0.009   10.467    0.000    0.627    0.627
    SR                0.210    0.015   14.127    0.000    0.679    0.679
  SE ~~                                                                 
    SR                0.091    0.009   10.191    0.000    0.764    0.764

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .W1_FBP1           3.394    0.023  147.261    0.000    3.394    3.792
   .W1_FBP2           2.941    0.027  110.275    0.000    2.941    2.840
   .W1_FBP3           3.232    0.026  125.156    0.000    3.232    3.224
   .W1_FBP4           3.245    0.024  135.924    0.000    3.245    3.500
   .W1_FBP5           2.800    0.028   98.440    0.000    2.800    2.534
   .W1_SE1            3.552    0.018  194.684    0.000    3.552    5.018
   .W1_SE2            2.963    0.023  130.449    0.000    2.963    3.359
   .W1_SE3            3.355    0.020  164.465    0.000    3.355    4.234
   .W1_SE4            2.862    0.026  110.737    0.000    2.862    2.853
   .W1_SE5            3.133    0.022  145.393    0.000    3.133    3.744
   .W1_SE6            3.084    0.023  135.652    0.000    3.084    3.493
   .W1_SE7            3.166    0.022  142.529    0.000    3.166    3.670
   .W1_SE8            2.751    0.026  104.818    0.000    2.751    2.698
   .W1_SE9            2.903    0.025  117.137    0.000    2.903    3.016
   .W1_SR1            3.206    0.023  139.602    0.000    3.206    3.601
   .W1_SR2            2.576    0.026   99.917    0.000    2.576    2.574
   .W1_SR3            2.710    0.027   98.553    0.000    2.710    2.539
   .W1_SR4            2.815    0.027  103.474    0.000    2.815    2.665
   .W1_SR5            2.631    0.027   95.762    0.000    2.631    2.466
   .W1_SR6            2.542    0.026   96.493    0.000    2.542    2.485
   .W1_SR7            3.109    0.024  129.462    0.000    3.109    3.336
   .W1_SR8            2.789    0.026  105.833    0.000    2.789    2.725
   .W1_SR9            2.845    0.027  104.595    0.000    2.845    2.695
   .W1_SR10           2.721    0.027  100.079    0.000    2.721    2.577
   .W1_SR11           3.179    0.023  141.229    0.000    3.179    3.636
   .W1_SR12           2.981    0.028  105.010    0.000    2.981    2.704
    FBP               0.000                               0.000    0.000
    SE                0.000                               0.000    0.000
    SR                0.000                               0.000    0.000

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .W1_FBP1           0.401    0.017   23.683    0.000    0.401    0.500
   .W1_FBP2           0.422    0.019   21.667    0.000    0.422    0.393
   .W1_FBP3           0.506    0.021   24.067    0.000    0.506    0.503
   .W1_FBP4           0.290    0.015   19.961    0.000    0.290    0.337
   .W1_FBP5           0.418    0.021   20.310    0.000    0.418    0.342
   .W1_SE1            0.442    0.016   26.851    0.000    0.442    0.883
   .W1_SE2            0.474    0.019   24.727    0.000    0.474    0.609
   .W1_SE3            0.529    0.020   26.663    0.000    0.529    0.843
   .W1_SE4            0.520    0.022   23.367    0.000    0.520    0.517
   .W1_SE5            0.658    0.024   27.179    0.000    0.658    0.939
   .W1_SE6            0.365    0.016   22.449    0.000    0.365    0.468
   .W1_SE7            0.361    0.016   22.862    0.000    0.361    0.485
   .W1_SE8            0.654    0.026   24.813    0.000    0.654    0.629
   .W1_SE9            0.511    0.021   24.013    0.000    0.511    0.551
   .W1_SR1            0.553    0.022   25.348    0.000    0.553    0.698
   .W1_SR2            0.656    0.027   24.700    0.000    0.656    0.655
   .W1_SR3            0.887    0.034   26.074    0.000    0.887    0.778
   .W1_SR4            0.986    0.037   26.824    0.000    0.986    0.883
   .W1_SR5            1.011    0.038   26.842    0.000    1.011    0.888
   .W1_SR6            0.613    0.026   23.809    0.000    0.613    0.586
   .W1_SR7            0.660    0.026   25.844    0.000    0.660    0.761
   .W1_SR8            0.774    0.030   25.707    0.000    0.774    0.739
   .W1_SR9            0.857    0.033   26.025    0.000    0.857    0.769
   .W1_SR10           0.753    0.030   25.126    0.000    0.753    0.676
   .W1_SR11           0.595    0.023   26.089    0.000    0.595    0.778
   .W1_SR12           0.798    0.032   24.925    0.000    0.798    0.656
    FBP               0.400    0.027   15.012    0.000    1.000    1.000
    SE                0.059    0.009    6.378    0.000    1.000    1.000
    SR                0.240    0.022   10.737    0.000    1.000    1.000

Visualize it with a path diagram

To make the diagram a little less full, I removed the residual estimates for items using residuals = FALSE.

semPlot::semPaths(cfa_fit, whatLabels = "standardized", residuals = FALSE)

Now, specify the SEM model

Notice, with a latent mediation model, we have two steps. Step 1 is specifying the measurement model. This is identical to the code for the CFA model above. Step 2 is the structural model. This is where we actually define the mediation portion. The code here is very similar to what we did for the health data above.

sem.model <- ' 
#Measurement Model
FBP =~ W1_FBP1 + W1_FBP2 + W1_FBP3 + W1_FBP4 + W1_FBP5
SE =~ W1_SE1 + W1_SE2 + W1_SE3 + W1_SE4 + W1_SE5 + W1_SE6 + W1_SE7 + W1_SE8 + W1_SE9
SR =~ W1_SR1 + W1_SR2 + W1_SR3 + W1_SR4 + W1_SR5 + W1_SR6 + W1_SR7 + W1_SR8 + W1_SR9 + W1_SR10 + W1_SR11 + W1_SR12

#Structural Model
## direct effect
SR ~ c*SE
## mediator
SR ~ b*FBP
FBP ~ a*SE
## indirect effect (a*b)
ab := a*b
## total effect
total := c + (a*b)
'

sem_fit <- sem(sem.model, data=writing, missing = "ml")
summary(sem_fit, fit.measures=TRUE, standardized=TRUE)
lavaan 0.6-7 ended normally after 92 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                         81
                                                      
  Number of observations                          1509
  Number of missing patterns                        27
                                                      
Model Test User Model:
                                                      
  Test statistic                              1988.299
  Degrees of freedom                               296
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                             13338.258
  Degrees of freedom                               325
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.870
  Tucker-Lewis Index (TLI)                       0.857

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)             -48264.380
  Loglikelihood unrestricted model (H1)     -47270.231
                                                      
  Akaike (AIC)                               96690.761
  Bayesian (BIC)                             97121.616
  Sample-size adjusted Bayesian (BIC)        96864.301

Root Mean Square Error of Approximation:

  RMSEA                                          0.062
  90 Percent confidence interval - lower         0.059
  90 Percent confidence interval - upper         0.064
  P-value RMSEA <= 0.05                          0.000

Standardized Root Mean Square Residual:

  SRMR                                           0.050

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Observed
  Observed information based on                Hessian

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  FBP =~                                                                
    W1_FBP1           1.000                               0.633    0.707
    W1_FBP2           1.276    0.047   27.173    0.000    0.807    0.779
    W1_FBP3           1.117    0.044   25.105    0.000    0.707    0.705
    W1_FBP4           1.194    0.040   29.778    0.000    0.755    0.814
    W1_FBP5           1.416    0.050   28.119    0.000    0.896    0.811
  SE =~                                                                 
    W1_SE1            1.000                               0.243    0.343
    W1_SE2            2.274    0.190   11.942    0.000    0.552    0.625
    W1_SE3            1.293    0.126   10.291    0.000    0.314    0.396
    W1_SE4            2.873    0.236   12.166    0.000    0.697    0.695
    W1_SE5            0.852    0.111    7.680    0.000    0.207    0.247
    W1_SE6            2.654    0.214   12.391    0.000    0.644    0.729
    W1_SE7            2.552    0.207   12.308    0.000    0.619    0.718
    W1_SE8            2.559    0.217   11.768    0.000    0.621    0.609
    W1_SE9            2.657    0.219   12.107    0.000    0.645    0.670
  SR =~                                                                 
    W1_SR1            1.000                               0.490    0.550
    W1_SR2            1.201    0.070   17.251    0.000    0.588    0.588
    W1_SR3            1.026    0.070   14.625    0.000    0.503    0.471
    W1_SR4            0.738    0.065   11.329    0.000    0.361    0.342
    W1_SR5            0.729    0.066   11.120    0.000    0.357    0.335
    W1_SR6            1.344    0.074   18.180    0.000    0.658    0.643
    W1_SR7            0.931    0.062   15.106    0.000    0.456    0.489
    W1_SR8            1.068    0.068   15.638    0.000    0.523    0.511
    W1_SR9            1.036    0.070   14.878    0.000    0.507    0.480
    W1_SR10           1.228    0.073   16.846    0.000    0.601    0.570
    W1_SR11           0.842    0.057   14.847    0.000    0.412    0.471
    W1_SR12           1.320    0.077   17.073    0.000    0.646    0.586

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  SR ~                                                                  
    SE         (c)    1.127    0.117    9.611    0.000    0.558    0.558
    FBP        (b)    0.254    0.027    9.478    0.000    0.329    0.329
  FBP ~                                                                 
    SE         (a)    1.635    0.148   11.066    0.000    0.627    0.627

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .W1_FBP1           3.394    0.023  147.261    0.000    3.394    3.792
   .W1_FBP2           2.941    0.027  110.275    0.000    2.941    2.840
   .W1_FBP3           3.232    0.026  125.156    0.000    3.232    3.224
   .W1_FBP4           3.245    0.024  135.924    0.000    3.245    3.500
   .W1_FBP5           2.800    0.028   98.440    0.000    2.800    2.534
   .W1_SE1            3.552    0.018  194.684    0.000    3.552    5.018
   .W1_SE2            2.963    0.023  130.449    0.000    2.963    3.359
   .W1_SE3            3.355    0.020  164.465    0.000    3.355    4.234
   .W1_SE4            2.862    0.026  110.737    0.000    2.862    2.853
   .W1_SE5            3.133    0.022  145.393    0.000    3.133    3.744
   .W1_SE6            3.084    0.023  135.653    0.000    3.084    3.493
   .W1_SE7            3.166    0.022  142.529    0.000    3.166    3.670
   .W1_SE8            2.751    0.026  104.818    0.000    2.751    2.698
   .W1_SE9            2.903    0.025  117.137    0.000    2.903    3.016
   .W1_SR1            3.206    0.023  139.602    0.000    3.206    3.601
   .W1_SR2            2.576    0.026   99.917    0.000    2.576    2.574
   .W1_SR3            2.710    0.027   98.553    0.000    2.710    2.539
   .W1_SR4            2.815    0.027  103.474    0.000    2.815    2.665
   .W1_SR5            2.631    0.027   95.762    0.000    2.631    2.466
   .W1_SR6            2.542    0.026   96.493    0.000    2.542    2.485
   .W1_SR7            3.109    0.024  129.462    0.000    3.109    3.336
   .W1_SR8            2.789    0.026  105.833    0.000    2.789    2.725
   .W1_SR9            2.845    0.027  104.595    0.000    2.845    2.695
   .W1_SR10           2.721    0.027  100.079    0.000    2.721    2.577
   .W1_SR11           3.179    0.023  141.229    0.000    3.179    3.636
   .W1_SR12           2.981    0.028  105.010    0.000    2.981    2.704
   .FBP               0.000                               0.000    0.000
    SE                0.000                               0.000    0.000
   .SR                0.000                               0.000    0.000

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .W1_FBP1           0.401    0.017   23.683    0.000    0.401    0.500
   .W1_FBP2           0.422    0.019   21.667    0.000    0.422    0.393
   .W1_FBP3           0.506    0.021   24.067    0.000    0.506    0.503
   .W1_FBP4           0.290    0.015   19.961    0.000    0.290    0.337
   .W1_FBP5           0.418    0.021   20.310    0.000    0.418    0.342
   .W1_SE1            0.442    0.016   26.852    0.000    0.442    0.883
   .W1_SE2            0.474    0.019   24.727    0.000    0.474    0.609
   .W1_SE3            0.529    0.020   26.663    0.000    0.529    0.843
   .W1_SE4            0.520    0.022   23.367    0.000    0.520    0.517
   .W1_SE5            0.658    0.024   27.179    0.000    0.658    0.939
   .W1_SE6            0.365    0.016   22.449    0.000    0.365    0.468
   .W1_SE7            0.361    0.016   22.862    0.000    0.361    0.485
   .W1_SE8            0.654    0.026   24.813    0.000    0.654    0.629
   .W1_SE9            0.511    0.021   24.013    0.000    0.511    0.551
   .W1_SR1            0.553    0.022   25.348    0.000    0.553    0.698
   .W1_SR2            0.656    0.027   24.700    0.000    0.656    0.655
   .W1_SR3            0.887    0.034   26.073    0.000    0.887    0.778
   .W1_SR4            0.986    0.037   26.824    0.000    0.986    0.883
   .W1_SR5            1.011    0.038   26.842    0.000    1.011    0.888
   .W1_SR6            0.613    0.026   23.809    0.000    0.613    0.586
   .W1_SR7            0.660    0.026   25.844    0.000    0.660    0.761
   .W1_SR8            0.774    0.030   25.707    0.000    0.774    0.739
   .W1_SR9            0.857    0.033   26.025    0.000    0.857    0.769
   .W1_SR10           0.753    0.030   25.126    0.000    0.753    0.676
   .W1_SR11           0.595    0.023   26.089    0.000    0.595    0.778
   .W1_SR12           0.798    0.032   24.925    0.000    0.798    0.656
   .FBP               0.243    0.017   13.939    0.000    0.607    0.607
    SE                0.059    0.009    6.378    0.000    1.000    1.000
   .SR                0.084    0.009    9.340    0.000    0.350    0.350

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
    ab                0.416    0.054    7.731    0.000    0.206    0.206
    total             1.543    0.142   10.868    0.000    0.764    0.764

Same model, but with bootstrapped standard errors (I’m only doing 10 here because it takes forever) and 95% confidence intervals!

sem_fit_boot <- sem(sem.model, data = writing, se = "bootstrap", bootstrap = 10, missing = "ml")
lavaan WARNING:
    The variance-covariance matrix of the estimated parameters (vcov)
    does not appear to be positive definite! The smallest eigenvalue
    (= -4.807122e-16) is smaller than zero. This may be a symptom that
    the model is not identified.
summary(sem_fit_boot, standardized=TRUE, ci = .95)
lavaan 0.6-7 ended normally after 92 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                         81
                                                      
  Number of observations                          1509
  Number of missing patterns                        27
                                                      
Model Test User Model:
                                                      
  Test statistic                              1988.299
  Degrees of freedom                               296
  P-value (Chi-square)                           0.000
extreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpointsextreme order statistics used as endpoints

Parameter Estimates:

  Standard errors                            Bootstrap
  Number of requested bootstrap draws               10
  Number of successful bootstrap draws              10

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
  FBP =~                                                                                  
    W1_FBP1           1.000                               1.000    1.000    0.633    0.707
    W1_FBP2           1.276    0.056   22.708    0.000    1.196    1.386    0.807    0.779
    W1_FBP3           1.117    0.041   26.983    0.000    1.058    1.191    0.707    0.705
    W1_FBP4           1.194    0.035   34.125    0.000    1.125    1.253    0.755    0.814
    W1_FBP5           1.416    0.065   21.917    0.000    1.331    1.517    0.896    0.811
  SE =~                                                                                   
    W1_SE1            1.000                               1.000    1.000    0.243    0.343
    W1_SE2            2.274    0.277    8.198    0.000    1.989    2.960    0.552    0.625
    W1_SE3            1.293    0.099   13.087    0.000    1.270    1.592    0.314    0.396
    W1_SE4            2.873    0.358    8.021    0.000    2.483    3.724    0.697    0.695
    W1_SE5            0.852    0.099    8.591    0.000    0.701    1.042    0.207    0.247
    W1_SE6            2.654    0.309    8.599    0.000    2.236    3.311    0.644    0.729
    W1_SE7            2.552    0.323    7.909    0.000    2.204    3.296    0.619    0.718
    W1_SE8            2.559    0.253   10.096    0.000    2.258    3.155    0.621    0.609
    W1_SE9            2.657    0.341    7.796    0.000    2.238    3.339    0.645    0.670
  SR =~                                                                                   
    W1_SR1            1.000                               1.000    1.000    0.490    0.550
    W1_SR2            1.201    0.057   20.907    0.000    1.107    1.287    0.588    0.588
    W1_SR3            1.026    0.044   23.230    0.000    0.916    1.050    0.503    0.471
    W1_SR4            0.738    0.038   19.251    0.000    0.637    0.775    0.361    0.342
    W1_SR5            0.729    0.045   16.189    0.000    0.668    0.801    0.357    0.335
    W1_SR6            1.344    0.056   23.907    0.000    1.262    1.435    0.658    0.643
    W1_SR7            0.931    0.077   12.081    0.000    0.837    1.089    0.456    0.489
    W1_SR8            1.068    0.090   11.929    0.000    0.923    1.258    0.523    0.511
    W1_SR9            1.036    0.091   11.332    0.000    0.896    1.207    0.507    0.480
    W1_SR10           1.228    0.067   18.440    0.000    1.099    1.312    0.601    0.570
    W1_SR11           0.842    0.058   14.593    0.000    0.755    0.948    0.412    0.471
    W1_SR12           1.320    0.075   17.715    0.000    1.216    1.470    0.646    0.586

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
  SR ~                                                                                    
    SE         (c)    1.127    0.187    6.031    0.000    0.944    1.572    0.558    0.558
    FBP        (b)    0.254    0.022   11.409    0.000    0.213    0.283    0.329    0.329
  FBP ~                                                                                   
    SE         (a)    1.635    0.176    9.288    0.000    1.501    2.069    0.627    0.627

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
   .W1_FBP1           3.394    0.017  200.782    0.000    3.359    3.421    3.394    3.792
   .W1_FBP2           2.941    0.026  114.238    0.000    2.906    3.001    2.941    2.840
   .W1_FBP3           3.232    0.020  162.358    0.000    3.201    3.267    3.232    3.224
   .W1_FBP4           3.245    0.016  198.363    0.000    3.231    3.278    3.245    3.500
   .W1_FBP5           2.800    0.026  106.768    0.000    2.771    2.860    2.800    2.534
   .W1_SE1            3.552    0.013  275.605    0.000    3.544    3.579    3.552    5.018
   .W1_SE2            2.963    0.022  136.341    0.000    2.931    3.011    2.963    3.359
   .W1_SE3            3.355    0.019  176.406    0.000    3.327    3.387    3.355    4.234
   .W1_SE4            2.862    0.023  124.430    0.000    2.805    2.896    2.862    2.853
   .W1_SE5            3.133    0.024  133.136    0.000    3.078    3.172    3.133    3.744
   .W1_SE6            3.084    0.028  108.585    0.000    3.038    3.140    3.084    3.493
   .W1_SE7            3.166    0.022  145.207    0.000    3.125    3.202    3.166    3.670
   .W1_SE8            2.751    0.020  140.236    0.000    2.730    2.796    2.751    2.698
   .W1_SE9            2.903    0.014  214.867    0.000    2.897    2.935    2.903    3.016
   .W1_SR1            3.206    0.028  115.531    0.000    3.167    3.255    3.206    3.601
   .W1_SR2            2.576    0.028   91.139    0.000    2.538    2.640    2.576    2.574
   .W1_SR3            2.710    0.035   78.046    0.000    2.677    2.798    2.710    2.539
   .W1_SR4            2.815    0.032   87.306    0.000    2.776    2.882    2.815    2.665
   .W1_SR5            2.631    0.025  104.664    0.000    2.613    2.688    2.631    2.466
   .W1_SR6            2.542    0.029   86.442    0.000    2.507    2.624    2.542    2.485
   .W1_SR7            3.109    0.021  151.183    0.000    3.082    3.146    3.109    3.336
   .W1_SR8            2.789    0.014  194.336    0.000    2.752    2.807    2.789    2.725
   .W1_SR9            2.845    0.021  133.668    0.000    2.807    2.888    2.845    2.695
   .W1_SR10           2.721    0.023  119.529    0.000    2.697    2.772    2.721    2.577
   .W1_SR11           3.179    0.030  105.958    0.000    3.137    3.232    3.179    3.636
   .W1_SR12           2.981    0.017  174.294    0.000    2.968    3.023    2.981    2.704
   .FBP               0.000                               0.000    0.000    0.000    0.000
    SE                0.000                               0.000    0.000    0.000    0.000
   .SR                0.000                               0.000    0.000    0.000    0.000

Variances:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
   .W1_FBP1           0.401    0.022   17.968    0.000    0.362    0.442    0.401    0.500
   .W1_FBP2           0.422    0.027   15.793    0.000    0.386    0.469    0.422    0.393
   .W1_FBP3           0.506    0.019   26.337    0.000    0.481    0.543    0.506    0.503
   .W1_FBP4           0.290    0.017   16.992    0.000    0.243    0.302    0.290    0.337
   .W1_FBP5           0.418    0.024   17.128    0.000    0.369    0.463    0.418    0.342
   .W1_SE1            0.442    0.017   25.580    0.000    0.401    0.460    0.442    0.883
   .W1_SE2            0.474    0.028   16.767    0.000    0.424    0.523    0.474    0.609
   .W1_SE3            0.529    0.018   28.969    0.000    0.490    0.546    0.529    0.843
   .W1_SE4            0.520    0.009   60.046    0.000    0.502    0.528    0.520    0.517
   .W1_SE5            0.658    0.017   38.356    0.000    0.620    0.682    0.658    0.939
   .W1_SE6            0.365    0.015   25.032    0.000    0.337    0.384    0.365    0.468
   .W1_SE7            0.361    0.020   18.300    0.000    0.330    0.412    0.361    0.485
   .W1_SE8            0.654    0.029   22.469    0.000    0.615    0.713    0.654    0.629
   .W1_SE9            0.511    0.031   16.390    0.000    0.457    0.568    0.511    0.551
   .W1_SR1            0.553    0.024   22.721    0.000    0.521    0.591    0.553    0.698
   .W1_SR2            0.656    0.030   22.181    0.000    0.599    0.700    0.656    0.655
   .W1_SR3            0.887    0.036   24.476    0.000    0.838    0.936    0.887    0.778
   .W1_SR4            0.986    0.030   33.385    0.000    0.921    1.015    0.986    0.883
   .W1_SR5            1.011    0.032   31.538    0.000    0.939    1.064    1.011    0.888
   .W1_SR6            0.613    0.031   20.072    0.000    0.574    0.698    0.613    0.586
   .W1_SR7            0.660    0.023   29.130    0.000    0.611    0.680    0.660    0.761
   .W1_SR8            0.774    0.029   26.396    0.000    0.744    0.860    0.774    0.739
   .W1_SR9            0.857    0.032   26.960    0.000    0.809    0.910    0.857    0.769
   .W1_SR10           0.753    0.030   25.284    0.000    0.694    0.794    0.753    0.676
   .W1_SR11           0.595    0.019   31.304    0.000    0.561    0.616    0.595    0.778
   .W1_SR12           0.798    0.029   27.401    0.000    0.753    0.846    0.798    0.656
   .FBP               0.243    0.022   10.859    0.000    0.207    0.279    0.607    0.607
    SE                0.059    0.013    4.536    0.000    0.038    0.080    1.000    1.000
   .SR                0.084    0.011    7.593    0.000    0.061    0.097    0.350    0.350

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper   Std.lv  Std.all
    ab                0.416    0.053    7.884    0.000    0.363    0.520    0.206    0.206
    total             1.543    0.223    6.923    0.000    1.337    2.082    0.764    0.764

To obtain R Squared Values

lavInspect(sem_fit, "r2")
W1_FBP1 W1_FBP2 W1_FBP3 W1_FBP4 W1_FBP5  W1_SE1  W1_SE2  W1_SE3  W1_SE4  W1_SE5  W1_SE6 
  0.500   0.607   0.497   0.663   0.658   0.117   0.391   0.157   0.483   0.061   0.532 
 W1_SE7  W1_SE8  W1_SE9  W1_SR1  W1_SR2  W1_SR3  W1_SR4  W1_SR5  W1_SR6  W1_SR7  W1_SR8 
  0.515   0.371   0.449   0.302   0.345   0.222   0.117   0.112   0.414   0.239   0.261 
 W1_SR9 W1_SR10 W1_SR11 W1_SR12     FBP      SR 
  0.231   0.324   0.222   0.344   0.393   0.650 

Visualize it with a path diagram

semPlot::semPaths(sem_fit, whatLabels = "standardized", residuals = FALSE)

LS0tCnRpdGxlOiAiTXVsdGl2YXJpYXRlIFN0YXRpc3RpY3MsIE1vZHVsZSAjMTI6IFN0cnVjdHVyYWwgRXF1YXRpb24gTW9kZWxpbmcgKFNFTSksIFBhcnQgMiIKYXV0aG9yOiAiRHIuIEJyb2RhIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgpUaGlzIHdlZWssIHdlIHRhY2tsZSBlc3RpbWF0aW5nIGEgbWVkaWF0aW9uIG1vZGVsIGluIGFuIFNFTSBmcmFtZXdvcmssIGJvdGggd2l0aCBvYnNlcnZlZCB2YXJpYWJsZXMgYW5kIHdpdGggbGF0ZW50IGZhY3RvcnMuCgojIEVzdGltYXRpbmcgQSBtZWRpYXRpb24gbW9kZWwgd2l0aCBvYnNlcnZlZCB2YXJpYWJsZXMgKG5vIGxhdGVudCkKIyMgTG9hZCBpbiBuZWNlc3NhcnkgcGFja2FnZXMKYGBge3J9CmxpYnJhcnkodGlkeXZlcnNlKQpsaWJyYXJ5KHBzeWNoKQpsaWJyYXJ5KGxhdmFhbikKYGBgCiMjIExvYWQgaW4gdGhlIGhlYWx0aCBkYXRhCmBgYHtyfQpsaWJyYXJ5KGhhdmVuKQpoZWFsdGhfb2xkIDwtIHJlYWRfZHRhKCJoZWFsdGhfb2xkLmR0YSIpCmdsaW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