# Predicting in Structural Equation Modelling

So I would like to do some basic predictions with the SEM model I have produced.

So far I've come across 'lavPredict' but i'm struggling to work it.

library(lavaan)

iq.model7 <- 'verbal_letters =~ reason.4 + reason.16 + reason.17 + reason.19 + letter.7 + letter.33 + letter.34 + letter.58
matrix =~ matrix.45 + matrix.55
rotate =~ rotate.3 + rotate.6 + rotate.8
generaliq =~ verbal_letters + matrix + rotate'

iq.fit7 <-  cfa(iq.model7, data = iqitems)
summary(iq.fit7, standardized = TRUE, fit.measures = TRUE)

sempreds <- lavPredict(iq.fit7, type = "lv")

#this produces a matrix with predictions for all 4 of my latent variables:
#verbal_letters, matrix and rotate which are the latents predicted by the
#manifest vars, then the top level latent 'generaliq', which is predicted by the
#previous 3 latents.


If I then try and produce a basic graph of one prediction against another, it creates an error, like this:

ggplot(sempreds, aes(x = matrix, y = generaliq)) + geom_point() + stat_smooth()
'Error: data must be a data frame, or other object coercible by fortify(),
not an S3 object with class lavaan.matrix/matrix'



Any ideas on how to get my matrix into a suitable DF?

Also if you look at 'sempreds', which are my predicted latent vars, they are not centred around 0. Would it be sensible to transform these, perhaps with such methods as a logarithm or by adding the most negative value to all the data points within a vector?

I very much welcome any other advice on making predictions with SEM as it's all new to me, thanks.

You can just run as.data.frame() on the sempreds object to turn it into a data.frame. Note that this isn't actually prediction; you're estimating factor scores. This covered in any SEM textbook (e.g., Bollen 1989). Factor scores are imperfect estimates of the latent variable that can often be used in subsequent analyses or for descriptive purposes. That graph you made isn't very useful because you already know (from your previous post) that the latent variables are highly correlated. They are constrained to have a normal distribution so you probably won't find any interesting values. The factor scores come from your model, not directly from the data, so it isn't clear to me how looking at their distributions will be informative.