Skip to content

Commit

Permalink
Implementation and documentation improvements to the colMeans.mcmc.li…
Browse files Browse the repository at this point in the history
…st() family.
  • Loading branch information
krivit committed Sep 23, 2024
1 parent 4e8cb54 commit 733aa11
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: statnet.common
Version: 4.10.0-442
Date: 2024-01-30
Version: 4.10.0-447
Date: 2024-09-23
Title: Common R Scripts and Utilities Used by the Statnet Project Software
Authors@R: c(
person(c("Pavel", "N."), "Krivitsky", role=c("aut","cre"), email="[email protected]", comment=c(ORCID="0000-0002-9101-3362", affiliation="University of New South Wales")),
Expand All @@ -13,7 +13,7 @@ BugReports: https://github.com/statnet/statnet.common/issues
License: GPL-3 + file LICENSE
URL: https://statnet.org
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Encoding: UTF-8
Suggests: covr,
rlang (>= 1.1.1),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ importFrom(coda,as.mcmc)
importFrom(coda,as.mcmc.list)
importFrom(methods,is)
importFrom(stats,as.formula)
importFrom(stats,var)
importFrom(utils,capture.output)
importFrom(utils,getAnywhere)
importFrom(utils,modifyList)
Expand Down
30 changes: 19 additions & 11 deletions R/mcmc-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,37 @@
#' @description \code{colMeans.mcmc.list} is a "method" for (non-generic) [colMeans()] applicable to [`mcmc.list`] objects.
#'
#' @param x a \code{\link{mcmc.list}} object.
#' @param \dots additional arguments to \code{\link{colMeans}} or
#' \code{\link{sweep}}.
#' @param \dots additional arguments to the functions evaluated on each chain.
#' @return \code{colMeans.mcmc} returns a vector with length equal to
#' the number of mcmc chains in \code{x} with the mean value for
#' each chain.
#' @seealso [colMeans()], [`mcmc.list`]
#'
#' @details These implementations should be equivalent (within
#' numerical error) to the same function being called on
#' `as.matrix(x)`, while avoiding construction of the large matrix.
#'
#' @seealso [`mcmc.list`]
#'
#' [colMeans()]
#' @examples
#' data(line, package="coda")
#' summary(line) # coda
#' colMeans(as.matrix(line)) # also coda
#' colMeans.mcmc.list(line) # "Method"
#' \dontshow{
#' stopifnot(isTRUE(all.equal(summary(line)$statistics[,"Mean"],colMeans.mcmc.list(line))))
#' stopifnot(isTRUE(all.equal(colMeans(as.matrix(line)),colMeans.mcmc.list(line))))
#' }
#' @export colMeans.mcmc.list
colMeans.mcmc.list<-function(x,...) colMeans(as.matrix(x),...)
colMeans.mcmc.list <- function(x,...){
nchain <- length(x)
Reduce(`+`, lapply(x, colMeans, ...)) / nchain
}

#' @rdname mcmc-utilities
#'
#' @description \code{var.mcmc.list} is a "method" for (non-generic)
#' [var()] applicable to [`mcmc.list`] objects. Since MCMC chains
#' are assumed to all be sampling from the same underlying
#' distribution, pooled mean is used. This implementation should be
#' equivalent (within numerical error) to `var(as.matrix(x))` while
#' avoiding constructing the large matrix.
#' distribution, their pooled mean is used.
#'
#' @seealso [var()]
#' @examples
Expand All @@ -46,12 +53,13 @@ colMeans.mcmc.list<-function(x,...) colMeans(as.matrix(x),...)
#' \dontshow{
#' stopifnot(isTRUE(all.equal(var.mcmc.list(line), var(as.matrix(line)))))
#' }
#' @importFrom stats var
#' @export var.mcmc.list
var.mcmc.list <- function(x, ...){
nchain <- length(x)
niter <- NROW(x[[1]])
SSW <- Reduce(`+`, lapply(x, cov)) * (niter-1)
SSB <- if(nchain > 1) cov(t(sapply(x, colMeans))) * niter else 0
SSW <- Reduce(`+`, lapply(x, var, ...)) * (niter-1)
SSB <- if(nchain > 1) var(t(sapply(x, colMeans, ...))) * niter else 0
(SSW+SSB) / (niter*nchain-1)
}

Expand Down
20 changes: 12 additions & 8 deletions man/mcmc-utilities.Rd

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

0 comments on commit 733aa11

Please sign in to comment.