combn2R: Randomly Choosing R Combinations of Two Groups of n and n2...

Description Usage Arguments Details Value Note Author(s) See Also Examples

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

1
2
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 n2 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.

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.

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.

Note

Note that the results are not necessarily in order, which is a difference from combn.

Author(s)

Long Qu

See Also

combn in utils or combinat,

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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)

Example output

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    2    2    3
[2,]    2    3    4    3    4    4
attr(,"sample.method")
[1] "noReplace"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2    2    2    4    4    6
[2,]    4    6    8    6    8    8
attr(,"sample.method")
[1] "noReplace"
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    1    1    1    1    1    1    1    1     1     1     1     1     1
[2,]    2    2    2    2    2    3    3    3    3     3     4     4     4     4
[3,]    1    2    3    4    5    1    2    3    4     5     1     2     3     4
     [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,]     1     2     2     2     2     2     2     2     2     2     2     3
[2,]     4     3     3     3     3     3     4     4     4     4     4     4
[3,]     5     1     2     3     4     5     1     2     3     4     5     1
     [,27] [,28] [,29] [,30]
[1,]     3     3     3     3
[2,]     4     4     4     4
[3,]     2     3     4     5
attr(,"sample.method")
[1] "all"
 [1]  4  5  6  7  8  5  6  7  8  9  6  7  8  9 10  6  7  8  9 10  7  8  9 10 11
[26]  8  9 10 11 12
attr(,"sample.method")
[1] "all"
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    3    3    4
attr(,"sample.method")
[1] "noReplace"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    2    2    3
[2,]    2    3    4    3    4    4
attr(,"sample.method")
[1] "noReplace"
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    1    1    2    2    3    3
[2,]    2    2    3    4    3    4    4    4
[3,]    1    4    4    4    3    3    1    4
attr(,"sample.method")
[1] "all"
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    2    2    2    2    2    2    2    2    2     2     2     2     2     2
[2,]    4    4    4    4    4    6    6    6    6     6     8     8     8     8
[3,]    1    2    3    4    5    1    2    3    4     5     1     2     3     4
     [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,]     2     4     4     4     4     4     4     4     4     4     4     6
[2,]     8     6     6     6     6     6     8     8     8     8     8     8
[3,]     5     1     2     3     4     5     1     2     3     4     5     1
     [,27] [,28] [,29] [,30]
[1,]     6     6     6     6
[2,]     8     8     8     8
[3,]     2     3     4     5
attr(,"sample.method")
[1] "all"
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    4    4    6    6
[2,]    8    6    6    8    8
[3,]    5    4    5    3    4
attr(,"sample.method")
[1] "all"
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    2    4    4    6
[2,]    4    6    6    8    8
[3,]    5    1    3    4    2
attr(,"sample.method")
[1] "diff2"
     [,1] [,2] [,3] [,4] [,5]
[1,]    6    9    6    6    6
[2,]    3    3    2    2    3
attr(,"sample.method")
[1] "replace"
     [,1] [,2] [,3] [,4] [,5]
[1,]   21   26   54   55   55
[2,]   52   50   60   58   59
[3,]   89   54   78   93   62
[4,]   82   20   34    1   31
[5,]   90   50   45   67   65
[6,]  100   55   96   68   90
attr(,"sample.method")
[1] "all"

pi0 documentation built on May 2, 2019, 4:47 p.m.