1.
Why is it important to mix the balls in the bowl before we take a new sample?
Answer.
To randomize selection and reduce dependence or bias between samples.

bowl has 2400 rows representing the 2400 balls in the bowl shown in FigureΒ 7.1.1. You can view and scroll through the entire contents of the bowl in RStudioβs data viewer by running View(bowl). The first variable ball_ID is used as an identification variable; none of the balls in the actual bowl are marked with numbers. The second variable color indicates whether a particular virtual ball is red or white.
bowl
# A tibble: 2,400 Γ 2
ball_ID color
<int> <chr>
1 1 white
2 2 white
3 3 white
4 4 red
5 5 white
6 6 white
7 7 red
8 8 white
9 9 red
10 10 white
# βΉ 2,390 more rows
TRUE if a ball is red and FALSE otherwise. The variable is_red returns the Boolean (logical) value TRUE for each row where color == "red" and FALSE for every row where color is not equal to "red". Since R treats TRUE like the number 1 and FALSE like the number 0, accounting for TRUEs and FALSEs is equivalent to working with 1βs and 0βs. In particular, adding all the 1βs and 0βs is equivalent to counting how many red balls are in the bowl.
bowl |>
mutate(is_red = (color == "red"))
# A tibble: 2,400 Γ 3
ball_ID color is_red
<int> <chr> <lgl>
1 1 white FALSE
2 2 white FALSE
3 3 white FALSE
4 4 red TRUE
5 5 white FALSE
6 6 white FALSE
7 7 red TRUE
8 8 white FALSE
9 9 red TRUE
10 10 white FALSE
# βΉ 2,390 more rows
sum() function inside the summarize() function. Recall from Section on summarize() that summarize() takes a data frame with many rows and returns a data frame with a single row containing summary statistics such as the sum():
bowl |>
mutate(is_red = (color == "red")) |>
summarize(num_red = sum(is_red))
# A tibble: 1 Γ 1
num_red
<int>
1 900
sum() has added all the 1βs and 0βs and has effectively counted the number of red balls. There are 900 red balls in the bowl. Since the bowl contains 2400 balls, the proportion of red balls is 900/2400 = 0.375. We could ask R to find the proportion directly by replacing the sum() for the mean() function inside summarize(). The average of 1βs and 0βs is precisely the proportion of red balls in the bowl:
bowl |>
mutate(is_red = (color == "red")) |>
summarize(prop_red = mean(is_red))
# A tibble: 1 Γ 1
prop_red
<dbl>
1 0.375
is_red before finding the proportion, we could write both steps simultaneously in a single line of code:
bowl |>
summarize(prop_red = mean(color == "red"))
# A tibble: 1 Γ 1
prop_red
<dbl>
1 0.375







tactile_prop_red data frame included in the moderndive package. The first 10 rows are given:
tactile_prop_red
# A tibble: 33 Γ 4 group replicate red_balls prop_red <chr> <int> <int> <dbl> 1 Ilyas, Yohan 1 21 0.42 2 Morgan, Terrance 2 17 0.34 3 Martin, Thomas 3 21 0.42 4 Clark, Frank 4 21 0.42 5 Riddhi, Karina 5 18 0.36 6 Andrew, Tyler 6 19 0.38 7 Julia 7 19 0.38 8 Rachel, Lauren 8 11 0.22 9 Daniel, Caroline 9 15 0.3 10 Josh, Maeve 10 17 0.34 # βΉ 23 more rows
group the data frame provides their names, the number of red_balls observed in the sample, and the calculated proportion of red balls in the sample, prop_red. We also have a replicate variable enumerating each of the 33 groups. We chose this name because each row can be viewed as one instance of a replicated (in other words "repeated") activity.
ggplot() with geom_histogram(). To align the bins in the computerized histogram version so it matches the hand-drawn histogram shown in FigureΒ 7.1.6, the arguments boundary = 0.4 and binwidth = 0.05 were used. The former indicates that we want a binning scheme, such that, one of the binsβ boundaries is at 0.4; the latter fixes the width of the bin to 0.05 units.
ggplot(tactile_prop_red, aes(x = prop_red)) +
geom_histogram(binwidth = 0.05, boundary = 0.4, color = "white") +
labs(x = "Proportion of red balls in each sample",
title = "Histogram of 33 proportions")

