TE(T)=0
TTempT⋆♯(⋅)♯(T⋆)ppts=pleft+pright
pleft=♯(T⋆<=min(Temp,−Temp))♯(T⋆)
pright=♯(T⋆>=max(Temp,−Temp))♯(T⋆)
(suponiendo que tengamos la distribución completa de permutación). Comparemos ambos enfoques para el caso de dos muestras independientes cuando podamos calcular la distribución exacta (completa) de permutación.
set.seed(1234)
Nj <- c(9, 8) # group sizes
DVa <- rnorm(Nj[1], 5, 20)^2 # data group 1
DVb <- rnorm(Nj[2], 10, 20)^2 # data group 2
DVab <- c(DVa, DVb) # data from both groups
IV <- factor(rep(c("A", "B"), Nj)) # grouping factor
idx <- seq(along=DVab) # all indices
idxA <- combn(idx, Nj[1]) # all possible first groups
# function to calculate test statistic for a given permutation x
getDM <- function(x) { mean(DVab[x]) - mean(DVab[!(idx %in% x)]) }
resDM <- apply(idxA, 2, getDM) # test statistic for all permutations
diffM <- mean(DVa) - mean(DVb) # empirical stest statistic
pcoin
pleft≠prightpts
> (pL <- sum(resDM <= min(diffM, -diffM)) / length(resDM)) # left p-value
[1] 0.1755245
> (pR <- sum(resDM >= max(diffM, -diffM)) / length(resDM)) # right p-value
[1] 0.1585356
> 2*pL # doubling left p-value
[1] 0.351049
> 2*pR # doubling right p-value
[1] 0.3170712
> pL+pR # two-sided p-value
[1] 0.3340601
> sum(abs(resDM) >= abs(diffM)) / length(resDM) # two-sided p-value (more concise)
[1] 0.3340601
# validate with coin implementation
> library(coin) # for oneway_test()
> oneway_test(DVab ~ IV, alternative="two.sided", distribution="exact")
Exact 2-Sample Permutation Test
data: DVab by IV (A, B)
Z = 1.0551, p-value = 0.3341
alternative hypothesis: true mu is not equal to 0
p
pleft=♯(T⋆<=min(Temp,−Temp))+1♯(T⋆)+1
pright=♯(T⋆>=max(Temp,−Temp))+1♯(T⋆)+1
pts=♯(abs(T⋆)>=abs(Temp))+1♯(T⋆)+1
p