I'm having trouble understanding the relationship between the latent variables produced by the CFA function in R's Lavaan package, and linear regression.
I have used one of Lavaan's built-in data sets to create a latent variable. I have then used all of the variables in the data set to produce a simple linear regression on a subset of the data (a training data set). The result is a linear model that perfectly predicts (R-squared = 1.0, RMSE = 0) the resulting latent variable on a hold-out validation data set. I am wondering why this happens.
Here's my sample code:
library(lavaan) ## Load the data lavData = lavaan::HolzingerSwineford1939 ## Define the model (as in the tutorial at http://lavaan.ugent.be/tutorial/cfa.html) HS.model <- ' visual =~ x1 + x2 + x3 ' ## Fit the model fit <- lavaan::cfa(HS.model, data=lavData) ## Generate the latent variable lavData$latent_variable <- lavPredict(fit, type = "lv", newdata = lavData) ## Split the data into training and test idx <- sample(1:nrow(lavData), size = 100, replace = F) trainData <- lavData[idx, ] testData <- lavData[-idx, ] ## Define the linear model using all predictors in the data, including the three LV components lm1 <- lm(latent_variable ~ ., data = trainData) ## Generate the predictions and residuals testData$yhat <- predict(lm1, newdata = testData) testData$resid <- testData$latent_variable - testData$yhat ## Summarize the residuals summary(round(testData$resid, digits = 5))
Now, if the model contained only the three components of the latent variable (x1, x2, x3) as predictors, then I could understand why this would happen. But the model contains 12 other predictors which should influence the predictions away from their true values, no?
Can anybody explain what exactly is going on here? Thanks!