R/util.R

Defines functions alloc my.format printf normalize onLoad

Documented in alloc

.onLoad <- function(libname, pkgname){
	options(allocation.prec.bits = 256)
	options(allocation.print.decimals = 4)
}

normalize <- function(x)
{
	x / sum(x)
}

printf <- function(msg, ...)
{
	cat(sprintf(msg, ...))
}

#' @importFrom Rmpfr asNumeric formatMpfr
my.format <- function(x, decimal.digits = getOption("allocation.print.decimals"))
{
	L <- length(x)

	idx.notna <- which(!is.na(x) & abs(x) > 1)
	whole.digits <- rep(1,L)
	whole.digits[idx.notna] <- asNumeric(ceiling(log10(abs(x[idx.notna]))))
	print.digits <- whole.digits + decimal.digits

	out <- numeric(L)
	for (l in 1:L) {
		out[l] <- formatMpfr(x[l], digits = print.digits[l], big.mark = ",")
	}
	return(out)
}


#' @title
#' Accessor for Solved Allocation
#' 
#' @description 
#' Extract the solved allocation from the result of an an allocation method.
#' 
#' @param object Result from an allocation method
#'
#' @return A numeric vector; each element contains the allocation for the
#' corresponding stratum.
#' @export
alloc <- function(object)
{
	UseMethod("alloc")
}
andrewraim/tommysampling documentation built on May 6, 2018, 5:03 a.m.