tests/testthat/test_q_fct_mediation_sem_indicators_standardized.R

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

test_that("q function: mediation with indicators: measurement model: standardized", {

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 = "measurement_model",
          boot_ci = FALSE,
          R = 100,
          seed = 1234,
          parallel = FALSE,
          progress = !is_testing())

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

fit <- sem(
  mod,
  data = data_sem_rev,
  missing = "fiml"
)

est <- parameterEstimates(fit, standardized = TRUE)

est_chk <- out$lm_out_lav$m$coefs_lm
i <- (est$lhs == "m") & (est$op == "~")
est_i <- est[i, "est"]
names(est_i) <- est$rhs[i]
expect_equal(est_i,
             est_chk[names(est_i), "Estimate"],
             tolerance = 1e-4)

est_chk <- out$lm_out_lav$y$coefs_lm
i <- (est$lhs == "y") & (est$op == "~")
est_i <- est[i, "std.all"]
names(est_i) <- est$rhs[i]
expect_equal(est_i,
             est_chk[names(est_i), "betaS"],
             tolerance = 1e-4)

})

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.