Learn R Programming

pi0 (version 1.4-1)

combn2R: Randomly Choosing R Combinations of Two Groups of n and n2 Elements Taken m and m2 at a Time

Description

This is the enhanced version of combn. There are two groups, with n1 and n2 elements, respectively. Each time, m1 elements will be randomly chosen from group 1; and m2 elements will be randomly chosen from group 2. These m1+m2 elements form one combination. This function generate either all choose(n1,m1)*choose(n2,m2) such combinations or a subset of R of them. A function, possibly identity, is then applied to each selected combination.

Usage

combn2R(x, m, x2, m2, R, FUN = c, simplify = TRUE, 
        sample.method="all", try.rest=TRUE, ...)

Arguments

x

group 1 vector for combinations, or positive integer \(n\) for x <- seq(n).

m

number of elements to choose from x, i.e., group 1.

x2

group 2 vector for combinations, or positive integer \(n_2\) for x2 <- seq(n). If missing, it reduces to get R combinations from x taken m at a time. See details.

m2

number of elements to choose from x2, i.e., group 2. If missing, it reduces to get R combinations from x taken m at a time. See details.

R

the number of combinations to be randomly chosen from all choose(n,m)*choose(n2,m2) combinations. If missing or larger than all possible combinations, results from all combinations will be tried, but not guaranteed when the total number of possible combinations is too large. See details.

FUN

a function to be applied to each chosen combination. When neither x2 nor m2 is missing, and neither m=0 nor m2=0, this function needs to accept at least two arguments, the first of which is a vector of length m, which is a subset of x; and the second argument of FUN is a vector of length m2, which is a subset of x2. Additional arguments are supplied with dots. When combn2R is only used for one group situtation (similar to combn), the second argument of FUN is not required.

simplify

logical, indicating if the result should be simplified to an array (typically a matrix); see combn.

sample.method

character, specifying how samples should be taken, if not all combinations are generated; possible choices are "diff2", "all", and "replace" for two-group situation, and "replace" and "noReplace" in one-group situation. See Details.

try.rest

logical, together with sample.method, specifying how samples should be taken, if not all combinations are generated; see Details.

optionally, further arguments to FUN.

Value

a list or array (in nondegenerate cases), similar to combn. An attribute "sample.method" will be added to the list or array, which stores the actual sampling method that has been used, which may or may not be the same as specified in the argument.

Details

This function enhances combn in two ways. One is to deal with two-group situation, which is commonly seen in real designs; the other is to choose only a random sample of size R from all possible combinations to avoid unnecessary computation. When neither x2 nor m2 is missing and neither m=0 nor m2=0, the function works in two-group mode. In this situation,

(A) if sample.method="diff2", combn2R will try to sample R combinations from each group separately. That is, first sample R combinations from x taken m at a time, and then sample R combinations from x2 taken m2 at time. The results are then combined to give R combinations from the two groups. This sampling method will make the samples as different from each other as possible. But when R is larger than min(choose(n1,m1),choose(n2,m2)), it is not possible to get R samples from each group separately. If this happens and try.rest=FALSE, then R will be reset to min(choose(n,m),choose(n2,m2)) and the function works as before; otherwise, if try.rest=TRUE, then sample.method will be reset to "all" and the function will try to get R samples from all choose(n1,m1)*choose(n2,m2) combinations (see below).

(B) if sample.method="all", combn2R will try to sample R samples from all choose(n1,m1)*choose(n2,m2) combinations directly. This means two samples of size m+m2 may have the same sample of size m (or m2) which comes from x (or x2). For example, if x=1:3, m=1, x2=4:6, m=2 and R=2, then it is possible to get one sample to be 1 and 4,5, but the other sample is 1 and 5,6. That is, the same sample from x is used in both results. This will not happen when sample.method='diff2'. However, this will guarantee any two samples of size m+m2 will differ in at least one element.

(C) if sample.method='replace', combn2R will not guarantee the uniqueness of the R combinations in any way. It is possible to have two exactly the same samples of size m+m2.

Because the number of possible combinations grows very fast, computational limitations may be reached. In this case, if try.rest=TRUE, then sample.method will be reset to "replace", which uses the least computational resources; otherwise, an error will be generated.

When either x2 or m2 is missing, or one of m and m2 is zero, the function works in one-group mode. In this situation, sample.method="diff2" and sample.method="all" will be treated the same as sample.method="noReplace", and combn2R will try to obtain R different combinations from all possible combinations for the non-missing group. Again, if this fails due to computational limitations, sample.method will be reset to "replace" and no guarantee will be made to ensure the R combinations are different from each other.

See Also

combn in utils or combinat,

Examples

Run this code
# NOT RUN {
combn2R(4,2) ### the same as combn(4,2), except an additional attribute
combn2R(1:4*2,2)
combn2R(4,2,5,1)
combn2R(4,2,5,1,FUN=sum)
set.seed(992722)
combn2R(4,2,R=3) ### the same as combnR(4,2,3), except an additional attribute
combn2R(4,2,R=10) ### only 6 combinations possible
combn2R(4,2,5,1,R=8)
combn2R(1:4*2,2,5,1,R=50) ### only 30 combinations possible
combn2R(1:4*2,2,5,1,R=5) ### when considering only one group, there are several common samples.
### no common samples, even considering only one group
combn2R(1:4*2,2,5,1,R=5, sample.method="diff2") 
combn2R(1:3*3,1,3,1,R=5, sample.method="replace") ### two pairs of exactly common samples
combn2R(100,3,100,3,R=5, sample.method="all") ### 'all' combinations not feasible (~3e10)
# }

Run the code above in your browser using DataLab