Skip to main content
Statistics LibreTexts

25.1: Computing Covariance and Correlation (Section 24.3)

  • Page ID
    8847
  • Let’s first look at our toy example of covariance and correlation. For this example, we first start by generating a set of X values.

    df <-
      tibble(x = c(3, 5, 8, 10, 12))

    Then we create a related Y variable by adding some random noise to the X variable:

    We compute the deviations and multiply them together to get the crossproduct:

    And then we compute the covariance and correlation:

    results_df <- tibble(
      covXY=sum(df$crossproduct) / (nrow(df) - 1),
      corXY= sum(df$crossproduct) / 
        ((nrow(df) - 1) * sd(df$x) * sd(df$y)))
    
    kable(results_df)
    covXY corXY
    17 0.89