1
---
2
name: Correlation Analysis
3
description: Measure relationships between variables using correlation coefficients, correlation matrices, and association tests for correlation measurement, relationship analysis, and multicollinearity detection
4
---
5
6
# Correlation Analysis
7
8
## Overview
9
10
Correlation analysis measures the strength and direction of relationships between variables, helping identify which features are related and detect multicollinearity.
11
12
## When to Use
13
14
- Identifying relationships between numerical variables
15
- Detecting multicollinearity before regression modeling
16
- Exploratory data analysis to understand feature dependencies
17
- Feature selection and dimensionality reduction
18
- Validating assumptions about variable relationships
19
- Comparing linear and non-linear associations
20
21
## Correlation Types
22
23
- **Pearson**: Linear correlation (continuous variables)
24
- **Spearman**: Rank-based correlation (ordinal/non-linear)
25
- **Kendall**: Rank correlation (robust alternative)
26
- **Cramér's V**: Association for categorical variables
27
- **Mutual Information**: Non-linear dependencies
28
29
## Key Concepts
30
31
- **Correlation Coefficient**: Ranges from -1 to +1
32
- **Positive Correlation**: Variables move together
33
- **Negative Correlation**: Variables move oppositely
34
- **Multicollinearity**: High correlations between predictors
35
36
## Implementation with Python
37
38
```python
39
import pandas as pd
40
import numpy as np
41
import matplotlib.pyplot as plt
42
import seaborn as sns
43
from scipy.stats import pearsonr, spearmanr, kendalltau
44
45
# Sample data
46
np.random.seed(42)
47
n = 200
48
age = np.random.uniform(20, 70, n)
49
income = age * 2000 + np.random.normal(0, 10000, n)
50
education_years = age / 2 + np.random.normal(0, 3, n)
51
satisfaction = income / 50000 + np.random.normal(0, 0.5, n)
52
53
df = pd.DataFrame({
54
'age': age,
55
'income': income,
56
'education_years': education_years,
57
'satisfaction': satisfaction,
58
'years_employed': age - education_years - 6
59
})
60
61
# Pearson correlation (linear)
62
corr_matrix = df.corr(method='pearson')
63
print("Pearson Correlation Matrix:")
64
print(corr_matrix)
65
66
# Individual correlation with p-value
67
corr_coef, p_value = pearsonr(df['age'], df['income'])
68
print(f"\nPearson correlation (age vs income): r={corr_coef:.4f}, p-value={p_value:.4f}")
69
70
# Spearman correlation (rank-based)
71
spearman_matrix = df.corr(method='spearman')
72
print("\nSpearman Correlation Matrix:")
73
print(spearman_matrix)
74
75
spearman_coef, p_value = spearmanr(df['age'], df['income'])
76
print(f"Spearman correlation (age vs income): rho={spearman_coef:.4f}, p-value={p_value:.4f}")
77
78
# Kendall tau correlation
79
kendall_coef, p_value = kendalltau(df['age'], df['income'])
80
print(f"Kendall correlation (age vs income): tau={kendall_coef:.4f}, p-value={p_value:.4f}")
81
82
# Correlation heatmap
83
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
84
85
# Pearson heatmap
86
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', center=0,
87
square=True, ax=axes[0], vmin=-1, vmax=1)
88
axes[0].set_title('Pearson Correlation Heatmap')
89
90
# Spearman heatmap
91
sns.heatmap(spearman_matrix, annot=True, cmap='coolwarm', center=0,
92
square=True, ax=axes[1], vmin=-1, vmax=1)
93
axes[1].set_title('Spearman Correlation Heatmap')
94
95
plt.tight_layout()
96
plt.show()
97
98
# Correlation with significance testing
99
def correlation_with_pvalue(df):
100
rows, cols = [], []
101
for col1 in df.columns:
102
for col2 in df.columns:
103
if col1 < col2: # Avoid duplicates
104
r, p = pearsonr(df[col1], df[col2])
105
rows.append({
106
'Variable 1': col1,
107
'Variable 2': col2,
108
'Correlation': r,
109
'P-value': p,
110
'Significant': 'Yes' if p < 0.05 else 'No'
111
})
112
return pd.DataFrame(rows)
113
114
corr_table = correlation_with_pvalue(df)
115
print("\nCorrelation with P-values:")
116
print(corr_table)
117
118
# Scatter plots with regression lines
119
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
120
121
pairs = [('age', 'income'), ('age', 'education_years'),
122
('income', 'satisfaction'), ('education_years', 'years_employed')]
123
124
for idx, (var1, var2) in enumerate(pairs):
125
ax = axes[idx // 2, idx % 2]
126
ax.scatter(df[var1], df[var2], alpha=0.5)
127
128
# Add regression line
129
z = np.polyfit(df[var1], df[var2], 1)
130
p = np.poly1d(z)
131
x_line = np.linspace(df[var1].min(), df[var1].max(), 100)
132
ax.plot(x_line, p(x_line), "r--", linewidth=2)
133
134
r, p_val = pearsonr(df[var1], df[var2])
135
ax.set_title(f'{var1} vs {var2}\nr={r:.4f}, p={p_val:.4f}')
136
ax.set_xlabel(var1)
137
ax.set_ylabel(var2)
138
ax.grid(True, alpha=0.3)
139
140
plt.tight_layout()
141
plt.show()
142
143
# Multicollinearity detection (VIF)
144
from statsmodels.stats.outliers_influence import variance_inflation_factor
145
146
X = df[['age', 'education_years', 'years_employed']]
147
vif_data = pd.DataFrame()
148
vif_data['Variable'] = X.columns
149
vif_data['VIF'] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
150
151
print("\nVariance Inflation Factor (VIF):")
152
print(vif_data)
153
print("\nVIF > 10: High multicollinearity")
154
print("VIF > 5: Moderate multicollinearity")
155
156
# Partial correlation (controlling for confounding)
157
def partial_correlation(df, x, y, control_vars):
158
from scipy.stats import linregress
159
160
# Residuals of x after removing control variables
161
x_residuals = df[x] - np.poly1d(
162
np.polyfit(df[control_vars].values, df[x], deg=1)
163
)(df[control_vars].values)
164
165
# Residuals of y after removing control variables
166
y_residuals = df[y] - np.poly1d(
167
np.polyfit(df[control_vars].values, df[y], deg=1)
168
)(df[control_vars].values)
169
170
return pearsonr(x_residuals, y_residuals)[0]
171
172
partial_corr = partial_correlation(df, 'income', 'satisfaction', ['age'])
173
print(f"\nPartial correlation (income vs satisfaction, controlling for age): {partial_corr:.4f}")
174
175
# Distance correlation (non-linear relationships)
176
try:
177
from dcor import distance_correlation
178
dist_corr = distance_correlation(df['age'], df['income'])
179
print(f"Distance correlation (age vs income): {dist_corr:.4f}")
180
except ImportError:
181
print("dcor library not installed for distance correlation")
182
183
# Correlation stability over time
184
fig, ax = plt.subplots(figsize=(12, 5))
185
186
rolling_corr = df['age'].rolling(window=50).corr(df['income'])
187
ax.plot(rolling_corr.index, rolling_corr.values)
188
ax.set_title('Rolling Correlation (age vs income, window=50)')
189
ax.set_ylabel('Correlation Coefficient')
190
ax.grid(True, alpha=0.3)
191
plt.show()
192
```
193
194
## Interpretation Guidelines
195
196
- **|r| = 0.0-0.3**: Weak correlation
197
- **|r| = 0.3-0.7**: Moderate correlation
198
- **|r| = 0.7-1.0**: Strong correlation
199
- **p < 0.05**: Statistically significant
200
- **High VIF (>10)**: Multicollinearity problem
201
202
## Important Notes
203
204
- Correlation ≠ Causation
205
- Non-linear relationships missed by Pearson
206
- Outliers can distort correlations
207
- Sample size affects significance
208
- Temporal trends can create spurious correlations
209
210
## Visualization Strategies
211
212
- Heatmaps for overview
213
- Scatter plots for relationships
214
- Pair plots for multivariate analysis
215
- Rolling correlations for time-varying relationships
216
217
## Deliverables
218
219
- Correlation matrices (Pearson, Spearman)
220
- Correlation heatmaps with annotations
221
- Statistical significance table
222
- Scatter plots with regression lines
223
- Multicollinearity assessment (VIF)
224
- Partial correlation analysis
225
- Relationship interpretation report
226