tests/testthat/test-conj_test.R

# Testing script the Conjunctive IU Test functions
# `calc_pwr_conj_test()`  - calculates power
# `calc_K_conj_test()`    - calculates # of clusters per treatment group
# `calc_m_conj_test()`    - calculates cluster size

# `calc_pwr_conj_test()` ----------------------------------------------------

# Test for Conjunctive IU test power for t distribution
test_that("Conjunctive IU test power works (t dist)", {
  result <- calc_pwr_conj_test(dist = "T",
                               K = 15,
                               m = 300,
                               alpha = 0.05,
                               beta1 = 0.1,
                               beta2 = 0.1,
                               varY1 = 0.23,
                               varY2 = 0.25,
                               rho01 = 0.025,
                               rho02 = 0.025,
                               rho1 = 0.01,
                               rho2 = 0.05,
                               r = 1)
  expect_equal(round(result, 3), 0.899, tolerance = 0.02,
              info = paste(result))
})

# Test for Conjunctive IU test power for MVN distribution
test_that("Conjunctive IU test power works (MVN dist)", {
  result <- calc_pwr_conj_test(dist = "MVN",
                               K = 15,
                               m = 300,
                               alpha = 0.05,
                               beta1 = 0.1,
                               beta2 = 0.1,
                               varY1 = 0.23,
                               varY2 = 0.25,
                               rho01 = 0.025,
                               rho02 = 0.025,
                               rho1 = 0.01,
                               rho2 = 0.05,
                               r = 1)
  expect_equal(round(result, 4), 0.9143, tolerance = 0.02,
              info = paste(result))
})

# `calc_K_conj_test()` ------------------------------------------------------

# Test for Conjunctive IU test treatment group K for t distribution
test_that("Conjunctive IU test treatment group 'K' works (t dist)", {
  expect_equal(calc_K_conj_test(dist = "T",
                                power = 0.8,
                                m = 300,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1)$`Treatment (K)`,
               12, tolerance = 1)
})

# Test for Conjunctive IU test control group K for t distribution
test_that("Conjunctive IU test control group 'K' works (t dist)", {
  expect_equal(calc_K_conj_test(dist = "T",
                                power = 0.8,
                                m = 300,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1)$`Control (K)`,
               12, tolerance = 1)
})

# Test for Conjunctive IU test treatment group K for MVN distribution
test_that("Conjunctive IU test treatment group 'K' works (MVN dist)", {
  expect_equal(calc_K_conj_test(dist = "MVN",
                                power = 0.8,
                                m = 300,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1)$`Treatment (K)`,
               11, tolerance = 1)
})

# Test for Conjunctive IU test control group K for MVN distribution
test_that("Conjunctive IU test control group 'K' works (MVN dist)", {
  expect_equal(calc_K_conj_test(dist = "MVN",
                                power = 0.8,
                                m = 300,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1)$`Control (K)`,
               11, tolerance = 1)
})

# `calc_m_conj_test()` ------------------------------------------------------

# Test for Conjunctive IU test m for t distribution
test_that("Conjunctive IU test 'm' works (t dist)", {
  expect_equal(calc_m_conj_test(dist = "T",
                                power = 0.8,
                                K = 15,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1),
               86, tolerance = 1)
})

# Test for Conjunctive IU test m for MVN distribution
test_that("Conjunctive IU test 'm' works (MVN dist)", {
  expect_equal(calc_m_conj_test(dist = "MVN",
                                power = 0.8,
                                K = 15,
                                alpha = 0.05,
                                beta1 = 0.1,
                                beta2 = 0.1,
                                varY1 = 0.23,
                                varY2 = 0.25,
                                rho01 = 0.025,
                                rho02 = 0.025,
                                rho1 = 0.01,
                                rho2 = 0.05,
                                r = 1),
               74, tolerance = 1)
})


# Testing that all 3 functions align -------------------------------------------

# K aligns with power for t distribution
test_that("Conjunctive IU test K calculation aligns with power (t dist)", {
  result <- calc_K_conj_test(dist = "T",
                             power = calc_pwr_conj_test(dist = "T",
                                                        K = 15,
                                                        m = 300,
                                                        alpha = 0.05,
                                                        beta1 = 0.1,
                                                        beta2 = 0.1,
                                                        varY1 = 0.23,
                                                        varY2 = 0.25,
                                                        rho01 = 0.025,
                                                        rho02 = 0.025,
                                                        rho1 = 0.01,
                                                        rho2 = 0.05,
                                                        r = 1),
                             m = 300,
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)[[1]]
  expect_equal(result, 15, tolerance = 2,
              info = "The result should be 15, but with rounding between 15 and 16")
})

# K aligns with m for t distribution
test_that("Conjunctive IU test K calculation aligns with power (t dist)", {
  result <- calc_K_conj_test(dist = "T",
                             power = 0.8,
                             m = calc_m_conj_test(dist = "T",
                                                  power = 0.8,
                                                  K = 15,
                                                  alpha = 0.05,
                                                  beta1 = 0.1,
                                                  beta2 = 0.1,
                                                  varY1 = 0.23,
                                                  varY2 = 0.25,
                                                  rho01 = 0.025,
                                                  rho02 = 0.025,
                                                  rho1 = 0.01,
                                                  rho2 = 0.05,
                                                  r = 1),
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)$`Treatment (K)`
  expect_equal(result, 15, tolerance = 2,
              info = "The result should be 15, but with rounding between 15 and 16")
})

