R Correlation
Correlation measures the strength and direction of the linear relationship between two numeric variables. A correlation coefficient ranges from -1 (perfect negative) to +1 (perfect positive), with 0 meaning no linear relationship. R calculates correlation in one function call and visualizes it with a few more.
The Correlation Coefficient
Value range: -1 ────── 0 ────── +1 +1 Perfect positive: as X rises, Y rises proportionally +0.7 Strong positive relationship +0.3 Weak positive relationship 0 No linear relationship -0.3 Weak negative relationship -0.7 Strong negative relationship -1 Perfect negative: as X rises, Y falls proportionally
Scatter plot shapes: r = +1 r = +0.7 r = 0 r = -0.7 · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ↗ ↗ → scattered ↘
Pearson Correlation
study_hours <- c(1,2,3,4,5,6,7,8,9,10) exam_score <- c(45,52,58,65,70,74,80,85,88,95) cor(study_hours, exam_score) # [1] 0.9973 → very strong positive correlation # With hypothesis test cor.test(study_hours, exam_score) # t = 41.7, df = 8, p-value = 1.8e-10 # 95% CI: [0.990, 0.999] # cor = 0.997
Spearman Rank Correlation
# Use when data is not normally distributed or has outliers cor(study_hours, exam_score, method="spearman") # Kendall's tau — another non-parametric option cor(study_hours, exam_score, method="kendall")
Method Assumption Best For ────────────────────────────────────────────────────────────── Pearson Linear, normal Continuous, normal data Spearman Monotonic Ordinal or non-normal data Kendall Monotonic Small samples, many ties
Correlation Matrix
students <- data.frame( study_hrs = c(5,8,3,9,6,4,7,10,2,6), sleep_hrs = c(7,6,8,6,7,8,7,5,9,6), score = c(72,88,58,95,78,62,84,96,45,74), stress = c(4,6,3,7,5,3,5,8,2,4) ) cor(students) # study_hrs sleep_hrs score stress # study_hrs 1.000 -0.652 0.982 0.905 # sleep_hrs -0.652 1.000 -0.614 -0.790 # score 0.982 -0.614 1.000 0.876 # stress 0.905 -0.790 0.876 1.000
Visualizing a Correlation Matrix
library(ggplot2)
library(reshape2)
cor_matrix <- cor(students)
melted <- melt(cor_matrix)
ggplot(melted, aes(Var1, Var2, fill=value)) +
geom_tile(color="white") +
geom_text(aes(label=round(value,2)), size=3) +
scale_fill_gradient2(low="tomato", mid="white", high="steelblue",
midpoint=0, limits=c(-1,1)) +
labs(title="Correlation Heatmap", x=NULL, y=NULL, fill="r") +
theme_minimal() +
theme(axis.text.x=element_text(angle=45, hjust=1))
corrplot Package — Dedicated Correlation Visualization
library(corrplot)
corrplot(cor(students),
method = "circle", # "color","number","pie","shade"
type = "upper", # show upper triangle only
tl.col = "black",
tl.srt = 45,
addCoef.col = "white", # add coefficients
col = COL2("RdBu"))
Correlation vs Causation
High correlation does NOT mean one variable causes the other.
Example: Ice cream sales and drowning rates correlate strongly.
Cause: Both increase in summer (confounding variable = temperature).
Ice cream sales ↑ r = 0.85 Drowning rates ↑
↑ ↑
└──────── Summer heat ─────────┘
(true cause of both)
Always look for confounding variables before claiming causation.
Partial Correlation
library(ppcor) # Correlation between study hours and score, # controlling for stress level pcor.test(students$study_hrs, students$score, students$stress) # Partial correlation = 0.943, p = 0.0008
Correlation analysis is the starting point for understanding relationships in your data. Run a correlation matrix on all numeric variables at the beginning of any analysis — it reveals which variables move together, guides variable selection for modeling, and immediately flags suspicious patterns worth investigating further.