rep_slice_sample() function from the moderndive package.
bowl included in the moderndive package. The virtual analog to the 50-ball shovel seen in FigureΒ 7.1.2 can be achieved using the rep_slice_sample() function included in the moderndive package. This function allows us to take repeated (or replicated) random samples of size n. We start by taking a single sample of 50 balls:
virtual_shovel <- bowl |>
rep_slice_sample(n = 50)
virtual_shovel
# A tibble: 50 Γ 3
ball_ID color replicate
<int> <chr> <int>
1 155 white 1
2 2369 white 1
3 2515 red 1
4 826 white 1
5 886 white 1
6 2433 red 1
7 1794 white 1
8 1400 white 1
9 2266 red 1
10 2664 red 1
# βΉ 40 more rows
virtual_shovel has 50 rows corresponding to our virtual sample of size 50. The ball_ID variable identifies which of the 2400 balls from bowl are included in our sample of 50 balls while color denotes whether its white or red. The replicate variable is equal to 1 for all 50 rows because we have decided to take only one sample right now. Later on, we take more samples, and replicate will take more values.
virtual_shovel |>
summarize(prop_red = mean(color == "red"))
# A tibble: 1 Γ 1
prop_red
<dbl>
1 0.36
virtual_shovelβs 50 balls were red! We proceed finding the sample proportion for more random samples.
rep_slice_sample() and this time adding the reps = 33 argument as we want to retrieve 33 random samples. We save these samples in the data frame virtual_samples, as shown, and then provide a preview of its first 10 rows. If you want to inspect the entire virtual_samples data frame, use RStudioβs data viewer by running View(virtual_samples).
virtual_samples <- bowl |>
rep_slice_sample(n = 50, reps = 33)
virtual_samples
# A tibble: 1,650 Γ 3
ball_ID color replicate
<int> <chr> <int>
1 1399 white 1
2 2031 white 1
3 979 red 1
4 1660 white 1
5 596 white 1
6 2582 red 1
7 1357 white 1
8 2081 white 1
9 596 white 1
10 1020 white 1
# βΉ 1,640 more rows
replicate are equal to 1, the next 50 rows of replicate are equal to 2, and so on. The first 50 rows correspond to the first sample of 50 balls while the next 50 rows correspond to the second sample of 50 balls. This pattern continues for all reps = 33 replicates, and thus virtual_samples has 33 Β· 50 = 1650 rows.
virtual_samples we find the proportion of red balls for each replicate. We use the same dplyr verbs as before. In particular, we add group_by() of the replicate variable. Recall that by assigning the grouping variable "meta-data" before summarize(), we perform the calculations needed for each replicate separately. The other line of code, as explained in the case of one sample, calculates the sample proportion of red balls. A preview of the first 10 rows is presented:
virtual_prop_red <- virtual_samples |>
group_by(replicate) |>
summarize(prop_red = mean(color == "red"))
virtual_prop_red
# A tibble: 33 Γ 2
replicate prop_red
<int> <dbl>
1 1 0.42
2 2 0.40
3 3 0.36
4 4 0.34
5 5 0.36
6 6 0.30
7 7 0.36
8 8 0.30
9 9 0.42
10 10 0.40
# βΉ 23 more rows
rep_slice_sample() already groups the data by replicate, so it is not necessary to include group_by() in the code. Moreover, using dplyr pipes in R we could simplify the work and write everything at once:rep_slice_sample(), we have 33 replicates (each being a random sample of 50 balls) and
summarize() with mean() on the Boolean values, we determine the proportion of red balls for each sample.
virtual_prop_red and print the first 10 sample proportions (for the first 10 samples) as an illustration:
virtual_prop_red <- bowl |>
rep_slice_sample(n = 50, reps = 33) |>
summarize(prop_red = mean(color == "red"))
virtual_prop_red
# A tibble: 33 Γ 2
replicate prop_red
<int> <dbl>
1 1 0.42
2 2 0.40
3 3 0.36
4 4 0.34
5 5 0.36
6 6 0.30
7 7 0.36
8 8 0.30
9 9 0.42
10 10 0.40
# βΉ 23 more rows
ggplot(), geom_histogram(), and including the arguments binwidth = 0.05 and boundary = 0.4.
ggplot(virtual_prop_red, aes(x = prop_red)) +
geom_histogram(binwidth = 0.05, boundary = 0.4, color = "white") +
labs(x = "Sample proportion",
title = "Histogram of 33 sample proportions")


rep_slice_sample() function with a sample size set to be 50. This time, however, we set the number of replicates (reps) to 1000, and use summarize() and mean() again on the Boolean values to calculate the sample proportions. We compute virtual_prop_red with the count of red balls and the corresponding sample proportion for all 1000 random samples. The proportions for the first 10 samples are shown:
virtual_prop_red <- bowl |>
rep_slice_sample(n = 50, reps = 1000) |>
summarize(prop_red = mean(color == "red"))
virtual_prop_red
# A tibble: 1,000 Γ 2
replicate prop_red
<int> <dbl>
1 1 0.36
2 2 0.40
3 3 0.32
4 4 0.36
5 5 0.40
6 6 0.30
7 7 0.38
8 8 0.40
9 9 0.38
10 10 0.36
# βΉ 990 more rows
ggplot(virtual_prop_red, aes(x = prop_red)) +
geom_histogram(binwidth = 0.04, boundary = 0.4, color = "white") +
labs(x = "Sample proportion", title = "Histogram of 1000 sample proportions")


# Segment 1: sample size = 25 ------------------------------
# 1.a) Compute sample proportions for 1000 samples, each sample of size 25
virtual_prop_red_25 <- bowl |>
rep_slice_sample(n = 25, reps = 1000) |>
summarize(prop_red = mean(color == "red"))
# 1.b) Plot a histogram to represent the distribution of the sample proportions
ggplot(virtual_prop_red_25, aes(x = prop_red)) +
geom_histogram(binwidth = 0.05, boundary = 0.4, color = "white") +
labs(x = "Proportion of 25 balls that were red", title = "25")
# Segment 2: sample size = 50 ------------------------------
# 2.a) Compute sample proportions for 1000 samples, each sample of size 50
virtual_prop_red_50 <- bowl |>
rep_slice_sample(n = 50, reps = 1000) |>
summarize(prop_red = mean(color == "red"))
# 2.b) Plot a histogram to represent the distribution of the sample proportions
ggplot(virtual_prop_red_50, aes(x = prop_red)) +
geom_histogram(binwidth = 0.05, boundary = 0.4, color = "white") +
labs(x = "Proportion of 50 balls that were red", title = "50")
# Segment 3: sample size = 100 ------------------------------
# 3.a) Compute sample proportions for 1000 samples, each sample of size 100
virtual_prop_red_100 <- bowl |>
rep_slice_sample(n = 100, reps = 1000) |>
summarize(prop_red = mean(color == "red"))
# 3.b) Plot a histogram to represent the distribution of the sample proportions
ggplot(virtual_prop_red_100, aes(x = prop_red)) +
geom_histogram(binwidth = 0.05, boundary = 0.4, color = "white") +
labs(x = "Proportion of 100 balls that were red", title = "100")
