Learn R Programming

permute (version 0.10-0)

partitions: Generate random assignments to groups of fixed size

Description

Generate all assignments, one random assignment, or a set of random assignments of observations to labelled groups whose sizes are fixed by a grouping factor.

Usage

allPartitions(strata, control = how(), check = TRUE)

shufflePartition(strata, control = how())

shufflePartitionSet( strata, nset, control = how(), check = TRUE, quietly = FALSE )

Value

shufflePartition() returns an integer vector of length length(strata). allPartitions() and shufflePartitionSet() return permutation matrices with one assignment per row.

Arguments

strata

A factor, or an object coercible to a factor, containing the group membership of every observation.

control

An object of class "how". Any blocking factor and the permutation-count controls are retained; its plots and within components are replaced by the partition design.

check

Logical; should the permutation design be checked?

nset

The number of random assignments to generate. If missing, it is obtained from control.

quietly

Logical; should messages about complete enumeration be suppressed?

Details

The number assigned to each group is fixed by table(strata). If the group sizes are \(n_1, \ldots, n_K\), the number of distinct assignments is $$n! / \prod_{k=1}^K n_k!.$$

Each assignment is returned as a permutation of observation indices, so it can be used wherever output from shuffle(), shuffleSet(), or allPerms() is accepted. The relative order of observations originally belonging to the same group is retained. This selects one canonical index permutation for each distinct arrangement of the group labels and omits permutations that differ only by reordering observations carrying the same label.

These functions are convenience wrappers for a design constructed with Plots(strata = strata, type = "partition") and Within(type = "none").

See Also

allPerms(), shuffle(), shuffleSet(), and how().

Examples

Run this code
groups <- factor(c("a", "a", "a", "b", "b"))

## One of 5! / (3! 2!) = 10 assignments
set.seed(1)
(p <- shufflePartition(groups))
groups[p]

## A set of random assignments
shufflePartitionSet(groups, nset = 5, check = FALSE)

## Complete enumeration, excluding the observed assignment by default
allPartitions(groups)

Run the code above in your browser using DataLab