|
| 1 | +source(test_path("data-for-mcmc-tests.R")) |
| 2 | + |
| 3 | +# param_range -------------------------------------------------------------- |
| 4 | + |
| 5 | +test_that("param_range selects correct parameters by index", { |
| 6 | + all_pars <- c("alpha", "beta[1]", "beta[2]", "beta[3]", "sigma") |
| 7 | + result <- param_range("beta", c(1, 3), vars = all_pars) |
| 8 | + expect_equal(result, c(2L, 4L)) |
| 9 | +}) |
| 10 | + |
| 11 | +test_that("param_range returns empty integer when no matches", { |
| 12 | + all_pars <- c("alpha", "gamma[1]", "gamma[2]") |
| 13 | + result <- param_range("beta", 1:3, vars = all_pars) |
| 14 | + expect_identical(result, integer(0)) |
| 15 | +}) |
| 16 | + |
| 17 | +test_that("param_range handles single index", { |
| 18 | + all_pars <- c("alpha", "beta[1]", "beta[2]", "sigma") |
| 19 | + result <- param_range("beta", 2, vars = all_pars) |
| 20 | + expect_equal(result, 3L) |
| 21 | +}) |
| 22 | + |
| 23 | +test_that("param_range drops non-matching indices silently", { |
| 24 | + all_pars <- c("alpha", "beta[1]", "beta[3]") |
| 25 | + # beta[2] does not exist, should be silently dropped |
| 26 | + result <- param_range("beta", 1:3, vars = all_pars) |
| 27 | + expect_equal(result, c(2L, 3L)) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that("param_range errors when vars is not NULL or character", { |
| 31 | + expect_error( |
| 32 | + param_range("beta", 1:3, vars = list("a", "b")), |
| 33 | + "'vars' must be NULL or a character vector." |
| 34 | + ) |
| 35 | + expect_error( |
| 36 | + param_range("beta", 1:3, vars = 1:5), |
| 37 | + "'vars' must be NULL or a character vector." |
| 38 | + ) |
| 39 | +}) |
| 40 | + |
| 41 | +# param_glue --------------------------------------------------------------- |
| 42 | + |
| 43 | +test_that("param_glue selects correct parameters with one expression", { |
| 44 | + all_pars <- c("alpha[1]", "alpha[2]", "alpha[3]", "sigma") |
| 45 | + result <- param_glue("alpha[{i}]", i = c(1, 3), vars = all_pars) |
| 46 | + expect_equal(result, c(1L, 3L)) |
| 47 | +}) |
| 48 | + |
| 49 | +test_that("param_glue selects correct parameters with multiple expressions", { |
| 50 | + all_pars <- c( |
| 51 | + "b[X:1]", "b[X:2]", "b[Y:1]", "b[Y:2]", "sigma" |
| 52 | + ) |
| 53 | + result <- param_glue("b[{var}:{lev}]", var = c("X", "Y"), lev = c(1, 2), |
| 54 | + vars = all_pars) |
| 55 | + expect_equal(result, c(1L, 3L, 2L, 4L)) |
| 56 | +}) |
| 57 | + |
| 58 | +test_that("param_glue returns empty integer when no matches", { |
| 59 | + all_pars <- c("alpha", "sigma") |
| 60 | + result <- param_glue("beta[{i}]", i = 1:3, vars = all_pars) |
| 61 | + expect_identical(result, integer(0)) |
| 62 | +}) |
| 63 | + |
| 64 | +test_that("param_glue drops non-matching names silently", { |
| 65 | + all_pars <- c("b[X:1]", "b[Y:2]", "sigma") |
| 66 | + # b[X:2] and b[Y:1] don't exist |
| 67 | + result <- param_glue("b[{var}:{lev}]", var = c("X", "Y"), lev = c(1, 2), |
| 68 | + vars = all_pars) |
| 69 | + expect_equal(result, c(1L, 2L)) |
| 70 | +}) |
| 71 | + |
| 72 | +test_that("param_glue errors when vars is not NULL or character", { |
| 73 | + expect_error( |
| 74 | + param_glue("alpha[{i}]", i = 1:3, vars = list("a")), |
| 75 | + "'vars' must be NULL or a character vector." |
| 76 | + ) |
| 77 | + expect_error( |
| 78 | + param_glue("alpha[{i}]", i = 1:3, vars = 42), |
| 79 | + "'vars' must be NULL or a character vector." |
| 80 | + ) |
| 81 | +}) |
| 82 | + |
| 83 | +# tidyselect_parameters ---------------------------------------------------- |
| 84 | + |
| 85 | +test_that("tidyselect_parameters selects by name", { |
| 86 | + all_pars <- c("alpha", "beta[1]", "beta[2]", "sigma") |
| 87 | + selected <- tidyselect_parameters(all_pars, vars(alpha, sigma)) |
| 88 | + expect_equal(selected, c("alpha", "sigma")) |
| 89 | +}) |
| 90 | + |
| 91 | +test_that("tidyselect_parameters works with tidyselect helpers", { |
| 92 | + all_pars <- c("alpha", "beta[1]", "beta[2]", "sigma") |
| 93 | + selected <- tidyselect_parameters(all_pars, vars(starts_with("beta"))) |
| 94 | + expect_equal(selected, c("beta[1]", "beta[2]")) |
| 95 | +}) |
| 96 | + |
| 97 | +test_that("tidyselect_parameters works with negation", { |
| 98 | + all_pars <- c("alpha", "beta[1]", "beta[2]", "sigma") |
| 99 | + selected <- tidyselect_parameters(all_pars, vars(-alpha)) |
| 100 | + expect_equal(selected, c("beta[1]", "beta[2]", "sigma")) |
| 101 | +}) |
| 102 | + |
| 103 | +test_that("tidyselect_parameters errors when nothing matches", { |
| 104 | + all_pars <- c("alpha", "beta[1]", "sigma") |
| 105 | + expect_error( |
| 106 | + tidyselect_parameters(all_pars, vars(starts_with("zzz"))), |
| 107 | + "No parameters were found matching those names." |
| 108 | + ) |
| 109 | +}) |
| 110 | + |
| 111 | +test_that("tidyselect_parameters works with contains()", { |
| 112 | + all_pars <- c("b[(Intercept) X:1]", "b[(Intercept) X:2]", "sigma") |
| 113 | + selected <- tidyselect_parameters(all_pars, vars(contains("Intercept"))) |
| 114 | + expect_equal(selected, c("b[(Intercept) X:1]", "b[(Intercept) X:2]")) |
| 115 | +}) |
| 116 | + |
| 117 | +# Integration with vars() -------------------------------------------------- |
| 118 | + |
| 119 | +test_that("param_range works inside vars() via prepare_mcmc_array", { |
| 120 | + result <- prepare_mcmc_array(mat, pars = vars(param_range("beta", 1:2))) |
| 121 | + pars <- dimnames(result)[[3]] |
| 122 | + expect_equal(pars, c("beta[1]", "beta[2]")) |
| 123 | +}) |
| 124 | + |
| 125 | +test_that("param_glue works inside vars() via prepare_mcmc_array", { |
| 126 | + result <- prepare_mcmc_array( |
| 127 | + mat, |
| 128 | + pars = vars(param_glue("b[(Intercept) x:{i}]", i = c(1, 3))) |
| 129 | + ) |
| 130 | + pars <- dimnames(result)[[3]] |
| 131 | + expect_equal(pars, c("b[(Intercept) x:1]", "b[(Intercept) x:3]")) |
| 132 | +}) |
0 commit comments