references:
- ISI, example 3.5a, p.194
- Tom Bradley (American politician), Wikipedia

library(tidyverse)
library(knitr)

## hypotheses

Define $$\pi$$. See ISI, p.195.

$H_0 : \pi = 0.3645$ $H_a : \pi > 0.3645$

## simulation

pi <- 0.3645
n <- 778

Design an experiment: choose “success” with probabiity 0.3645 … repeat 778 times … report the proportion of successes.

Bradley778 <- function(){
samp <- sample(0:1, size = n, prob = c(1 - pi, pi), replace = TRUE)
p.hat <- mean(samp)
return(p.hat)
}

Repeat the experiment 10 times.

results <- replicate(10, Bradley778())
results
##  [1] 0.3483290 0.3701799 0.3958869 0.3701799 0.3714653 0.3598972 0.3830334
##  [8] 0.3251928 0.3457584 0.3894602

## sampling distribution of $$\widehat{p}$$

Repeat the experiment 1000 times and display the results.

n.experiments <- 1000
df <- data.frame(p.hat = replicate(n.experiments, Bradley778()))
str(df)
## 'data.frame':    1000 obs. of  1 variable:
##  \$ p.hat: num  0.348 0.383 0.407 0.365 0.361 ...
ggplot(df, aes(p.hat)) +
geom_histogram(color = "saddlebrown", fill = "wheat") +
labs(title = "Simulation")