redistribute {dembase} | R Documentation

## Randomly allocate counts in proportion to weights.

### 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

    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

    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)

[Package dembase version 0.0.0.119 Index]