Skip to content
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

introduce add_userdata() #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions R/mcmc_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,36 @@ set_userdata <- function(...) {
get_userdata <- function() {
get_("userdata")
}

#' Add userdata to the fmcmc output
#'
#' Combines list of dataframes produced by fmcmc::get_userdata()
#' with mcmc.list() produced by fmcmc::MCMC ensuring that the
#' chains and iterations match
#'
#' @param x mcmc.list to add userdata to. Note, that userdata
#' is taken from the environment (whatever fmcmc::get_userdata()
#' returns)
#'
#' @return combined mcmc.list
#' @rdname mcmc-loop
#' @export
#' @importFrom coda mcpar mcmc as.mcmc.list
#' @examples
add_userdata <- function(x){
ud <- fmcmc::get_userdata()
stopifnot("Number of chains do not match"=
(length(x)==length(ud)))
res <- mapply(function(x,y){
iters_y <- as.numeric(rownames(x))
min_iters_y <- min(iters_y)
max_iters_y <- max(iters_y)
mcpar_x <- coda::mcpar(x)
mcpar_y <- c(min_iters_y, max_iters_y, 1)
stopifnot("Iterations are not matching"=
all.equal(mcpar_x, mcpar_y))
my <- as.matrix(y, dimnames=list(iters_y,colnames(y)))
coda::mcmc(cbind(x,my), start = min_iters_y, end = max_iters_y)
},x=x, y=ud, SIMPLIFY = FALSE)
coda::as.mcmc.list(res)
}