knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

1 Import Library & Membaca Dataset

library(readr)
library(dplyr)
library(psych)
library(factoextra)
library(corrplot)

data <- read.csv("WHR2024.csv")

2 Menampilkan Dataset

head(data)
##   Country.name Ladder.score upperwhisker lowerwhisker
## 1      Finland        7.741        7.815        7.667
## 2      Denmark        7.583        7.665        7.500
## 3      Iceland        7.525        7.618        7.433
## 4       Sweden        7.344        7.422        7.267
## 5       Israel        7.341        7.405        7.277
## 6  Netherlands        7.319        7.383        7.256
##   Explained.by..Log.GDP.per.capita Explained.by..Social.support
## 1                            1.844                        1.572
## 2                            1.908                        1.520
## 3                            1.881                        1.617
## 4                            1.878                        1.501
## 5                            1.803                        1.513
## 6                            1.901                        1.462
##   Explained.by..Healthy.life.expectancy
## 1                                 0.695
## 2                                 0.699
## 3                                 0.718
## 4                                 0.724
## 5                                 0.740
## 6                                 0.706
##   Explained.by..Freedom.to.make.life.choices Explained.by..Generosity
## 1                                      0.859                    0.142
## 2                                      0.823                    0.204
## 3                                      0.819                    0.258
## 4                                      0.838                    0.221
## 5                                      0.641                    0.153
## 6                                      0.725                    0.247
##   Explained.by..Perceptions.of.corruption Dystopia...residual
## 1                                   0.546               2.082
## 2                                   0.548               1.881
## 3                                   0.182               2.050
## 4                                   0.524               1.658
## 5                                   0.193               2.298
## 6                                   0.372               1.906

3 Cek Struktur Dataset

str(data)
## 'data.frame':    143 obs. of  11 variables:
##  $ Country.name                              : chr  "Finland" "Denmark" "Iceland" "Sweden" ...
##  $ Ladder.score                              : num  7.74 7.58 7.53 7.34 7.34 ...
##  $ upperwhisker                              : num  7.82 7.67 7.62 7.42 7.41 ...
##  $ lowerwhisker                              : num  7.67 7.5 7.43 7.27 7.28 ...
##  $ Explained.by..Log.GDP.per.capita          : num  1.84 1.91 1.88 1.88 1.8 ...
##  $ Explained.by..Social.support              : num  1.57 1.52 1.62 1.5 1.51 ...
##  $ Explained.by..Healthy.life.expectancy     : num  0.695 0.699 0.718 0.724 0.74 0.706 0.704 0.708 0.747 0.692 ...
##  $ Explained.by..Freedom.to.make.life.choices: num  0.859 0.823 0.819 0.838 0.641 0.725 0.835 0.801 0.759 0.756 ...
##  $ Explained.by..Generosity                  : num  0.142 0.204 0.258 0.221 0.153 0.247 0.224 0.146 0.173 0.225 ...
##  $ Explained.by..Perceptions.of.corruption   : num  0.546 0.548 0.182 0.524 0.193 0.372 0.484 0.432 0.498 0.323 ...
##  $ Dystopia...residual                       : num  2.08 1.88 2.05 1.66 2.3 ...

4 Pembentukan Variabel Baru dan Seleksi Variabel Penelitian

data <- data %>%
  mutate(
    GDP_social_interaction =
      Explained.by..Log.GDP.per.capita *
      Explained.by..Social.support,

    health_freedom_index =
      Explained.by..Healthy.life.expectancy *
      Explained.by..Freedom.to.make.life.choices,

    corruption_inverse =
      1 / (Explained.by..Perceptions.of.corruption + 0.001)
  )

data_selected <- data %>%
  select(
    Explained.by..Log.GDP.per.capita,
    Explained.by..Social.support,
    Explained.by..Healthy.life.expectancy,
    Explained.by..Freedom.to.make.life.choices,
    Explained.by..Generosity,
    Explained.by..Perceptions.of.corruption,
    Dystopia...residual,
    GDP_social_interaction,
    health_freedom_index,
    corruption_inverse
  ) %>%
  na.omit()

