R/n-chain.R

Defines functions n_chain.jags n_chain.stanfit n_chain.data.table n_chain.NULL n_chain.mcmc.list n_chain.mcmc n_chain.default n_chain

Documented in n_chain

#' @title Dimensions of MCMC objects
#'
#' @description Retrieve dimensions of an MCMC object:
#'   * `n_chain(x)` the number of chains
#'   * `n_var(x)` the number of variables
#'   * `n_iter(x)` the number of iterations
#'
#' @details This is similar to `coda::nchain()` but more general, working for
#'   the following classes:
#'     * `mcmc`
#'     * `mcmc.list`
#'     * `data.table` (generated by `mcmc_to_dt`)
#'     * `stanfit`
#'     * `jags`
#'
#' @param x an mcmc object, see "Details" for a list of supported mcmc objects.
#'
#' @name mcmc-dims
#'
#' @export
n_chain <- function(x){
    UseMethod("n_chain")
}

#' @export
n_chain.default <- function(x){
    stop_not_right_for_mmcc()
}

#' @export
n_chain.mcmc <- function(x){
    # if is.mcmc(x) 1
    1L
}

#' @export
n_chain.mcmc.list <- function(x){
    # if is.mcmc.list length(x)
    length(x)
    # need to protect against length(0)?
}

#' @export
n_chain.NULL <- function(x){
    NULL
}

#' @export
n_chain.data.table <- function(x){
    max(x$chain)
}

#' @export
n_chain.stanfit <- function(x){
    as.integer(x@sim$chains)
}

#' @export
n_chain.jags <- function(x){
    x$nchain()
}
njtierney/dsmcmc documentation built on Oct. 4, 2021, 10:17 p.m.