Variance & Standard Deviation

This week we will begin our statistics adventure by learning about population and estimated parameters (video 1), variance and standard deviation (video 2) and as an optional bonus why dividing by N underestimates the variance (video 3).

As mentioned previously, you will learn about the topics listed above by first studying the theory, provided via YouTube videos (this week 3 different StatQuest videos). Then you will work through some exercises, utilizing the R language to demonstrate various topics in a more “hands on” way, hopefully cementing the ideas presented in the YouTube videos. We have designed the exercises in ways we hope will help you to think statistically and generate discussion points. We will use the Thursday group meetings to discuss the exercises and how conclusions drawn from them relate to the theory presented in the videos.

Let’s jump in and get started with Video 1 (Population and Estimated Parameters, Clearly Explained!!!).

  1. Probably the most important concept introduced in video 1 is the idea that when estimating population parameters from samples, the sample size affects how confident we can be in the accuracy of the estimates. To illustrate this point, take a look at the code below. This code chunk will draw random samples (5 different samples) from a normally distributed population with population parameters mean = 20 and sd = 10 (these parameters correspond to the values used by Josh in the video).
five_samples <- map(1:5, \(x) tibble(values = rnorm(n = 5,
                                                    mean = 20, 
                                                    sd = 10))) %>%
  list_rbind(names_to = "sample")
  1. In Video 1, Josh showed how we can use a population distribution (histogram) to calculate probabilities and statistics. In the video example a population of 240 billion liver cells was used. This is a lot of observations, and even modern computers struggle a bit when working with such large numbers. Many times in statistics we rely on the assumption that samples around 10,000 observations generate population estimates close to the true population parameters.
    Generate a large sample (n = 10000) using the same population parameters as in we used for exercise 1 (samp_10k <- tibble(values = rnorm(10000, 20, 10))).

We will now move on to Video 2 (Calculating the Mean, Variance and Standard Deviation, Clearly Explained!!!)

  1. What does the fun_x() function in the code chunk below calculate? Explain what each line of the code is doing.
fun_x <- function(x){
  dev <- x - mean(x)
  dev_sq <- dev ^ 2
  s_dev_sq <- sum(dev_sq)
  s_dev_sq / length(x)
}
  1. Using the tibble() function, create a tibble with two columns (x and y). Both of these columns (samples) should have 6 observations each (concatenate values using the c() function), and no duplicated values per column are allowed.

If you want to go deeper into the theory regarding variance (this is optional), more specifically why when estimating the population variance we divide by n - 1, check out Video 3 (Why Dividing By N Underestimates the Variance). In this video, Josh does a great job explaining why we underestimate the variance when using the sample mean, but the explanation is somewhat theoretical. We can make use of some rather simple simulation code to show empirically that dividing by n - 1 works better than dividing by N (and other options we may want to try).

  1. Study the code below and explain what each part is doing. Run the code. What does the output tell us?
var_a <- function(x, a){
  ssq <- sum((x - mean(x)) ^ 2)
  ssq / (length(x) + a)
}
samples_df <- map(1:10000, \(i) tibble(x = rnorm(n = 10, 
                                                 mean = 0, 
                                                 sd = 1)))
samples_df %>% 
  list_rbind(names_to = "sample") %>% 
  group_by(sample) %>% 
  summarise(n_minus_2 = var_a(x, a = -2),
            n_minus_1 = var_a(x, a = -1),
            n = var_a(x, a = 0),
            n_plus_1 = var_a(x, a = 1),
            n_plus_2 = var_a(x, a = 2)) %>%
  summarize(across(2:6, mean))