easystats / bayestestR

:ghost: Utilities for analyzing Bayesian models and posterior distributions
https://easystats.github.io/bayestestR/
GNU General Public License v3.0
563 stars 55 forks source link

Issues with blavaan #627

Closed strengejacke closed 2 months ago

strengejacke commented 1 year ago

Hm, there were actually quite some bugs in package functions which were not captured by the CI tests? @mattansb Here's an example that seems to have worked in the past (at least, we had a test for it), but now no longer works:

library(bayestestR)
library(blavaan)
#> Loading required package: Rcpp
#> This is blavaan 0.5-1
#> 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
"

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

  # regressions
  dem65 ~ 0*dem60

  # residual correlations
  y1 ~~ 0*y5
"
suppressWarnings(capture.output({
  bfit <- blavaan::bsem(model,
    data = PoliticalDemocracy,
    n.chains = 1, burnin = 50, sample = 100
  )
  bfit2 <- blavaan::bsem(model2,
    data = PoliticalDemocracy,
    n.chains = 1, burnin = 50, sample = 100
  )
}))

bayesfactor_models(bfit, bfit2)
#> Warning: Bayes factors might not be precise.
#>   For precise Bayes factors, sampling at least 40,000 posterior samples is
#>   recommended.
#> Error in apply(diffs, 2, sum): dim(X) must have a positive length

Created on 2023-10-02 with reprex v2.0.2

Originally posted by @strengejacke in https://github.com/easystats/bayestestR/issues/625#issuecomment-1742759315

mattansb commented 11 months ago

This is actually an error in blavaan:

library(bayestestR)
library(blavaan)

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

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

  # regressions
  dem65 ~ dem60

  # residual correlations
  y1 ~~ y5
"

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

  # regressions
  dem65 ~ 0*dem60

  # residual correlations
  y1 ~~ 0*y5
"
suppressWarnings(capture.output({
  bfit <- blavaan::bsem(model,
                        data = PoliticalDemocracy,
                        n.chains = 1, burnin = 50, sample = 100
  )
  bfit2 <- blavaan::bsem(model2,
                         data = PoliticalDemocracy,
                         n.chains = 1, burnin = 50, sample = 100
  )
}))

blavaan::blavCompare(bfit, bfit2)
#> Error in apply(diffs, 2, sum) : dim(X) must have a positive length

Opened an issue over there (https://github.com/ecmerkle/blavaan/issues/68)

mattansb commented 11 months ago

This was recently discussed at the thread below, and it should be fixed on github.

So I think we can skip this test for now, but leave this issue open until that fix is on CRAN.

strengejacke commented 2 months ago

The main issue is fixed, but no BF is returned.

strengejacke commented 2 months ago

Sorry, wasn't meaning to close this.

mattansb commented 2 months ago

From Ed's response it seems that there are cases where BF cannot be computed and so NA is returned, and this is one of those cases. Should we give an error in such cases?


Also, I seem to be getting:

> insight::check_if_installed("blavaan")
Package `blavaan` is installed, but package version `0.55` is required.

Would you like to update it? [y/n] 

Which seems to be coming from here:

insight:::.get_dep_version(dep = "blavaan")
#> [1] "0.55"

Which as far as I can tell is happening because the "0.5-5" is being parsed as "0.55"?

strengejacke commented 2 months ago

The latter issue is fixed here: https://github.com/easystats/insight/commit/144cdc15a49a102088c744dfd56cf47edd196de9

Agree on your suggestion that we should warn/error if BF is not possible to compute.

mattansb commented 2 months ago

Thanks for taking care of this @strengejacke !

DominiqueMakowski commented 2 months ago

Can I submit?

strengejacke commented 2 months ago

👍