ecmerkle / blavaan

An R package for Bayesian structural equation modeling
https://ecmerkle.github.io/blavaan
86 stars 23 forks source link

*blavaan* broken after latest update of *lavaan* #79

Closed strengejacke closed 3 months ago

strengejacke commented 3 months ago

This code no longer works, and lavaan was recently updated on CRAN. I think that could be a reason for the code failure.

library(blavaan)
#> Loading required package: Rcpp
#> This is blavaan 0.5-4
#> On multicore systems, we suggest use of future::plan("multicore") or
#>   future::plan("multisession") for faster post-MCMC computations.

data("PoliticalDemocracy", package = "lavaan")

model <- "
    # latent variable definitions
    dem60 =~ y1 + a*y2
    dem65 =~ y5 + a*y6

    # regressions
    dem65 ~ dem60

    # residual correlations
    y1 ~~ y5
  "

bfit <- blavaan::bsem(
  model,
  data = PoliticalDemocracy,
  n.chains = 1,
  burnin = 50,
  sample = 100
)
#> Error: lavaan->lav_options_checkvalues():  
#>    invalid value in model.type option: "bsem".

Created on 2024-06-09 with reprex v2.1.0

TDJorgensen commented 3 months ago

Thanks for the reprex. Have you tried the code after installing the latest development version of blavaan from GitHub?

strengejacke commented 3 months ago

Seems to improve the situation :-) One error remains, probably when printing:

library(blavaan)
#> Loading required package: Rcpp
#> This is blavaan 0.5-4.1279
#> On multicore systems, we suggest use of future::plan("multicore") or
#>   future::plan("multisession") for faster post-MCMC computations.
data("PoliticalDemocracy", package = "lavaan")

model <- "
    # latent variable definitions
    dem60 =~ y1 + a*y2
    dem65 =~ y5 + a*y6

    # regressions
    dem65 ~ dem60

    # residual correlations
    y1 ~~ y5
  "

bfit <- blavaan::bsem(
  model,
  data = PoliticalDemocracy,
  n.chains = 1,
  burnin = 50,
  sample = 100
)
#> 
#> SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.000214 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 2.14 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: WARNING: There aren't enough warmup iterations to fit the
#> Chain 1:          three stages of adaptation as currently configured.
#> Chain 1:          Reducing each adaptation stage to 15%/75%/10% of
#> Chain 1:          the given number of warmup iterations:
#> Chain 1:            init_buffer = 7
#> Chain 1:            adapt_window = 38
#> Chain 1:            term_buffer = 5
#> Chain 1: 
#> Chain 1: Iteration:   1 / 150 [  0%]  (Warmup)
#> Chain 1: Iteration:  15 / 150 [ 10%]  (Warmup)
#> Chain 1: Iteration:  30 / 150 [ 20%]  (Warmup)
#> Chain 1: Iteration:  45 / 150 [ 30%]  (Warmup)
#> Chain 1: Iteration:  51 / 150 [ 34%]  (Sampling)
#> Chain 1: Iteration:  65 / 150 [ 43%]  (Sampling)
#> Chain 1: Iteration:  80 / 150 [ 53%]  (Sampling)
#> Chain 1: Iteration:  95 / 150 [ 63%]  (Sampling)
#> Chain 1: Iteration: 110 / 150 [ 73%]  (Sampling)
#> Chain 1: Iteration: 125 / 150 [ 83%]  (Sampling)
#> Chain 1: Iteration: 140 / 150 [ 93%]  (Sampling)
#> Chain 1: Iteration: 150 / 150 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.096 seconds (Warm-up)
#> Chain 1:                0.174 seconds (Sampling)
#> Chain 1:                0.27 seconds (Total)
#> Chain 1:
#> Warning: The largest R-hat is 1.12, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Computing post-estimation metrics (including lvs if requested)...
#> Error in if (verbose) cat(" done.\n"): argument is of length zero

Created on 2024-06-09 with reprex v2.1.0

ecmerkle commented 3 months ago

Thanks, I think the remaining error is related to this recent lavaan commit. It looks like they moved verbose (and warn and debug) out of the @Options slot and now treat them as environment variables.

I just pushed a simple fix to blavaan and hope to get it on CRAN in the coming day. Though I worry this is just a patch over a larger issue with the verbose option. Will try to look more soon.

yrosseel commented 3 months ago

Oh dear. I am sorry for the trouble. Under the hood, many changes were made in lavaan 0.6-18, in particular wrt infrastructure and housekeeping.

It is somewhat unfortunate that we did not detect this. We did detect (and fix) many issues with other packages. The revdep checks (e.g., tools::check_packages_in_dir()) rely on the examples of the package, and (mostly testthat) tests, if available. But most examples in blavaan are enclosed in a \dontrun{} statement, and that is why we did not pick this up. Of course, I understand the reason for the \dontrun{} (sampling takes a long time), but to avoid this in the future: would it be possible to remove the `\dontrun{} in a few examples, and fit the model with a (very) small number of samples? Just to make sure it 'runs'?

ecmerkle commented 3 months ago

No problem! I have been seeing the changes to lavaan and expected that some changes to blavaan would be needed.

Yes, blavaan is now faster for many models than it was originally, so I will take a couple out of \dontrun. I also wonder what is the optimal thing for blavaan to do, for the new treatment of the verbose and warn and debug options. Right now, I just added verbose back to the Options slot to restore existing functionality.

ecmerkle commented 3 months ago

A new version is now on CRAN, which should fix the problems. (the submission process was unusually fast today)