This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(ggplot2)
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# Load dataset
data <- read.csv("2015.csv")
# Pilih variabel yang akan digunakan
df <- data[, c("Happiness.Rank", "Happiness.Score", "Standard.Error",
"Economy..GDP.per.Capita.", "Family", "Health..Life.Expectancy.",
"Freedom", "Trust..Government.Corruption.", "Generosity",
"Dystopia.Residual")]
# ---------------------------
# 1. STATISTIKA DESKRIPTIF
# ---------------------------
summary(df)
## Happiness.Rank Happiness.Score Standard.Error Economy..GDP.per.Capita.
## Min. : 1.00 Min. :2.839 Min. :0.01848 Min. :0.0000
## 1st Qu.: 40.25 1st Qu.:4.526 1st Qu.:0.03727 1st Qu.:0.5458
## Median : 79.50 Median :5.232 Median :0.04394 Median :0.9102
## Mean : 79.49 Mean :5.376 Mean :0.04788 Mean :0.8461
## 3rd Qu.:118.75 3rd Qu.:6.244 3rd Qu.:0.05230 3rd Qu.:1.1584
## Max. :158.00 Max. :7.587 Max. :0.13693 Max. :1.6904
## Family Health..Life.Expectancy. Freedom
## Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.8568 1st Qu.:0.4392 1st Qu.:0.3283
## Median :1.0295 Median :0.6967 Median :0.4355
## Mean :0.9910 Mean :0.6303 Mean :0.4286
## 3rd Qu.:1.2144 3rd Qu.:0.8110 3rd Qu.:0.5491
## Max. :1.4022 Max. :1.0252 Max. :0.6697
## Trust..Government.Corruption. Generosity Dystopia.Residual
## Min. :0.00000 Min. :0.0000 Min. :0.3286
## 1st Qu.:0.06168 1st Qu.:0.1506 1st Qu.:1.7594
## Median :0.10722 Median :0.2161 Median :2.0954
## Mean :0.14342 Mean :0.2373 Mean :2.0990
## 3rd Qu.:0.18025 3rd Qu.:0.3099 3rd Qu.:2.4624
## Max. :0.55191 Max. :0.7959 Max. :3.6021
cor(df)
## Happiness.Rank Happiness.Score Standard.Error
## Happiness.Rank 1.0000000 -0.9921053 0.15851647
## Happiness.Score -0.9921053 1.0000000 -0.17725381
## Standard.Error 0.1585165 -0.1772538 1.00000000
## Economy..GDP.per.Capita. -0.7852669 0.7809655 -0.21765082
## Family -0.7336435 0.7406052 -0.12072789
## Health..Life.Expectancy. -0.7356130 0.7241996 -0.31028686
## Freedom -0.5568861 0.5682109 -0.12977324
## Trust..Government.Corruption. -0.3723151 0.3951986 -0.17832465
## Generosity -0.1601416 0.1803185 -0.08843875
## Dystopia.Residual -0.5219995 0.5304735 0.08398094
## Economy..GDP.per.Capita. Family
## Happiness.Rank -0.78526692 -0.73364353
## Happiness.Score 0.78096553 0.74060520
## Standard.Error -0.21765082 -0.12072789
## Economy..GDP.per.Capita. 1.00000000 0.64529939
## Family 0.64529939 1.00000000
## Health..Life.Expectancy. 0.81647800 0.53110399
## Freedom 0.37029971 0.44151821
## Trust..Government.Corruption. 0.30788520 0.20560511
## Generosity -0.01046544 0.08751324
## Dystopia.Residual 0.04005876 0.14811704
## Health..Life.Expectancy. Freedom
## Happiness.Rank -0.73561296 -0.55688609
## Happiness.Score 0.72419960 0.56821090
## Standard.Error -0.31028686 -0.12977324
## Economy..GDP.per.Capita. 0.81647800 0.37029971
## Family 0.53110399 0.44151821
## Health..Life.Expectancy. 1.00000000 0.36047653
## Freedom 0.36047653 1.00000000
## Trust..Government.Corruption. 0.24833468 0.49352352
## Generosity 0.10833522 0.37391613
## Dystopia.Residual 0.01897932 0.06278344
## Trust..Government.Corruption. Generosity
## Happiness.Rank -0.37231512 -0.16014157
## Happiness.Score 0.39519858 0.18031853
## Standard.Error -0.17832465 -0.08843875
## Economy..GDP.per.Capita. 0.30788520 -0.01046544
## Family 0.20560511 0.08751324
## Health..Life.Expectancy. 0.24833468 0.10833522
## Freedom 0.49352352 0.37391613
## Trust..Government.Corruption. 1.00000000 0.27612270
## Generosity 0.27612270 1.00000000
## Dystopia.Residual -0.03310457 -0.10130111
## Dystopia.Residual
## Happiness.Rank -0.52199947
## Happiness.Score 0.53047352
## Standard.Error 0.08398094
## Economy..GDP.per.Capita. 0.04005876
## Family 0.14811704
## Health..Life.Expectancy. 0.01897932
## Freedom 0.06278344
## Trust..Government.Corruption. -0.03310457
## Generosity -0.10130111
## Dystopia.Residual 1.00000000
# ---------------------------
# 2. PRINCIPAL COMPONENT ANALYSIS (PCA)
# ---------------------------
# Normalisasi data
df_scaled <- scale(df)
# Jalankan PCA
pca_model <- prcomp(df_scaled, center = TRUE, scale. = TRUE)
# Varians yang dijelaskan oleh setiap komponen utama
summary(pca_model)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.1694 1.2190 1.0966 0.92183 0.83919 0.71331 0.62002
## Proportion of Variance 0.4706 0.1486 0.1202 0.08498 0.07042 0.05088 0.03844
## Cumulative Proportion 0.4706 0.6192 0.7395 0.82447 0.89489 0.94577 0.98421
## PC8 PC9 PC10
## Standard deviation 0.38389 0.10251 0.0001958
## Proportion of Variance 0.01474 0.00105 0.0000000
## Cumulative Proportion 0.99895 1.00000 1.0000000
# Visualisasi Scree Plot untuk menentukan jumlah komponen utama
fviz_eig(pca_model)
# Visualisasi Biplot PCA
fviz_pca_biplot(pca_model, label = "var", repel = TRUE)
# ---------------------------
# 3. FACTOR ANALYSIS (FA)
# ---------------------------
# Uji KMO untuk melihat apakah data cocok untuk FA
KMO(df_scaled)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = df_scaled)
## Overall MSA = 0.24
## MSA for each item =
## Happiness.Rank Happiness.Score
## 0.97 0.33
## Standard.Error Economy..GDP.per.Capita.
## 0.88 0.27
## Family Health..Life.Expectancy.
## 0.23 0.25
## Freedom Trust..Government.Corruption.
## 0.18 0.11
## Generosity Dystopia.Residual
## 0.04 0.08
# Uji Bartlett untuk memastikan ada korelasi antar variabel
cortest.bartlett(df_scaled)
## R was not square, finding R from data
## $chisq
## [1] 3601.152
##
## $p.value
## [1] 0
##
## $df
## [1] 45
# Tentukan jumlah faktor yang optimal dengan analisis scree plot
fa.parallel(df_scaled, fa = "fa")
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect. Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected. Examine the results carefully
## Parallel analysis suggests that the number of factors = 3 and the number of components = NA
# Jalankan Factor Analysis dengan 2 faktor dan rotasi Varimax
fa_model <- fa(df_scaled, nfactors = 2, rotate = "varimax", fm = "ml")
# Lihat hasil Factor Analysis
print(fa_model$loadings, cutoff = 0.4)
##
## Loadings:
## ML2 ML1
## Happiness.Rank -0.750 -0.653
## Happiness.Score 0.747 0.661
## Standard.Error
## Economy..GDP.per.Capita. 0.881
## Family 0.741
## Health..Life.Expectancy. 0.834
## Freedom 0.601
## Trust..Government.Corruption. 0.472
## Generosity
## Dystopia.Residual 0.984
##
## ML2 ML1
## SS loadings 3.897 2.001
## Proportion Var 0.390 0.200
## Cumulative Var 0.390 0.590
# Visualisasi hasil Factor Analysis
fviz_pca_var(pca_model, col.var = "contrib", repel = TRUE) # Plot berdasarkan kontribusi variabel