# Power aligns with K for t distribution
test_that("Conjunctive IU test K calculation aligns with power (t dist)", {
  result <- calc_m_conj_test(dist = "T",
                             power = calc_pwr_conj_test(dist = "T",
                                                        m = 300,
                                                        K = 15,
                                                        alpha = 0.05,
                                                        beta1 = 0.1,
                                                        beta2 = 0.1,
                                                        varY1 = 0.23,
                                                        varY2 = 0.25,
                                                        rho01 = 0.025,
                                                        rho02 = 0.025,
                                                        rho1 = 0.01,
                                                        rho2 = 0.05,
                                                        r = 1),
                             K = 15,
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)
  expect_equal(result, 300, tolerance = 5,
              info = "The result should be 300, but with rounding between 298 and 303")
})








# K aligns with power for MVN distribution
test_that("Conjunctive IU test K calculation aligns with power (MVN dist)", {
  result <- calc_K_conj_test(dist = "MVN",
                             power = calc_pwr_conj_test(dist = "MVN",
                                                        K = 15,
                                                        m = 300,
                                                        alpha = 0.05,
                                                        beta1 = 0.1,
                                                        beta2 = 0.1,
                                                        varY1 = 0.23,
                                                        varY2 = 0.25,
                                                        rho01 = 0.025,
                                                        rho02 = 0.025,
                                                        rho1 = 0.01,
                                                        rho2 = 0.05,
                                                        r = 1),
                             m = 300,
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)[[1]]
  expect_equal(result, 15, tolerance = 2,
              info = "The result should be 15, but with rounding between 14 and 16")
})

# K aligns with m for MVN distribution
test_that("Conjunctive IU test K calculation aligns with power (MVN dist)", {
  result <- calc_K_conj_test(dist = "MVN",
                             power = 0.8,
                             m = calc_m_conj_test(dist = "MVN",
                                                  power = 0.8,
                                                  K = 15,
                                                  alpha = 0.05,
                                                  beta1 = 0.1,
                                                  beta2 = 0.1,
                                                  varY1 = 0.23,
                                                  varY2 = 0.25,
                                                  rho01 = 0.025,
                                                  rho02 = 0.025,
                                                  rho1 = 0.01,
                                                  rho2 = 0.05,
                                                  r = 1),
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)$`Treatment (K)`
  expect_equal(result, 15, tolerance = 2,
              info = "The result should be 15, but with rounding between 15 and 16")
})

# Power aligns with K for t distribution
test_that("Conjunctive IU test MVN calculation aligns with power (MVN dist)", {
  result <- calc_m_conj_test(dist = "MVN",
                             power = calc_pwr_conj_test(dist = "MVN",
                                                        m = 300,
                                                        K = 15,
                                                        alpha = 0.05,
                                                        beta1 = 0.1,
                                                        beta2 = 0.1,
                                                        varY1 = 0.23,
                                                        varY2 = 0.25,
                                                        rho01 = 0.025,
                                                        rho02 = 0.025,
                                                        rho1 = 0.01,
                                                        rho2 = 0.05,
                                                        r = 1),
                             K = 15,
                             alpha = 0.05,
                             beta1 = 0.1,
                             beta2 = 0.1,
                             varY1 = 0.23,
                             varY2 = 0.25,
                             rho01 = 0.025,
                             rho02 = 0.025,
                             rho1 = 0.01,
                             rho2 = 0.05,
                             r = 1)
  expect_equal(result, 300, tolerance = 5,
              info = "The result should be 300, but with rounding between 298 and 303")
})




# Check that it aligns with the source code ------------------------------------

# test_that("Conjunctive IU power calculation aligns with GitHub source code", {
#
#   devtools::source_url("https://github.com/siyunyang/coprimary_CRT/blob/main/powerSampleCal_varCluster_ttest.R?raw=TRUE")
#
#   result1 <- round(calc_pwr_conj_test(m = 300,
#                                 K = 15,
#                                 alpha = 0.05,
#                                 beta1 = 0.1,
#                                 beta2 = 0.1,
#                                 varY1 = 0.23,
#                                 varY2 = 0.25,
#                                 rho01 = 0.025,
#                                 rho02 = 0.025,
#                                 rho1 = 0.01,
#                                 rho2 = 0.05,
#                                 r = 1), 3)
#
#   result2 <- round(calPower_ttestIU(betas = c(0.1, 0.1),
#                               deltas = c(0, 0),
#                               vars = c(0.23, 0.25),
#                               rho01 = matrix(c(0.025, 0.01,
#                                                0.01, 0.025),
#                                              2, 2),
#                               rho2 = matrix(c(1, 0.05,
#                                               0.05, 1),
#                                             2, 2),
#                               N = 30,
#                               r = .5,
#                               m = 300,
#                               K = 2,
#                               alpha = 0.05), 3)
#
#   expect_true(result1 == result2,
#               info = paste0("Does not align with the original source code (", result1, " vs. ", result2, ")"))
# })

Try the crt2power package in your browser

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

crt2power documentation built on June 8, 2025, 10:16 a.m.