From e0610a44fcf10b5d2a832cf45ddf48a86f5b5e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul-Christian=20B=C3=BCrkner?= Date: Tue, 17 Dec 2024 18:28:32 +0100 Subject: [PATCH 1/2] fix as_array_matrix_list --- R/as_draws_array.R | 11 ++--------- tests/testthat/test-as_draws.R | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/R/as_draws_array.R b/R/as_draws_array.R index 63724f7..cfe263b 100644 --- a/R/as_draws_array.R +++ b/R/as_draws_array.R @@ -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 diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index a1b422f..b5d5579 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -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) }) From bf68d98f2335f3d2325a1d9831fc859efceeb8dc Mon Sep 17 00:00:00 2001 From: jgabry Date: Tue, 17 Dec 2024 13:06:37 -0700 Subject: [PATCH 2/2] Fix merge conflict --- DESCRIPTION | 4 ++-- NEWS.md | 6 ++++++ R/as_draws.R | 9 +++++---- R/as_draws_array.R | 16 +++++++++++++--- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a6289bf..726f973 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: posterior Title: Tools for Working with Posterior Distributions -Version: 1.6.0 -Date: 2024-06-28 +Version: 1.6.0.9000 +Date: 2024-12-17 Authors@R: c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.com", role = c("aut", "cre")), person("Jonah", "Gabry", email = "jsg2201@columbia.edu", role = c("aut")), person("Matthew", "Kay", email = "mjskay@northwestern.edu", role = c("aut")), diff --git a/NEWS.md b/NEWS.md index 7ae17cd..0ca3d52 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# posterior 1.6.0+ + +### Enhancements + +* Convert lists of matrices to `draws_array` objects. + # posterior 1.6.0 ### Enhancements diff --git a/R/as_draws.R b/R/as_draws.R index c52e75a..84c4e66 100644 --- a/R/as_draws.R +++ b/R/as_draws.R @@ -76,10 +76,11 @@ closest_draws_format <- function(x) { out <- "rvars" } else if (is_draws_list_like(x)) { out <- "list" - } - else { - stop_no_call("Don't know how to transform an object of class ", - "'", class(x)[1L], "' to any supported draws format.") + } else { + stop_no_call( + "Don't know how to transform an object of class '", + class(x)[1L], "' to any supported draws format." + ) } paste0("draws_", out) } diff --git a/R/as_draws_array.R b/R/as_draws_array.R index ab123a4..b00e195 100644 --- a/R/as_draws_array.R +++ b/R/as_draws_array.R @@ -130,7 +130,11 @@ as_draws_array.mcmc.list <- function(x, ...) { # try to convert any R object into a 'draws_array' object .as_draws_array <- function(x) { - x <- as.array(x) + if (is_matrix_list_like(x)) { + x <- as_array_matrix_list(x) + } else { + x <- as.array(x) + } new_dimnames <- list(iteration = NULL, chain = NULL, variable = NULL) if (!is.null(dimnames(x)[[3]])) { new_dimnames[[3]] <- dimnames(x)[[3]] @@ -177,7 +181,14 @@ is_draws_array <- function(x) { # is an object looking like a 'draws_array' object? is_draws_array_like <- function(x) { - is.array(x) && length(dim(x)) == 3L + is.array(x) && length(dim(x)) == 3L || + is_matrix_list_like(x) +} + +# is an object likely a list of matrices? +# such an object can be easily converted to a draws_array +is_matrix_list_like <- function(x) { + is.list(x) && length(dim(x[[1]])) == 2L } #' Extract parts of a `draws_array` object @@ -238,4 +249,3 @@ empty_draws_array <- function(variables = character(0), nchains = 0, class(out) <- class_draws_array() out } -