The PCA method is one of the methods in multivariate analysis used to reduce variables into certain dimensions or components (Jolliffe, 2014). From various dimensions or variables in assessing welfare, this study aims to determine the main components formed from variables in the Better Life Index dimension according to the OECD in the case of G20 member countries. This study also aims to determine the position of Indonesia in the cluster.
# library
library(knitr)
library(REdaS)
library(factoextra)
library(clValid)
library(tidyverse)
library(cluster)
library(ggplot2)
library(tidyverse)
library(grDevices)
library(dplyr)
library(fclust)
library(psych)
Analysis begins with reading and getting to know the data. This process is also called descriptive analysis.
library(readxl)
dataset <- read_excel("STATA.xlsx", sheet = "Without HC_With BS",
col_names = TRUE) # dataset analisis
dataset <- as.data.frame(dataset)
rownames(dataset) <- dataset$Negara
dataset <- dataset[,-1]
colnames(dataset)=c("Basic Sanitation", "Median Income",
"Unemployment", "Years of Schooling", "Air Pollution",
"Regulatory Quality", "Life Expectancy", "Happiness Index",
"Homicide Rate", "Working Hours")
library(gt)
library(gtExtras)
Variabel <- c("Basic Sanitation", "Median Income",
"Unemployment", "Years of Schooling", "Air Pollution",
"Regulatory Quality", "Life Expectancy", "Happiness Index",
"Homicide Rate", "Working Hours")
Unit <- c("percent (%)", "USD, constant 2015", "percent (%)", "Years",
"Micrograms per cubic meter", "Score", "Years", "Score", "per 100,000 people",
"hours per week")
Source <- c("World Bank", "World Bank", "World Bank", "UNDP", "World Bank",
"World Bank", "World Health Organization", "World Happiness Report",
"World Health Organization", "ILO")
df1 <- data.frame(Variabel, Unit, Source)
df1 %>%
gt() %>%
gt_theme_538() # generate tabel
| Variabel | Unit | Source |
|---|---|---|
| Basic Sanitation | percent (%) | World Bank |
| Median Income | USD, constant 2015 | World Bank |
| Unemployment | percent (%) | World Bank |
| Years of Schooling | Years | UNDP |
| Air Pollution | Micrograms per cubic meter | World Bank |
| Regulatory Quality | Score | World Bank |
| Life Expectancy | Years | World Health Organization |
| Happiness Index | Score | World Happiness Report |
| Homicide Rate | per 100,000 people | World Health Organization |
| Working Hours | hours per week | ILO |
# Visualisasi
library(corrplot)
library(viridis)
C <- cor(dataset)
library(RColorBrewer)
corrplot(C, method = "color", outline = T, addgrid.col = "darkgray", order="alphabet",
tl.col = "black", addCoef.col = "black", number.digits = 1,
number.cex =0.75, col =colorRampPalette(c("#8B0000", "white", "grey"))(100))
boxplot(dataset, col = colorRampPalette(c("#8B0000","white","grey"))(100))
When there are several variables with different unit sizes, it is necessary to normalize the data first.
# Normalisasi
datascaled <- scale(dataset)
datascaled <- as.data.frame(datascaled)
boxplot(datascaled, col = colorRampPalette(c("#8B0000","white","grey"))(100))
Regarding observation cluster analysis, factor analysis is required first. Factor analysis consists of data sufficiency analysis and multicollinearity analysis. Data sufficiency analysis can be tested with Kaiser-Meyer Olkin (KMO) by paying attention to the Measure of Sampling Adequacy (MSA) value of each variable. Data will be processed to clustering analysis if the KMO and MSA values are greater than 0.5. Meanwhile, multicollinearity can be detected through the Bartlett-Sphericity test. If the p-value < α, then there is multicollinearity in the data. PCA will be performed if there is multicollinearity in the data (Luthfi & Wijayanto, 2021; Afira & Wijayanto, 2021).
library(REdaS)
KMO_res <- KMOS(datascaled)
as.data.frame(KMO_res$MSA)
## KMO_res$MSA
## Basic Sanitation 0.7175191
## Median Income 0.7399388
## Unemployment 0.5236364
## Years of Schooling 0.6941534
## Air Pollution 0.7796938
## Regulatory Quality 0.7080481
## Life Expectancy 0.6806854
## Happiness Index 0.7675867
## Homicide Rate 0.6369711
## Working Hours 0.8330062
KMO_res$KMO
## [1] 0.7171577
bs_res <- bart_spher(datascaled) # metode Barlett test of sphericity
bs_res
## Bartlett's Test of Sphericity
##
## Call: bart_spher(x = datascaled)
##
## X2 = 284.066
## df = 45
## p-value < 2.22e-16
Test <- c("Kaiser Meyer Olkin (KMO)", "Bartlett-Sphericity")
Value <- c(0.7171577, 2.22e-16)
factor_analysis <- data.frame(Test, Value)
factor_analysis %>%
gt() %>%
gt_theme_538()
| Test | Value |
|---|---|
| Kaiser Meyer Olkin (KMO) | 7.171577e-01 |
| Bartlett-Sphericity | 2.220000e-16 |
The table above shows that there is multicollinearity in the data. This indicates the need for Principal Component Analysis.
# Principal Component Analysis
PCA <- prcomp(datascaled)
PCA
## Standard deviations (1, .., p=10):
## [1] 2.2864420 1.2771166 0.8875965 0.8588440 0.6691782 0.6346937 0.6064621
## [8] 0.4524031 0.3603092 0.2505839
##
## Rotation (n x k) = (10 x 10):
## PC1 PC2 PC3 PC4 PC5
## Basic Sanitation -0.3598144 -0.001805468 0.05567502 -0.37608011 0.18584605
## Median Income -0.3935204 0.041013042 0.05616831 -0.02108076 -0.40615146
## Unemployment 0.1751425 0.418733048 0.79456401 -0.24063747 -0.01006975
## Years of Schooling -0.3242909 0.096243580 0.10826354 0.62879635 0.03963471
## Air Pollution 0.2714083 -0.424048623 0.11742210 -0.22388649 -0.58598845
## Regulatory Quality 0.2728626 0.382233032 -0.51365475 -0.30490900 0.16649947
## Life Expectancy -0.3702465 -0.091097328 -0.00328146 -0.49486335 0.13967271
## Happiness Index -0.3353680 0.295086018 -0.25279384 -0.08683493 -0.57326186
## Homicide Rate 0.2590961 0.559483297 -0.07463951 0.09149083 -0.25854900
## Working Hours 0.3391672 -0.281778481 -0.05980603 0.03054234 -0.11287244
## PC6 PC7 PC8 PC9
## Basic Sanitation -0.547256744 0.384434069 0.246960780 0.17042837
## Median Income 0.296948914 0.007424408 -0.520619832 -0.05019177
## Unemployment 0.001315893 -0.002921670 0.001039330 -0.32186767
## Years of Schooling -0.474844955 0.172446217 -0.316092266 -0.23395310
## Air Pollution -0.494000784 -0.179757285 -0.195202930 0.08391455
## Regulatory Quality -0.270265706 -0.118538548 -0.360165350 -0.41050763
## Life Expectancy 0.190250528 0.190236524 -0.369922944 0.09640055
## Happiness Index 0.029047961 0.019162640 0.494895712 -0.31473411
## Homicide Rate -0.026842085 0.337233018 -0.135520717 0.63020766
## Working Hours 0.178919155 0.791051055 -0.009802288 -0.35920971
## PC10
## Basic Sanitation -0.39260748
## Median Income -0.55955638
## Unemployment 0.03221557
## Years of Schooling 0.25890539
## Air Pollution 0.13315201
## Regulatory Quality -0.09788061
## Life Expectancy 0.60966270
## Happiness Index 0.23492140
## Homicide Rate 0.09531128
## Working Hours -0.03761905
eigen_value <- get_eigenvalue(PCA)
eigen_value %>%
gt() %>%
gt_theme_538()