15.6 Spearman’s Rank-Correlation Test

Michael Sullivan

2023-08-30

To perform a Spearman’s Rank-Correlation test in Base R use the cor.test() function and include method=‘spearman’ in the command line.

We will follow Example 1 within Section 15.6. Enter the data in Table 14 as two vectors.

mpg <- c(100,102, 103, 101, 105, 100,99,105)
dist <- c(257, 264, 274, 266, 277, 263, 258, 275)

Now run the cor.test function with method=‘spearman. The default is a two-tailed test. For a right-tailed test, include alternative=’greater’; For a left-tailed test, include alternative=‘less’.

cor.test(mpg, dist, method = "spearman")

## Warning in cor.test.default(mpg, dist, method = "spearman"): Cannot compute
## exact p-value with ties

##
##  Spearman's rank correlation rho
##
## data:  mpg and dist
## S = 6.0666, p-value = 0.0008915
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho
## 0.9277782

The Spearman correlation is 0.928 and the P-value is 0.0008915.