Skip to content

add as_mcmc_list function #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(as_draws,CmdStanMLE)
S3method(as_draws,CmdStanVB)
export(as_cmdstan_fit)
export(as_draws)
export(as_mcmc.list)
export(check_cmdstan_toolchain)
export(cmdstan_default_install_path)
export(cmdstan_default_path)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ recompilation of Stan models. (#580)
* New methods for `posterior::as_draws()` for CmdStanR fitted model objects.
These are just wrappers around the `$draws()` method provided for convenience. (#532)

* New function `as_mcmc.list()` for converting CmdStanMCMC objects to mcmc.list
objects from the coda package. (#584, @MatsuuraKentaro)

# cmdstanr 0.4.0

### Bug fixes
Expand Down
43 changes: 43 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,46 @@ maybe_convert_draws_format <- function(draws, format) {
stop("Invalid draws format.", call. = FALSE)
)
}


# convert draws for external packages ------------------------------------------

#' Convert `CmdStanMCMC` to `mcmc.list`
#'
#' This function converts a `CmdStanMCMC` object to an `mcmc.list` object
#' compatible with the \pkg{coda} package. This is primarily intended for users
#' of Stan coming from BUGS/JAGS who are used to \pkg{coda} for plotting and
#' diagnostics. In general we recommend the more recent MCMC diagnostics in
#' \pkg{posterior} and the \pkg{ggplot2}-based plotting functions in
#' \pkg{bayesplot}, but for users who prefer \pkg{coda} this function provides
#' compatibility.
#'
#' @export
#' @param x A [CmdStanMCMC] object.
#' @return An `mcmc.list` object compatible with the \pkg{coda} package.
#' @examples
#' \dontrun{
#' fit <- cmdstanr_example()
#' x <- as_mcmc.list(fit)
#' }
#'
as_mcmc.list <- function(x) {
if (!inherits(x, "CmdStanMCMC")) {
stop("Currently only CmdStanMCMC objects can be converted to mcmc.list.",
call. = FALSE)
}
sample_array <- x$draws(format = "array")
n_chain <- posterior::nchains(sample_array)
n_iteration <- posterior::niterations(sample_array)
class(sample_array) <- 'array'
mcmc_list <- lapply(seq_len(n_chain), function(chain) {
x <- sample_array[, chain, ]
dimnames(x) <- list(iteration = dimnames(sample_array)$iteration,
variable = dimnames(sample_array)$variable)
attr(x, 'mcpar') <- c(1, n_iteration, 1)
class(x) <- 'mcmc'
x
})
class(mcmc_list) <- 'mcmc.list'
return(mcmc_list)
}
30 changes: 30 additions & 0 deletions man/as_mcmc.list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ if (not_on_cran()) {
set_cmdstan_path()
fit_mcmc <- testing_fit("logistic", method = "sample",
seed = 123, chains = 2)
fit_mle <- testing_fit("logistic", method = "opt", seed = 123)
}

test_that("check_divergences() works", {
Expand Down Expand Up @@ -178,3 +179,20 @@ test_that("require_suggested_package() works", {
"Please install the 'not_a_real_package' package to use this function."
)
})

test_that("as_mcmc.list() works", {
x <- as_mcmc.list(fit_mcmc)
expect_length(x, fit_mcmc$num_chains())
expect_s3_class(x, "mcmc.list")
expect_s3_class(x[[1]], "mcmc")

draws <- fit_mcmc$draws()
x1 <- x[[1]]
expect_equal(dim(x1), c(posterior::niterations(draws), posterior::nvariables(draws)))
expect_equal(dimnames(x1)$variable, posterior::variables(draws))

expect_error(
as_mcmc.list(fit_mle),
"Currently only CmdStanMCMC objects can be converted to mcmc.list"
)
})