# Sample size for an internet contract poll


Find this notebook on the web at
<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/how_big_sample.html#nte-poll_sample_size">Note <span>30.5</span></a>.

We want to know how many voters we should survey to be confident that
the ratio that approve a proposal (to give an internet contract) has
better than 50:50 approval. We are thinking of taking a sample size of
50. We believe that the actual proportion in favor is 70%. We have
estimated that we can be confident the sample does not reflect a 50:50
universe if there are 32 or more voters in favor of the proposal. For
the 70:30 universe, how many simulated samples give us this value of 32,
or greater?

```{r}
# Number of trials.
n_trials <- 10000

# Make array to store results for each trial.
results <- numeric(n_trials)

# Do 10,00 trials
for (i in 1:n_trials) {
    # Generate 50 voters with 70% chance of "yes“.
    voters <- sample(c("yes", "no"), size=50, replace=TRUE, prob=c(0.7, 0.3))
    # Count the "yeses".
    n_yes <- sum(voters == 'yes')
    # Keep score of the result.
    results[i] <- n_yes
}

# Count how often the sample result >= our 32 cutoff (recall that samples
# with 31 or fewer "yeses" cannot be ruled out of a 50/50 universe).
k <- sum(results >= 32)
# Convert to a proportion
kk <- k / n_trials

message('p of 50 voter samples in 70:30 universe >= 32 "yes": ', kk)
```
