Pengaturan global laporan & ukuran grafik
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
fig.align = "center",
fig.width = 7,
fig.height = 4.5
)
Load Library
library(tidyverse)
library(cluster)
library(corrplot)
library(knitr)
Import Dataset
data_whr <- read.csv("world-happiness-report-2021.csv", fileEncoding = "UTF-8-BOM")
dim(data_whr)
## [1] 149 20
kable(head(data_whr[, 1:8]), digits = 3)
| Finland |
Western Europe |
7.842 |
0.032 |
7.904 |
7.780 |
10.775 |
0.954 |
| Denmark |
Western Europe |
7.620 |
0.035 |
7.687 |
7.552 |
10.933 |
0.954 |
| Switzerland |
Western Europe |
7.571 |
0.036 |
7.643 |
7.500 |
11.117 |
0.942 |
| Iceland |
Western Europe |
7.554 |
0.059 |
7.670 |
7.438 |
10.878 |
0.983 |
| Netherlands |
Western Europe |
7.464 |
0.027 |
7.518 |
7.410 |
10.932 |
0.942 |
| Norway |
Western Europe |
7.392 |
0.035 |
7.462 |
7.323 |
11.053 |
0.954 |
Tahap 3: Preprocessing Data
str(data_whr)
## 'data.frame': 149 obs. of 20 variables:
## $ Country.name : chr "Finland" "Denmark" "Switzerland" "Iceland" ...
## $ Regional.indicator : chr "Western Europe" "Western Europe" "Western Europe" "Western Europe" ...
## $ Ladder.score : num 7.84 7.62 7.57 7.55 7.46 ...
## $ Standard.error.of.ladder.score : num 0.032 0.035 0.036 0.059 0.027 0.035 0.036 0.037 0.04 0.036 ...
## $ upperwhisker : num 7.9 7.69 7.64 7.67 7.52 ...
## $ lowerwhisker : num 7.78 7.55 7.5 7.44 7.41 ...
## $ Logged.GDP.per.capita : num 10.8 10.9 11.1 10.9 10.9 ...
## $ Social.support : num 0.954 0.954 0.942 0.983 0.942 0.954 0.934 0.908 0.948 0.934 ...
## $ Healthy.life.expectancy : num 72 72.7 74.4 73 72.4 73.3 72.7 72.6 73.4 73.3 ...
## $ Freedom.to.make.life.choices : num 0.949 0.946 0.919 0.955 0.913 0.96 0.945 0.907 0.929 0.908 ...
## $ Generosity : num -0.098 0.03 0.025 0.16 0.175 0.093 0.086 -0.034 0.134 0.042 ...
## $ Perceptions.of.corruption : num 0.186 0.179 0.292 0.673 0.338 0.27 0.237 0.386 0.242 0.481 ...
## $ Ladder.score.in.Dystopia : num 2.43 2.43 2.43 2.43 2.43 2.43 2.43 2.43 2.43 2.43 ...
## $ Explained.by..Log.GDP.per.capita : num 1.45 1.5 1.57 1.48 1.5 ...
## $ Explained.by..Social.support : num 1.11 1.11 1.08 1.17 1.08 ...
## $ Explained.by..Healthy.life.expectancy : num 0.741 0.763 0.816 0.772 0.753 0.782 0.763 0.76 0.785 0.782 ...
## $ Explained.by..Freedom.to.make.life.choices: num 0.691 0.686 0.653 0.698 0.647 0.703 0.685 0.639 0.665 0.64 ...
## $ Explained.by..Generosity : num 0.124 0.208 0.204 0.293 0.302 0.249 0.244 0.166 0.276 0.215 ...
## $ Explained.by..Perceptions.of.corruption : num 0.481 0.485 0.413 0.17 0.384 0.427 0.448 0.353 0.445 0.292 ...
## $ Dystopia...residual : num 3.25 2.87 2.84 2.97 2.8 ...
colSums(is.na(data_whr))
## Country.name
## 0
## Regional.indicator
## 0
## Ladder.score
## 0
## Standard.error.of.ladder.score
## 0
## upperwhisker
## 0
## lowerwhisker
## 0
## Logged.GDP.per.capita
## 0
## Social.support
## 0
## Healthy.life.expectancy
## 0
## Freedom.to.make.life.choices
## 0
## Generosity
## 0
## Perceptions.of.corruption
## 0
## Ladder.score.in.Dystopia
## 0
## Explained.by..Log.GDP.per.capita
## 0
## Explained.by..Social.support
## 0
## Explained.by..Healthy.life.expectancy
## 0
## Explained.by..Freedom.to.make.life.choices
## 0
## Explained.by..Generosity
## 0
## Explained.by..Perceptions.of.corruption
## 0
## Dystopia...residual
## 0
sum(duplicated(data_whr))
## [1] 0
fitur_cluster <- c(
"Logged.GDP.per.capita",
"Social.support",
"Healthy.life.expectancy",
"Freedom.to.make.life.choices",
"Generosity",
"Perceptions.of.corruption"
)
data_cluster <- data_whr %>%
select(all_of(fitur_cluster))
rownames(data_cluster) <- data_whr$Country.name
Tahap 4: Exploratory Data Analysis (EDA)
Statistik Deskriptif
ringkasan <- data_whr %>%
select(Ladder.score, all_of(fitur_cluster)) %>%
summarise(across(everything(), list(
Mean = mean,
SD = sd,
Min = min,
Median = median,
Max = max
))) %>%
pivot_longer(everything(), names_to = c("Variabel", ".value"), names_sep = "_")
kable(ringkasan, digits = 3, caption = "Statistik Deskriptif Variabel Utama")
Statistik Deskriptif Variabel Utama
| Ladder.score |
5.533 |
1.074 |
2.523 |
5.534 |
7.842 |
| Logged.GDP.per.capita |
9.432 |
1.159 |
6.635 |
9.569 |
11.647 |
| Social.support |
0.815 |
0.115 |
0.463 |
0.832 |
0.983 |
| Healthy.life.expectancy |
64.993 |
6.762 |
48.478 |
66.603 |
76.953 |
| Freedom.to.make.life.choices |
0.792 |
0.113 |
0.382 |
0.804 |
0.970 |
| Generosity |
-0.015 |
0.151 |
-0.288 |
-0.036 |
0.542 |
| Perceptions.of.corruption |
0.727 |
0.179 |
0.082 |
0.781 |
0.939 |
Distribusi Data (Histogram)
Ladder Score
ggplot(data_whr, aes(x = Ladder.score)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Ladder Score", x = "Ladder Score", y = "Frekuensi")
### Logged GDP per Capita
ggplot(data_whr, aes(x = Logged.GDP.per.capita)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Logged GDP per Capita", x = "Logged GDP per Capita", y = "Frekuensi")
### Social Support
ggplot(data_whr, aes(x = Social.support)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Social Support", x = "Social Support", y = "Frekuensi")
### Healthy Life Expectancy
ggplot(data_whr, aes(x = Healthy.life.expectancy)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Healthy Life Expectancy", x = "Healthy Life Expectancy", y = "Frekuensi")

Freedom to Make Life Choices
ggplot(data_whr, aes(x = Freedom.to.make.life.choices)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Freedom to Make Life Choices", x = "Freedom to Make Life Choices", y = "Frekuensi")

Generosity
ggplot(data_whr, aes(x = Generosity)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Generosity", x = "Generosity", y = "Frekuensi")

Perceptions of Corruption
ggplot(data_whr, aes(x = Perceptions.of.corruption)) +
geom_histogram(bins = 20, fill = "steelblue", color = "white") +
theme_minimal() +
labs(title = "Histogram Perceptions of Corruption", x = "Perceptions of Corruption", y = "Frekuensi")

Matriks Korelasi
data_corr <- data_whr %>%
select(Ladder.score, all_of(fitur_cluster))
matriks_korelasi <- cor(data_corr)
corrplot(
matriks_korelasi,
method = "color",
type = "upper",
addCoef.col = "black",
tl.col = "black",
tl.srt = 45,
number.cex = 0.75,
title = "Matriks Korelasi Antar Variabel",
mar = c(0, 0, 2, 0)
)
# Tahap 5: Standardisasi Data
data_scaled <- scale(data_cluster)
kable(head(round(data_scaled, 3)), caption = "Cuplikan Data Setelah Standardisasi")
Cuplikan Data Setelah Standardisasi
| Finland |
1.159 |
1.212 |
1.036 |
1.389 |
-0.550 |
-3.021 |
| Denmark |
1.295 |
1.212 |
1.140 |
1.362 |
0.300 |
-3.060 |
| Switzerland |
1.454 |
1.108 |
1.391 |
1.124 |
0.266 |
-2.430 |
| Iceland |
1.248 |
1.465 |
1.184 |
1.442 |
1.162 |
-0.304 |
| Netherlands |
1.294 |
1.108 |
1.095 |
1.071 |
1.262 |
-2.173 |
| Norway |
1.399 |
1.212 |
1.229 |
1.486 |
0.718 |
-2.552 |
Tahap 6: Penentuan Jumlah Klaster Optimal
Metode Elbow
set.seed(123)
wss <- sapply(1:10, function(k) {
kmeans(data_scaled, centers = k, nstart = 25)$tot.withinss
})
df_elbow <- data.frame(k = 1:10, wss = wss)
ggplot(df_elbow, aes(x = k, y = wss)) +
geom_line(color = "steelblue") +
geom_point(color = "steelblue", size = 3) +
geom_vline(xintercept = 3, linetype = "dashed", color = "red") +
scale_x_continuous(breaks = 1:10) +
theme_minimal() +
labs(
title = "Penentuan Jumlah Klaster Optimal dengan Metode Elbow",
x = "Jumlah Klaster (k)",
y = "Total Within Sum of Squares (Inertia)"
)
## Metode Silhouette
set.seed(123)
dist_matrix <- dist(data_scaled)
sil_avg <- sapply(2:10, function(k) {
km <- kmeans(data_scaled, centers = k, nstart = 25)
ss <- silhouette(km$cluster, dist_matrix)
mean(ss[, 3])
})
df_sil <- data.frame(k = 2:10, silhouette = sil_avg)
ggplot(df_sil, aes(x = k, y = silhouette)) +
geom_line(color = "steelblue") +
geom_point(color = "steelblue", size = 3) +
scale_x_continuous(breaks = 2:10) +
theme_minimal() +
labs(
title = "Penentuan Jumlah Klaster Optimal dengan Metode Silhouette",
x = "Jumlah Klaster (k)",
y = "Rata-rata Lebar Silhouette"
)

Tahap 7: Pemodelan K-Means Clustering
set.seed(123)
kmeans_model <- kmeans(data_scaled, centers = 3, nstart = 25)
kmeans_model$size
## [1] 22 83 44
data_hasil <- data_whr %>%
mutate(Cluster = as.factor(kmeans_model$cluster))
Tahap 8: Evaluasi & Profiling Karakteristik Klaster
Evaluasi Silhouette
sil <- silhouette(kmeans_model$cluster, dist(data_scaled))
rata_sil <- round(mean(sil[, 3]), 3)
df_sil_plot <- data.frame(
cluster = factor(sil[, 1]),
sil_width = sil[, 3]
) %>%
arrange(cluster, desc(sil_width)) %>%
mutate(order = row_number())
ggplot(df_sil_plot, aes(x = order, y = sil_width, fill = cluster)) +
geom_col(width = 1) +
geom_hline(yintercept = rata_sil, linetype = "dashed", color = "red") +
theme_minimal() +
labs(
title = paste("Plot Silhouette K-Means (Rata-rata =", rata_sil, ")"),
x = "Observasi (Negara)",
y = "Lebar Silhouette",
fill = "Klaster"
)

Profil Rata-rata Tiap Klaster
profil_klaster <- data_hasil %>%
group_by(Cluster) %>%
summarise(
Jumlah_Negara = n(),
Mean_Ladder_Score = mean(Ladder.score),
Mean_GDP = mean(Logged.GDP.per.capita),
Mean_Social_Support = mean(Social.support),
Mean_Life_Expectancy = mean(Healthy.life.expectancy),
Mean_Freedom = mean(Freedom.to.make.life.choices),
Mean_Generosity = mean(Generosity),
Mean_Corruption = mean(Perceptions.of.corruption)
) %>%
arrange(desc(Mean_Ladder_Score))
kable(t(profil_klaster), digits = 3, caption = "Profil Rata-rata Karakteristik Tiap Klaster")
Profil Rata-rata Karakteristik Tiap Klaster
| Cluster |
1 |
2 |
3 |
| Jumlah_Negara |
22 |
83 |
44 |
| Mean_Ladder_Score |
7.015455 |
5.711000 |
4.455455 |
| Mean_GDP |
10.841500 |
9.795410 |
8.042432 |
| Mean_Social_Support |
0.9286364 |
0.8570723 |
0.6779545 |
| Mean_Life_Expectancy |
72.55732 |
67.32652 |
56.80830 |
| Mean_Freedom |
0.9080455 |
0.7987952 |
0.7197955 |
| Mean_Generosity |
0.06800000 |
-0.07861446 |
0.06304545 |
| Mean_Corruption |
0.3983182 |
0.7975904 |
0.7597045 |
Contoh Negara per Klaster & Posisi Indonesia
contoh_negara <- data_hasil %>%
group_by(Cluster) %>%
slice_max(order_by = Ladder.score, n = 5) %>%
summarise(Contoh_Negara = paste(Country.name, collapse = ", "))
kable(contoh_negara, caption = "5 Negara Teratas di Masing-masing Klaster")
5 Negara Teratas di Masing-masing Klaster
| 1 |
Finland, Denmark, Switzerland, Iceland,
Netherlands |
| 2 |
Israel, Costa Rica, Czech Republic, United States,
Belgium |
| 3 |
Indonesia, Congo (Brazzaville), Ivory Coast, Nepal,
Cameroon |
indonesia <- data_hasil %>%
filter(Country.name == "Indonesia") %>%
select(Country.name, Regional.indicator, Ladder.score, Cluster)
kable(indonesia, digits = 3, caption = "Posisi Klaster Indonesia")
Posisi Klaster Indonesia
| Indonesia |
Southeast Asia |
5.345 |
3 |
Tahap 9: Visualisasi Hasil Clustering
Visualisasi Klaster Berbasis PCA
pca_res <- prcomp(data_scaled, scale. = FALSE)
var_exp <- round(100 * (pca_res$sdev^2 / sum(pca_res$sdev^2)), 1)
df_pca <- data.frame(
PC1 = pca_res$x[, 1],
PC2 = pca_res$x[, 2],
Cluster = data_hasil$Cluster
)
hulls <- df_pca %>%
group_by(Cluster) %>%
slice(chull(PC1, PC2))
ggplot(df_pca, aes(x = PC1, y = PC2, color = Cluster, fill = Cluster)) +
geom_polygon(data = hulls, alpha = 0.15) +
geom_point(size = 2.8, alpha = 0.85) +
theme_minimal() +
labs(
title = "Visualisasi Klaster Negara (PCA Projection)",
x = paste0("PC1 (", var_exp[1], "%)"),
y = paste0("PC2 (", var_exp[2], "%)"),
color = "Klaster",
fill = "Klaster"
)

Scatter Plot GDP vs Social Support
ggplot(data_hasil, aes(x = Logged.GDP.per.capita, y = Social.support, color = Cluster)) +
geom_point(size = 2.8, alpha = 0.85) +
theme_minimal() +
labs(
title = "Hasil K-Means Clustering: Logged GDP per Capita vs Social Support",
x = "Logged GDP per Capita",
y = "Social Support",
color = "Klaster"
)
