Skip to content

Commit

Permalink
A few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krisrs1128 committed Sep 4, 2024
1 parent 57612f1 commit c59ea1b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/testthat/test-nullify.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,25 @@ test_that("Can nullify the mediator to outcome path.", {
expect_contains(edges$state, c("active", "inactive"))
expect_equal(as.integer(table(edges$state)), c(16, 1))
})


test_that("Can contrast data simulated from real and synthetic.", {
contrast_data <- fit |>
null_contrast(exper)
expect_named(
contrast_data,
c("source", "outcome", "indirect_setting", "contrast", "direct_effect")
)
expect_equal(unique(contrast_data$source), c("real", "synthetic"))
})

test_that("Can compute false discovery rates given contrast data.", {
fdr_data <- fit |>
null_contrast(exper) |>
fdr_summary("direct_effect")
expect_named(
fdr_data,
c("source", "outcome", "direct_effect", "rank", "fdr_hat", "keep")
)
expect_equal(fdr_data$source, c("real", "synthetic"))
})
40 changes: 40 additions & 0 deletions tests/testthat/test-treatment_profile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

exper <- demo_joy() |>
mediation_data("PHQ", "treatment", starts_with("ASV"))
fit <- multimedia(exper) |>
estimate(exper)

test_that("Can create a treatment profile from a data.frame.", {
t1 <- data.frame(treatment = factor(rep(c(0, 1), each = 5)))
profile <- setup_profile(fit, t_mediator = t1, t_outcome = t1)
expect_s4_class(profile, "treatment_profile")
expect_named(profile@t_mediator, paste0("ASV", 1:5))
expect_named(profile@t_outcome, "PHQ")
expect_equal(
unique(profile@t_mediator$ASV1$treatment),
as.factor(c("0", "1"))
)
expect_s3_class(profile@t_mediator$ASV1$treatment, "factor")
})

test_that("Can create treatment profile from a vector.", {
t1 <- rep(0:1, 5)
profile <- setup_profile(fit, t_mediator = t1, t_outcome = t1)
expect_s4_class(profile, "treatment_profile")
expect_named(profile@t_mediator, paste0("ASV", 1:5))
expect_named(profile@t_outcome, "PHQ")
expect_equal(
unique(profile@t_mediator$ASV1$treatment),
c(0, 1)
)
expect_type(profile@t_mediator$ASV1$treatment, "integer")
expect_equal(profile@t_mediator$ASV1$treatment, t1)
})

test_that("Can create treatment profile from scratch.", {
t1 <- list(m = data.frame(treatment = rep(0:1, 5)))
profile <- new("treatment_profile", t_mediator = t1, t_outcome = t1)
expect_s4_class(profile, "treatment_profile")
t2 <- list(t = data.frame(treatment = rep(0:1, 2)))
expect_error(new("treatment_profile", t_mediator = t1, t_outcome = t2))
})

0 comments on commit c59ea1b

Please sign in to comment.