c("coin", "e1071")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
Not limited to just two independent samples.
coin
set.seed(123)
c(7, 8)
Nj <- 20
sigma <- round(rnorm(Nj[1], 100, sigma))
DVa <- round(rnorm(Nj[2], 110, sigma))
DVb <- data.frame(DV=c(DVa, DVb),
tIndDf <-IV=factor(rep(c("A", "B"), Nj)))
library(coin)
oneway_test(DV ~ IV, alternative="less", data=tIndDf, distribution="exact")) (ot <-
Exact 2-Sample Permutation Test
data: DV by IV (A, B)
Z = 0.1356, p-value = 0.5602
alternative hypothesis: true mu is less than 0
Compare with parametric \(t\)-test
t.test(DV ~ IV, alternative="less", var.equal=TRUE, data=tIndDf)
tRes <-$p.value tRes
[1] 0.5510091
seq(along=tIndDf$DV)
idx <- combn(idx, Nj[1])
idxA <- function(x) { mean(tIndDf$DV[!(idx %in% x)]) - mean(tIndDf$DV[x]) }
getDM <- apply(idxA, 2, getDM)
resDM <- diff(tapply(tIndDf$DV, tIndDf$IV, mean))
diffM <-
# don't use <= because of floating point arithmetic problems
apply(idxA, 2, getDM)
DMstar <- mean(DVa) - mean(DVb)
DMbase <- .Machine$double.eps^0.5
tol <- (DMstar < DMbase) | (abs(DMstar-DMbase) < tol)
DMsIsLEQ <- sum(DMsIsLEQ) / length(DMstar)) (pVal <-
[1] 0.55338
Check density of permutation distribution.
support(ot)
supp <- sapply(supp, dperm, object=ot)
dens <-plot(supp, dens, xlab="Support", ylab=NA, pch=20, main="Density permutation distribution")
QQ-plot against standard normal distribution.
sapply(ppoints(supp), qperm, object=ot)
qEmp <-qqnorm(qEmp, xlab="Normal quantiles", ylab="Permutation quantiles",
pch=20, main="Permutation quantiles vs. normal quantiles")
abline(a=0, b=1, lwd=2, col="blue")
Empirical cumulative distribution function.
plot(qEmp, ecdf(qEmp)(qEmp), col="gray60", pch=16,
xlab="Difference in means", ylab="Cumulative relative frequency",
main="Cumulative relative frequency and normal CDF")
Not limited to just two dependent samples.
coin
12
N <- factor(rep(1:N, times=2))
id <- rnorm(N, 100, 20)
DVpre <- rnorm(N, 110, 20)
DVpost <- data.frame(DV=c(DVpre, DVpost),
tDepDf <-IV=factor(rep(0:1, each=N), labels=c("pre", "post")))
library(coin)
oneway_test(DV ~ IV | id, alternative="less", distribution=approximate(B=9999), data=tDepDf)
Approximative 2-Sample Permutation Test
data: DV by IV (pre, post)
stratified by id
Z = -2.1232, p-value = 0.0138
alternative hypothesis: true mu is less than 0
t.test(DV ~ IV, alternative="less", paired=TRUE, data=tDepDf)$p.value
[1] 0.0129621
DVpre - DVpost
DVd <- lapply(numeric(N), function(x) { c(-1, 1) } )
sgnLst <- data.matrix(expand.grid(sgnLst))
sgnMat <- function(x) { mean(abs(DVd) * x) }
getMD <- apply(sgnMat, 1, getMD)
MDstar <- mean(DVd)
MDbase <-
# don't use <= because of floating point arithmetic problems
.Machine$double.eps^0.5
tol <- (MDstar < MDbase) | (abs(MDstar-MDbase) < tol)
MDsIsLEQ <- sum(MDsIsLEQ) / length(MDstar)) (pVal <-
[1] 0.01391602
8
Nf <- rbinom(Nf, size=1, prob=0.5)
DV1 <- rbinom(Nf, size=1, prob=0.5)
DV2 <-fisher.test(DV1, DV2, alternative="greater")$p.value
[1] 0.6428571
library(e1071)
permutations(Nf)
permIdx <- function(idx) { sum(diag(table(DV1, DV2[idx]))) }
getAgree <- apply(permIdx, 1, getAgree)
resAgree <- sum(diag(table(DV1, DV2)))
agree12 <- sum(resAgree >= agree12) / length(resAgree)) (pVal <-
[1] 0.6428571
Packages resample
and vegan
provide more ways to implement flexible permutation strategies for various designs.
try(detach(package:e1071))
try(detach(package:coin))
try(detach(package:survival))
try(detach(package:splines))
R markdown - markdown - R code - all posts