## Chapter 2 
##
## Code to support Video Vignette
##
## Videos and supporting code are not a complete portrayal
## of Chapter content.

## heights of women from class
y <- c(65, 65, 69, 67, 66, 66, 67, 64, 68, 68, 68, 69, 70, 65, 70, 
       67, 65, 62, 69, 68, 65, 68, 64, 64)
n <- length(y)

## what google says is typical of American women
mu <- 64.5          ## mu_0 from the notes
sigma2 <- 2.5^2     ## assume known

## observed statistic; book uses s <- mean(y)
s <- sum(y) 

## MC Simulation for H0: mu = 64.5a
N <- 10000
Ss <- rep(NA, N) 
for(i in 1:N) { 
  Ys <- rnorm(n, mu, sqrt(sigma2))  ## only change: generate n Gaussian samps
  Ss[i] <- sum(Ys)                  ## book uses Ss[i] <- mean(Ys)
}

## visualizing simulation
hist(Ss, main="", xlim=range(s, 2*n*mu - s, Ss)) 
abline(v=c(s, 2*n*mu - s), lty=1:2, col=2, lwd=2)
legend(1570, 1650, c("s", "reflect"), col=2, lty=1:2, lwd=2, bty="n")

## p-value calculation
pval <- 2*mean(Ss >= s)
pval
