tests/testthat/test-mediation.R

test_that("Method: skewnorm", {
  set.seed(1234)
  X <- rnorm(100)
  M <- 0.5 * X + rnorm(100)
  Y <- 0.7 * M + rnorm(100)

  dat <- data.frame(X = X, Y = Y, M = M)
  mod <- "
    # Direct effect
    Y ~ c*X

    # Mediators
    M ~ a*X
    Y ~ b*M

    # Indirect effect (a*b)
    ab := a*b

    # Total effect
    total := c + (a*b)
  "

  fit_lav <- lavaan::sem(mod, dat)
  expect_no_error({
    fit <- asem(
      mod,
      dat,
      verbose = FALSE
    )
  })
  expect_no_error(out <- capture.output(summary(fit)))

  expect_s4_class(fit, "INLAvaan")
  expect_equal(coef(fit), coef(fit_lav), tolerance = 0.1)
  # Convergence (dx ~ 0) depends on the optimiser path, which varies with the
  # platform's BLAS/compiler -- too fragile to assert on CRAN's check farm.
  skip_on_cran()
  expect_equal(fit@optim$dx, rep(0, length(coef(fit))), tolerance = 1e-3)
})

Try the INLAvaan package in your browser

Any scripts or data that you put into this service are public.

INLAvaan documentation built on Oct. 2, 2026, 1:07 a.m.