How to manually compute fitted values to replicate those produced by lavPredict()
I need to manually predict fitted values for a growth model in R...
I know I can use lavPredict(fit, type="yhat") but I want to report/explain how these values are computed in my dissertation. What parameters from the growth model are included in the calculation of the fitted values?
Thanks for the help!
Solution 1:
I expect this question is more appropriately posted on CrossValidated, which is about statistics rather than general programming.
lavPredict(fit, type="yhat")
Note that these are not growth-factor scores, which are the random effects in a MLM, returned by lavPredict(fit, type="lv")
. These are expected values for each individual subject (conditional expectations, given factor scores). These are a function of indicator intercepts ($\nu$, which are fixed to zero in a LGCM), factor scores ($\eta_i$), and factor loadings ($\Lambda$); residuals are omitted for expected values, but otherwise the regression equation looks the same.
$$ \hat{y}_i = \nu + eta_i Lambda $$
Note that if anything predicts the growth factors, then those also have a regression model that can get substituted into the equation above:
$$ \eta_i = \alpha + X_i B $$
where B are "Beta" paths, $\alpha$ is a vector of factor intercepts, and $X_i$ is subject i's values of predictors (really, they are also included in $eta$; even observed predictors are internally "promoted" as single-indicator factors).
Alternatively, lavInspect(fit, "mean.ov")
gives you the marginal means (expected values) of the observed variables, interpreted as the expected values for people with average factor scores (average latent intercept and slope). Is that what you are after?
The marginal means of any SEM are a function of indicator intercepts ($\nu$, which are fixed to zero in a LGCM), factor intercepts ($\alpha$), factor loadings ($\Lambda$), and latent regressions (Beta, if any). If any observed variables predict growth factors, consider them as single-indicator factors (that is how lavaan
treats them in the background). You can save the set of parameter matrices using
EST <- lavInspect(fit, "est")
It is a list, so you can extract each matrix using EST$lambda
, etc.
Then use the following formula to calculate model-implied marginal means:
$$\mu = \nu + \Lambda (I - B)^{-1} \alpha$$
where $I$ is an identity matrix with the same dimensions as Beta.
These estimates can also be used to calculate conditional means, but you would have to choose one of the available methods for estimating factor scores and save them from lavPredict()
.