Advanced Data Visualization – 001 | Lesson 05 — Correlation Heatmaps, Regression and Multivariate Relationships

AI-GENERATED COURSE CONTENT — PUBLISHED AS IS. This new lesson is AI-generated, based on questions and topic collections from Open Education, including source articles attributed to Sayed Ahmed where indicated. The new AI-generated prose, original synthetic-data plots and original code are offered for reuse without copyright restrictions asserted by this site. Third-party references have separate rights. AI can make mistakes; independently check code, statistics, and interpretations.

Course: Advanced Data Visualization – 001 — course syllabus. Original source posts: Post 16176 · Post 76580 · Post 78189 · Post 19912.

Learning focus. Correlation heatmaps organize pairwise relationships, while scatterplots show the observations behind any summary score. A regression line summarizes a specific model, not an unquestionable physical law.

Original illustrative visualization: Correlation Heatmaps, Regression and Multivariate Relationships.
Original course-created illustrative plot using synthetic observations. Follow the code below to create your own figure.

Theory and algorithmic reasoning

Pearson correlation targets linear relationships; Spearman correlation uses ranks and can capture monotonic associations. Before interpreting a matrix, inspect missing-value handling, extreme points, nonlinear patterns, subgroup effects and multiplicity of tests.

A heatmap colour scale should be centered at zero for correlations; a diverging palette and numeric annotation aid interpretation. A high correlation does not imply causation, and repeated variables or data leakage can inflate apparent relationships.

Mathematical ideas

For observations (xᵢ,yᵢ), least-squares slope β̂₁=Σ(xᵢ−x̄)(yᵢ−ȳ)/Σ(xᵢ−x̄)²; intercept β̂₀=ȳ−β̂₁x̄. Correlation r=Σ(x−x̄)(y−ȳ)/√[Σ(x−x̄)²Σ(y−ȳ)²].

Python: Reproducible plot

import pandas as pd
import numpy as np, matplotlib.pyplot as plt
df=pd.DataFrame({"A":[1,2,3,4,5,6],
 "B":[2,4,5,8,9,12],"C":[6,5,4,3,2,1]})
corr=df.corr(method="pearson")
fig,ax=plt.subplots(figsize=(5,4))
im=ax.imshow(corr,vmin=-1,vmax=1,cmap="RdBu_r")
ax.set_xticks(range(3),df.columns)
ax.set_yticks(range(3),df.columns)
for i in range(3):
 for j in range(3):
  ax.text(j,i,f"{corr.iloc[i,j]:.2f}",ha="center",va="center")
fig.colorbar(im,ax=ax,label="Pearson r")
plt.tight_layout();plt.show()

R: Reproducible plot with ggplot2

library(ggplot2)
df <- data.frame(A=1:6,B=c(2,4,5,8,9,12),C=6:1)
M <- cor(df,method="pearson")
long <- as.data.frame(as.table(M))
ggplot(long,aes(Var1,Var2,fill=Freq))+
 geom_tile(color="white")+
 geom_text(aes(label=sprintf("%.2f",Freq)))+
 scale_fill_gradient2(low="#b44f5c",mid="white",
                      high="#367bb2",midpoint=0,limits=c(-1,1))+
 coord_equal()+theme_minimal()+
 labs(fill="Pearson r",title="Synthetic correlation")

Expected output and interpretation

Variable A is perfectly negatively correlated with C (r=−1). A and B are strongly positively correlated. This toy matrix illustrates how relationships can be identified but not explained causally by colour alone.

Questions and answers

Q1. What is the difference between Pearson and Spearman correlations?

Answer. Pearson uses numerical values for linear association; Spearman applies Pearson correlation to ranks and targets monotonic association.

Q2. Why should correlations be centred at zero on a diverging palette?

Answer. Because zero denotes no linear association and signs indicate opposite directions.

Q3. What can an outlier do?

Answer. It can substantially change Pearson r and fitted regression coefficients; inspect scatterplots and use robust sensitivity analyses.

Assessment and practical exercise

Produce a correlation heatmap and scatterplot matrix; compute both Pearson and Spearman and explain any disagreement.

Translated to Bengali / বাংলায় অনুবাদ

