Exercise

Calculate the variance manually

As a reminder, we use the following process to calculate the sample variance:

  1. Calculate the sample mean
  2. Calculate the squared difference between each data point and the sample mean
  3. Sum these squared differences (i.e. compute the sum of squares)
  4. Divide the sum of squares by \(N-1\) (i.e. the sample size minus 1)

Let's calculate the sample variance of Michael Jordan's points per game!

Instructions

100 XP

The dataset data_jordan is loaded into your workspace.

  • Calculate the mean points per game and save the result to mean_ppg.
  • Subtract the mean points per game from the vector of points scored in each game and assign the result to diff.
  • Square this vector of differences and save to squared_diff.
  • Calculate the sample variance by summing the values in squared_diff with sum() and dividing by the sample size minus 1 using length() to count the number of games in the sample. Just print the result without saving it.
  • Check your result by calculating the variance with R's built-in var() function.