Confirmatory Factor Analysis with Lavaan - Private-Projects237/Statistics GitHub Wiki
Overview
Here we will give an introductory overview to running CFA models in Lavaan.
Part 1: Running a model using Raw Data, Covariance, and Correlation
There are at least three different ways to prepare data for it be used in a CFA model. These include:
- Raw data
- Covariance matrix
- Correlation matrix
1.1 Generating raw data
# set seed
set.seed(123)
# set sample size
n <- 300
# Create a distribution for a factor
F1 <- rnorm(n, mean = 0, sd = 1)
# Create observable variables from that factor (so they become correlated)
X1 <- F1 + rnorm(n, mean = 0, sd = 0.3)
X2 <- 0.8 * F1 + rnorm(n, mean = 0, sd = 0.4)
X3 <- 0.6 * F1 + rnorm(n, mean = 0, sd = 0.5)
# Combine into a data frame
dat <- data.frame(X1 = X1, X2 = X2, X3 = X3)
# Calculate, sd, cov, and cor
dat_sd <- sapply(dat, sd)
dat_cov <- cov(dat)
dat_cor <- cor(dat)
1.2 Run three separate CFA models with the same model relationships
mod1 <- '
# Measurement model (fixed = 1; free = 2)
F1 =~ 1*X1 + X2 + X3
# Latent variable (free = 1)
F1 ~~ F1
# Residual variance (free = 3)
X1 ~~ X1
X2 ~~ X2
X3 ~~ X3
# Total estimated parameters = 3*4/2 = 6
# Freely estimated parameters = 2 + 1 + 3 = 6
# df = 6 - 6 = 0
'
# Calculate CFA
library(lavaan)
fit1 <- lavaan(mod1, dat)
fit2 <- cfa(mod1, sample.cov = dat_cov, sample.nobs = n)
fit3 <- cfa(mod1, sample.cov = dat_cor, std.lv = FALSE,sample.nobs = n)
1.3 Showcase the unstandardized and standardized outputs
# Showcases outputs in a matrix
inspect(fit1, "std")
inspect(fit2, "std")
inspect(fit3, "std")
inspect(fit1, "coef")
inspect(fit2, "coef")
inspect(fit3, "coef")
# Showcase outputs as rows
parameterEstimates(fit1)
parameterEstimates(fit2)
parameterEstimates(fit3)
standardizedSolution(fit1)
standardizedSolution(fit2)
standardizedSolution(fit3)
1.4 Raw Data, Covariance, and Correlation all Produce the same Standardized Estimates
Output from CFA using Raw Data | Output from CFA using Covariance | Output from CFA using Correlation |
---|---|---|
Takeaway: You can successfully run a CFA model from raw data (rare), covariance (common), and correlations (common). Specifically, each approach will produce identical standardized estimates as shown here. However, there is a limitation for correlations, they unfortunately cannot produce the correct unstandardized estimates, but for most purposes this is not a big deal.
1.5 Converting Correlation to Covariance
Due to the limitation mentioned before, if standard deviations are present for the observable variables, then that can be used to convert correlations into covariances.
# Converting correlations into covariances
dat_cov2 <- dat_cor * (dat_sd %*% t(dat_sd))
# Print the original covariance and the calculate one
round(cbind(dat_cov, dat_cov2),3)
Original Covariance vs Calculate Covariance from Correlations |
---|