ns <- 50 # samples of size 50
nt <- 10^4 # compare 10^4 tests
# pU and pT will contain the p-values of the tests
pU <- rep(nt,0)
pT <- rep(nt,0)
# simulate data and perform tests nt times
for (i in 1:nt) {
# some funny distribution with three modes
xy <- c(-1,0,0,0,1)[1+rbinom(ns,4,0.5)]
y <- rnorm(ns,xy,0.1)
xz <- c(-1,0,0,0,1)[1+rbinom(ns,4,0.5)]
z <- rnorm(ns,xz,0.1)
# perform tests
pT[i] <- t.test(y,z)$p.value
pU[i] <- wilcox.test(y,z)$p.value
}
# plot results of different p values
plot(pT,pU, xlim = c(0,0.3), ylim = c(0,0.3),
xlab = "p value t-test", ylab = "p value Mann-Whitney test",
main = "comparing two different tests", pch = 21 , col = 8, bg = 8, cex = 0.5)
# plotting percentage of points in different regions
lines(c(0.05)*c(1,1),c(0,1), col = 2, lty = 2)
lines(c(0,1), c(0.05)*c(1,1), col = 2, lty = 2)
text(0.025,0.025, paste0(sum100*sum((pT <= 0.05)*(pU <= 0.05))/nt, " %"), cex = 0.7, col= 2 )
text(0.15,0.025, paste0(sum100*sum((pT > 0.05)*(pU <= 0.05))/nt, " %"), cex = 0.7, col= 2 )
text(0.025,0.15, paste0(sum100*sum((pT <= 0.05)*(pU > 0.05))/nt, " %"), cex = 0.7, col= 2 )
# plotting the shape of the population distribution
# from which the samples where drawn
t <- seq(-2,2,0.01)
plot(t,0.5^4*dnorm(t,-1,0.1)+0.5^4*dnorm(t,1,0.1)+(1-0.5^3)*dnorm(t,0,0.1), type = 'l',
xlab = "value", ylab = "density", main = "funny distribution")