Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## caracal for
- ## http://stats.stackexchange.com/questions/6127/which-permutation-test-implementation-in-r-to-use-instead-of-t-tests-paired-and
- ## paired data (two dependent groups)
- x1 <- c(99, 99.5, 65, 100, 99, 99.5, 99, 99.5, 99.5, 57, 100, 99.5,
- 99.5, 99, 99, 99.5, 89.5, 99.5, 100, 99.5)
- y1 <- c(99, 99.5, 99.5, 0, 50, 100, 99.5, 99.5, 0, 99.5, 99.5, 90,
- 80, 0, 99, 0, 74.5, 0, 100, 49.5)
- ## shorten data to something where we can do a complete permutation test
- Nexact <- 12
- x1 <- x1[1:Nexact]
- y1 <- y1[1:Nexact]
- DV <- c(x1, y1)
- IV <- factor(rep(c("A", "B"), c(length(x1), length(y1))))
- id <- factor(rep(1:length(x1), 2))
- library(lmPerm) # for aovp()
- summary(aovp(DV ~ IV + Error(id/IV))) # p-value is far off from correct, see below
- library(coin) # for oneway_test()
- oneway_test(DV ~ IV | id, alternative="two.sided", distribution=approximate(B=9999)) # close to correct
- ## exact test (two-sided) for correct permutation p-value
- DVd <- y1-x1
- sgnLst <- lapply(numeric(Nexact), function(x) { c(-1, 1) } )
- sgnMat <- as.matrix(expand.grid(sgnLst))
- getMD <- function(x) { mean(abs(DVd) * x) }
- resMD <- apply(sgnMat, 1, getMD)
- (pVal <- sum(abs(resMD) >= abs(mean(DVd))) / length(resMD))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement