SHARE
TWEET

Untitled

a guest May 24th, 2012 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## caracal for
  2. ## http://stats.stackexchange.com/questions/6127/which-permutation-test-implementation-in-r-to-use-instead-of-t-tests-paired-and
  3. ## paired data (two dependent groups)
  4. x1 <- c(99, 99.5, 65, 100, 99, 99.5, 99, 99.5, 99.5, 57, 100, 99.5,
  5.         99.5, 99, 99, 99.5, 89.5, 99.5, 100, 99.5)
  6. y1 <- c(99, 99.5, 99.5, 0, 50, 100, 99.5, 99.5, 0, 99.5, 99.5, 90,
  7.         80, 0, 99, 0, 74.5, 0, 100, 49.5)
  8.  
  9. ## shorten data to something where we can do a complete permutation test
  10. Nexact <- 12
  11. x1 <- x1[1:Nexact]
  12. y1 <- y1[1:Nexact]
  13. DV <- c(x1, y1)
  14. IV <- factor(rep(c("A", "B"), c(length(x1), length(y1))))
  15. id <- factor(rep(1:length(x1), 2))
  16.  
  17. library(lmPerm)                        # for aovp()
  18. summary(aovp(DV ~ IV + Error(id/IV)))  # p-value is far off from correct, see below
  19.  
  20. library(coin)                          # for oneway_test()
  21. oneway_test(DV ~ IV | id, alternative="two.sided", distribution=approximate(B=9999))  # close to correct
  22.  
  23. ## exact test (two-sided) for correct permutation p-value
  24. DVd    <- y1-x1
  25. sgnLst <- lapply(numeric(Nexact), function(x) { c(-1, 1) } )
  26. sgnMat <- as.matrix(expand.grid(sgnLst))
  27. getMD  <- function(x) { mean(abs(DVd) * x) }
  28. resMD  <- apply(sgnMat, 1, getMD)
  29. (pVal  <- sum(abs(resMD) >= abs(mean(DVd))) / length(resMD))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top