tests/testthat/test-x_from_power_for_es_short.R

skip_on_cran()

library(testthat)

test_that("x_from_power: es", {

# Case 1

mod <-
"
m ~ x
y ~ m + x
"

mod_es <-
"
m ~ x: s
y ~ m: m
y ~ x: s
"

out <- power4test(nrep = 50,
                  model = mod,
                  pop_es = mod_es,
                  n = 100,
                  fit_model_args = list(fit_function = "lm"),
                  test_fun = test_parameters,
                  test_args = list(pars = "y~m"),
                  iseed = 1234,
                  parallel = FALSE,
                  progress = FALSE)
out_power <- rejection_rates(out)
out_power

expect_no_error(tmp <- x_from_power(out,
                    x = "es",
                    pop_es_name = "y ~ m",
                    target_power = .80,
                    final_nrep = 60,
                    max_trials = 2,
                    seed = 1234,
                    progress = TRUE,
                    simulation_progress = FALSE,
                    algorithm = "power_curve"))
expect_no_error(print(summary(tmp)))
expect_true((tmp$power_final > .70) &&
            (tmp$power_final < .90))

tmp2 <- x_from_power(tmp,
                    x = "es",
                    pop_es_name = "y ~ m",
                    target_power = .80,
                    final_nrep = 60,
                    max_trials = 2,
                    seed = 1234,
                    progress = TRUE,
                    simulation_progress = FALSE,
                    algorithm = "power_curve")
expect_identical(tmp2,
                 tmp)

tmp3 <- x_from_power(tmp$power4test_trials,
                    x = "es",
                    pop_es_name = "y ~ m",
                    target_power = .80,
                    final_nrep = 60,
                    max_trials = 2,
                    seed = 1234,
                    progress = TRUE,
                    simulation_progress = FALSE,
                    algorithm = "power_curve")
expect_identical(tmp3$x_tried,
                 tmp$x_tried)
expect_identical(tmp3$x_final,
                 tmp$x_final)

out <- power4test(nrep = 10,
                  model = mod,
                  pop_es = mod_es,
                  n = 100,
                  fit_model_args = list(fit_function = "lm"),
                  test_fun = test_parameters,
                  test_args = list(pars = "y~m"),
                  iseed = 1234,
                  parallel = FALSE,
                  progress = FALSE)
out_power <- rejection_rates(out)
out_power

expect_no_error(tmp <- x_from_power(out,
                    x = "es",
                    pop_es_name = "y ~ m",
                    target_power = .80,
                    final_nrep = 60,
                    max_trials = 2,
                    seed = 1234,
                    progress = TRUE,
                    simulation_progress = FALSE,
                    algorithm = "power_curve"))
expect_no_error(print(summary(tmp)))
expect_true((tmp$power_final > .60) &&
            (tmp$power_final < .90))
})

Try the power4mome package in your browser

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

power4mome documentation built on Sept. 9, 2025, 5:35 p.m.