liveBook · Manning

Cor() function

As explained in Statistics Slam Dunk

The base R cor() function computes correlation coefficients between numeric variables.

The base R cor() function computes correlation coefficients between numeric variables. Use it to ask practical questions such as whether playing time, games played, or win shares move together. Select continuous variables first; passing noncontinuous data can produce an error. section 2.5.4

A typical correlation workflow is:

  • Subset the data to continuous variables such as Age, G, MP, WS, and WS48. section 2.5.4
  • Pass the resulting data frame to cor() to calculate every pairwise coefficient. section 2.5.4
  • Inspect the matrix: coefficients range from negative to positive, diagonal values equal 1, and each pair appears twice. section 2.5.4
  • Reshape the matrix with reshape2::melt() when a heat map needs one row per variable pair. section 2.5.4

Comparing hustle statistics with wins

Running cor(hustle2) returns a correlation matrix for selected hustle variables. The coefficient between deflections and wins is 0.24, indicating a positive but unremarkable association. A separate comparison of annual salary and win shares returns 0.76, indicating a strong positive correlation. section 5.6.1 section 4.5.3

cor_matrix <- cor(cor_draft)
print(cor_matrix)
##             Age          G         MP         WS       WS48
## Age   1.0000000 -0.2189601 -0.2327846 -0.2509647 -0.1801535
## G    -0.2189601  1.0000000  0.7921621  0.8004797  0.6165429
## MP   -0.2327846  0.7921621  1.0000000  0.7758876  0.6597869
## WS   -0.2509647  0.8004797  0.7758876  1.0000000  0.6942061
## WS48 -0.1801535  0.6165429  0.6597869  0.6942061  1.0000000

Inspect an executable correlation-matrix call and its printed coefficients for five continuous draft variables. section 2.5.4

How Statistics Slam Dunk covers it

How deep it goes
Explains selection, matrix output, reshaping, and interpretation, while deferring statistical testing and deeper correlation theory.
Examples
Uses player age, games, minutes, win shares, hustle statistics, wins, salaries, and annual win shares.
What it assumes
Assumes numeric data frames and teaches selecting continuous variables before computing correlations.
What you can do after
Apply it via base R to calculate pairwise coefficients, inspect matrices, and identify potentially useful regression predictors.
How it connects
Connects correlation coefficients with correlation matrices, heat maps, scatterplots, ggplot2, and linear regression.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.