wants <- c("e1071", "permute")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
\({n \choose k}\)
[1] 5
[1] 5
[,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"
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
[1] 6 7 8 9
[1] 1.8 2.1 2.3 2.8
[1] 5040
[1] "C" "J" "B" "H" "F" "I" "A" "G" "E" "D"
[1] 10 5 3 8 1 4 6 9 7 2
[,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
[,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"
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
N <- length(grp)
nPerms <- 100
library(permute)
pCtrl <- how(nperm=nPerms, complete=FALSE)
for(i in 1:5) {
perm <- permute(i, n=N, control=pCtrl)
print(grp[perm])
}
[1] "c" "a" "c" "c" "a" "b" "a" "b" "b"
[1] "a" "a" "c" "b" "b" "c" "a" "c" "b"
[1] "c" "a" "a" "a" "b" "c" "b" "b" "c"
[1] "b" "a" "a" "b" "c" "b" "a" "c" "c"
[1] "a" "c" "a" "c" "b" "c" "b" "b" "a"
Permute group membership in a two-way ANOVA design. Here: artificially low number of permutations.
Njk <- 4 ## cell size
P <- 2 ## levels factor A
Q <- 3 ## levels factor B
N <- Njk*P*Q
nPerms <- 10 ## number of permutations
id <- 1:(Njk*P*Q)
IV1 <- factor(rep(1:P, each=Njk*Q)) ## factor A
IV2 <- factor(rep(1:Q, times=Njk*P)) ## factor B
(myDf <- data.frame(id, IV1, IV2))
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)
pCtrlA <- how(plots=Plots(strata=IV2), complete=FALSE, nperm=nPerms)
## only permute across B (within A)
pCtrlB <- how(plots=Plots(strata=IV1), complete=FALSE, nperm=nPerms)
Get permutations for test of factor A
Get permutations for test of factor B
All combinations of elements from \(n\) different sets (cartesian product)
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
[,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
R markdown - markdown - R code - all posts