rm(list=ls(all=TRUE))  # clear all variables
# Programmed by Greg Francis (gfrancis@purdue.edu) October 2019

# sample size
n = 100

PopulationMean = c(0.0) 
PopulationSD = c(1)
alpha = 0.05  # Type I erorr rate (must be bigger than 0 and less than 1)

numRepeats = 20000

pValues <-0*c(1:numRepeats) # To hold result of t-test

for(rep in c(1:numRepeats)){

	# Draw a random sample
     Xvalues  <- rnorm(n, mean= PopulationMean[1], sd= PopulationSD[1])

	testResults <- t.test(Xvalues, mu=0, conf.level = (1-alpha))
	
	pValues[rep] <- testResults$p.value
	
}

# Compute proportion of significant test results
propSignificant <-length(pValues[pValues <= alpha])/length(pValues) 

cat("------\n")

cat("Proportion significant tests: ", propSignificant, "\n")


# Produce histogram of p-values
plot(hist(pValues, breaks=100), xlim = c(0,1))



