Learn R Programming

randomizr (version 2.0.1)

obtain_permutation_probabilities: Obtain the probabilities of permutations

Description

Returns how likely each assignment in the permutation matrix was. Most designs make every possible assignment equally likely, in which case these are all the same and can be ignored. Blocked and clustered designs of unequal size do not, and there the probabilities are needed to weight the randomization distribution correctly.

Usage

obtain_permutation_probabilities(declaration)

Value

A vector with one entry per possible assignment, giving the probability that the design produces that assignment. The entries sum to 1 and are in the same order as the columns of obtain_permutation_matrix(), so the two can be used together.

Arguments

declaration

A random assignment declaration, created by declare_ra(). (required)

References

Andrews, G. E. (1976). The Theory of Partitions. Encyclopedia of Mathematics and its Applications, Volume 2. Reading, MA: Addison-Wesley.

See Also

obtain_permutation_matrix(), obtain_num_permutations()

Examples

Run this code

# A design in which the possible assignments are *not* equally likely: with
# N = 5 and prob = 0.51, either 2 or 3 units are treated, and those two cases
# do not come up equally often.
declaration <- declare_ra(N = 5, prob_each = c(0.49, 0.51))

obtain_num_permutations(declaration)

perms <- obtain_permutation_matrix(declaration)
perm_probs <- obtain_permutation_probabilities(declaration)

# perms has one column per possible assignment and perm_probs has one entry
# per column, in the same order
dim(perms)
length(perm_probs)

# Each unit's probability of assignment to treatment, according to the
# declaration. Recovering these from perms is the check that the two objects
# line up.
true_probabilities <- declaration$probabilities_matrix[, 2]
true_probabilities

# The unweighted average across columns is WRONG here: it treats every
# assignment as equally likely, which this design does not.
rowMeans(perms)

# Weighting each column by how likely it is recovers the true probabilities.
# The matrix product does the weighted average: row i of perms times
# perm_probs sums unit i's treatment indicators weighted by column
# probability, which is exactly Pr(unit i treated).
perms %*% perm_probs

Run the code above in your browser using DataLab