Guest User

Untitled

a guest
Jul 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.65 KB | None | 0 0
  1. library(ggplot2)
  2. library(perm)
  3.  
  4. testWith <- function(n, mu, tst)
  5. {
  6.         pvals <- replicate(400, tst(rnorm(n, 0, 1), rnorm(n, mu, 1))$p.value)
  7.         data.frame(mean(pvals), mean(pvals < 0.05))
  8. }
  9.  
  10. tbl <- data.frame()
  11. for(mu in seq(-2, 2, 0.02)) for(n in seq(10, 100, 5)) { tbl <- rbind(tbl, data.frame(mu=mu, n=n)) }
  12. wres <- cbind(tbl, do.call(rbind, apply(tbl, c(1), function(row) testWith(row[2], row[1], wilcox.test))))
  13. tres <- cbind(tbl, do.call(rbind, apply(tbl, c(1), function(row) testWith(row[2], row[1], permTS))))
  14.  
  15. diff <- tbl
  16. diff$pvals <- tres$mean.pvals. - wres$mean.pvals.
  17. diff$power <- tres$mean.pvals...0.05. - wres$mean.pvals...0.05.
  18.  
  19. svg("tres_pvals.svg")
  20. ggplot(tres, aes(x=n, y=mu)) + geom_tile(aes(fill=mean.pvals.)) + scale_fill_gradientn(colours = rainbow(7))
  21. dev.off()
  22. svg("tres_power.svg")
  23. ggplot(tres, aes(x=n, y=mu)) + geom_tile(aes(fill=mean.pvals...0.05.)) + scale_fill_gradientn(colours = rainbow(7))
  24. dev.off()
  25.  
  26. svg("wres_pvals.svg")
  27. ggplot(wres, aes(x=n, y=mu)) + geom_tile(aes(fill=mean.pvals.)) + scale_fill_gradientn(colours = rainbow(7))
  28. dev.off()
  29. svg("wres_power.svg")
  30. ggplot(wres, aes(x=n, y=mu)) + geom_tile(aes(fill=mean.pvals...0.05.)) + scale_fill_gradientn(colours = rainbow(7))
  31. dev.off()
  32.  
  33. svg("diff_pvals.svg")
  34. ggplot(diff, aes(x=n, y=mu)) + geom_tile(aes(fill=pvals)) + scale_fill_gradientn(colours = rainbow(7))
  35. dev.off()
  36. svg("diff_power.svg")
  37. ggplot(diff, aes(x=n, y=mu)) + geom_tile(aes(fill=power)) + scale_fill_gradientn(colours = rainbow(7))
  38. dev.off()
  39.  
  40. print(sprintf("diff pvals: max %f, avg %f; diff power: max %f, avg %f", max(diff$pvals), mean(diff$pvals), max(diff$power), mean(diff$power)))
Add Comment
Please, Sign In to add comment