liveBook · Manning

Cor.test() function

As explained in Statistics Slam Dunk

The cor.test() function in R performs a hypothesis test for correlation between numeric variables and reports associated test statistics and p-values.

Use cor.test() in R to test whether the correlation between two numeric variables differs from zero. It reports the correlation estimate, test statistic, degrees of freedom, p-value, confidence interval, and alternative hypothesis. The test formalizes a practical question such as whether NBA team payrolls and regular-season wins have a meaningful relationship. section 10.4.1 section 14.4.2

A typical correlation-testing workflow is:

  • Prepare two numeric variables and decide how missing values should be handled before testing. section 10.4.1 section 10.5
  • State the null hypothesis as no correlation and use a predefined significance level, commonly 0.05. section 14.4.2
  • Run cor.test(x, y), then inspect the p-value alongside the estimated correlation and confidence interval. section 10.4.1 section 14.4.2
  • Reject or fail to reject the null hypothesis according to the p-value, while treating statistical association as distinct from causation. section 10.4.1 section 10.5

Testing payrolls against wins

After combining team-season payroll and win data, run cor.test(salaries_cor2$salary, salaries_cor2$wins). The Pearson test returns r = 0.211, t = 4.9844, 533 degrees of freedom, p = 0.0000008417, and a 95% confidence interval from 0.1286 to 0.2906. Because the p-value is below 0.05, reject the no-correlation hypothesis; interpret the result as evidence of association, not proof that payrolls cause wins. section 10.4.1

options(scipen = 999)

cor.test(nba_stats$z_wins, nba_stats$z_o_pts)
## 
##  Pearson's product-moment correlation
## 
## data:  nba_stats$z_wins and nba_stats$z_o_pts
## t = -12.434, df = 298, p-value < 0.00000000000000022
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.6543987 -0.5046282
## sample estimates:
##        cor 
## -0.5844698

cor.test(nba_stats$z_wins, nba_stats$z_pts)
## 
##  Pearson's product-moment correlation
## 
## data:  nba_stats$z_wins and nba_stats$z_pts
## t = 10.74, df = 298, p-value < 0.00000000000000022
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4414140 0.6052828
## sample estimates:
##       cor 
## 0.5282497

Compare two executable Pearson tests: one relates wins to points allowed and the other relates wins to points scored, with estimates, confidence intervals, and p-values shown together. section 14.4.2

How Statistics Slam Dunk covers it

How deep it goes
Explains hypotheses, significance decisions, output fields, missing values, and association limits without developing test mathematics.
Examples
Uses NBA payroll-versus-wins and points-allowed-versus-points-scored tests, including printed Pearson output and confidence intervals.
What it assumes
Assumes numeric variables and basic correlation knowledge while teaching null hypotheses, significance thresholds, and p-value decisions.
What you can do after
Evaluate/understand correlation significance and interpret p-values, estimates, and confidence intervals in R.
How it connects
Connects correlation coefficient, p-value, null hypothesis, confidence interval, Pearson correlation, and causation.

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.