# Sample size for detecting fruitfly sex difference


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

We are interested in a procedure to change the sex ratio of fruit flies
to give more males. We are going to take samples of 80 flies. We know
that, in order to detect a convincing difference from a 50:50 ratio, we
need 48 of 80, or more, to be male. We suspect that the treatment gives
a 60:40 ratio. Drawing random samples in this 60:40 universe, how often
do we get the convincing \&gt;=48 male count? In other words, for how many
trials in this situation, will we see a convincing answer?

```{r}
n_trials <- 10000

# Results for each trial.
results <- numeric(n_trials)

# Do 10,000 trials
for (i in 1:n_trials) {
    # Generate 80 "flies" with 0.6 chance of male, 0.4 of female>
    flies <- sample(c('male', 'female'), size=80, replace=TRUE, prob=c(0.6, 0.4))
    # Count the males.
    n_males <- sum(flies == 'male')
    # Keep score.
    results[i] <- n_males
}

# How many of the trials produced more than 48 males?
k <- sum(results >= 48)
# Convert to a proportion
kk <- k / n_trials

message('Proportion of 60/40 trials giving >= 48 males: ', kk)
```

If the result `kk` is close to 1, we then know that samples of size 80
will almost always produce samples with enough males to avoid misleading
us into thinking that they could have come from a universe in which
males and females are split 50-50.
