Robust Correlation in R

Arndt Regorz, Dipl. Kfm. & M.Sc. Psychology, 03/22/2022



(Note: When you click on this video you are using a service offered by YouTube.)


Here is the R code for the Youtube tutorial about robust correlation methods in R.

You will need the following R package, which must be installed once before use, e.g. install.packages("WRS2"):

  • WRS2

R code

# Scatterplot
attach(data_correlation)
plot(X, Y)
detach(data_correlation)

# Pearson correlation

# Pearson correlation raw data
attach(data_correlation)
cor.test(X,Y)
detach(data_correlation)

# Pearson correlation manually excluding outliers
data_correlation_cleaned <- subset(data_correlation, Y < 10)

attach(data_correlation_cleaned)
plot(X, Y)
cor.test(X,Y)
detach(data_correlation_cleaned)

# Classical robust correlations

# Spearman correlation
attach(data_correlation)
cor.test(X,Y, method="spearman")
detach(data_correlation)

# Kendall's tau
attach(data_correlation)
cor.test(X,Y, method="kendall")
detach(data_correlation)

# Modern robust correlations

library(WRS2)

# Percentage bend correlation
attach(data_correlation)
pbcor(X,Y)
detach(data_correlation)

# Winsorized correlation
attach(data_correlation)
wincor(X,Y)
detach(data_correlation)

# Robust methods without outliers

attach(data_correlation_cleaned)
cor.test(X,Y)
cor.test(X,Y, method="spearman")
cor.test(X,Y, method="kendall")
pbcor(X,Y)
wincor(X,Y)
detach(data_correlation_cleaned)



Additional tutorials about robust statistical methods with R