redistribute: Randomly allocate counts in proportion to weights.

Description Usage Arguments Details Value See Also Examples

Description

weights should have one or more dimensions that counts does not. The contents of each cell in counts are randomly distributed across these extra dimensions.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
redistribute(counts, weights, means = FALSE, n = NULL)

## S4 method for signature 'Counts,DemographicArray'
redistribute(counts, weights, means = FALSE, n = NULL)

## S4 method for signature 'Values,DemographicArray'
redistribute(counts, weights, n = NULL)

## S4 method for signature 'numeric,DemographicArray'
redistribute(counts, weights, means = FALSE, n = NULL)

Arguments

counts

Object of class Counts consisting of non-negative integers, or a single non-negative integer.

weights

Object of class DemographicArray with no negative values.

means

If FALSE (the default) the allocation is done randomly; if TRUE, values are set equal to their means.

n

Number of iterations to generate. Ignored if counts or weights has a dimension with dimtype "iteration".

Details

Each cell in counts is redistributed by drawing from a multinomial distribution with size equal to the value of the cell and probabiltiies proportional to the values of the corresponding cells in weights.

weights is typically a set of observed counts, such a cross-tabulation of records with no missing data. However, weights could also be a sample from a posterior distribution.

Value

An Counts object with the same dimensions and metadata as weights.

See Also

rmultinom

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
x <- Counts(array(c(10, 12),
                  dim = 2,
                  dimnames = list(sex = c("Female", "Male"))))
y <- Counts(array(0:5,
                  dim = c(2, 3),
                  dimnames = list(sex = c("Female", "Male"),
                      region = c("A", "B", "C"))))

redistribute(x, weights = y)

## specify the number of iterations
redistribute(x, weights = y, n = 3)

ans <- redistribute(x, weights = y)
ans
## reverse the effects
collapseDimension(ans, dimension = "region")

## give all cells a chance of being non-zero
redistribute(x, weights = y + 0.1, n = 10)

## 'counts' is a single value
redistribute(10, weights = y)

StatisticsNZ/dembase documentation built on Dec. 25, 2021, 4:49 p.m.