Advertisement
celestialgod

two sample cvm.test

Oct 27th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.91 KB | None | 0 0
  1. library(data.table)
  2.  
  3. cvm.test <- function(x, y) {
  4.   DT <- data.table(value = c(sort(x), sort(y)), id = rep(1:2, c(length(x), length(y))),
  5.                    rank = c(1:length(x), 1:length(y)))
  6.   DT[ , overall_rank := rank(DT$value)]
  7.   statDT <- DT[ , .(U = sum((rank - overall_rank)**2), N = .N), by = .(id)]
  8.   prod_N <- product(statDT$N)
  9.   sum(statDT$N * statDT$U) / (prod_N * nrow(DT)) - (4 * prod_N - 1) / 6 / nrow(DT)
  10. }
  11.  
  12. mean(sapply(1:1e4, function(i){
  13.   X <- rnorm(40, 4, 7)
  14.   Y <- rnorm(20, 4, 7)
  15.   cvm.test(X, Y) > 0.461
  16. }))
  17. # 0.0513
  18.  
  19. mean(sapply(1:1e4, function(i){
  20.   X <- rnorm(40, 4, 7)
  21.   Y <- rnorm(20, 4, 10)
  22.   cvm.test(X, Y) > 0.461
  23. }))
  24. # 0.0913
  25.  
  26. mean(sapply(1:1e4, function(i){
  27.   X <- rnorm(40, 4, 7)
  28.   Y <- rnorm(20, 4, 14)
  29.   cvm.test(X, Y) > 0.461
  30. }))
  31. # 0.2315
  32.  
  33. mean(sapply(1:1e4, function(i){
  34.   X <- rnorm(40, 4, 7)
  35.   Y <- rnorm(20, 8, 7)
  36.   cvm.test(X, Y) < 0.461
  37. }))
  38. # 0.4832
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement