1 Pendahuluan

Selamat datang pada LBB 3 saya. Pada LBB ini kita akan melakukan prediksi life expectancy dari data world happiness report tahun 2022. Data ini didapatkan dari website kaggle (https://www.kaggle.com/datasets/mathurinache/world-happiness-report/data)

2 Data PreProcessing

Pertama-tama kita akan lakukan data prepocessing dengan melakukan perubahan beberapa kolom ke tipe data yang tepat dan memeriksa missing value yang ada.

library(dplyr)
library(GGally)
library(MLmetrics)
library(lmtest)
library(car)
happy <- read.csv("datainput/2022.csv") 
head(happy)
##   RANK     Country Happiness.score Whisker.high Whisker.low
## 1    1     Finland           7,821        7,886       7,756
## 2    2     Denmark           7,636        7,710       7,563
## 3    3     Iceland           7,557        7,651       7,464
## 4    4 Switzerland           7,512        7,586       7,437
## 5    5 Netherlands           7,415        7,471       7,359
## 6    6 Luxembourg*           7,404        7,501       7,307
##   Dystopia..1.83....residual Explained.by..GDP.per.capita
## 1                      2,518                        1,892
## 2                      2,226                        1,953
## 3                      2,320                        1,936
## 4                      2,153                        2,026
## 5                      2,137                        1,945
## 6                      2,042                        2,209
##   Explained.by..Social.support Explained.by..Healthy.life.expectancy
## 1                        1,258                                 0,775
## 2                        1,243                                 0,777
## 3                        1,320                                 0,803
## 4                        1,226                                 0,822
## 5                        1,206                                 0,787
## 6                        1,155                                 0,790
##   Explained.by..Freedom.to.make.life.choices Explained.by..Generosity
## 1                                      0,736                    0,109
## 2                                      0,719                    0,188
## 3                                      0,718                    0,270
## 4                                      0,677                    0,147
## 5                                      0,651                    0,271
## 6                                      0,700                    0,120
##   Explained.by..Perceptions.of.corruption
## 1                                   0,534
## 2                                   0,532
## 3                                   0,191
## 4                                   0,461
## 5                                   0,419
## 6                                   0,388
# Function untuk merubah string menjadi numeric
convert_number <- function(x){
    x <- as.character(x)
    x <- gsub(pattern = ",", replacement = ".",x = x, fixed = TRUE)
    x <- as.numeric(x)
    return(x)
}
# Merubah tipe data pada kolom 3 sampai 12 menjadi tipe data numeric
happy[c(3 : 12)] <- lapply(happy[c(3:12)], convert_number)
glimpse(happy)
## Rows: 147
## Columns: 12
## $ RANK                                       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, …
## $ Country                                    <chr> "Finland", "Denmark", "Icel…
## $ Happiness.score                            <dbl> 7.821, 7.636, 7.557, 7.512,…
## $ Whisker.high                               <dbl> 7.886, 7.710, 7.651, 7.586,…
## $ Whisker.low                                <dbl> 7.756, 7.563, 7.464, 7.437,…
## $ Dystopia..1.83....residual                 <dbl> 2.518, 2.226, 2.320, 2.153,…
## $ Explained.by..GDP.per.capita               <dbl> 1.892, 1.953, 1.936, 2.026,…
## $ Explained.by..Social.support               <dbl> 1.258, 1.243, 1.320, 1.226,…
## $ Explained.by..Healthy.life.expectancy      <dbl> 0.775, 0.777, 0.803, 0.822,…
## $ Explained.by..Freedom.to.make.life.choices <dbl> 0.736, 0.719, 0.718, 0.677,…
## $ Explained.by..Generosity                   <dbl> 0.109, 0.188, 0.270, 0.147,…
## $ Explained.by..Perceptions.of.corruption    <dbl> 0.534, 0.532, 0.191, 0.461,…
happy <- happy %>% rename("GDP.per.capita" = "Explained.by..GDP.per.capita", "Social.support" = "Explained.by..Social.support",
                          "Healthy.life.expectancy" = "Explained.by..Healthy.life.expectancy", "Freedom.of.choices" = "Explained.by..Freedom.to.make.life.choices", "Generosity" = "Explained.by..Generosity", "Perception.of.corruption" = "Explained.by..Perceptions.of.corruption")

Sekarang kita akan periksa apakah ada missing value pada data kita

anyNA(happy)
## [1] TRUE
table(is.na(happy))
## 
## FALSE  TRUE 
##  1754    10

karena missing value pada data kita bisa dikatakan sedikit maka aman untuk dihapus.

happy <- na.omit(happy)
anyNA(happy)
## [1] FALSE

3 Exploratory Data Analysis

Kemudian pada tahap ini kita akan melakukan eksplorasi data. pertama-tama kita akan memisahkan kolom yang akan kita gunakan sebagai variable prediktor.

h <- happy %>% select(,c(7:12))

Kemudian kita akan melihat korelasi dari bebrapa variable prediktor dengan variable health life expectancy

ggcorr(h, label = TRUE, label_size = 4 , hjust = 1)

dapat dilihat terdapat beberapa variable yang dapat mempengaruhi variable health life expectancy seperti social support, dan GDP per capita.

Kemudian kita periksa apakah terdapat outlier pada data yang kita miliki.

boxplot(h$Healthy.life.expectancy)

Setelah kita perhatikan, terdapat outlier untuk setiap variable. Sehingga kita perlu menghapus outlier tersebut

boxplot(h$Perception.of.corruption)

h$Perception.of.corruption[h$Perception.of.corruption > 0.34]
##  [1] 0.534 0.532 0.461 0.419 0.388 0.512 0.474 0.483 0.341 0.408 0.358 0.368
## [13] 0.587 0.383 0.341 0.544
h_no_outlier <- h
h_no_outlier <- h_no_outlier[h_no_outlier$GDP.per.capita > 0.3,]
h_no_outlier <- h_no_outlier[h_no_outlier$Social.support > 0.2,]
h_no_outlier <- h_no_outlier[h_no_outlier$Healthy.life.expectancy > 0.2,]
h_no_outlier <- h_no_outlier[h_no_outlier$Freedom.of.choices > 0.2,]
h_no_outlier <- h_no_outlier[h_no_outlier$Generosity < 0.36,]
h_no_outlier <- h_no_outlier[h_no_outlier$Perception.of.corruption < 0.3,]
h <- h_no_outlier
boxplot(h$Perception.of.corruption)

4 Regresi

model_LF_GDP <- lm(Healthy.life.expectancy ~ GDP.per.capita , data = h)
model_LF_GDP
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita, data = h)
## 
## Coefficients:
##    (Intercept)  GDP.per.capita  
##        0.09446         0.34929
summary(model_LF_GDP)
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita, data = h)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33945 -0.05393  0.01130  0.06720  0.17015 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.09446    0.03391   2.786  0.00628 ** 
## GDP.per.capita  0.34929    0.02355  14.833  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09053 on 111 degrees of freedom
## Multiple R-squared:  0.6647, Adjusted R-squared:  0.6616 
## F-statistic:   220 on 1 and 111 DF,  p-value: < 2.2e-16
m1 <- lm(Healthy.life.expectancy ~ ., data = h)
step(m1, direction = "backward")
## Start:  AIC=-538.05
## Healthy.life.expectancy ~ GDP.per.capita + Social.support + Freedom.of.choices + 
##     Generosity + Perception.of.corruption
## 
##                            Df Sum of Sq     RSS     AIC
## - Perception.of.corruption  1   0.00197 0.87107 -539.79
## - Generosity                1   0.00806 0.87715 -539.01
## <none>                                  0.86909 -538.05
## - Social.support            1   0.01578 0.88488 -538.02
## - Freedom.of.choices        1   0.01579 0.88488 -538.01
## - GDP.per.capita            1   0.48853 1.35763 -489.65
## 
## Step:  AIC=-539.79
## Healthy.life.expectancy ~ GDP.per.capita + Social.support + Freedom.of.choices + 
##     Generosity
## 
##                      Df Sum of Sq     RSS     AIC
## - Generosity          1   0.00911 0.88018 -540.62
## - Social.support      1   0.01434 0.88541 -539.95
## <none>                            0.87107 -539.79
## - Freedom.of.choices  1   0.01858 0.88965 -539.41
## - GDP.per.capita      1   0.50356 1.37462 -490.24
## 
## Step:  AIC=-540.62
## Healthy.life.expectancy ~ GDP.per.capita + Social.support + Freedom.of.choices
## 
##                      Df Sum of Sq     RSS     AIC
## - Social.support      1   0.01086 0.89104 -541.23
## - Freedom.of.choices  1   0.01353 0.89371 -540.89
## <none>                            0.88018 -540.62
## - GDP.per.capita      1   0.65388 1.53406 -479.84
## 
## Step:  AIC=-541.23
## Healthy.life.expectancy ~ GDP.per.capita + Freedom.of.choices
## 
##                      Df Sum of Sq     RSS     AIC
## <none>                            0.89104 -541.23
## - Freedom.of.choices  1   0.01861 0.90965 -540.90
## - GDP.per.capita      1   1.49896 2.39000 -431.74
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Freedom.of.choices, 
##     data = h)
## 
## Coefficients:
##        (Intercept)      GDP.per.capita  Freedom.of.choices  
##            0.05373             0.33701             0.11186
model_LF_back <- lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
    Perception.of.corruption, data = h)
summary(model_LF_back)
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
##     Perception.of.corruption, data = h)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.32732 -0.04529  0.01061  0.06655  0.16349 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               0.07072    0.03739   1.891   0.0612 .  
## GDP.per.capita            0.30882    0.03498   8.830 1.98e-14 ***
## Social.support            0.07568    0.04997   1.514   0.1328    
## Perception.of.corruption  0.10555    0.13010   0.811   0.4190    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09028 on 109 degrees of freedom
## Multiple R-squared:  0.6725, Adjusted R-squared:  0.6635 
## F-statistic: 74.61 on 3 and 109 DF,  p-value: < 2.2e-16
step(m1, direction = "forward")
## Start:  AIC=-538.05
## Healthy.life.expectancy ~ GDP.per.capita + Social.support + Freedom.of.choices + 
##     Generosity + Perception.of.corruption
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
##     Freedom.of.choices + Generosity + Perception.of.corruption, 
##     data = h)
## 
## Coefficients:
##              (Intercept)            GDP.per.capita            Social.support  
##                  0.06320                   0.29185                   0.07186  
##       Freedom.of.choices                Generosity  Perception.of.corruption  
##                  0.10989                  -0.13033                   0.06529
m2 <- lm(Healthy.life.expectancy ~ 1, data = h)
model_LF_forw <- lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
    Freedom.of.choices + Generosity + Perception.of.corruption, 
    data = h, scope = list(lower = m2 , upper = m1))
summary(model_LF_forw)
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
##     Freedom.of.choices + Generosity + Perception.of.corruption, 
##     data = h, scope = list(lower = m2, upper = m1))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.34826 -0.04398  0.00549  0.06137  0.15135 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               0.06320    0.04886   1.294    0.199    
## GDP.per.capita            0.29185    0.03763   7.755 5.47e-12 ***
## Social.support            0.07186    0.05156   1.394    0.166    
## Freedom.of.choices        0.10989    0.07882   1.394    0.166    
## Generosity               -0.13033    0.13084  -0.996    0.321    
## Perception.of.corruption  0.06529    0.13251   0.493    0.623    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09012 on 107 degrees of freedom
## Multiple R-squared:  0.6796, Adjusted R-squared:  0.6646 
## F-statistic: 45.39 on 5 and 107 DF,  p-value: < 2.2e-16
model_LF_cor <- lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support, data = h)
model_LF_cor 
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support, 
##     data = h)
## 
## Coefficients:
##    (Intercept)  GDP.per.capita  Social.support  
##        0.08111         0.31429         0.06892
summary(model_LF_GDP)$r.squared
## [1] 0.6646612
summary(model_LF_back)$r.squared
## [1] 0.6725167
summary(model_LF_forw)$r.squared
## [1] 0.679612
summary(model_LF_cor)$r.squared
## [1] 0.6705394
h_pred <- h
h_pred$pred_GDP <- predict(model_LF_GDP, newdata = h)
h_pred$pred_back <- predict(model_LF_back, newdata = h)
h_pred$pred_forw <- predict(model_LF_forw, newdata = h)
h_pred$pred_cor <- predict(model_LF_cor, newdata = h)
head(h_pred)
##    GDP.per.capita Social.support Healthy.life.expectancy Freedom.of.choices
## 3           1.936          1.320                   0.803              0.718
## 9           1.826          1.221                   0.818              0.568
## 16          1.982          1.182                   0.628              0.574
## 18          1.815          1.260                   0.715              0.660
## 19          1.907          1.106                   0.764              0.492
## 20          1.863          1.219                   0.808              0.567
##    Generosity Perception.of.corruption  pred_GDP pred_back pred_forw  pred_cor
## 3       0.270                    0.191 0.7706903 0.7886524 0.7792629 0.7805395
## 9       0.155                    0.143 0.7322682 0.7421236 0.7354154 0.7391449
## 16      0.220                    0.177 0.7867577 0.7909362 0.7725494 0.7854856
## 18      0.158                    0.048 0.7284260 0.7316511 0.7385235 0.7383757
## 19      0.049                    0.204 0.7605608 0.7648729 0.7602374 0.7566762
## 20      0.070                    0.266 0.7451920 0.7663810 0.7650694 0.7506357
RMSE(y_pred = h_pred$pred_GDP, y_true = h_pred$Healthy.life.expectancy)
## [1] 0.08972183
RMSE(y_pred = h_pred$pred_back, y_true = h_pred$Healthy.life.expectancy)
## [1] 0.0886647
RMSE(y_pred = h_pred$pred_forw, y_true = h_pred$Healthy.life.expectancy)
## [1] 0.08769893
RMSE(y_pred = h_pred$pred_cor, y_true = h_pred$Healthy.life.expectancy)
## [1] 0.08893198

