Import the dataset

library(readxl)
config2data <- read_excel("2_16_configural 2_data.xlsx")
View(config2data)

Call the lavaan package

library(lavaan)
## This is lavaan 0.6-7
## lavaan is BETA software! Please report any bugs.

Specify the factor structure

config.model <- '
  sa =~ NA*au7 + au2 + au10 +au12            #free up the estimation of au7
  sa ~~ 1*sa                                 #fix the variance of sa1 to 1
  au12 ~~ au10                               #correlate the two variables
  '

Specify the configural model, using the argument group= to specify the grouping variable

fit <- cfa(config.model, data = config2data, group = "gender")

Take a look at the model summary with model fit statistics

summary(fit, fit.measures=T)
## lavaan 0.6-7 ended normally after 48 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         26
##                                                       
##   Number of observations per group:                   
##     1                                               43
##     2                                              238
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 2.451
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.294
##   Test statistic for each group:
##     1                                            1.874
##     2                                            0.576
## 
## Model Test Baseline Model:
## 
##   Test statistic                               396.446
##   Degrees of freedom                                12
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.999
##   Tucker-Lewis Index (TLI)                       0.993
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1783.240
##   Loglikelihood unrestricted model (H1)      -1782.014
##                                                       
##   Akaike (AIC)                                3618.479
##   Bayesian (BIC)                              3713.077
##   Sample-size adjusted Bayesian (BIC)         3630.632
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.040
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.177
##   P-value RMSEA <= 0.05                          0.412
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.007
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## 
## Group 1 [1]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   sa =~                                               
##     au7               1.465    0.233    6.286    0.000
##     au2               1.190    0.212    5.627    0.000
##     au10              1.312    0.225    5.834    0.000
##     au12              1.393    0.203    6.876    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .au10 ~~                                             
##    .au12              0.438    0.259    1.690    0.091
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .au7               3.535    0.268   13.195    0.000
##    .au2               3.000    0.237   12.650    0.000
##    .au10              3.349    0.248   13.501    0.000
##    .au12              3.302    0.238   13.853    0.000
##     sa                0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     sa                1.000                           
##    .au7               0.939    0.325    2.888    0.004
##    .au2               1.002    0.274    3.662    0.000
##    .au10              0.924    0.320    2.892    0.004
##    .au12              0.502    0.254    1.972    0.049
## 
## 
## Group 2 [2]:
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   sa =~                                               
##     au7               0.965    0.108    8.918    0.000
##     au2               0.908    0.108    8.397    0.000
##     au10              0.769    0.102    7.545    0.000
##     au12              0.765    0.103    7.458    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .au10 ~~                                             
##    .au12              0.611    0.125    4.889    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .au7               3.529    0.091   38.942    0.000
##    .au2               3.336    0.093   35.976    0.000
##    .au10              2.987    0.086   34.654    0.000
##    .au12              2.790    0.087   32.190    0.000
##     sa                0.000                           
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     sa                1.000                           
##    .au7               1.023    0.171    5.995    0.000
##    .au2               1.222    0.169    7.238    0.000
##    .au10              1.178    0.147    8.025    0.000
##    .au12              1.202    0.149    8.093    0.000

If we want to look at specific data-model fit measures, we can use the function fitmeasures()

fitmeasures(fit, c("chisq","df","pvalue","cfi","tli","rmsea","rmsea.ci.lower","rmsea.ci.upper","AIC","srmr"))
##          chisq             df         pvalue            cfi            tli 
##          2.451          2.000          0.294          0.999          0.993 
##          rmsea rmsea.ci.lower rmsea.ci.upper            aic           srmr 
##          0.040          0.000          0.177       3618.479          0.007

There is an easier way to check all the four invariance model statistics:

library(semTools)
## 
## ###############################################################################
## This is semTools 0.5-4
## All users of R (or SEM) are invited to submit functions or ideas for functions.
## ###############################################################################
measurementInvariance(model = config.model, data = config2data, group = "gender")
## Warning: The measurementInvariance function is deprecated, and it will cease to
## be included in future versions of semTools. See help('semTools-deprecated) for
## details.
## Warning in lavaan::lavTestLRT(...): lavaan WARNING: method = "satorra.bentler.2001"
##   but no robust test statistics were used;
##   switching to the standard chi-square difference test
## 
## Measurement invariance models:
## 
## Model 1 : fit.configural
## Model 2 : fit.loadings
## Model 3 : fit.intercepts
## Model 4 : fit.means
## 
## Chi-Squared Difference Test
## 
##                Df    AIC    BIC   Chisq Chisq diff Df diff Pr(>Chisq)   
## fit.configural  2 3618.5 3713.1  2.4506                                 
## fit.loadings    6 3622.2 3702.2 14.1246    11.6740       4   0.019947 * 
## fit.intercepts  9 3630.2 3699.3 28.1854    14.0608       3   0.002824 **
## fit.means      10 3628.3 3693.8 28.2565     0.0711       1   0.789674   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Fit measures:
## 
##                  cfi rmsea cfi.delta rmsea.delta
## fit.configural 0.999 0.040        NA          NA
## fit.loadings   0.979 0.098     0.020       0.058
## fit.intercepts 0.950 0.123     0.029       0.025
## fit.means      0.953 0.114     0.002       0.009
Here are some frequently used syntax in lavaan:

predict, used for regression of observed outcome to observed predictors
=~ indicator, used for latent variable to observed indicator in factor analysis measurement models
~~ covariance
~1 intercept or mean (e.g., q01 ~ 1 estimates the mean of variable q01)
1* fixes parameter or loading to one
NA* frees parameter or loading (useful to override default marker method)
a* labels the parameter ‘a’, used for model constraints