dim(data_selected)
## [1] 140  10

5 Transformasi dan Pembentukan Variabel Baru

data$GDP_social_interaction <- data$Explained.by..Log.GDP.per.capita *
                               data$Explained.by..Social.support

data$health_freedom_index <- data$Explained.by..Healthy.life.expectancy *
                             data$Explained.by..Freedom.to.make.life.choices

data$corruption_inverse <- 1 / (data$Explained.by..Perceptions.of.corruption + 0.001)

6 Standarisasi Data dan Uji Kelayakan Analisis Faktor

data_scaled <- scale(data_selected)
KMO(data_scaled)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = data_scaled)
## Overall MSA =  0.57
## MSA for each item = 
##           Explained.by..Log.GDP.per.capita 
##                                       0.58 
##               Explained.by..Social.support 
##                                       0.56 
##      Explained.by..Healthy.life.expectancy 
##                                       0.57 
## Explained.by..Freedom.to.make.life.choices 
##                                       0.42 
##                   Explained.by..Generosity 
##                                       0.47 
##    Explained.by..Perceptions.of.corruption 
##                                       0.78 
##                        Dystopia...residual 
##                                       0.48 
##                     GDP_social_interaction 
##                                       0.59 
##                       health_freedom_index 
##                                       0.60 
##                         corruption_inverse 
##                                       0.42

7 Uji Kelayakan Analisis Faktor

cor_matrix <- cor(data_selected)
cortest.bartlett(cor_matrix, n = nrow(data_selected))
## $chisq
## [1] 1502.535
## 
## $p.value
## [1] 1.561919e-285
## 
## $df
## [1] 45

8 Menampilkan Deskripsi Data yang dipilih

describe(data_selected)
##                                            vars   n  mean    sd median trimmed
## Explained.by..Log.GDP.per.capita              1 140  1.38  0.43   1.43    1.40
## Explained.by..Social.support                  2 140  1.13  0.33   1.24    1.17
## Explained.by..Healthy.life.expectancy         3 140  0.52  0.16   0.55    0.53
## Explained.by..Freedom.to.make.life.choices    4 140  0.62  0.16   0.64    0.64
## Explained.by..Generosity                      5 140  0.15  0.07   0.14    0.14
## Explained.by..Perceptions.of.corruption       6 140  0.15  0.13   0.12    0.13
## Dystopia...residual                           7 140  1.58  0.54   1.64    1.60
## GDP_social_interaction                        8 140  1.67  0.83   1.72    1.69
## health_freedom_index                          9 140  0.33  0.15   0.36    0.34
## corruption_inverse                           10 140 20.51 85.47   8.23    9.93
##                                             mad   min     max  range  skew
## Explained.by..Log.GDP.per.capita           0.50  0.00    2.14   2.14 -0.50
## Explained.by..Social.support               0.30  0.00    1.62   1.62 -0.97
## Explained.by..Healthy.life.expectancy      0.17  0.00    0.86   0.86 -0.53
## Explained.by..Freedom.to.make.life.choices 0.15  0.00    0.86   0.86 -1.00
## Explained.by..Generosity                   0.07  0.00    0.40   0.40  0.65
## Explained.by..Perceptions.of.corruption    0.09  0.00    0.58   0.58  1.49
## Dystopia...residual                        0.39 -0.07    3.00   3.07 -0.59
## GDP_social_interaction                     1.09  0.00    3.04   3.04 -0.21
## health_freedom_index                       0.18  0.00    0.61   0.61 -0.23
## corruption_inverse                         6.35  1.74 1000.00 998.26 10.79
##                                            kurtosis   se
## Explained.by..Log.GDP.per.capita              -0.42 0.04
## Explained.by..Social.support                   0.40 0.03
## Explained.by..Healthy.life.expectancy         -0.44 0.01
## Explained.by..Freedom.to.make.life.choices     1.16 0.01
## Explained.by..Generosity                       0.72 0.01
## Explained.by..Perceptions.of.corruption        1.83 0.01
## Dystopia...residual                            0.68 0.05
## GDP_social_interaction                        -1.18 0.07
## health_freedom_index                          -1.01 0.01
## corruption_inverse                           120.28 7.22

