2
$\begingroup$

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)
iqitems <- read_csv('http://vincentarelbundock.github.io/Rdatasets/csv/psych/iqitems.csv')

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ yeah I have since converted to a DF and done a plot_grid of the three latent vars on the x-axis and generaliq on the y-axis, but like you have said, this doesnt show anything new than what the output/path diagram I have shows. What would a prediction in SEM look like then? Is it similar to a more simple regression eg logistic, where you can use a predict function on the model, then add these to a data frame/tibble that you've created with fixed values of the predictor vars? Which in this case would be the manifest vars? More crucially, should I be predicting manifest from latent, or opposite? $\endgroup$ Commented Jan 6, 2020 at 9:04
  • $\begingroup$ This should be its own question. $\endgroup$
    – Noah
    Commented Jan 13, 2020 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.