# Trump/Clinton poll simulation


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

What is the probability that a sample outcome such as actually observed
(840 Trump, 660 Clinton) would occur by chance if Clinton is “really”
ahead — that is, if Clinton has 50 percent (or more) of the support? To
restate in sharper statistical language: What is the probability that
the observed sample or one even more favorable to Trump would occur if
the universe has a mean of 50 percent or below?

Here is a procedure that responds to that question:

1.  Create a benchmark universe with one ball marked “Trump” and another
    marked “Clinton”
2.  Draw a ball, record its marking, and replace. (We sample with
    replacement to simulate the practically-infinite population of U. S.
    voters.)
3.  Repeat step 2 1500 times and count the number of “Trump”s. If 840 or
    greater, record “Y”; otherwise, record “N.”
4.  Repeat steps 3 and 4 perhaps 1000 or 10,000 times, and count the
    number of “Y”s. The outcome estimates the probability that 840 or
    more Trump choices would occur if the universe is “really” half or
    more in favor of Clinton.

Before we come to the simulation, we need some new code to tune our
histograms (see <a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/probability_theory_3.html#sec-on-histograms"><span>Section 12.15.2</span></a>). We are
going to set the bins for the histogram using advanced ranges.

<div __quarto_custom="true" __quarto_custom_context="Block" __quarto_custom_id="26" __quarto_custom_type="Callout">
<div __quarto_custom_scaffold="true">

Advanced ranges

</div>
<div __quarto_custom_scaffold="true">

So far (<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/resampling_with_code.html#sec-ranges"><span>Section 5.9</span></a>) we have used the
colon syntax to make regular sequences of integers. For example, to make
a vector of the sequential integers from 3 through 12, we could use:</div></div>

```{r}
3:12
```

R also has a function `seq` that makes sequences. In practice we would
likely use the colon syntax for the problem above, but we could also use
`seq`, like this:

```{r}
seq(3, 12)
```

The first argument is the starting value, and the second is the stopping
value. The result is all the sequential integers, up to and including
the stopping value.

Sometimes we want to be able to specify a step size — the gap between
the numbers in the sequence. In the sequence above, the gap (step)
between each number is 1. We might want some other step size. To create
a sequence of integers from 3 through 33 in steps of 5, we could write:

```{r}
seq(3, 33, by=5)
```

Read this as “give me the sequence (range) of numbers, starting at 3, up
to and including 33, in steps(`by`) of 5.

So far we have used integers as the start, stop and step(`by`) values,
but we could also use floating point values. For example, to get a
sequence of values starting at 0.1 up to and including 0.9, in steps of
0.2:

```{r}
seq(0.1, 0.9, by=0.2)
```





With that background, we can proceed with the R implementation of the
simulation procedure.

```{r}
# Number of repeats we will run.
n <- 10000

# Make an array to store the counts.
trumps <- numeric(n)

for (i in 1:n) {
    votes <- sample(c('Trump', 'Clinton'), size=1500, replace=TRUE)
    trumps[i] <- sum(votes == 'Trump')
}

# Integer bins from 670 through 830 in steps of 5.
hist(trumps, breaks=seq(670, 830, by=5),
     main='Number of Trump voters of 1500 in null-world simulation')

# How often >= 840 Trump votes in random draw?
k <- sum(trumps >= 840)
# As a proportion of simulated resamples.
kk <- k / n

message('Proportion voting for Trump: ', kk)
```

The value for `kk` is our estimate of the probability that Trump’s
“victory” in the sample would occur by chance if he really were behind.
In this case, our probability estimate is less than 1 in 10,000 (\&lt;
0.0001).
