Obtaining factor scores in blavaan

227 views
Skip to first unread message

David Kaplan

unread,
Dec 20, 2017, 12:25:32 PM12/20/17
to blavaan
Greetings,

Is it possible to obtain the factor scores in blavaan via data augmentation methods or other possibilities?

Thanks

David

Ed Merkle

unread,
Dec 20, 2017, 3:18:37 PM12/20/17
to blavaan
Hi David,

This is available in the development version from my website (which should go to CRAN in January). When you run the model, you add the argument save.lvs=TRUE. Then you can use an extra command afterwards to get factor scores:

fscores <- blavInspect(fit, "lvs")

where fit is the blavaan model. This gives you the factor scores sampled on each iteration, as an mcmc object. So, if you sampled for 10k iterations, you will get back 10k factor score samples (where each sample contains a unique score for each person on each factor). If you only want the posterior mean factor scores, you can do

blavInspect(fit, "lvs", "means")

For info on installing the development version, see

http://faculty.missouri.edu/~merklee/blavaan/source.html

Ed

David Kaplan

unread,
Dec 20, 2017, 10:14:33 PM12/20/17
to blavaan
Hi again, Ed. 

Does this only work for bcfa?  I tried it with bgrowth and received the following error message

fit <- bgrowth(model.syntax, save.lvs=TRUE, data=Demo.growth,
               jagcontrol=list(method="rjparallel"))
blavInspect(fit,"lvs","means")

