# Enter number corresponding to UID
set.seed(551346)
samp = runif(n = 40, min = -100, max = 100)
hist(samp)
Sampled observations must be randomly sampled and independent. The population distribution must be nearly normal or n > 30 and the population distribution is not extremely skewed. If we are using a sample proportion, the number of successes and failures must both be greater than 15.
Hnull: true mean = 0 Halternative: true mean is not equal to 0
mean(samp)
## [1] 0.2934
s = sd(samp)
(0.293 - 0)/(s/sqrt(40))
## [1] 0.031
The mean is .293, the standard deviation is 59.8, and the test statistic is 0.031.
2 * pnorm(-0.031, 0, 59.786)
## [1] 0.9996
Because our p-value of .9996 is above our significant level of 0.05, we do not reject the null hypothesis. Note that we doubled the p-value because the test is two sided. Anyone who did reject it made a Type I error, rejecting the null when the null is true.
bigN = 20000
n = 40
sample_means <- rep(0, bigN)
T.star = rep(0, bigN)
mu = 0
for (i in 1:bigN) {
samp <- runif(n, -100, 100)
sample_means[i] <- mean(samp)
T.star[i] <- (sample_means[i] - mu)/(sd(samp)/sqrt(n))
}
hist(sample_means)
mean(sample_means)
## [1] 0.03915
sd(sample_means)
## [1] 9.084
hist(T.star)
mean(T.star)
## [1] 0.004209
sd(T.star)
## [1] 1.023
Both distributions appear normally distributed with a mean of approximately 0. The sample means have a standard deviation of 9.084 and the t-statistics have a standard deviation of 1.023.
qnorm(0.025)
## [1] -1.96
reject <- subset(T.star, T.star < -1.96 | T.star > 1.96)
hist(reject)
length(reject)
## [1] 1140
At alpha = 0.05 significant level, we would expect 5% of the tests to reject the null hypothesis. Because the test is two sided, we can only reject when the probability to getting such or more extreme sample is 0.025. This agrees with our data, where we rejected the null 1140 times or 1140/200=5.7% of the time.