বাংলা অনুবাদ: এই পাঠের ব্যাখ্যামূলক লেখা ও প্রশ্নোত্তরের সহজ, যথাযথ বাংলা অনুবাদ নিচে দেওয়া হলো। Python ও R কোড অপরিবর্তিত রয়েছে; উপরের কোড চালিয়ে চার্ট তৈরি করুন।

শেখার বিষয়: সহসম্পর্কের হিটম্যাপ একাধিক চলকের জোড়াভিত্তিক সম্পর্ক দেখায়। স্ক্যাটারপ্লট সেই সম্পর্কের পেছনের প্রকৃত বিন্দুগুলো প্রকাশ করে। রিগ্রেশন রেখা একটি মডেলের সারাংশ, কোনো নিশ্চিত প্রাকৃতিক নিয়ম নয়।

Pearson রৈখিক সম্পর্ক মাপে; Spearman র‌্যাঙ্কভিত্তিক একমুখী সম্পর্ক ধরতে পারে। হিটম্যাপ ব্যাখ্যার আগে অনুপস্থিত মান, ব্যতিক্রম, অরৈখিকতা, উপগোষ্ঠী এবং বহু পরীক্ষার প্রভাব যাচাই করুন।

সহসম্পর্কের হিটম্যাপের রং শূন্যকে কেন্দ্র করে দুইদিকে ছড়ানো উচিত। বেশি সহসম্পর্ক মানেই কারণ-ফল নয়; একই তথ্যের পুনরাবৃত্তি বা ভবিষ্যৎ তথ্যের ফাঁস সম্পর্ককে কৃত্রিমভাবে বড় করতে পারে।

গাণিতিক ধারণা: ক্ষুদ্রতম বর্গপদ্ধতিতে ঢাল β̂₁=Σ(xᵢ−x̄)(yᵢ−ȳ)/Σ(xᵢ−x̄)² এবং ছেদ β̂₀=ȳ−β̂₁x̄। সহসম্পর্ক r দুটি চলকের কেন্দ্রিত গুণফলকে তাদের বিচ্যুতির গুণফল দিয়ে ভাগ করে পাওয়া যায়।

প্রত্যাশিত ফল: A ও C-এর সহসম্পর্ক −১, অর্থাৎ সম্পূর্ণ বিপরীত রৈখিক সম্পর্ক। A ও B-এর ইতিবাচক সম্পর্ক শক্তিশালী। হিটম্যাপ সম্পর্ক বোঝায়, কিন্তু কারণ নির্ধারণ করে না।

প্রশ্ন 1. Pearson ও Spearman-এর পার্থক্য কী?

উত্তর: Pearson মূল মানের রৈখিক সম্পর্ক, Spearman র‌্যাঙ্কভিত্তিক একমুখী সম্পর্ক মাপে।

প্রশ্ন 2. সহসম্পর্কে শূন্যকে কেন্দ্র করা কেন দরকার?

উত্তর: শূন্য মানে রৈখিক সম্পর্ক নেই; ধনাত্মক ও ঋণাত্মক চিহ্ন বিপরীত দিক নির্দেশ করে।

প্রশ্ন 3. ব্যতিক্রমী মান কী করতে পারে?

উত্তর: একটি চরম মান সহসম্পর্ক ও রিগ্রেশন রেখা উল্লেখযোগ্যভাবে বদলে দিতে পারে।

বাংলা অনুশীলন

সহসম্পর্কের হিটম্যাপ ও স্ক্যাটারপ্লট ম্যাট্রিক্স তৈরি করুন; Pearson ও Spearman-এর ফল তুলনা করে পার্থক্য ব্যাখ্যা করুন।

উৎস ও ব্যবহার: মূল লেখাগুলোর লিংক উপরে দেওয়া আছে। নতুন এআই-তৈরি ব্যাখ্যা ও নিজস্ব নমুনা-চিত্র পুনর্ব্যবহারযোগ্য; বাহ্যিক উৎসের স্বত্ব আলাদা। এআই ভুল করতে পারে, তাই ফল যাচাই করুন।


Discover more from Open Education

Subscribe to get the latest posts sent to your email.

Leave a Reply

Discover more from Open Education

Subscribe now to keep reading and get access to the full archive.

Continue reading