Functions

  • Duration: ~15 Minutes
  • Followed By: 5 Minute Break
Photo of the Titanic

Learning Objectives

  • Disclaimer
  • Example

Titanic Ad For Journey From New York To Europe

Disclaimer

  • Will demonstrate.
  • Want you to know it is possible.
  • You don't have to fully understand.
Photo of John Jacob Astor

John Jacob Astor in 1909. Richest man to die aboard the Titanic

Demo: skew

  • R does not have a built-in skew function.
    • Perhaps, because it is easy to program?
  • Several packages include skew.
  • I want us to program our own.

Calculating Skew


## Helpful variables
mean_win   <- mean(take_five$winning_numbers)
median_win <- median(take_five$winning_numbers)
sd_win     <- sd(take_five$winning_numbers)

## Is it skewed?
3*(mean_win - median_win) / sd(take_five$winning_numbers)
    

[1] -0.01921861
    

Function: skew()


#' skew
#'
#' Pearson's second skew coefficient
#' https://en.wikipedia.org/wiki/Skewness
#'
#' @param x A vector of numbers we can
#' @return Pearson's second skewness coefficient of the numbers in x.
#' 
skew <- function(x) {
    ## In-scope Variables.
    x_median <- median(x, na.rm=TRUE)
    x_mean <- mean(x, na.rm=TRUE)
    x_sd <- sd(x, na.rm=TRUE)

    x_skew <- 3*(x_mean - x_median) / x_sd

    return(x_skew)
}
    

Functions!

Photo of John Jacob Astor in 1919

John Jacob Astor in 1919