Nothing
library(manymome)
library(testthat)
suppressMessages(library(lavaan))
test_that("q function: mediation with indicators: SAM", {
# ==== 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())
out_simple <- q_simple_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")),
data = data_sem_rev,
fit_method = "sem",
indicator_method = "sam",
boot_ci = FALSE,
# R = 100,
# seed = 1234,
parallel = FALSE,
progress = !is_testing())
out_parallel <- q_parallel_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")),
data = data_sem_rev,
fit_method = "sem",
indicator_method = "sam",
boot_ci = FALSE,
# R = 100,
# seed = 1234,
parallel = FALSE,
progress = !is_testing())
out_serial <- q_serial_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")),
data = data_sem_rev,
fit_method = "sem",
indicator_method = "sam",
boot_ci = FALSE,
# R = 100,
# seed = 1234,
parallel = FALSE,
progress = !is_testing())
out_user <- q_mediation(
x = "x10",
y = "y",
m = "m",
cov = c("c2", "x12"),
model = c("x10 -> m -> y",
"x10 -> y"),
indicators = list(y = c("x01", "-x02", "x03"),
m = c("x04", "x05", "x09"),
c2 = c("x11", "x13", "-x14")),
data = data_sem_rev,
fit_method = "sem",
indicator_method = "sam",
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 <- sam(
mod,
data = data_sem
)
ind <- indirect_effect(
x = "x10",
y = "y",
m = "m",
fit = fit)
expect_identical(coef(out$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_simple$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_parallel$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_serial$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_user$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
indstdxy <- indirect_effect(
x = "x10",
y = "y",
m = "m",
fit = fit,
standardized_x = TRUE,
standardized_y = TRUE)
expect_identical(coef(out$ind_out$stdxy),
coef(indstdxy),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_simple$ind_out$stdxy),
coef(indstdxy),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_parallel$ind_out$stdxy),
coef(indstdxy),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_serial$ind_out$stdxy),
coef(indstdxy),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(coef(out_user$ind_out$stdxy),
coef(indstdxy),
tolerance = 1e-5,
ignore_attr = TRUE)
# Reliability
fit_rel <- fit@internal$sam.mm.rel[[1]]
expect_equal(out_simple$reliability,
fit_rel[names(out_simple$reliability)],
tolerance = 1e-5)
expect_equal(out_simple$loadings[names(get_loadings(fit))],
get_loadings(fit),
tolerance = 1e-5)
# Print
expect_output(print(out),
"indicators")
})
test_that("q function: mediation with indicators: SAM: boot_ci", {
# ==== q function: mediation with indicators: SAM: boot_ci ====
skip_on_cran()
data_sem_rev <- data_sem
data_sem_rev$x02 <- -data_sem_rev$x02
data_sem_rev$x14 <- -data_sem_rev$x14
suppressWarnings(
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 = TRUE,
R = 5,
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
"
# Suppress the harmless warning that will
# appear in lavaan 0.7-1
suppressWarnings(
fit <- sam(
mod,
data = data_sem,
se = "bootstrap",
bootstrap.args = list(R = 5),
iseed = 1234
)
)
suppressWarnings(
ind <- indirect_effect(
x = "x10",
y = "y",
m = "m",
fit = fit,
boot_ci = TRUE)
)
expect_identical(coef(out$ind_out$ustd),
coef(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
expect_identical(confint(out$ind_out$ustd),
confint(ind),
tolerance = 1e-5,
ignore_attr = TRUE)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.