From c59ea1b951e236286ca6668cce431340ed4b60a0 Mon Sep 17 00:00:00 2001 From: Kris Sankaran Date: Tue, 3 Sep 2024 20:07:55 -0500 Subject: [PATCH] A few more tests --- tests/testthat/test-nullify.R | 22 ++++++++++++++ tests/testthat/test-treatment_profile.R | 40 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/testthat/test-treatment_profile.R diff --git a/tests/testthat/test-nullify.R b/tests/testthat/test-nullify.R index c7fd3cb..2844ad3 100644 --- a/tests/testthat/test-nullify.R +++ b/tests/testthat/test-nullify.R @@ -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")) +}) \ No newline at end of file diff --git a/tests/testthat/test-treatment_profile.R b/tests/testthat/test-treatment_profile.R new file mode 100644 index 0000000..777cba2 --- /dev/null +++ b/tests/testthat/test-treatment_profile.R @@ -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)) +}) \ No newline at end of file