R: R EDA: التحليل الاستكشافي للبيانات — منهجية شاملة
آخر تحديث: 2026-08-26
في الدروس الستة السابقة، تناولنا الإحصاء الوصفي، والتوزيعات الاحتمالية، واختبار الفرضيات، والانحدار — وجميعها أدوات «تحليلية». ومع ذلك، فإن الخطوة الأولى في تحليل البيانات ليست النمذجة، بل التحليل الاستكشافي للبيانات (EDA) — أي استكشاف «شكل» البيانات. في هذا الدرس، سنتعرف على منهجية التحليل الاستكشافي للبيانات (EDA) الكاملة في لغة R.
بعد الانتهاء من هذا الدرس، ستتمكن من استخدام لغة R لاستكشاف أي مجموعة بيانات بشكل شامل — بما في ذلك شكلها وتوزيعها والقيم المتطرفة والقيم المفقودة والارتباطات — مما يمهد الطريق لعمليات النمذجة المستقبلية.
1. ما ستتعلمه
- عملية EDA الكاملة (5 خطوات)
- شكل البيانات: str / dim / nrow / ncol
- التوزيع: ملخص / قراءة سريعة
- القيم المفقودة: md.pattern / naniar
- تصور التوزيع: الرسم البياني التكراري / الرسم البياني الصندوقي / الرسم البياني الكثافي
- العلاقات: مخطط الانتشار / مصفوفة الارتباط
- القيم المتطرفة: مخطط الصندوق والخطوط + المدى الربيعي
- EDA الآلية (DataExplorer / SmartEDA)
2. التحديات التي تنطوي عليها مجموعة البيانات غير المألوفة
(1) التحدي: من أين أبدأ مع ملف CSV مكون من 100 عمود؟
تلقى بوب مجموعة بيانات تتألف من 100 عمود و10,000 صف، فسأله مديره: «ألقِ نظرة على هذه البيانات أولاً». وظل يحدق في ملف «إكسل» لمدة خمس دقائق، غير عارف من أين يبدأ.
(2) الحل باستخدام لغة R
library(DataExplorer)
# One line EDA Report
create_report(df, output_file = "eda_report.html")
سطر واحد من التعليمات البرمجية → تقرير HTML كامل من EDA (أكثر من 30 صفحة من الرسوم البيانية والإحصاءات).
3. عملية EDA الكاملة (5 خطوات)
(1) طريقة الخمس خطوات
graph TB
A[1. Data Shape] --> B[2. Data Types]
B --> C[3. Missing Value Analysis]
C --> D[4. Distribution Exploration]
D --> E[5. Exploring Relationships]
A --> A1[dim/str/head]
B --> B1[summary/skimr]
C --> C1[md.pattern/naniar]
D --> D1[Histogram/Box plot]
E --> E1[Scatter/Correlation Matrix]
style A fill:#fff3cd
style B fill:#cce5ff
style C fill:#d4edda
style D fill:#f8d7da
style E fill:#e1d4ff
(2) دليل مرجعي سريع من 5 خطوات
| الخطوة | الهدف | الوظائف الرئيسية |
|---|---|---|
| 1. الشكل | ما حجم البيانات؟ | dim() nrow() ncol() str() |
| 2. النوع | ما هو نوع العمود؟ | sapply(df, class) summary() |
| 3. مفقود | كم عدد المفقودين؟ | colSums(is.na()) skimr naniar |
| 4. التوزيع | النطاق العددي؟ | hist() boxplot() geom_density() |
| 5. العلاقات | هل توجد علاقة بين الأعمدة؟ | pairs() cor() corrplot |
4. الخطوة 1: شكل البيانات
# Prepare the data
df <- iris # Use iris as demo
# Basic Information
dim(df) # [1] 150 5 ← 150 row 5 col
nrow(df) # [1] 150
ncol(df) # [1] 5
names(df) # Listed
str(df) # Structure
# 'data.frame': 150 obs. of 5 variables:
# $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ...
# $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ...
# $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ...
# $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ...
# $ Species : Factor w/ 3 levels "setosa",...
# Data Preview
head(df, 3) # First 3 rows
tail(df, 3) # Last 3 rows
5. الخطوة 2: النوع والملخص
(1) الأعمدة الرقمية مقابل الأعمدة التصنيفية
# Column Types
sapply(df, class)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# "numeric" "numeric" "numeric" "numeric" "factor"
# Automatic Recognition
library(dplyr)
df |> summarise(across(everything(), class))
(2) summary() — ملخص من سطر واحد
summary(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width
# Min. :4.30 Min. :2.00 Min. :1.00 Min. :0.1
# 1st Qu.:5.10 1st Qu.:2.80 1st Qu.:1.60 1st Qu.:0.3
# Median :5.80 Median :3.00 Median :4.35 Median :1.3
# Mean :5.84 Mean :3.05 Mean :3.76 Mean :1.2
# 3rd Qu.:6.40 3rd Qu.:3.30 3rd Qu.:5.10 3rd Qu.:1.8
# Max. :7.90 Max. :4.40 Max. :6.90 Max. :2.5
# Species
# setosa :50
# versicolor:50
# virginica :50
(3) ملخص متقدم لـ skimr
install.packages("skimr")
library(skimr)
skim(iris)
# ── Data Summary ────────────────────────
# Values
# Number of rows 150
# Number of columns 5
# ── Variable type: factor ──
# Species: 1 unique, 50 each
# ── Variable type: numeric ──
# Sepal.Length: mean=5.84, sd=0.83, p0=4.3, p25=5.1, p50=5.8, p75=6.4, p100=7.9
# ... (Each variable 20+ metrics)
6. الخطوة 3: تحليل القيم المفقودة
(1) إحصائيات حول القيم المفقودة
# Simulating Data with Missing Values
df <- data.frame(
a = c(1, 2, NA, 4),
b = c("x", NA, "z", "w"),
c = c(NA, 2, 3, NA)
)
# Number of missing values
colSums(is.na(df))
# a b c
# 1 1 2
# Proportion of Missing Values
colMeans(is.na(df)) * 100
# a b c
# 25 25 50
# Entire rows with NA
sum(!complete.cases(df)) # [1] 3
(2) naniar: التصور المتقدم للقيم المفقودة
install.packages("naniar")
library(naniar)
# Missing Value Patterns
vis_miss(iris) # iris has no missing values, can draw empty chart
# Simulation with Missing Values
df_with_na <- iris
df_with_na[sample(150, 20), 1] <- NA
df_with_na[sample(150, 10), 3] <- NA
vis_miss(df_with_na)
# Missing Correlation
gg_miss_upset(df_with_na)
# Missing vs Variable Relationships
ggplot(df_with_na, aes(x = Sepal.Length, y = Petal.Length)) +
geom_miss_point() # The red dot indicates a missing item.
7. الخطوة 4: تصور التوزيع
(1) التوزيع أحادي المتغير
library(ggplot2)
# Histogram
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram(bins = 30, fill = "skyblue", color = "white") +
labs(title = "Sepal.Length Distribution")
# Density Plot
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.5) +
labs(title = "Sepal.Length Distribution by Species")
# Box-and-Whisker Plot (Identifying Outliers)
ggplot(iris, aes(y = Sepal.Length)) +
geom_boxplot(fill = "lightblue", outlier.color = "red") +
labs(title = "Sepal.Length Box-and-Whisker Plot")
# Box-and-Whisker Plot by Species
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot() +
labs(title = "Sepal.Length Distribution by Species")
(2) التوزيعات متعددة المتغيرات
# Scatter Plot + Return
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Sepal vs Petal Length")
# Pair Graphs (Scatter Plot Matrix)
library(GGally)
ggpairs(iris, aes(color = Species))
(3) مصفوفة الارتباط
# CalculateCorrelation Coefficient
cor_matrix <- cor(iris |> select(-Species))
print(round(cor_matrix, 2))
# Visualization of Related Matrices
library(corrplot)
corrplot(cor_matrix, method = "circle", type = "upper")
# ggplot2 Version
library(reshape2)
melted <- melt(cor_matrix)
ggplot(melted, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
8. الخطوة 5: الكشف عن القيم المتطرفة
(1) طريقة IQR (الأكثر متانة)
detect_outliers <- function(x) {
q1 <- quantile(x, 0.25, na.rm = TRUE)
q3 <- quantile(x, 0.75, na.rm = TRUE)
iqr <- q3 - q1
lower <- q1 - 1.5 * iqr
upper <- q3 + 1.5 * iqr
x < lower | x > upper
}
# Applications
iris |>
mutate(across(where(is.numeric), detect_outliers, .names = "{.col}_out")) |>
select(ends_with("_out")) |>
summarise(across(everything(), sum))
# Sepal.Length_out Sepal.Width_out Petal.Length_out Petal.Width_out
# 0 4 0 0
(2) طريقة 3σ (للبيانات ذات التوزيع الطبيعي فقط)
detect_outliers_z <- function(x, threshold = 3) {
z <- (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
abs(z) > threshold
}
9. التصميم الإلكتروني الآلي (EDA)
(1) DataExplorer
install.packages("DataExplorer")
library(DataExplorer)
# 1. One-Line Report
create_report(iris, output_file = "eda_report.html")
# 2. Report Contents
plot_intro(iris) # Data Overview
plot_missing(iris) # Missing values
plot_histogram(iris) # Histogram of All Numeric Columns
plot_density(iris) # Density Plot
plot_bar(iris) # Bar Chart by Category
plot_boxplot(iris) # Box-and-Whisker Plot
plot_scatterplot(iris) # Scatter Plot Matrix
plot_correlation(iris) # Related Matrices
(2) SmartEDA
install.packages("SmartEDA")
library(SmartEDA)
ExpReport(iris, op_file = "smarteda_report.html")
ExpNumStat(iris) # Statistical Data
ExpCatStat(iris) # Categorical Statistics
10. مثال كامل: التحليل الاستكشافي للبيانات (EDA) باستخدام مجموعتي بيانات «إيريس» و«MPG»
فيما يلي مثال على مسار عمل كامل يربط بين جميع مفاهيم تصميم الإلكترونيات (EDA) التي تم تناولها في هذا الدرس.
▶ مثال: إجراء تحليل البيانات الاستكشافي (EDA) الكامل لمجموعتي البيانات «Iris» و«MPG»
# ============================================
# iris + mpg Both Datasets Complete EDA
# Features: 5-step EDA Complete Process
# ============================================
library(ggplot2)
library(dplyr)
library(skimr)
library(naniar)
library(GGally)
# 1. Step 1: Data Shape
cat("=== Step 1: Data Shape ===\n")
cat("iris:", nrow(iris), "rows x", ncol(iris), "cols\n")
cat("mpg:", nrow(mpg), "rows x", ncol(mpg), "cols\n\n")
# 2. Step 2: Type + Abstract
cat("=== Step 2: Type and Abstract ===\n")
cat("iris Column Types:\n")
print(sapply(iris, class))
cat("\nmpg Column Types:\n")
print(sapply(mpg, class))
cat("\niris Summary:\n")
print(summary(iris))
cat("\nmpg Summary:\n")
print(summary(mpg |> select(displ, year, cyl, cty, hwy)))
# 3. Step 3: Missing Value Analysis
cat("\n=== Step 3: Missing values ===\n")
cat("iris Missing values:", sum(is.na(iris)), "\n")
cat("mpg Missing values:", sum(is.na(mpg)), "\n\n")
# Simulation with Missing Data
set.seed(42)
iris_na <- iris
iris_na[sample(150, 20), 1] <- NA
iris_na[sample(150, 10), 3] <- NA
vis_miss(iris_na, cluster = TRUE)
gg_miss_upset(iris_na)
# 4. Step 4: Distribution Visualization
cat("\n=== Step 4: Distribution Visualization ===\n")
# 4.1 Distribution of Numeric Variables
iris |>
select(-Species) |>
pivot_longer(everything(), names_to = "variable", values_to = "value") |>
ggplot(aes(x = value)) +
geom_histogram(bins = 20, fill = "skyblue", color = "white") +
facet_wrap(~ variable, scales = "free") +
labs(title = "iris 4 Distribution of a Numeric Variable")
# 4.2 By Component
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot() +
labs(title = "Sepal.Length Box-and-Whisker Plot by Species")
# 4.3 Scatter Plot Matrix
ggpairs(iris, aes(color = Species))
# 5. Step 5: Correlation Matrix
cat("\n=== Step 5: Correlation Matrix ===\n")
cor_matrix <- cor(iris |> select(-Species))
print(round(cor_matrix, 2))
library(corrplot)
corrplot(cor_matrix, method = "circle", type = "upper",
addCoef.col = "black", number.cex = 0.8)
# 6. Outlier Detection
cat("\n=== Step 6: Outlier Detection ===\n")
detect_outliers <- function(x) {
q1 <- quantile(x, 0.25, na.rm = TRUE)
q3 <- quantile(x, 0.75, na.rm = TRUE)
iqr <- q3 - q1
lower <- q1 - 1.5 * iqr
upper <- q3 + 1.5 * iqr
sum(x < lower | x > upper, na.rm = TRUE)
}
iris |>
summarise(across(where(is.numeric), detect_outliers)) |>
print()
# 7. Automated Reports
cat("\n=== Step 7: Automated Reports ===\n")
library(DataExplorer)
create_report(iris, output_file = "iris_eda_report.html")
cat("iris_eda_report.html Generated\n")
create_report(mpg, output_file = "mpg_eda_report.html")
cat("mpg_eda_report.html Generated\n")
# 8. Summary of Business Insights
cat("\n=== Business Insights ===\n")
cat("1. iris 4 None of the numerical variables have missing values.\n")
cat("2. Setosa Species' Petal.Length Significantly smaller than the other two\n")
cat("3. Petal.Length and Petal.Width Highly correlated (0.96)\n")
cat("4. Sepal.Width The distribution is approximately normal\n")
cat("5. mpg 11 Numeric variables, 38 Vehicle models\n")
النتائج المتوقعة: أكثر من 30 صفحة من تقارير EDA بتنسيق HTML + العديد من الرسوم البيانية الإحصائية.
❓ أسئلة شائعة
📖 ملخص
- طريقة EDA المكونة من 5 خطوات: الشكل → النوع → البيانات المفقودة → التوزيع → العلاقات
- الشكل:
dimnrowncolstrحسب حجم البيانات - النوع:
summaryskimrعرض الإحصائيات الخاصة بكل متغير - مفقود:
colSums(is.na)العد +naniar::vis_missالعرض المرئي - التوزيع:
hist/boxplot/density(أحادي المتغيرات) +pairs/ggpairs(متعدد المتغيرات) - ذات صلة:
cor()+corrplotتصور مصفوفة الارتباط - القيم المتطرفة: طريقة IQR (المقاومة) < Q1 - 1.5×IQR أو > Q3 + 1.5×IQR
- الأتمتة:
DataExplorer::create_report()إنشاء تقرير HTML كامل في سطر واحد - يُعد تحليل البيانات (EDA) أمرًا ضروريًا قبل النمذجة — لا نمذجة بدون تحليل البيانات
- الارتباط ≠ السببية — التجارب وحدها هي التي يمكنها إثبات السببية
📝 تمارين
-
تمرين أساسي: قم بإجراء تحليل بيانات استكشافي (EDA) كامل على مجموعة البيانات المدمجة
mtcarsفي لغة R: ① الشكل ② الملخص ③ القيم المفقودة ④ الرسوم البيانية التوزيعية لأربعة متغيرات عددية ⑤ مصفوفة الارتباط. احفظ لقطة شاشة. -
تمرين أساسي: قم بمحاكاة إطار بيانات (1,000 صف، 5 أعمدة) يحتوي على 20% من القيم المفقودة. استخدم
naniar::vis_miss()لتصور أنماط القيم المفقودة ومقارنة نتائج ثلاث استراتيجيات للتعامل معها (الحذف، والاستكمال بالمتوسط، والاستكمال بالوسيط). -
تمرين أساسي: استخدم
irisلرسم مصفوفة الارتباط (corrplot) للمتغيرات العددية الأربعة، وحدد الزوجين الأكثر ارتباطًا من بين المتغيرات، وتحقق من النتائج باستخدام مخطط الانتشار. -
تمرين متقدم: قم بإجراء تحليل EDA كامل باستخدام مجموعة البيانات
mpg: ① الشكل ② الملخص ③ مخططات الصندوق المجمعة حسبclass④ مصفوفة مخططات الانتشار حسبclass⑤ مصفوفة الارتباط ⑥ التقرير الآلي. احفظ لقطات الشاشة. -
التحدي: قم بإنشاء مجموعة بيانات مختلطة تحتوي على 10,000 صف و20 عمودًا (تتضمن قيمًا عددية وفئوية وقيمًا مفقودة وقيمًا شاذة)، واستخدم
DataExplorer::create_report()لإنشاء تقرير شامل للتحليل الاستكشافي للبيانات (EDA) (30+ صفحة بتنسيق HTML)، واكتب ملخصًا للرؤى التجارية (500 حرف) استنادًا إلى التقرير.