# How is $R^2$ calculated in the lavaan package for structural equation modeling?

I am doing a study for which I tested a model like this:

Model1: Var1 --> var2--> var3 --> var4. All variables are latent variables and have several indicators.

Now, the $R^2$ of var3 is 0.4 in this case. However, something strange happens when I test the model:

Model2: Var1 --> var2 --> var3.

In this case, although the variables BEFORE var3 remain exactly the same, the $R^2$ of var3 becomes much smaller (0.28).

Does anyone have a possible explanation for this? How does lavaan package in R calculate $R^2$? Especially for mediating variables like var3 in this case? The indicators for var3 remain the same. I've already checked whether this is due to missings in var4 but this is not the case.

In Lavaan, $R^2$ is a byproduct of the other parameters, it's not a parameter that is directly calculated. So $R^2$ for Var3 depends on the variance of var3, and the size of the path from var2 to var3. And these all depend on the loading.
When you add something to a model (var4) this can have effects all over the model - people sometimes talk about mis-specification 'flowing through the model'. Let's say that var2 and var4 are not correlated, but var2 and var3 are correlated, and var3 and var4 are correlated. If the estimates stay the same, the predicted correlation between var2 and var3 will be too high, so these the var2-> var3 estimate is reduced, lowering $R^2$, and this would be indicated by a lack of fit.