AI Features

Virtual Sampling

Learn about virtual sampling and develop an understanding through practical demonstrations.

We performed this sampling activity by hand first so that we can develop a firm understanding of the root ideas behind sampling. Now, we’ll mimic this tactile sampling activity with a virtual sampling activity. In other words, we’ll use a virtual analog to the bowl of balls and a virtual analog to the shovel.

Using the virtual shovel once

Let’s start by performing the virtual analog of the tactile sampling exercise we performed. We first need a virtual analog of the bowl. To this end, we included a data frame named bowl in the moderndive package. The rows of bowl correspond exactly with the contents of the actual bowl.

R
bowl

Observe that bowl has 2,400 rows, which tells us that the bowl contains 2,400 equally sized balls. 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. We’ll view the contents of the bowl and scroll through the contents to convince ourselves that bowl is indeed a virtual analog of the actual bowl.

Now that we’ve a virtual analog of our bowl, we now need a virtual analog to the shovel to generate virtual samples of 50 balls. We’re going to use the rep_sample_n() function included in the moderndive package. This function allows us to take repeated, or replicated, samples of size n.

R
virtual_shovel <- bowl %>%
rep_sample_n(size = 50)
virtual_shovel

Observe that the virtual_shovel has 50 rows corresponding to our virtual sample of size 50. The ball_ID variable identifies which of the 2,400 balls ...