Correlation Analysis in R: Pearson Correlation with Significance Testing and Visualization

Correlation analysis is one of the most commonly used statistical techniques for measuring the strength and direction of the relationship between two continuous variables. It is widely applied in data science, finance, healthcare, marketing, engineering, and scientific research to identify patterns and associations in data.

R provides several built-in functions and packages that make it easy to calculate correlation coefficients, test their statistical significance, and visualize relationships between variables.

This tutorial explains Pearson’s correlation coefficient, how to compute correlation matrices, perform significance tests, and create correlation plots in R.

What Is Correlation Analysis?

Correlation analysis measures the degree to which two variables move together.

For example:

  • Do advertising expenses increase sales?
  • Does study time improve exam scores?
  • Is blood pressure associated with age?
  • Does rainfall influence crop yield?

Correlation does not imply causation. A strong correlation simply indicates that two variables tend to vary together, not that one causes the other.

Pearson’s Correlation Coefficient

The most widely used measure of linear association is the Pearson Product-Moment Correlation Coefficient, commonly denoted by r.

The coefficient ranges from −1 to +1.

  • +1 indicates a perfect positive linear relationship.
  • −1 indicates a perfect negative linear relationship.
  • 0 indicates no linear relationship.

The sample correlation coefficient is denoted by r, while the population correlation coefficient is represented by the Greek letter ρ (rho).

Pearson’s correlation coefficient was developed by Karl Pearson in the late 19th century and remains one of the foundational measures in statistics.

Interpreting Correlation Coefficients

Although interpretation depends on the field of study, the following guidelines are commonly used:

Correlation (r)Interpretation
0.00–0.19Very weak
0.20–0.39Weak
0.40–0.59Moderate
0.60–0.79Strong
0.80–1.00Very strong

The sign indicates the direction:

  • Positive values indicate both variables increase together.
  • Negative values indicate one variable decreases as the other increases.

Examples:

  • r = 0.82 indicates a strong positive relationship.
  • r = −0.72 indicates a strong negative relationship.
  • r = 0.05 indicates almost no linear relationship.

Assumptions of Pearson Correlation

Before applying Pearson’s correlation, verify these assumptions:

  • Both variables are continuous.
  • Observations are independent.
  • The relationship is approximately linear.
  • The variables are approximately normally distributed (particularly for hypothesis testing).
  • There are no influential outliers that distort the relationship.

If these assumptions are violated, consider Spearman’s rank correlation or Kendall’s rank correlation instead.

Generate Sample Data

Let’s create a sample dataset.

set.seed(123)

dt <- data.frame(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)

head(dt)

Example output:

            a          b          c          d
1 -0.56047565 1.2240818 -1.0678237 0.4264642
2 -0.23017749 0.3598138 -0.2179749 -0.2950715
3 1.55870831 0.4007715 -1.0260044 0.8951257
4 0.07050839 0.1106827 -0.7288912 0.8781335

Calculate the Correlation Matrix

The base R cor() function computes pairwise correlations among variables.

cor(dt)

Example output:

          a          b          c          d
a 1.000000 0.631037 -0.043326 -0.331661
b 0.631037 1.000000 -0.270769 -0.193033
c -0.043326 -0.270769 1.000000 0.180283
d -0.331661 -0.193033 0.180283 1.000000

Interpretation:

  • Variables a and b have a strong positive correlation.
  • Variables a and d show a weak negative relationship.
  • Variables a and c are nearly uncorrelated.

Correlation Methods in R

The cor() function supports several methods.

cor(
x,
method = "pearson"
)

Available methods include:

MethodUse Case
"pearson"Linear relationships between continuous variables
"spearman"Monotonic relationships or non-normal data
"kendall"Ordinal data or datasets with many tied ranks

Visualizing the Correlation Matrix

Visualizing correlations makes it easier to identify strong relationships.

One popular package is corrplot.

Install the package if necessary:

install.packages("corrplot")

Load the package:

library(corrplot)

Create the correlation plot:

corrplot(
cor(dt),
method = "color"
)

Typically:

  • Blue colors indicate positive correlations.
  • Red colors indicate negative correlations.
  • Darker shades represent stronger relationships.

Testing Correlation Significance

A correlation coefficient alone is not enough. You should also determine whether the observed relationship is statistically significant.

Use the cor.test() function.

cor.test(
dt$a,
dt$b,
method = "pearson"
)

Example output:

Pearson's product-moment correlation

t = 2.29

df = 8

p-value = 0.050

cor = 0.631

Interpreting the Results

Suppose the output reports:

  • Correlation coefficient = 0.631
  • p-value = 0.050

Interpretation:

  • The variables have a moderately strong positive linear relationship.
  • At a 5% significance level, the evidence is borderline statistically significant.
  • Always interpret the p-value in the context of your predefined significance level (commonly 0.05).

Testing Multiple Correlations

The following example calculates Pearson correlations and associated p-values for every pair of variables.

library(dplyr)
library(purrr)
library(tidyr)
library(broom)

pairs <- combn(names(dt), 2, simplify = FALSE)

cor_results <- map_dfr(pairs, function(vars) {

test <- cor.test(
dt[[vars[1]]],
dt[[vars[2]]],
method = "pearson"
)

tidy(test) %>%
mutate(
Variable1 = vars[1],
Variable2 = vars[2]
)
})

cor_results %>%
select(
Variable1,
Variable2,
estimate,
p.value
)

This produces a table containing:

  • Variable pair
  • Correlation coefficient
  • Confidence interval
  • Test statistic
  • P-value

Filtering Significant Correlations

To display only statistically significant relationships:

cor_results %>%
filter(
p.value < 0.05
) %>%
select(
Variable1,
Variable2,
estimate,
p.value
)

This returns only variable pairs with significant correlations at the 5% significance level.

Practical Applications

Correlation analysis is widely used in:

  • Financial market analysis
  • Portfolio management
  • Healthcare research
  • Customer behavior analytics
  • Marketing effectiveness studies
  • Machine learning feature selection
  • Quality control
  • Environmental science
  • Psychology
  • Education research

Common Mistakes

Avoid these common errors:

  • Assuming correlation implies causation.
  • Ignoring influential outliers.
  • Applying Pearson correlation to nonlinear relationships.
  • Using Pearson correlation for ordinal variables.
  • Focusing only on p-values without considering effect size.
  • Interpreting statistically significant but very small correlations as practically important.

Pearson vs Spearman vs Kendall

MethodBest For
PearsonContinuous variables with linear relationships
SpearmanOrdinal or non-normal continuous data with monotonic relationships
KendallOrdinal data, small samples, or data with many tied ranks

Conclusion

Correlation analysis is a fundamental statistical technique for exploring relationships between variables. In R, the cor() function allows you to compute correlation matrices, while cor.test() evaluates whether the observed relationships are statistically significant. Visualization tools such as corrplot make it easy to identify patterns across multiple variables.

Before interpreting correlation results, always verify that the assumptions of the chosen method are met. Pearson’s correlation is ideal for continuous variables with approximately linear relationships, whereas Spearman’s and Kendall’s methods are better suited to ordinal data, non-normal distributions, or monotonic relationships. Most importantly, remember that correlation quantifies association—not causation—and significant correlations should be interpreted alongside subject-matter knowledge and additional analyses.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

ten + seventeen =