set.seed(1)
nsim_pval=300
for (h in seq(10, 10, 10)) {
p_value=vector("numeric",nsim_pval)
null=vector("numeric",300)
for (k in 1:nsim_pval ){
obs = mean(sample(population, h, replace = TRUE)) -
mean(sample(population, h, replace = TRUE))
for (i in 1:300 ) {
# control= sample(population, h)
# treatment= sample(population, h)
control= sample(population, h, replace = TRUE)
treatment= sample(population, h, replace = TRUE)
null[i] =mean(treatment)- mean(control)
}
# print(abs(null))
p_value[k]=mean(null >= obs)
ppp=mean(p_value)
nnn=mean(null)
}
hist(p_value, main=paste("N=", h, "\n mean p-value=", ppp, "\n n
pvalue=",nsim_pval))
}
below is some code for a permutation test based on the original control and treatment data (not the population data). It will give a p-value of ~0.045051 which is close to the 0.0519 value you got from your 2 tail t.test
control= subset(fem, Diet=="chow", select = "Bodyweight")
treatment= subset(fem, Diet=="hf", select = "Bodyweight")
obs = mean(treatment$Bodyweight) - mean(control$Bodyweight)
combined = c(treatment$Bodyweight, control$Bodyweight)
fn <- function(x) {
return(mean(temp = sample(combined,12,24)
replace = TRUE)return( mean(temp[1:12]) - mean(sample(combined,12, replace = TRUE) temp[13:24]) )
}
tmp = sapply(1:10000,fn)
mean(abs(tmp)>=obs) ## two sided p-value