karena model LF forward memiliki r squared yang tertinggi dan memiliki error terendah maka kita akan menggunakan model LF forward

5 Inteprtasi Model

summary(model_LF_forw)
## 
## Call:
## lm(formula = Healthy.life.expectancy ~ GDP.per.capita + Social.support + 
##     Freedom.of.choices + Generosity + Perception.of.corruption, 
##     data = h, scope = list(lower = m2, upper = m1))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.34826 -0.04398  0.00549  0.06137  0.15135 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               0.06320    0.04886   1.294    0.199    
## GDP.per.capita            0.29185    0.03763   7.755 5.47e-12 ***
## Social.support            0.07186    0.05156   1.394    0.166    
## Freedom.of.choices        0.10989    0.07882   1.394    0.166    
## Generosity               -0.13033    0.13084  -0.996    0.321    
## Perception.of.corruption  0.06529    0.13251   0.493    0.623    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09012 on 107 degrees of freedom
## Multiple R-squared:  0.6796, Adjusted R-squared:  0.6646 
## F-statistic: 45.39 on 5 and 107 DF,  p-value: < 2.2e-16

Prediktor yang secara signifikan yang mempengaruhi target adalah GDP per capita.

Kemudian mari kita lihat persebaran error/residual kita.

hist(model_LF_forw$residuals)

Kemudian mari kita lakukan test normality of residual.

shapiro.test(model_LF_forw$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model_LF_forw$residuals
## W = 0.9578, p-value = 0.00128

Seperti yang dapat kita lihat, p-value dari shapiro test yang dilakukan adalah 0.0128 artinya nilai p-value < nilai alpha. Kesimpulannya error/residual yang kita miliki tidak berdistribusi normal.

Kemudian mari kita lakukan test Homoscedasticity of Residuals dengan bptest()

bptest(model_LF_forw)
## 
##  studentized Breusch-Pagan test
## 
## data:  model_LF_forw
## BP = 6.6427, df = 5, p-value = 0.2486

sesuai dengan hasil yang kita dapat nilai p-value 0.2486 > alpha = 0.05, sehingga erorr kita menyebar konstan.

Kemudian mari kita lakukan test No Multicollinearity dengan vif()

vif(model_LF_forw)
##           GDP.per.capita           Social.support       Freedom.of.choices 
##                 2.576589                 2.346115                 1.274025 
##               Generosity Perception.of.corruption 
##                 1.240233                 1.082518

sesuai dengan hasil yang kita dapat model kita bebas multikolinearity (hubungan korelasi kuat antar prediktor)

6 Kesimpulan

Model model_LF_forw yang didapatkan memiliki R-square 0.6646 dan memiliki RMSE sebesar 0.08769893. Kemudian setelah diuji analisis didapat model kita memiliki residual yang tidak berdistribusi normal, tetapi error tersebut menyebar secara konstan dan model yang kita miliki bebas dari multikolinearity.

Berdasarkan model ini, nilai harapan hidup berkorelasi positif secara signifikan dengan nilai GDP dan memiliki hubungan positif dengan social support, freedom of choices, dan persepsi korupsi. Tetapi memiliki hubungan negatif dengan generosity atau bisa diartikan semakin besar nilai GDP maka akan meningkatkan tingkat ekspektasi hidup.