I am trying to understand properly the significance tests using permutation. I am wondering if this code is correct. Let us say I want to test the difference in mean for mpg
by vs
library(dplyr)
library(mtcars)
mtcars %>% summarise(meandiff = mean( mpg[vs == 1] - mpg[vs == 0] ))
# 7.3
I was thinking of doing the simulation this way
tot = 1:20
vt = vector('list', 1000)
for(i in 1:1000){
num1 = sample(tot, 13)
num2 = tot[!(tot %in% num1)]
vt[[i]] = mtcars$mpg[num1] %>% mean() - mtcars$mpg[num2] %>% mean()
}
unlist(vt) %>% hist(breaks = 100)
So now I want to determine the probability that the difference in mean of 7
occurred by chance only.
Can I use the normal distribution command here?
qnorm(0.95, mean=mean(vta), sd=sd(vta))
# 5
So can I conclude with $\alpha = 5\%$ that a difference in mean of 7 is statistically significant?
From a centile point of view, the $\alpha = 5\%$ can be visualised like this:
vtas = vta %>% sort()
vtas %>% plot
abline(h = vtas[950])
- Is this a correct way to do and interpret this result?
- It seems to me that the permutation test requires less assumptions than other types of test. Is that right?
vs
has no effect, so you can use this directly to determine a p-value. If we believe that the normal approximation is a good one, then we might as well just use a t-test instead of a permutation test. $\endgroup$