Calculating the between group variance

Now that we've got our grand mean and the means of the different genres, we can calculate the between group variance. The formula for the between group variance is the following: $$\frac{n_1 * (\bar{y}_1 - \bar{y})^2 + n_2 * (\bar{y}_2 - \bar{y})^2 + ... + n_g * (\bar{y}_g - \bar{y})^2}{g - 1}$$

Okay, this formula looks really complicated so let's chop it up into parts. n here represents the sample size, so \(n_1\) represents the sample size of group 1 while \(n_g\) represents the sample size of gth group. In our current example, we only have three different genres and thus three different groups so this formula would go up \(n_3\). \(\bar{y}_1\) represents the average of group 1. \(\bar{y}\) would represent our overall average which is available in the variable grand_mean. g here represents the number of groups.

In the current exercise our overall average is stored in the console in the variable grand_mean while our group averages are stored in the variables classical_average, hiphop_average and pop_average. The dataframes classical_data, hiphop_data and pop_data contain our samples per genre.

This exercise is part of the course

Inferential Statistics

View Course

Exercise instructions

  • Calculate the sample size of each group. Store the sample size of the classical genre in the variable called sample_classical, the sample size of hip hop genre in the variable called sample_hiphop and the sample size of the pop genre in the variable sample_pop. Because these are all dataframes, you can use the nrow() function on for instance the dataframes classical_data to calculate the sample size of this genre.
  • Calculate the between group variance and store it in a variable called between_group_variance.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# calculate the sample size of the classical genre


# calculate the sample size of the hip hop genre


# calculate the sample size of the pop genre


# calculate the between group variance