## 9.3 Sampling error

Sampling error is the chance difference, caused by sampling, between an estimate and the population parameter being estimated

Here we’ll get a feel for sampling error using the human gene data.

Let’s use the slice_sample function to randomly sample n = 20 rows from the genelengths tibble, and store them in a tibble object called “randsamp1”:

set.seed(12)
randsamp1 <- genelengths %>%
slice_sample(n = 20, replace = FALSE) %>%
select(size)

In the preceding chunk, we:

• set the seed (here using integer 12), so that everyone gets the same output for this code chunk
• tell R that we want the output from our code to be stored in an object named “randsamp1”
• use the slice_sample function to randomly sample 20 rows from the genelengths tibble, and to do so without replacement
• use the select function to tell R to return only the “size” variable from the newly generated (sampled) tibble

Now let’s use the mean and sd base functions to calculate the mean and standard deviation using our sample.

We’ll assign the calculations to an object “randsamp1.mean.sd”, then we’ll present the output using the kable function.

randsamp1.mean.sd <- randsamp1 %>%
summarise(
Mean_genelength = mean(size, na.rm = TRUE),
SD_genelength = sd(size, na.rm = TRUE)
)

Now present the output using the kable function so we can control the maximum number of digits shown.

Using the kable approach to presenting tibble outputs ensures that, when knitted, your output shows a sufficient number of decimal places… something that doesn’t always happen without using the kable function.

kable(randsamp1.mean.sd, digits = 4)
Mean_genelength SD_genelength
3408.25 1443.698

Do your numbers match those above?

Let’s now draw another random sample of the same size (20), making sure to give the resulting tibble object a different name (“randsamp2”). Here, we won’t set the seed again, because all that is required is for everyone to get a different sample from the first one above; we don’t need to have everyone get the same sample, but it’s ok if we do.

randsamp2 <- genelengths %>%
slice_sample(n = 20, replace = FALSE) %>%
select(size)

And calculate the mean and sd:

randsamp2.mean.sd <- randsamp2 %>%
summarise(
Mean_genelength = mean(size, na.rm = TRUE),
SD_genelength = sd(size, na.rm = TRUE)
)

And show using the kable function:

kable(randsamp2.mean.sd, digits = 4)
Mean_genelength SD_genelength
4381.35 2081.471

Are they the same as we saw using the first random sample? NO! This reflects sampling error.

1. Repeat the process above to get a third sample of size 20 genes, and calculate the mean and standard deviation of gene length for that sample.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7481535,"math_prob":0.9942471,"size":2654,"snap":"2023-40-2023-50","text_gpt3_token_len":685,"char_repetition_ratio":0.14264151,"word_repetition_ratio":0.08983452,"special_character_ratio":0.25094196,"punctuation_ratio":0.12273642,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99478483,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-27T19:12:28Z\",\"WARC-Record-ID\":\"<urn:uuid:28deb980-076d-4995-84a6-b6e7d10b2702>\",\"Content-Length\":\"61756\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a9156a38-6d7b-4184-8b7f-1e5c120e6e75>\",\"WARC-Concurrent-To\":\"<urn:uuid:42119bdc-7474-4192-84c4-7aa472184e73>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"https://ubco-biology.github.io/BIOL202/sampling_error.html\",\"WARC-Payload-Digest\":\"sha1:6E6U6VW43LAJL7ZR6PL6OM4H4MD2QBGY\",\"WARC-Block-Digest\":\"sha1:5FIPJUGKMNDEX5C6UKPR7DO2BWGCJRXY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510319.87_warc_CC-MAIN-20230927171156-20230927201156-00367.warc.gz\"}"}