Skip to contents

Get casewise log-likelihood for complete data, useful for WAIC, LOOIC, ... Does not work meta-analysis models.

Usage

casewise_log_likelihood(
  object,
  include_residuals = FALSE,
  use_armadillo = TRUE
)

Arguments

object

(mbsem) An object of mbsem-class returned by minorbsem.

include_residuals

(LOGICAL) TRUE: Include minor factor residual covariances in model-implied covariance matrix; FALSE: Exclude them. If TRUE, different models fit to the data will hardly be distinguishable. See details below.

use_armadillo

(LOGICAL) TRUE: Use RccpArmadillo for log-likelihood computations. FALSE: Use base R implementation from bayesm package.

Value

matrix (posterior iterations BY sample size) containing log-likelihood

Details

If comparing two models fit using the same method in minorbsem, it is reasonable to exclude the residual covariance matrix that captures the influences of minor factors when computing the log-likelihood. If they are not excluded, the log-likelihood will be near identical for models with different structures. This is because the minor factor residual covariances capture the degree of misspecification with the hypothesized structure.

The option to set include_residuals = TRUE is included to allow (i) comparison of models fit with different minorbsem methods and; (ii) comparisons of models fit with minorbsem and models fit with other packages.

When the influence of minor factors is non-trivial, one can expect that models fit with minorbsem will have better fit to the data than models fit with other packages since minorbsem models simultaneously model the degree of model misspecification.

Examples

if (FALSE) {
# Comparing two models using LOOCV
fit_1 <- minorbsem("F1 =~ x1 + x2 + x3
                    F2 =~ x4 + x5 + x6
                    F3 =~ x7 + x8 + x9", HS)
# Compute case wise log-likelihood, exclude minor factor residuals
ll_mat_1 <- casewise_log_likelihood(fit_1, include_residuals = FALSE)
chain_id <- posterior::as_draws_df(fit_1@stan_fit)$.chain
fit_2 <- minorbsem("F1 =~ x1 + x2 + x3 + x9
                    F2 =~ x4 + x5 + x6
                    F3 =~ x7 + x8 + x9", HS)
# Compute case wise log-likelihood, exclude minor factor residuals
ll_mat_2 <- casewise_log_likelihood(fit_2, include_residuals = FALSE)

# LOO compuations
loo_1 <- loo::loo(
  ll_mat_1,
  r_eff = loo::relative_eff(ll_mat_1, chain_id = chain_id)
)
print(loo_1)
loo_2 <- loo::loo(
  ll_mat_2,
  r_eff = loo::relative_eff(ll_mat_2, chain_id = chain_id)
)
print(loo_2)

# Compare both models
print(loo::loo_compare(loo_1, loo_2), simplify = FALSE)
}