pieces

references:
- ISI, exploration 3.4b, p.190
- Reese’s Pieces
- R Programming for Statistics, p.135

library(tidyverse)
library(knitr)

hypotheses

Define \(\pi\)

\[H_0 : \pi = 0.50\] \[H_a : \pi \not= 0.50\]

simulation

pi <- 0.50
n <- 100

Design an experiment: choose “success” (= orange) with probabiity 0.50 … repeat 100 times … report the proportion of successes.

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

Repeat the experiment 10 times.

results <- replicate(10, pieces100())
results
##  [1] 0.40 0.57 0.52 0.48 0.50 0.52 0.50 0.49 0.55 0.43

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, pieces100()))
str(df)
## 'data.frame':    1000 obs. of  1 variable:
##  $ p.hat: num  0.55 0.49 0.53 0.48 0.55 0.43 0.54 0.51 0.5 0.46 ...
ggplot(df, aes(p.hat)) +
  geom_histogram(color = "saddlebrown", fill = "wheat") +
  labs(title = "Simulation")