library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(psych)
library(factoextra)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
## Welcome to factoextra!
## Want to learn more? See two factoextra-related books at https://www.datanovia.com/en/product/practical-guide-to-principal-component-methods-in-r/
library(corrplot)
## corrplot 0.95 loaded
Langkah pertama adalah memanggil data lagu Spotify. Memilih 11 variabel numerik yang merepresentasikan karakteristik audio sebuah lagu.
# Membaca dataset
data_spotify <- read.csv("dataset.csv")
# Memilih variabel numerik dan membuang data kosong
data_bersih <- data_spotify %>%
select(duration_ms, popularity, danceability, energy, loudness,
speechiness, acousticness, instrumentalness, liveness, valence, tempo) %>%
na.omit()
# Menampilkan 5 data teratas
head(data_bersih, 5)
## duration_ms popularity danceability energy loudness speechiness acousticness
## 1 230666 73 0.676 0.4610 -6.746 0.1430 0.0322
## 2 149610 55 0.420 0.1660 -17.235 0.0763 0.9240
## 3 210826 57 0.438 0.3590 -9.734 0.0557 0.2100
## 4 201933 71 0.266 0.0596 -18.515 0.0363 0.9050
## 5 198853 82 0.618 0.4430 -9.681 0.0526 0.4690
## instrumentalness liveness valence tempo
## 1 1.01e-06 0.3580 0.715 87.917
## 2 5.56e-06 0.1010 0.267 77.489
## 3 0.00e+00 0.1170 0.120 76.332
## 4 7.07e-05 0.1320 0.143 181.740
## 5 0.00e+00 0.0829 0.167 119.949
Sebelum melakukan reduksi dimensi, perlu melihat bagaimana variabel-variabel ini saling berhubungan. Jika banyak warna biru tua atau merah tua, berarti data kita cocok untuk PCA/FA.
# Menghitung matriks korelasi
matriks_korelasi <- cor(data_bersih)
# visualisasi
corrplot(matriks_korelasi, method = "color", type = "upper",
tl.col = "black", tl.srt = 45, addCoef.col = "black",
number.cex = 0.7, title = "\n\nHeatmap Korelasi Fitur Spotify",
mar = c(0,0,1,0))
Memastikan data ini “layak” untuk dijadikan faktor-faktor baru.
# Uji KMO (Syarat > 0.5)
kmo_hasil <- KMO(data_bersih)
print(kmo_hasil)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = data_bersih)
## Overall MSA = 0.6
## MSA for each item =
## duration_ms popularity danceability energy
## 0.68 0.41 0.49 0.59
## loudness speechiness acousticness instrumentalness
## 0.66 0.35 0.70 0.53
## liveness valence tempo
## 0.47 0.54 0.81
# Uji Bartlett (Syarat p-value < 0.05)
bartlett_hasil <- cortest.bartlett(cor(data_bersih), n = nrow(data_bersih))
print(bartlett_hasil)
## $chisq
## [1] 320940.9
##
## $p.value
## [1] 0
##
## $df
## [1] 55
PCA membantu melihat berapa banyak informasi (variansi) yang bisa diringkas.
# Melakukan proses PCA dengan standarisasi
hasil_pca <- prcomp(data_bersih, center = TRUE, scale. = TRUE)
# Visualisasi Scree Plot
fviz_eig(hasil_pca,
addlabels = TRUE,
ylim = c(0, 50),
title = "Scree Plot: Variansi Komponen Utama",
barfill = "steelblue",
barcolor = "darkblue")
Karena data sangat besar, Menggunakan sampel 500 lagu agar grafik terbaca dengan jelas.
set.seed(123)
data_sampel <- data_bersih[sample(1:nrow(data_bersih), 500), ]
pca_sampel <- prcomp(data_sampel, scale. = TRUE)
fviz_pca_biplot(pca_sampel,
repel = TRUE,
col.var = "red",
col.ind = "gray",
alpha.ind = 0.5,
geom.ind = "point",
title = "Biplot PCA (Sampel 500 Lagu Spotify)")
Berdasarkan Scree Plot, kita akan meringkas 11 variabel asli menjadi 4 faktor utama.
# Melakukan FA dengan rotasi Varimax agar interpretasi lebih mudah
hasil_fa <- fa(data_bersih, nfactors = 4, rotate = "varimax", fm = "ml")
# Loading Factor (Hanya menampilkan angka di atas 0.3)
print(hasil_fa$loadings, cutoff = 0.3)
##
## Loadings:
## ML2 ML1 ML3 ML4
## duration_ms
## popularity
## danceability 0.989
## energy 0.951
## loudness 0.750
## speechiness 0.377
## acousticness -0.783
## instrumentalness 0.969
## liveness 0.423
## valence 0.488
## tempo
##
## ML2 ML1 ML3 ML4
## SS loadings 2.215 1.369 1.103 0.500
## Proportion Var 0.201 0.124 0.100 0.045
## Cumulative Var 0.201 0.326 0.426 0.472
Berdasarkan output di atas, kita dapat mengelompokkan variabel sebagai berikut:
```