1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
library(bayesplot)
context("MCMC: scatter, hex, and parallel coordinates plots")
source(test_path("data-for-mcmc-tests.R"))
if (requireNamespace("rstanarm", quietly = TRUE)) {
suppressPackageStartupMessages(library(rstanarm))
# also fit an rstanarm model to use with mcmc_pairs
fit <- stan_glm(mpg ~ wt + am, data = mtcars, iter = 1000, chains = 2, refresh = 0)
post <- as.array(fit)
lp <- log_posterior(fit)
np <- ensure_divergences(nuts_params(fit))
}
# mcmc_scatter/hex --------------------------------------------------------
test_that("mcmc_scatter returns a ggplot object", {
expect_gg(mcmc_scatter(arr, pars = c("beta[1]", "beta[2]")))
expect_gg(mcmc_scatter(arr1chain, regex_pars = "beta", size = 3, alpha = 0.5))
expect_gg(mcmc_scatter(drawsarr, pars = c("theta[1]", "theta[2]")))
expect_gg(mcmc_scatter(mat, pars = c("sigma", "(Intercept)")))
expect_gg(mcmc_scatter(dframe, regex_pars = "x:[2,4]"))
expect_gg(mcmc_scatter(dframe_multiple_chains,
pars = c("sigma", "(Intercept)")))
})
test_that("mcmc_scatter throws error if number of parameters is not 2", {
expect_error(mcmc_scatter(arr, pars = c("sigma", "beta[1]", "beta[2]")), "exactly 2 parameters")
expect_error(mcmc_scatter(arr, pars = "sigma"), "exactly 2 parameters")
expect_error(mcmc_scatter(drawsarr, pars = "mu"), "exactly 2 parameters")
expect_error(mcmc_scatter(arr1), "exactly 2 parameters")
expect_error(mcmc_scatter(drawsarr1), "exactly 2 parameters")
expect_error(mcmc_scatter(mat1), "exactly 2 parameters")
})
test_that("mcmc_scatter accepts NUTS info", {
skip_if_not_installed("rstanarm")
expect_gg(mcmc_scatter(post, pars = c("wt", "sigma"), np = np))
div_style <- scatter_style_np(div_color = "orange", div_size = 2,
div_shape = 3, div_alpha = 0.5)
g <- mcmc_scatter(post, pars = c("wt", "sigma"), np = np, np_style = div_style)
expect_gg(g)
expect_named(g$data, c("x", "y", "Divergent"))
})
test_that("mcmc_hex returns a ggplot object", {
skip_if_not_installed("hexbin")
expect_gg(mcmc_hex(arr, pars = c("beta[1]", "beta[2]")))
expect_gg(mcmc_hex(arr1chain, regex_pars = "beta", binwidth = c(.5,.5)))
expect_gg(mcmc_hex(drawsarr, pars = c("theta[1]", "theta[2]")))
})
test_that("mcmc_hex throws error if number of parameters is not 2", {
skip_if_not_installed("hexbin")
expect_error(mcmc_hex(arr, pars = c("sigma", "beta[1]", "beta[2]")), "exactly 2 parameters")
expect_error(mcmc_hex(arr, pars = "sigma"), "exactly 2 parameters")
expect_error(mcmc_hex(drawsarr, pars = "mu"), "exactly 2 parameters")
expect_error(mcmc_hex(arr1), "exactly 2 parameters")
expect_error(mcmc_hex(mat1), "exactly 2 parameters")
})
# mcmc_pairs -------------------------------------------------------------
test_that("mcmc_pairs returns a bayesplot_grid object", {
g <- mcmc_pairs(arr, pars = c("(Intercept)", "sigma"))
expect_bayesplot_grid(g)
expect_equal(print(g), plot(g))
expect_bayesplot_grid(mcmc_pairs(arr, pars = "sigma", regex_pars = "beta"))
expect_bayesplot_grid(mcmc_pairs(drawsarr, pars = "mu", regex_pars = "theta"))
expect_bayesplot_grid(suppressWarnings(mcmc_pairs(arr1chain, regex_pars = "beta")))
expect_bayesplot_grid(suppressWarnings(mcmc_pairs(drawsarr1chain, regex_pars = "theta")))
expect_bayesplot_grid(suppressWarnings(mcmc_pairs(mat, pars = c("(Intercept)", "sigma"))))
expect_bayesplot_grid(suppressWarnings(mcmc_pairs(dframe, pars = c("(Intercept)", "sigma"))))
expect_bayesplot_grid(mcmc_pairs(dframe_multiple_chains, regex_pars = "beta"))
})
test_that("mcmc_pairs using hexbin works", {
skip_if_not_installed("hexbin")
expect_bayesplot_grid(mcmc_pairs(arr, regex_pars = "x:[1-3]",
transformations = "exp",
diag_fun = "dens", off_diag_fun = "hex",
diag_args = list(trim = FALSE),
off_diag_args = list(binwidth = c(0.5, 0.5))))
})
test_that("no mcmc_pairs non-NUTS 'condition's fail", {
expect_bayesplot_grid(
mcmc_pairs(arr, pars = "sigma", regex_pars = "beta",
condition = pairs_condition(chains = list(1, 2:4)))
)
expect_bayesplot_grid(
mcmc_pairs(arr, pars = "sigma", regex_pars = "beta",
condition = pairs_condition(draws = rep(c(T,F), length.out = prod(dim(arr)[1:2]))))
)
expect_bayesplot_grid(
mcmc_pairs(arr, pars = "sigma", regex_pars = "beta",
condition = pairs_condition(draws = 1/3))
)
expect_bayesplot_grid(
mcmc_pairs(arr, pars = "sigma", regex_pars = "beta",
condition = pairs_condition(chains = c(1,3)))
)
})
test_that("mcmc_pairs works with NUTS info", {
skip_if_not_installed("rstanarm")
expect_bayesplot_grid(mcmc_pairs(post, pars = c("wt", "am", "sigma"), np = np))
expect_bayesplot_grid(mcmc_pairs(post, pars = c("wt", "am"),
condition = pairs_condition(nuts="energy__"), np = np))
expect_bayesplot_grid(mcmc_pairs(post, pars = c("wt", "am"),
condition = pairs_condition(nuts="divergent__"), np = np))
expect_bayesplot_grid(mcmc_pairs(post, pars = c("wt", "am"),
condition = pairs_condition(nuts = "lp__"), lp=lp, np = np,
max_treedepth = 2))
p <- mcmc_pairs(
post,
pars = c("wt", "am"),
off_diag_fun = "scatter",
condition = pairs_condition(nuts = "lp__"),
lp = lp,
np = np,
np_style = pairs_style_np(div_color = "firebrick", td_color = "dodgerblue", div_size = 2, td_size = 2),
max_treedepth = with(np, max(Value[Parameter == "treedepth__"]) - 1)
)
expect_bayesplot_grid(p)
})
test_that("mcmc_pairs throws correct warnings and errors", {
skip_if_not_installed("rstanarm")
expect_warning(mcmc_pairs(arr1chain, regex_pars = "beta"),
"This plot is more useful with multiple chains")
expect_error(mcmc_pairs(arr, pars = "sigma"),
"requires at least two parameters")
expect_error(
mcmc_pairs(arr, condition = pairs_condition(draws = c(T, F))),
"length(condition) == (n_iter * n_chain) is not TRUE",
fixed = TRUE
)
expect_error(
mcmc_pairs(arr, condition = pairs_condition(nuts = "accept_stat__")),
"the 'np' argument to 'mcmc_pairs' must also be specified"
)
expect_error(
mcmc_pairs(arr, condition = pairs_condition(nuts = "lp__")),
"the 'lp' argument to 'mcmc_pairs' must also be specified"
)
expect_error(
mcmc_pairs(arr, condition = "lp__"),
'inherits(condition, "pairs_condition") is not TRUE',
fixed = TRUE
)
expect_error(
mcmc_pairs(post, pars = c("wt", "am"), max_treedepth = 2, np = np,
np_style = list(color = "green")),
'inherits(np_style, "nuts_style") is not TRUE',
fixed = TRUE
)
post2 <- post
post2[,1:2,"wt"] <- 0
expect_warning(
mcmc_pairs(post2, pars = c("wt", "am", "sigma")),
"parameters were dropped because they are constant: wt"
)
post[,, "sigma"] <- post[,, "am"]
expect_warning(
mcmc_pairs(post, pars = c("wt", "sigma", "am")),
"parameters were dropped because they are duplicative: am"
)
})
# pairs_style_np -------------------------------------------------------
test_that("pairs_style_np returns correct structure", {
style <- pairs_style_np(div_size = 3, td_color = "gray", td_shape = 1)
expect_s3_class(style, "nuts_style")
expect_named(style, c("color", "shape", "size", "alpha"), ignore.order = TRUE)
expect_named(style$color, c("div", "td"))
expect_named(style$size, c("div", "td"))
expect_named(style$shape, c("div", "td"))
expect_named(style$alpha, c("div", "td"))
})
test_that("pairs_style_np throws correct errors", {
expect_error(
pairs_style_np(div_size = "3"),
"is.numeric(div_size) is not TRUE",
fixed = TRUE
)
expect_error(
pairs_style_np(td_color = 1),
"is.character(td_color) is not TRUE",
fixed = TRUE
)
})
# pairs_condition ---------------------------------------------------------
test_that("pairs_condition returns correct structure", {
# default
cond0 <- pairs_condition()
expect_s3_class(cond0, "pairs_condition")
expect_equivalent(unclass(cond0), list())
expect_equal(attr(cond0, "type"), "default")
# chains
cond1 <- pairs_condition(chains = 1:4)
expect_s3_class(cond1, "integer")
expect_s3_class(cond1, "pairs_condition")
expect_equivalent(unclass(cond1), 1:4)
expect_equal(attr(cond1, "type"), "chain_vector")
cond2 <- pairs_condition(chains = list(1:4, 5:6))
expect_s3_class(cond2, "list")
expect_s3_class(cond2, "pairs_condition")
expect_equivalent(unclass(cond2), list(upper=1:4, lower=5:6))
expect_equal(attr(cond2, "type"), "chain_list")
# draws
cond3 <- pairs_condition(draws = 0.7)
expect_s3_class(cond3, "numeric")
expect_s3_class(cond3, "pairs_condition")
expect_equivalent(unclass(cond3), 0.7)
expect_equal(attr(cond3, "type"), "draws_proportion")
cond4 <- pairs_condition(draws = c(T, F, T))
expect_s3_class(cond4, "logical")
expect_s3_class(cond4, "pairs_condition")
expect_equivalent(unclass(cond4), c(T, F, T))
expect_equal(attr(cond4, "type"), "draws_selection")
# nuts
cond5 <- pairs_condition(nuts = "lp__")
expect_s3_class(cond5, "character")
expect_s3_class(cond5, "pairs_condition")
expect_equivalent(unclass(cond5), "lp__")
expect_equal(attr(cond5, "type"), "nuts")
})
test_that("pairs_condition throws correct errors", {
# chain
expect_error(
pairs_condition(chains = "abc"),
"must be an integer vector or a list of two integer vectors"
)
expect_error(
pairs_condition(chains = list(1:2, 3:4, 5:6)),
"length(chains) == 2 is not TRUE",
fixed = TRUE
)
expect_error(
pairs_condition(chains = list(1:2, 2:3)),
"Each chain can only be specified once"
)
expect_error(
pairs_condition(chains = c(1:3, 2)),
"Each chain can only be specified once"
)
# draws
expect_error(
pairs_condition(draws = "abc"),
"must be a single proportion or a logical vector"
)
expect_error(
pairs_condition(draws = 2),
"draws > 0 && draws < 1 is not TRUE",
fixed = TRUE
)
# nuts
expect_error(
pairs_condition(nuts = 2),
"must be a single string"
)
expect_error(
pairs_condition(nuts = c("lp__", "energy__")),
"must be a single string"
)
expect_error(
pairs_condition(nuts = "step_size__"),
"stepsize__"
)
})
test_that("pairs_condition message if multiple args specified", {
options(useFancyQuotes = FALSE)
expect_message(
pairs_condition(chains = 2, draws = 0.5, nuts = "lp__"),
"because they are superseded by 'chains': 'draws', 'nuts'",
fixed = TRUE
)
expect_message(
pairs_condition(chains = 2, nuts = "lp__"),
"because they are superseded by 'chains': 'nuts'",
fixed = TRUE
)
expect_message(
pairs_condition(draws = 0.5, nuts = "lp__"),
"because they are superseded by 'draws': 'nuts'",
fixed = TRUE
)
})
# mcmc_parcoord -----------------------------------------------------------
test_that("mcmc_parcoord returns a ggplot object", {
expect_gg(mcmc_parcoord(arr, pars = c("(Intercept)", "sigma")))
expect_gg(mcmc_parcoord(arr, pars = "sigma", regex_pars = "beta"))
})
test_that("mcmc_parcoord with nuts info returns a ggplot object", {
skip_if_not_installed("rstanarm")
expect_gg(mcmc_parcoord(post, pars = c("wt", "am", "sigma"), np = np))
})
test_that("mcmc_parcoord throws correct warnings and errors", {
skip_if_not_installed("rstanarm")
expect_error(mcmc_parcoord(arr, pars = "sigma"),
"requires at least two parameters")
expect_error(
mcmc_parcoord(post, np = np[, -1]),
"NUTS parameter data frame must have columns: Chain, Iteration, Parameter, Value",
fixed = TRUE
)
expect_error(
mcmc_parcoord(post, np = np, np_style = list(div_color = "green")),
'inherits(np_style, "nuts_style") is not TRUE',
fixed = TRUE
)
})
# parcoord_style_np -------------------------------------------------------
test_that("parcoord_style_np returns correct structure", {
style <- parcoord_style_np()
expect_s3_class(style, "nuts_style")
expect_named(style, c("color", "alpha", "size"), ignore.order = TRUE)
expect_named(style$color, c("div"))
expect_named(style$size, c("div"))
expect_named(style$alpha, c("div"))
})
test_that("parcoord_style_np throws correct errors", {
expect_error(
parcoord_style_np(div_size = "3"),
"is.numeric(div_size) is not TRUE",
fixed = TRUE
)
expect_error(
parcoord_style_np(td_color = 1),
"unused argument (td_color = 1)",
fixed = TRUE
)
})
# Visual tests -----------------------------------------------------------------
test_that("mcmc_scatter renders correctly", {
skip_on_cran()
skip_if_not_installed("vdiffr")
p_base <- mcmc_scatter(vdiff_dframe_chains)
vdiffr::expect_doppelganger("mcmc_scatter (default)", p_base)
p_custom <- mcmc_scatter(
vdiff_dframe_chains,
size = 2,
alpha = 0.2
)
vdiffr::expect_doppelganger("mcmc_scatter (size, alpha)", p_custom)
p_divergences <- mcmc_scatter(
vdiff_dframe_chains,
np = vdiff_dframe_chains_divergences
)
vdiffr::expect_doppelganger("mcmc_scatter (np)", p_divergences)
})
test_that("mcmc_hex renders correctly", {
skip_on_cran()
skip_if_not_installed("vdiffr")
skip_if_not_installed("hexbin")
p_base <- mcmc_hex(vdiff_dframe, pars = c("V1", "V2"))
vdiffr::expect_doppelganger("mcmc_hex (default)", p_base)
p_custom <- mcmc_hex(
vdiff_dframe,
pars = c("V1", "V2"),
binwidth = 0.1
)
vdiffr::expect_doppelganger("mcmc_hex (bw)", p_custom)
})
test_that("mcmc_parcoord renders correctly", {
skip_on_cran()
skip_if_not_installed("vdiffr")
p_base <- mcmc_parcoord(vdiff_dframe_chains)
vdiffr::expect_doppelganger("mcmc_parcoord (default)", p_base)
p_divergences <- mcmc_parcoord(
vdiff_dframe_chains,
np = vdiff_dframe_chains_divergences,
np_style = parcoord_style_np(div_size = 2)
)
vdiffr::expect_doppelganger("mcmc_parcoord (np)", p_divergences)
})
test_that("mcmc_pairs renders correctly", {
skip_on_cran()
skip_if_not_installed("vdiffr")
skip_if_not_installed("hexbin")
p_base <- mcmc_pairs(vdiff_dframe_chains)
vdiffr::expect_doppelganger("mcmc_pairs (default)", p_base)
p_divergences <- mcmc_pairs(
vdiff_dframe_chains,
np = vdiff_dframe_chains_divergences,
np_style = pairs_style_np(div_size = 2),
off_diag_fun = "hex"
)
vdiffr::expect_doppelganger("mcmc_pairs (divs, hex)", p_divergences)
p_treedepth <- mcmc_pairs(
vdiff_dframe_chains,
np = vdiff_dframe_chains_treedepth,
np_style = pairs_style_np(td_color = "green"),
max_treedepth = 9
)
vdiffr::expect_doppelganger("mcmc_pairs (td)", p_treedepth)
p_divs_treedepth_divergences <- mcmc_pairs(
vdiff_dframe_chains,
np = vdiff_dframe_chains_np,
np_style = pairs_style_np(div_size = 3, td_color = "green"),
max_treedepth = 9
)
vdiffr::expect_doppelganger("mcmc_pairs (divs, td)", p_divs_treedepth_divergences)
})
|