Skip to content

Commit 93afa91

Browse files
committed
Regression test for compute_column_density()
That function is also used in mcmc_dens_chains() even though it's not defined in the same R file
1 parent a01a7ad commit 93afa91

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/testthat/test-mcmc-distributions.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,38 @@ test_that("mcmc_dens_chains returns a ggplot object", {
111111
expect_gg(p2)
112112
})
113113

114+
test_that("mcmc_dens_chains_data computes densities per parameter-chain group", {
115+
# Regression test for compute_column_density().
116+
# This path groups by both parameter and chain, so it exercises the
117+
# group_split() + group_keys() replacement introduced in PR #448.
118+
# The goal is to verify that densities are still computed for the
119+
# correct parameter-chain groups, in the correct grouping structure.
120+
dens_data <- mcmc_dens_chains_data(arr, n_dens = 100)
121+
by_group <- split(
122+
dens_data,
123+
interaction(dens_data$parameter, dens_data$chain, drop = TRUE, lex.order = TRUE)
124+
)
125+
126+
raw <- melt_mcmc(prepare_mcmc_array(arr))
127+
raw_by_group <- split(
128+
raw,
129+
interaction(raw$Parameter, raw$Chain, drop = TRUE, lex.order = TRUE)
130+
)
131+
132+
manual_density <- function(df) {
133+
dens <- density(df$Value, from = min(df$Value), to = max(df$Value), n = 100)
134+
data.frame(x = dens$x, density = dens$y)
135+
}
136+
137+
expected <- lapply(raw_by_group, manual_density)
138+
expect_setequal(names(by_group), names(expected))
139+
for (nm in names(expected)) {
140+
expect_equal(by_group[[nm]]$x, expected[[nm]]$x)
141+
expect_equal(by_group[[nm]]$density, expected[[nm]]$density, tolerance = 1e-10)
142+
}
143+
})
144+
145+
114146
test_that("mcmc_dens_chains/mcmc_dens_overlay color chains", {
115147
p1 <- mcmc_dens_chains(arr, pars = "beta[1]", regex_pars = "x\\:",
116148
color_chains = FALSE)

0 commit comments

Comments
 (0)