c("e1071", "permute")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
\({n \choose k}\)
5
myN <- 4
myK <-choose(myN, myK)
[1] 5
factorial(myN) / (factorial(myK)*factorial(myN-myK))
[1] 5
combn(c("a", "b", "c", "d", "e"), myK)
[,1] [,2] [,3] [,4] [,5]
[1,] "a" "a" "a" "a" "b"
[2,] "b" "b" "b" "c" "c"
[3,] "c" "c" "d" "d" "d"
[4,] "d" "e" "e" "e" "e"
combn(c(1, 2, 3, 4), 3)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
combn(c(1, 2, 3, 4), 3, sum)
[1] 6 7 8 9
combn(c(1, 2, 3, 4), 3, weighted.mean, w=c(0.5, 0.2, 0.3))
[1] 1.8 2.1 2.3 2.8
factorial(7)
[1] 5040
set.seed(123)
LETTERS[1:10]
set <-sample(set, length(set), replace=FALSE)
[1] "C" "H" "D" "G" "F" "A" "J" "I" "B" "E"
library(permute)
shuffle(length(set))
[1] 10 5 6 9 1 7 8 4 3 2
LETTERS[1:3]
set <- length(set)
len <-library(e1071)
permutations(len)) (mat <-
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 1 3
[3,] 2 3 1
[4,] 1 3 2
[5,] 3 1 2
[6,] 3 2 1
apply(mat, 1, function(x) set[x])
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "A" "B" "B" "A" "C" "C"
[2,] "B" "A" "C" "C" "A" "B"
[3,] "C" "C" "A" "B" "B" "A"
rep(letters[1:3], each=3)) (grp <-
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
length(grp)
N <- 100
nPerms <-library(permute)
how(nperm=nPerms, complete=FALSE)
pCtrl <-for(i in 1:5) {
permute(i, n=N, control=pCtrl)
perm <-print(grp[perm])
}
[1] "c" "b" "b" "c" "b" "a" "a" "c" "a"
[1] "a" "c" "c" "b" "b" "a" "c" "a" "b"
[1] "a" "a" "a" "c" "b" "c" "c" "b" "b"
[1] "b" "a" "c" "a" "c" "b" "b" "a" "c"
[1] "a" "c" "c" "a" "b" "a" "c" "b" "b"
Permute group membership in a two-way ANOVA design. Here: artificially low number of permutations.
4 ## cell size
Njk <- 2 ## levels factor A
P <- 3 ## levels factor B
Q <- Njk*P*Q
N <- 10 ## number of permutations
nPerms <- 1:(Njk*P*Q)
id <- factor(rep(1:P, each=Njk*Q)) ## factor A
IV1 <- factor(rep(1:Q, times=Njk*P)) ## factor B
IV2 <- data.frame(id, IV1, IV2)) (myDf <-
id IV1 IV2
1 1 1 1
2 2 1 2
3 3 1 3
4 4 1 1
5 5 1 2
6 6 1 3
7 7 1 1
8 8 1 2
9 9 1 3
10 10 1 1
11 11 1 2
12 12 1 3
13 13 2 1
14 14 2 2
15 15 2 3
16 16 2 1
17 17 2 2
18 18 2 3
19 19 2 1
20 20 2 2
21 21 2 3
22 22 2 1
23 23 2 2
24 24 2 3
# choose permutation schemes for tests of factor A and B
library(permute) ## for how(), permute()
## only permute across A (within B)
how(plots=Plots(strata=IV2), complete=FALSE, nperm=nPerms)
pCtrlA <-
## only permute across B (within A)
how(plots=Plots(strata=IV1), complete=FALSE, nperm=nPerms) pCtrlB <-
Get permutations for test of factor A
for(i in 1:3) {
permute(i, n=N, control=pCtrlA)
perm <-print(myDf[perm, ])
# not shown
}
Get permutations for test of factor B
for(i in 1:3) {
permute(i, n=N, control=pCtrlB)
perm <-print(myDf[perm, ])
# not shown
}
All combinations of elements from \(n\) different sets (cartesian product)
c("control", "treatment")
IV1 <- c("f", "m")
IV2 <- c(1, 2)
IV3 <-expand.grid(IV1, IV2, IV3)
Var1 Var2 Var3
1 control f 1
2 treatment f 1
3 control m 1
4 treatment m 1
5 control f 2
6 treatment f 2
7 control m 2
8 treatment m 2
outer(1:5, 1:5, FUN="*")
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 4 6 8 10
[3,] 3 6 9 12 15
[4,] 4 8 12 16 20
[5,] 5 10 15 20 25
try(detach(package:e1071))
try(detach(package:permute))
R markdown - markdown - R code - all posts