tests/testthat/test_sam_q_mediation_std.R

library(manymome)
library(testthat)
suppressMessages(library(lavaan))

test_that("q function: mediation with indicators: SAM: Standardized", {

# ==== q function: mediation with indicators: SAM ====

data_sem_rev <- data_sem
data_sem_rev$x02 <- -data_sem_rev$x02
data_sem_rev$x14 <- -data_sem_rev$x14

out <- q_mediation(
          x = "x10",
          y = "y",
          m = "m",
          cov = c("c2", "x12"),
          indicators = list(y = c("x01", "-x02", "x03"),
                            m = c("x04", "x05", "x09"),
                            c2 = c("x11", "x13", "-x14")),
          model = "simple",
          data = data_sem_rev,
          fit_method = "sem",
          indicator_method = "sam",
          boot_ci = FALSE,
          R = 100,
          seed = 1234,
          parallel = FALSE,
          progress = !is_testing())

mod <-
"
m ~ a*x10 + c2 + x12
y ~ b*m + x10 + c2 + x12
m =~ x04 + x05 + x09
c2 =~ x11 + x13 + x14
y =~ x01 + x02 + x03
ab := a*b
"

fit <- sam(
  mod,
  data = data_sem_rev
)

ind <- indirect_effect(
  x = "x10",
  y = "y",
  m = "m",
  fit = fit)

indstdxy <- indirect_effect(
  x = "x10",
  y = "y",
  m = "m",
  fit = fit,
  standardized_x = TRUE,
  standardized_y = TRUE)

est <- parameterEstimates(fit, standardized = TRUE)
ind_std_est <- est[est$label == "ab", "std.all"]

expect_identical(coef(out$ind_out$stdxy),
                 coef(indstdxy),
                 tolerance = 1e-5,
                 ignore_attr = TRUE)

expect_identical(coef(out$ind_out$stdxy),
                 ind_std_est,
                 tolerance = 1e-5,
                 ignore_attr = TRUE)

})

Try the manymome package in your browser

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

manymome documentation built on June 8, 2026, 9:06 a.m.