pval_dist <- function(N.samples, sample.size, mean){
# declare vector to save sample means
sample.means <- vector()
# Loop from 1 to the number of samples
for ( i in 1:N.samples){
# save each mean to the corresponding position in the vector
sample.means[i] <- mean(rnorm(sample.size,mean,1))
}
# calculate the p-value using the sampling distribution
# Note the standard deviation for the sampling distribution (if your histogram did not look uniform this is probably the part where it went wrong)
pvals <- pnorm(sample.means,mean = 0,sd=sqrt(1/sample.size))
# check how many times we rejected the null by using the arbitrary threshold 0.05
reject <- sum(pvals < 0.05)
# Print message
cat("Null Hypothesis is rejected in ",reject ," out of",N.samples," cases","(",100 * reject/N.samples ,"% )", "\n")
# Plot histogram and scatter plot
par(mfrow=c(1,2))
hist(pvals, main = paste("N.samples = ",N.samples,"/ N=",sample.size,"~ N(0,1)",sep =" "), xlab = "P-values", col = "skyblue")
plot(pvals, ylab = "P-values", col = "skyblue")
}
pval_dist(250,200,0)