Compare Bayesian Models Fitted with INLAvaan
Details
The function computes the log Bayes Factor (logBF) relative to the best fitting model (the one with the highest Marginal Log-Likelihood).
The output table sorts models by descending Marginal Log-Likelihood.
Marg.Loglik: The approximated marginal log-likelihood.
DIC: Deviance Information Criterion (if available).
pD: Effective number of parameters (if available).
logBF: The natural logarithm of the Bayes Factor relative to the best model.
Examples
# \donttest{
# Model comparison on multigroup analysis (measurement invariance)
HS.model <- "
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
"
utils::data("HolzingerSwineford1939", package = "lavaan")
# Configural invariance
fit1 <- acfa(HS.model, data = HolzingerSwineford1939, group = "school")
#> ℹ Finding posterior mode.
#> ✔ Finding posterior mode. [236ms]
#>
#> ℹ Computing the Hessian.
#> ✔ Computing the Hessian. [703ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.024σ. [320ms]
#>
#> ⠙ Fitting skew normal to 0/60 marginals.
#> ⠹ Fitting skew normal to 6/60 marginals.
#> ⠸ Fitting skew normal to 31/60 marginals.
#> ⠼ Fitting skew normal to 57/60 marginals.
#> ✔ Fitting skew normal to 60/60 marginals. [7s]
#>
#> ℹ Sampling covariances and defined parameters.
#> ✔ Sampling covariances and defined parameters. [201ms]
#>
#> ⠙ Computing ppp and DIC.
#> ✔ Computing ppp and DIC. [785ms]
#>
# Weak invariance
fit2 <- acfa(
HS.model,
data = HolzingerSwineford1939,
group = "school",
group.equal = "loadings"
)
#> ℹ Finding posterior mode.
#> ✔ Finding posterior mode. [206ms]
#>
#> ℹ Computing the Hessian.
#> ✔ Computing the Hessian. [653ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.014σ. [352ms]
#>
#> ⠙ Fitting skew normal to 0/54 marginals.
#> ⠹ Fitting skew normal to 3/54 marginals.
#> ⠸ Fitting skew normal to 31/54 marginals.
#> ✔ Fitting skew normal to 54/54 marginals. [6s]
#>
#> ℹ Sampling covariances and defined parameters.
#> ✔ Sampling covariances and defined parameters. [192ms]
#>
#> ⠙ Computing ppp and DIC.
#> ⠹ Computing ppp and DIC.
#> ✔ Computing ppp and DIC. [745ms]
#>
# Strong invariance
fit3 <- acfa(
HS.model,
data = HolzingerSwineford1939,
group = "school",
group.equal = c("intercepts", "loadings")
)
#> ℹ Finding posterior mode.
#> ✔ Finding posterior mode. [186ms]
#>
#> ℹ Computing the Hessian.
#> ✔ Computing the Hessian. [575ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.015σ. [255ms]
#>
#> ⠙ Fitting skew normal to 0/48 marginals.
#> ⠹ Fitting skew normal to 15/48 marginals.
#> ⠸ Fitting skew normal to 47/48 marginals.
#> ✔ Fitting skew normal to 48/48 marginals. [4.6s]
#>
#> ℹ Sampling covariances and defined parameters.
#> ✔ Sampling covariances and defined parameters. [196ms]
#>
#> ⠙ Computing ppp and DIC.
#> ✔ Computing ppp and DIC. [740ms]
#>
# Compare models
compare(fit1, fit2, fit3)
#> Bayesian Model Comparison (INLAvaan)
#> Models ordered by marginal log-likelihood
#>
#> Model No.params Marg.Loglik DIC pD logBF
#> fit3 48 -3914.197 7661.318 123.9249 0.000
#> fit2 54 -3934.800 7594.939 110.7050 -20.603
#> fit1 60 -3958.118 7721.328 178.0657 -43.921
# }
