This question has received a number of views since it was first posed, but no answers. Here is a solution, which may be useful to future readers of this question.
To demonstrate it works I will first run a cfa()
model in using the HolzingerSwineford1939
. The model is taken from the lavaan
tutorial page.
library(lavaan)
dat<-data.frame(HolzingerSwineford1939[,7:15])
mod<-'
visual=~x1+x2+x3
textual=~x4+x5+x6
speed=~x7+x8+x9
'
fit<-cfa(mod, data = dat)
This returns the following solution:
> summary(fit)
lavaan (0.5-22) converged normally after 35 iterations
Number of observations 301
Estimator ML
Minimum Function Test Statistic 85.306
Degrees of freedom 24
P-value (Chi-square) 0.000
Parameter Estimates:
Information Expected
Standard Errors Standard
Latent Variables:
Estimate Std.Err z-value P(>|z|)
visual =~
x1 1.000
x2 0.554 0.100 5.554 0.000
x3 0.729 0.109 6.685 0.000
textual =~
x4 1.000
x5 1.113 0.065 17.014 0.000
x6 0.926 0.055 16.703 0.000
speed =~
x7 1.000
x8 1.180 0.165 7.152 0.000
x9 1.082 0.151 7.155 0.000
Covariances:
Estimate Std.Err z-value P(>|z|)
visual ~~
textual 0.408 0.074 5.552 0.000
speed 0.262 0.056 4.660 0.000
textual ~~
speed 0.173 0.049 3.518 0.000
Variances:
Estimate Std.Err z-value P(>|z|)
.x1 0.549 0.114 4.833 0.000
.x2 1.134 0.102 11.146 0.000
.x3 0.844 0.091 9.317 0.000
.x4 0.371 0.048 7.779 0.000
.x5 0.446 0.058 7.642 0.000
.x6 0.356 0.043 8.277 0.000
.x7 0.799 0.081 9.823 0.000
.x8 0.488 0.074 6.573 0.000
.x9 0.566 0.071 8.003 0.000
visual 0.809 0.145 5.564 0.000
textual 0.979 0.112 8.737 0.000
speed 0.384 0.086 4.451 0.000
When using raw data for input the lavPredict()
and predict()
return predicted values for the latent variables.
> head(lavPredict(fit))
visual textual speed
[1,] -0.81767524 -0.13754501 0.06150726
[2,] 0.04951940 -1.01272402 0.62549360
[3,] -0.76139670 -1.87228634 -0.84057276
[4,] 0.41934153 0.01848569 -0.27133710
[5,] -0.41590481 -0.12225009 0.19432951
[6,] 0.02325632 -1.32981727 0.70885348
Running the same model with the covariance matrix as input returns the same results, but as the original poster notes yields an error when attempting to derive the factor scores.
> COV<-cov(dat)
> fit1<-cfa(mod, sample.cov = COV, sample.nobs = 301, sample.mean = colMeans(dat))
> lavPredict(fit1)
Error in lavPredict(fit1) :
lavaan ERROR: sample statistics were used for fitting and newdata is empty
The solution is fairly straightforward as what the package needs is some raw data to "chew on" so to speak. Here you amend the code to identify the original dataset as raw data input for the prediction function (lavPredict(fit1, newdata = dat)
). This returns the following (which remember is the same model fitted in lavaan but using the covariance matrix as input).
> head(lavPredict(fit1, newdata = dat))
visual textual speed
[1,] -0.81767524 -0.13754501 0.06150726
[2,] 0.04951940 -1.01272402 0.62549360
[3,] -0.76139670 -1.87228634 -0.84057276
[4,] 0.41934153 0.01848569 -0.27133710
[5,] -0.41590481 -0.12225009 0.19432951
[6,] 0.02325632 -1.32981727 0.70885348
As you can see the results are identical.