blavaan ERROR: problem with translation from lavaan to MCMC syntax
lobandov opened this issue · 11 comments
Hi, I´m running a measurement invariance testing with blavaan but I´m getting de next error:
[1] "Error in lamsign[l1, 1] : subscript out of bounds\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<subscriptOutOfBoundsError in lamsign[l1, 1]: subscript out of bounds>
Error in blavaan(mod_sem_1, ordered = c("y3", "y4", "y6", "y7", "y8", :
blavaan ERROR: problem with translation from lavaan to MCMC syntax.
I´m working on Rstudio with the code:
fit_mg_g3 <- bsem(mod_sem_1,
ordered=c("y3","y4","y6","y7","y8","y9","y10","y11","y12","y13",
"x14","x16","x17","x18","x19","x20","x21","x22","x23","x24"),
std.lv=TRUE, dp=dpriors(lambda="normal(1,1)"),
group = "genero",
group.equal = "loadings",
n.chains = 3, burnin = 9000, sample = 1000,
data=datos_imputados)
I already tried to re-install blavaan package and I´m sure that the code, variable and data are fine. Actually, the multigroup model without constraints run succesfully, only after I add the line "group.equal = "loadings" the error show up. I really hope you can help me fix it. Thanks.
This is the model I´m working on:
mod_sem_1 <- '
# Modelo de medicion
# Parte endogena
eta1 =~ y3 + y4 + y5 # Patron de consumo
eta2 =~ y6 + y7 + y8 + y9 + y10 + y11 + y12 + y13 # Consumo problematico
# Parte exógena
xi1 =~ x14 + x15 + x16 # Factores individuales
xi2 =~ x17 + x18 + x19 + x20 + x21 # Factores micro-sociales
xi3 =~ x22 + x23 + x24 # Factores macro-sociales
#Modelo estructural
eta1 ~ xi1 + xi2 + xi3
eta2 ~ eta1
# Covariancias
xi1 ~~ xi2
xi1 ~~ xi3
xi2 ~~ xi3
'
only after I add the line
group.equal = "loadings"
the error show up
I don't know what causes the translation error, but loadings are not comparable across groups unless the latent-response scales are linked. You first need to establish threshold invariance before testing metric invariance. If your items are binary, these sources of DIF are confounded, so you have to simultaneously constrain thresholds, loadings, and intercepts. The semTools::measEq.syntax
function can help you write the lavaan model syntax.
Thanks for the report. If you haven't already, please ensure that blavaan is updated to 0.5-2, or to the newest version on github. Also, could you tell us how many groups you have, and how many categories does each ordinal variable have?
Finally, try removing these three lines from the model specification to see if it helps:
# Covariancias
xi1 ~~ xi2
xi1 ~~ xi3
xi2 ~~ xi3
The reason is that the bsem()
command automatically adds those covariances (via auto.cov.lv.x = TRUE
), and I have seen situations where blavaan messes up sign constraints when you manually add these "automatic" parameters.
Thanks for the answers. I am going to try you are suggesting to.
The grouping variable is gender, so I have two groups. Most of indicators are dichotomous (true or false) and some others have many cathegories like income.
I already updated lavaan package to newest version, here is my sessionInfo() if you want to check it out:
`R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: America/Costa_Rica
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] semTools_0.5-6 lavaan_0.6-16 semPlot_1.1.6 forcats_1.0.0 cowplot_1.1.1 ggplot2_3.4.4
[7] dplyr_1.1.4 lattice_0.21-9 MASS_7.3-60 nnet_7.3-19 car_3.1-2 carData_3.0-5
[13] blavaan_0.5-2 Rcpp_1.0.11
loaded via a namespace (and not attached):
[1] mnormt_2.1.1 pbapply_1.7-2 gridExtra_2.3 inline_0.3.19
[5] fdrtool_1.2.17 sandwich_3.0-2 rlang_1.1.2 magrittr_2.0.3
[9] rockchalk_1.8.157 matrixStats_1.1.0 compiler_4.3.2 loo_2.6.0
[13] reshape2_1.4.4 png_0.1-8 callr_3.7.3 vctrs_0.6.4
[17] OpenMx_2.21.11 stringr_1.5.1 quadprog_1.5-8 pkgconfig_2.0.3
[21] crayon_1.5.2 fastmap_1.1.1 backports_1.4.1 arm_1.13-1
[25] pbivnorm_0.6.0 utf8_1.2.4 rmarkdown_2.25 ps_1.7.5
[29] nloptr_2.0.3 xfun_0.41 kutils_1.73 jsonlite_1.8.7
[33] psych_2.3.9 jpeg_0.1-10 cluster_2.1.4 parallel_4.3.2
[37] prettyunits_1.2.0 R6_2.5.1 stringi_1.8.2 StanHeaders_2.26.28
[41] rpart_4.1.21 parallelly_1.36.0 boot_1.3-28.1 rstan_2.32.3
[45] knitr_1.45 future.apply_1.11.0 zoo_1.8-12 base64enc_0.1-3
[49] bayesplot_1.10.0 igraph_1.5.1 Matrix_1.6-4 splines_4.3.2
[53] tidyselect_1.2.0 rstudioapi_0.15.0 abind_1.4-5 yaml_2.3.7
[57] codetools_0.2-19 tmvnsim_1.0-2 processx_3.8.2 qgraph_1.9.8
[61] listenv_0.9.0 pkgbuild_1.4.2 tibble_3.2.1 nonnest2_0.5-6
[65] plyr_1.8.9 withr_2.5.2 coda_0.19-4 evaluate_0.23
[69] foreign_0.8-85 future_1.33.0 CompQuadForm_1.4.3 RcppParallel_5.1.7
[73] zip_2.3.0 pillar_1.9.0 checkmate_2.3.0 stats4_4.3.2
[77] generics_0.1.3 rstantools_2.3.1.1 munsell_0.5.0 scales_1.3.0
[81] minqa_1.2.6 gtools_3.9.5 globals_0.16.2 xtable_1.8-4
[85] glue_1.6.2 mi_1.1 Hmisc_5.1-1 tools_4.3.2
[89] data.table_1.14.8 lme4_1.1-35.1 openxlsx_4.2.5.2 mvtnorm_1.2-4
[93] XML_3.99-0.16 grid_4.3.2 sem_3.1-15 QuickJSR_1.0.8
[97] colorspace_2.1-0 nlme_3.1-163 htmlTable_2.4.2 Formula_1.2-5
[101] cli_3.6.1 fansi_1.0.5 glasso_1.11 corpcor_1.6.10
[105] gtable_0.3.4 runjags_2.2.2-1.1 digest_0.6.33 htmlwidgets_1.6.3
[109] htmltools_0.5.7 lifecycle_1.0.4 lisrelToR_0.1.5 `
Also, I just tried removing the lines of covariances from the model specification and simultaneously constrain thresholds, loadings, and intercepts, in the following way:
fit_mg_g3 <- blavaan::bsem(mod_sem_2, ordered=c("y3","y4","y6","y7","y8","y9","y10","y11","y12","y13", "x14","x16","x17","x18","x19","x20","x21","x22","x23","x24"), std.lv=TRUE, dp=dpriors(lambda="normal(1,1)"), group = "genero", group.equal = c("intercepts", "loadings", "thresholds"), n.chains = 3, burnin = 9000, sample = 1000, data=datos_imputados)
But the error persist.
If I try to constrain only intercepts or only regression coefficients from the structural equations, the model run without problems.
Ok, I think I can see the problem and I think it is related to different items having different numbers of categories. I will work on it some more soon.
Are you able to install blavaan from github, to see whether the latest updates have solved the problem? I reproduced a similar problem using the CRAN version, but the model worked under the github version.
To install from github:
remotes::install_github("ecmerkle/blavaan", INSTALL_opts = "--no-multiarch")
Thanks for the extra info, and happy 2024. The first error sounds like you might have R open in a different window, with the blavaan package loaded. Or maybe you don't have permissions to write to the C:\Users\ladyo folder, or maybe there was a failed installation.
Related to failed installations: installing blavaan from github is not entirely simple on Windows, because you need some extra tools on your machine to build the Stan model from source. If you haven't done it before, these instructions about RStan are relevant (see the C++ toolchain part):
https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
Or, if you wait a week or two, I hope to send a new blavaan version to CRAN, which would allow you to bypass this.
Ok, please re-open the issue if the next version causes problems!