Introduction

  • Importance
    • Mean, median and mode help us understand the distribution of data in a dataset.
    • They provide insights into the data’s central value and how spread out the data is.
  • Applications
    • Used in fields such as economics, psychology, sports, science and more!

Mean Formula

Mean formal definition:

\[ \text{Mean} = \frac{1}{N}\sum_{i=1}^{N} x_i \]

  • \(N\) is the total number of observations

  • \(x_i\) is the value of each observation

Mean in English

The way to find the average of a dataset

  • Add up all the observations in the dataset!
    • Take each value in your dataset and all them all together
  • Divide by how many observations you see!
    • After you’ve added up all the values, you divide the number of values you added together

Mean in R

  • Mean built in:
numbers <- c(1,2,3,4,5)
mean(numbers)
## [1] 3
  • Behind the scenes:
calculate_mean <- function(x) {
  sum(x) / length(x)
}
numbers <- c(1,2,3,4,5)
calculate_mean(numbers)
## [1] 3

Mean in Practice

## [1] "The mean is 15.4"

Median Formula (ODD)

  • if number of observations are odd \[ \text{Median} = x_{\frac{n+1}{2}} \]
  • \(n\) is the number of observations in the dataset.
  • \(x_{\frac{n+1}{2}}\) is the value at position \(\frac{n+1}{2}\) in the sorted dataset.

Median Formula (EVEN)

  • if number of observations are even \[ \text{Median} = \frac{x_{\frac{n}{2}} + x_{\frac{n}{2} + 1}}{2} \]
  • \(n\) is the number of observations in the dataset.
  • \(x_{\frac{n}{2}}\) and \(x_{\frac{n}{2} + 1}\) are the values at positions \(\frac{n}{2}\) and \(\frac{n}{2} + 1\) in the sorted dataset.

Median in English

THe middle value of a dataset when ordered from smallest to largest

  • Order your observations from smallest to largest
  • Find the middle observations
    • If there is an odd number of values, the median is the one in the middle
    • if there is an even number of values, the median is the average of the two middle numbers

Median in R

  • Median built in:
numbers <- c(5,1,3,2,4)
mean(numbers)
## [1] 3

Median in R

Behind the scenes:

calculate_median <- function(x) {
  sorted_x <- sort(x)
  n <- length(sorted_x)
  # IF EVEN
  if (n %% 2 == 1) {
    return(sorted_x[(n + 1) / 2])
  # ELSE ODD
  } else {
    return((sorted_x[n / 2] + sorted_x[n / 2 + 1]) / 2)
  }
}
numbers <- c(5,1,3,2,4)
calculate_median(numbers)
## [1] 3

Median in Practice

## [1] "The median is 15"

Mode Formula

Mode formal definition:

\[ \text{Mode} = \text{Value with highest frequency in the dataset} \]

Simple!

Mode in English

Mode in English

THe value that appears most frequently in a data set.

Mode sounds like “Most”

  • If one number appears more than the others, that number is the mode
  • If no number repeats, the dataset has no mode
  • If two or more numbers repeat the same number of times, each is a mode and the dataset is multimodal.

Mode in R

Built in function:

404 ERROR, mode not found!

but we can make our own!

mode <- function(x) {
  frequency_table <- table(x)
  max_frequency <- max(frequency_table)
  max_indexes <- frequency_table == max_frequency
  modes <- as.numeric(names(frequency_table[max_indexes]))
  
  if(length(modes) == length(x)) {
    return(NA)
  } else {
    return(modes)
  }
}

Mode in R

Tests:

## [1] "Input:"
## [1] 1 2 3 4
## [1] "Output:"
## [1] NA
## [1] "Input:"
## [1] 1 1 2 2 5 5
## [1] "Output:"
## [1] 1 2 5
## [1] "Input:"
## [1] 1 1 2 3
## [1] "Output:"
## [1] 1

Mode in Practice

## [1] "The mode is 20"