9 Menampilkan Corrplot Data yang dipilih

cor_matrix <- cor(data_selected)
corrplot(cor_matrix, method="color")

10 Ringkasan Hasil PCA

pca_result <- prcomp(data_scaled, center = TRUE, scale. = TRUE)
summary(pca_result)
## Importance of components:
##                           PC1    PC2    PC3    PC4     PC5     PC6     PC7
## Standard deviation     2.2062 1.1241 1.0711 1.0100 0.77885 0.76270 0.56002
## Proportion of Variance 0.4868 0.1264 0.1147 0.1020 0.06066 0.05817 0.03136
## Cumulative Proportion  0.4868 0.6131 0.7278 0.8298 0.89051 0.94868 0.98004
##                            PC8     PC9    PC10
## Standard deviation     0.42021 0.12997 0.07803
## Proportion of Variance 0.01766 0.00169 0.00061
## Cumulative Proportion  0.99770 0.99939 1.00000

11 Grafik Scree Plot Hasil PCA

fviz_eig(pca_result)

12 Histogram Data yang dipilih

par(mfrow=c(2,3))
for(i in 1:min(6, ncol(data_selected))){
  hist(data_selected[,i],
       main=names(data_selected)[i],
       col="skyblue")
}

13 Boxplot Data yang dipilih

par(mfrow=c(2,3))
for(i in 1:min(6, ncol(data_selected))){
  boxplot(data_selected[,i],
          main=names(data_selected)[i],
          col="orange")
}

14 Interpretasi Loading pada Komponen Utama

pca_result$rotation
##                                                    PC1          PC2
## Explained.by..Log.GDP.per.capita           -0.40703256 -0.276297245
## Explained.by..Social.support               -0.38546615 -0.066014205
## Explained.by..Healthy.life.expectancy      -0.40106553 -0.182043592
## Explained.by..Freedom.to.make.life.choices -0.30288969  0.433869741
## Explained.by..Generosity                   -0.05133338  0.622377796
## Explained.by..Perceptions.of.corruption    -0.24491330  0.136178022
## Dystopia...residual                        -0.06480643  0.493200739
## GDP_social_interaction                     -0.42826162 -0.175426386
## health_freedom_index                       -0.42768775  0.134147175
## corruption_inverse                          0.00150799  0.001926501
##                                                     PC3          PC4
## Explained.by..Log.GDP.per.capita            0.001675601  0.038672062
## Explained.by..Social.support                0.192936790 -0.075947040
## Explained.by..Healthy.life.expectancy       0.043493163  0.013535451
## Explained.by..Freedom.to.make.life.choices -0.011729703 -0.079705159
## Explained.by..Generosity                    0.009716100  0.581302526
## Explained.by..Perceptions.of.corruption    -0.514571451  0.215622361
## Dystopia...residual                         0.205045433 -0.703959650
## GDP_social_interaction                      0.065007913  0.001693138
## health_freedom_index                       -0.007786302 -0.011724262
## corruption_inverse                          0.805937269  0.325722665
##                                                     PC5         PC6         PC7
## Explained.by..Log.GDP.per.capita           -0.008278696 -0.11064795  0.01887290
## Explained.by..Social.support               -0.376349737 -0.06653277 -0.53059071
## Explained.by..Healthy.life.expectancy      -0.031855498 -0.11675031  0.67141017
## Explained.by..Freedom.to.make.life.choices  0.180997781  0.68903474 -0.16130740
## Explained.by..Generosity                   -0.432884440 -0.23578813  0.11551111
## Explained.by..Perceptions.of.corruption     0.604062195 -0.38936838 -0.24111559
## Dystopia...residual                         0.067468730 -0.43807251  0.07609362
## GDP_social_interaction                     -0.157219069 -0.15128889 -0.25260363
## health_freedom_index                        0.103714160  0.25561049  0.31027022
## corruption_inverse                          0.481882561 -0.08714683 -0.06292558
##                                                    PC8           PC9
## Explained.by..Log.GDP.per.capita           -0.69735586 -0.2829291050
## Explained.by..Social.support                0.50335627 -0.1915922993
## Explained.by..Healthy.life.expectancy       0.33560026 -0.3878613111
## Explained.by..Freedom.to.make.life.choices -0.11892621 -0.3353325071
## Explained.by..Generosity                   -0.12231489 -0.0245353685
## Explained.by..Perceptions.of.corruption     0.17780794 -0.0623206866
## Dystopia...residual                        -0.11085863 -0.0141794860
## GDP_social_interaction                     -0.23229983  0.4215941469
## health_freedom_index                        0.14188191  0.6617625717
## corruption_inverse                          0.02442088 -0.0009946777
##                                                    PC10
## Explained.by..Log.GDP.per.capita           -0.421265033
## Explained.by..Social.support               -0.293935429
## Explained.by..Healthy.life.expectancy       0.274646594
## Explained.by..Freedom.to.make.life.choices  0.231049580
## Explained.by..Generosity                   -0.010630996
## Explained.by..Perceptions.of.corruption    -0.007817490
## Dystopia...residual                        -0.014486339
## GDP_social_interaction                      0.662169979
## health_freedom_index                       -0.410449143
## corruption_inverse                          0.001229062