Error in lavInspect.lavaan("means", object = <S4 object of class "blavaan">,  : 
  unknown `what' argument in inspect function: `lvs'

Ed Merkle

unread,
Dec 20, 2017, 10:49:42 PM12/20/17
to David Kaplan, blavaan
It should work regardless of bsem/bcfa/bgrowth/blavaan. I guess you might see that error if you are using an older version of blavaan, because this factor score stuff is only in newer versions. If you do the command

packageDescription("blavaan")$Version

you should see 0.2-5.259 if you are at the newest development version (and see my last email for info on installing, if you need it).

It seems like Rstudio does something funny with changing package versions behind people's backs, so maybe that could be an explanation if you already installed the newest blavaan version.

Ed




--
You received this message because you are subscribed to the Google Groups "blavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/b82ddc39-a5f8-44bb-aaf6-d1e22cd2dea3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Kaplan

unread,
Dec 21, 2017, 2:05:54 PM12/21/17
to blavaan
So, I double checked the version and it's correct but there are new errors.  Here is the code I am running (your example in bgrowth, with blavInspect added)

## Not run: 
## linear growth model with a time-varying covariate
model.syntax <- '
# intercept and slope with fixed coefficients
i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4

# regressions
i ~ x1 + x2
s ~ x1 + x2

# time-varying covariates
t1 ~ c1
t2 ~ c2
t3 ~ c3
t4 ~ c4
'

fit <- bgrowth(model.syntax, save.lvs=TRUE,data=Demo.growth)
summary(fit)
blavInspect(fit,"lvs","means")

Here are the error messages after the 10000 post-burnin iterations

Calculating summary statistics...
Note: The monitored variables 'beta[3,1,1]', 'beta[4,1,1]', 'beta[5,1,1]',
'beta[6,1,1]', 'beta[3,2,1]', 'beta[4,2,1]', 'beta[5,2,1]', 'beta[6,2,1]',
'psi[7,7,1]', 'psi[8,8,1]', 'psi[9,9,1]', 'psi[10,10,1]', 'psi[11,11,1]',
'psi[12,12,1]', 'alpha[3,1,1]', 'alpha[4,1,1]', 'alpha[5,1,1]', 'alpha[6,1,1]',
'alpha[7,1,1]', 'alpha[8,1,1]', 'alpha[9,1,1]', 'alpha[10,1,1]', 'alpha[11,1,1]',
'alpha[12,1,1]' and 'psi[13,13,1]' appear to be non-stochastic; they will not be
included in the convergence diagnostic
Calculating the Gelman-Rubin statistic for 1246 variables....
Finished running the simulation
Error in ETA[, ov.x.dummy.lv.idx, drop = FALSE] : subscript out of bounds
In addition: Warning messages:
1: In lav_mvnorm_missing_loglik_samplestats(Yp = lavsamplestats@missing[[g]],  :
  lavaan WARNING: x.idx not supported yet (ignored)
2: In lav_mvnorm_missing_loglik_samplestats(Yp = lavsamplestats@missing[[g]],  :
  lavaan WARNING: x.idx not supported yet (ignored)
> summary(fit)
lavaan (0.6-1.1181) converged normally after  31 iterations

  Number of observations                           400
Error in lav_data_print_short(object@Data) : 
  no slot of name "weights" for this object of class "lavData"
> blavInspect(fit,"lvs","means")
Error: inherits(blavobject, "blavaan") is not TRUE

Thanks for your assistance on this.

David
To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+u...@googlegroups.com.

Ed Merkle

unread,
Dec 21, 2017, 3:05:56 PM12/21/17
to David Kaplan, blavaan
Thanks, I tried out the model and also see that error. It is some new features conflicting with one another, and it should be fixed soon.

If I have learned anything from this package, it is that some growth model will always make things break.

Ed

To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/fab04798-d164-4daf-a205-e2dc1b8f8670%40googlegroups.com.

David Kaplan

unread,
Dec 21, 2017, 4:23:10 PM12/21/17
to blavaan
Interesting.  Well, thanks very much for your time.  I'll check back periodically.

David

Ed Merkle

unread,
Dec 21, 2017, 11:16:22 PM12/21/17
to David Kaplan, blavaan
This should now be fixed in the version from my website. And you can now use the command

blavInspect(fit, "lvmeans")

instead of the more awkward blavInspect(fit, "lvs", "means"). So "lvs" gives you all the draws of the factor scores across iterations, whereas "lvmeans" just gives you each factor score mean in a matrix.

Ed





To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/bca94266-e755-4b8d-bb1d-cdd9b505fd88%40googlegroups.com.

David Kaplan

unread,
Dec 22, 2017, 3:18:46 PM12/22/17
to blavaan
Hi Ed,

Thanks for your attention to this. My apologies for all the emails.  I'm running the version from your webpage, version 0.2-5.264.  The code I am running is 

## Not run: 
## linear growth model with a time-varying covariate
model.syntax <- '
# intercept and slope with fixed coefficients
i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4

# regressions
i ~ x1 + x2
s ~ x1 + x2

# time-varying covariates
t1 ~ c1
t2 ~ c2
t3 ~ c3
t4 ~ c4
'

fit <- bgrowth(model.syntax, save.lvs=TRUE,data=Demo.growth)
summary(fit)
blavInspect(fit,"lvmeans")

and the error message I am now getting is

Finished running the simulation
Error in ETA[, ov.x.dummy.lv.idx, drop = FALSE] : subscript out of bounds
In addition: Warning messages:
1: In lav_mvnorm_missing_loglik_samplestats(Yp = lavsamplestats@missing[[g]],  :
  lavaan WARNING: x.idx not supported yet (ignored)
2: In lav_mvnorm_missing_loglik_samplestats(Yp = lavsamplestats@missing[[g]],  :
  lavaan WARNING: x.idx not supported yet (ignored)

Ed Merkle

unread,
Dec 22, 2017, 3:59:39 PM12/22/17
to David Kaplan, blavaan
Thanks for the report. I have not tried it with the default number of iterations (burnin of 4000, sample of 10k) because I am currently on a laptop that doesn't have enough memory for all those factor scores. But I tried it with 1000 burnin and 1000 sample iterations and it seemed to work. This leads me to think there may be some issues with particular versions of other packages, or maybe operating system (I am on linux, and there are occasional differences in windows). Here are my package versions, from sessionInfo() (and I also was successful with lavaan 0.6-1.1183)

other attached packages:
[1] blavaan_0.2-5.264 lavaan_0.6-1.1175 runjags_2.0.4-2 

loaded via a namespace (and not attached):
 [1] matrixStats_0.52.2 mvtnorm_1.0-6      quadprog_1.5-5     lattice_0.20-35  
 [5] zoo_1.8-0          MASS_7.3-47        nonnest2_0.5       grid_3.4.1       
 [9] MatrixModels_0.4-1 stats4_3.4.1       coda_0.19-1        SparseM_1.77     
[13] rjags_4-6          Matrix_1.2-11      pbivnorm_0.6.0     modeest_2.1      
[17] sandwich_2.4-0     tools_3.4.1        loo_1.1.0          CompQuadForm_1.4.3
[21] parallel_3.4.1     compiler_3.4.1     mnormt_1.5-5       mcmc_0.9-5       
[25] MCMCpack_1.4-0     quantreg_5.33



To unsubscribe from this group and stop receiving emails from it, send an email to blavaan+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blavaan/72ebf4b2-4ae3-4522-82a0-46db191d51bb%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages