Skip to content

Commit bfe5c66

Browse files
authored
Merge pull request #184 from stan-dev/coerce-to-array
Call as.array on input to prepare_mcmc_array()
2 parents 6c3ea04 + 9eaa56b commit bfe5c66

15 files changed

+98
-74
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<!-- Items for next release go here* -->
88

9+
* MCMC plots now also accept objects with an `as.array` method as
10+
input. (#175, #184)
11+
912
* [`mcmc_trace()`](http://mc-stan.org/bayesplot/reference/MCMC-traces.html)
1013
gains an argument `iter1` which can be used to label the traceplot starting
1114
from the first iteration after warmup. (#14, #155, @mcol)

R/helpers-mcmc.R

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ prepare_mcmc_array <- function(x,
1212
x <- df_with_chain2array(x)
1313
} else if (is_chain_list(x)) {
1414
x <- chain_list2array(x)
15+
} else if (is.data.frame(x)) {
16+
# data frame without Chain column
17+
x <- as.matrix(x)
18+
} else if (!is.array(x)) {
19+
x <- as.array(x)
20+
}
21+
22+
stopifnot(is.matrix(x) || is.array(x))
23+
if (is.array(x) && !(length(dim(x)) %in% c(2,3))) {
24+
stop("Arrays should have 2 or 3 dimensions. See help('MCMC-overview').")
25+
}
26+
if (anyNA(x)) {
27+
stop("NAs not allowed in 'x'.")
1528
}
16-
x <- validate_mcmc_x(x)
1729

1830
parnames <- parameter_names(x)
1931
pars <- select_parameters(
2032
explicit = pars,
2133
patterns = regex_pars,
22-
complete = parnames)
34+
complete = parnames
35+
)
2336

2437
# possibly recycle transformations (apply same to all pars)
2538
if (is.function(transformations) ||
@@ -152,21 +165,19 @@ df_with_chain2array <- function(x) {
152165
}
153166

154167

155-
# Check if an object is a list but not a data.frame
168+
# Check if an object is a list (but not a data.frame) that contains
169+
# all 2-D objects
156170
#
157171
# @param x object to check
158172
# @return TRUE or FALSE
159173
is_chain_list <- function(x) {
160-
!is.data.frame(x) && is.list(x)
174+
check1 <- !is.data.frame(x) && is.list(x)
175+
dims <- sapply(x, function(chain) length(dim(chain)))
176+
check2 <- isTRUE(all(dims == 2)) # all elements of list should be matrices/2-D arrays
177+
check1 && check2
161178
}
162179

163180
validate_chain_list <- function(x) {
164-
stopifnot(is_chain_list(x))
165-
dims <- sapply(x, function(chain) length(dim(chain)))
166-
if (!isTRUE(all(dims == 2))) {
167-
stop("If 'x' is a list then all elements must be matrices.")
168-
}
169-
170181
n_chain <- length(x)
171182
for (i in seq_len(n_chain)) {
172183
nms <- colnames(as.matrix(x[[i]]))
@@ -276,25 +287,6 @@ STOP_need_multiple_chains <- function(call. = FALSE) {
276287
}
277288

278289

279-
# Perform some checks on user's 'x' input for MCMC plots
280-
#
281-
# @param x User's 'x' input to one of the mcmc_* functions.
282-
# @return x, unless an error is thrown.
283-
validate_mcmc_x <- function(x) {
284-
stopifnot(!is_df_with_chain(x), !is_chain_list(x))
285-
if (is.data.frame(x)) {
286-
x <- as.matrix(x)
287-
}
288-
289-
stopifnot(is.matrix(x) || is.array(x))
290-
if (anyNA(x)) {
291-
stop("NAs not allowed in 'x'.")
292-
}
293-
294-
x
295-
}
296-
297-
298290
# Validate that transformations match parameter names
299291
validate_transformations <-
300292
function(transformations = list(),

R/mcmc-overview.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#' corresponds to a Markov chain. All of the matrices should have the same
1919
#' number of iterations (rows) and parameters (columns), and parameters should
2020
#' have the same names and be in the same order.
21-
#' \item \strong{matrix}: A \code{\link{matrix}} with one column per parameter.
22-
#' If using matrix there should only be a single Markov chain or all chains
23-
#' should already be merged (stacked).
21+
#' \item \strong{matrix (2-D array)}: A \code{\link{matrix}} with one column
22+
#' per parameter. If using matrix there should only be a single Markov chain or
23+
#' all chains should already be merged (stacked).
2424
#' \item \strong{data frame}: There are two types of \link[=data.frame]{data
2525
#' frames} allowed. Either a data frame with one column per parameter (if only
2626
#' a single chain or all chains have already been merged), or a data frame with

man-roxygen/args-mcmc-x.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#' @param x A 3-D array, matrix, list of matrices, or data frame of MCMC draws.
22
#' The \link{MCMC-overview} page provides details on how to specify each these
3-
#' allowed inputs.
3+
#' allowed inputs. It is also possible to use an object with an
4+
#' \code{as.array} method that returns the same kind of 3-D array described on
5+
#' the \link{MCMC-overview} page.

man/MCMC-combos.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-diagnostics.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-distributions.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-intervals.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-overview.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-parcoord.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)