15 Hasil FA

fa_result <- fa(data_scaled, nfactors=3, rotate="varimax")
print(fa_result, cut=0.4)
## Factor Analysis using method =  minres
## Call: fa(r = data_scaled, nfactors = 3, rotate = "varimax")
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                              MR1   MR3   MR2    h2    u2 com
## Explained.by..Log.GDP.per.capita            0.95             0.915 0.085 1.0
## Explained.by..Social.support                0.81             0.721 0.279 1.2
## Explained.by..Healthy.life.expectancy       0.87             0.767 0.233 1.0
## Explained.by..Freedom.to.make.life.choices  0.41  0.79       0.800 0.200 1.5
## Explained.by..Generosity                                     0.092 0.908 1.3
## Explained.by..Perceptions.of.corruption                 0.91 0.997 0.003 1.4
## Dystopia...residual                                          0.097 0.903 1.2
## GDP_social_interaction                      0.99             0.984 0.016 1.0
## health_freedom_index                        0.80  0.56       0.972 0.028 1.9
## corruption_inverse                                           0.051 0.949 1.0
## 
##                        MR1  MR3  MR2
## SS loadings           4.23 1.21 0.96
## Proportion Var        0.42 0.12 0.10
## Cumulative Var        0.42 0.54 0.64
## Proportion Explained  0.66 0.19 0.15
## Cumulative Proportion 0.66 0.85 1.00
## 
## Mean item complexity =  1.3
## Test of the hypothesis that 3 factors are sufficient.
## 
## df null model =  45  with the objective function =  11.14 with Chi Square =  1502.54
## df of  the model are 18  and the objective function was  3.54 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.05 
## 
## The harmonic n.obs is  140 with the empirical chi square  7.16  with prob <  0.99 
## The total n.obs was  140  with Likelihood Chi Square =  469.77  with prob <  2.3e-88 
## 
## Tucker Lewis Index of factoring reliability =  0.213
## RMSEA index =  0.423  and the 90 % confidence intervals are  0.392 0.459
## BIC =  380.82
## Fit based upon off diagonal values = 0.99
## Measures of factor score adequacy             
##                                                    MR1  MR3  MR2
## Correlation of (regression) scores with factors   0.99 0.95 1.00
## Multiple R square of scores with factors          0.98 0.90 0.99
## Minimum correlation of possible factor scores     0.97 0.81 0.99