Skip to content

Commit

Permalink
fix as_array_matrix_list
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Dec 17, 2024
1 parent 79d4521 commit e0610a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
11 changes: 2 additions & 9 deletions R/as_draws_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,8 @@ variance.draws_array <- function(x, ...) {
# convert a list of matrices to an array
as_array_matrix_list <- function(x) {
stopifnot(is.list(x))
if (length(x) == 1) {
tmp <- dimnames(x[[1]])
x <- x[[1]]
dim(x) <- c(dim(x), 1)
dimnames(x) <- tmp
} else {
x <- abind::abind(x, along = 3L)
}
x <- aperm(x, c(1, 3, 2))
x <- abind::abind(x, along = 3L)
aperm(x, c(1, 3, 2))
}

# create an empty draws_array object
Expand Down
15 changes: 12 additions & 3 deletions tests/testthat/test-as_draws.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,20 @@ test_that("lists of matrices can be transformed to draws_array objects", {
x <- round(rnorm(200), 2)
x <- matrix(x, nrow = 50)
colnames(x) <- paste0("theta", 1:4)
x <- list(x, x, x)

y <- as_draws(x)
# one chain
z1 <- list(x)
y <- as_draws(z1)
expect_is(y, "draws_array")
expect_equal(variables(y), colnames(z1[[1]]))
expect_equal(niterations(y), 50)
expect_equal(nchains(y), 1)

# multiple chains
z3 <- list(x, x, x)
y <- as_draws(z3)
expect_is(y, "draws_array")
expect_equal(variables(y), colnames(x[[1]]))
expect_equal(variables(y), colnames(z3[[1]]))
expect_equal(niterations(y), 50)
expect_equal(nchains(y), 3)
})
Expand Down

0 comments on commit e0610a4

Please sign in to comment.