# Load the required librarylibrary(dplyr)library(lavaan)# Create the example data frameset.seed(122) # For reproducibilityn <-2000# Number of samplesexample_data <-data.frame(intercept =rep(3, n)) %>%mutate(X1 =rnorm(n, mean =10, sd =1),X2 = X1 +rnorm(n, mean =15, sd =1),X3 = X2 +rnorm(n, mean =5, sd =1),residual =rnorm(n, mean =0, sd =1))# Let's add a linear relationship to create criteria variable Yexample_data <- example_data %>%mutate(Y = intercept +0.5* X1 +0.3* X2 +0.2* X3 + residual) %>%relocate(Y)example_data %>%slice(1:10)
Call:
lm(formula = Y ~ X1 + X2 + X3, data = example_data)
Residuals:
Min 1Q Median 3Q Max
-3.3822 -0.6897 0.0199 0.7067 3.2161
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.96843 0.43415 6.837 1.07e-11 ***
X1 0.50581 0.03166 15.977 < 2e-16 ***
X2 0.29510 0.03150 9.369 < 2e-16 ***
X3 0.20252 0.02246 9.019 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.013 on 1996 degrees of freedom
Multiple R-squared: 0.56, Adjusted R-squared: 0.5593
F-statistic: 846.8 on 3 and 1996 DF, p-value: < 2.2e-16
Do the same regression with lavaan
Code
# Define the modelmodel <-' # Regress Y on X1, X2, and X3 Y ~ X1 + X2 + X3'# Fit the modelfit <-sem(model, data = example_data)# View the summary of the modelsummary(fit)
lavaan 0.6.15 ended normally after 1 iteration
Estimator ML
Optimization method NLMINB
Number of model parameters 4
Number of observations 2000
Model Test User Model:
Test statistic 0.000
Degrees of freedom 0
Parameter Estimates:
Standard errors Standard
Information Expected
Information saturated (h1) model Structured
Regressions:
Estimate Std.Err z-value P(>|z|)
Y ~
X1 0.506 0.032 15.993 0.000
X2 0.295 0.031 9.378 0.000
X3 0.203 0.022 9.028 0.000
Variances:
Estimate Std.Err z-value P(>|z|)
.Y 1.023 0.032 31.623 0.000
… add estimations for the intercept and standardized weights:
Code
fit <-sem(model, data = example_data, meanstructure =TRUE)summary(fit, standardized =TRUE)
lavaan 0.6.15 ended normally after 16 iterations
Estimator ML
Optimization method NLMINB
Number of model parameters 5
Number of observations 2000
Model Test User Model:
Test statistic 0.000
Degrees of freedom 0
Parameter Estimates:
Standard errors Standard
Information Expected
Information saturated (h1) model Structured
Regressions:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
Y ~
X1 0.506 0.032 15.993 0.000 0.506 0.338
X2 0.295 0.031 9.378 0.000 0.295 0.271
X3 0.203 0.022 9.028 0.000 0.203 0.227
Intercepts:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.Y 2.968 0.434 6.844 0.000 2.968 1.946
Variances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.Y 1.023 0.032 31.623 0.000 1.023 0.440
Adding a measurement structure to all variables (endogeneous and exogeneous):
Code
# Define the modelmodel <-' # Regress Y on X1, X2, and X3 LY =~ Y LX1 =~ X1 LX2 =~ X2 LX3 =~ X3 LY ~ LX1 + LX2 + LX3'# Fit the modelfit <-sem(model, data = example_data, meanstructure =TRUE)# View the summary of the modelsummary(fit, standardize =TRUE)