Introduction
This activity aims to explore and analyze the relationship between a country’s development level and its health outcomes using Principal Component Analysis (PCA). The focus is on understanding how various health indicators (such as mortality rates, life expectancy, and disease prevalence) vary across countries with different stages of development.
About the dataset
The Global Health Observatory (GHO) data repository under World Health Organization (WHO) keeps track of the health status as well as many other related factors for all countries. The dataset related to life expectancy, health factors for 193 countries has been collected from the same WHO data repository website and its corresponding economic data was collected from United Nation website.
You can read more about the dataset in here: https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who?resource=download
library(tidyverse)
setwd("/Users/damirkumukov/Desktop/Datasets")
life_data<- read_csv("life_expectancy_who.csv")
# Fix the columns' names
names(life_data) <- tolower(names(life_data)) # Convert all column names to lowercase
# Replace spaces and slashes with underscores to improve readability
names(life_data) <- gsub(" ", "_", names(life_data))
names(life_data) <- gsub("/", "_", names(life_data))
head(life_data)
## # A tibble: 6 × 22
## country year status life_expectancy adult_mortality infant_deaths alcohol
## <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Afghanistan 2015 Devel… 65 263 62 0.01
## 2 Afghanistan 2014 Devel… 59.9 271 64 0.01
## 3 Afghanistan 2013 Devel… 59.9 268 66 0.01
## 4 Afghanistan 2012 Devel… 59.5 272 69 0.01
## 5 Afghanistan 2011 Devel… 59.2 275 71 0.01
## 6 Afghanistan 2010 Devel… 58.8 279 74 0.01
## # ℹ 15 more variables: percentage_expenditure <dbl>, hepatitis_b <dbl>,
## # measles <dbl>, bmi <dbl>, `under-five_deaths` <dbl>, polio <dbl>,
## # total_expenditure <dbl>, diphtheria <dbl>, hiv_aids <dbl>, gdp <dbl>,
## # population <dbl>, `thinness__1-19_years` <dbl>, `thinness_5-9_years` <dbl>,
## # income_composition_of_resources <dbl>, schooling <dbl>
Data Preparation:
The first step is to clean and filter the dataset to retain relevant variables. The code is set up, but you need to add other indicators that you believe can be relevant to this analysis. We focus on numerical health and development indicators and remove any missing values.
# Clean & filter: keep only numeric columns + identifiers
life_clean <- life_data |>
select(country, status, life_expectancy, adult_mortality, infant_deaths,alcohol,hiv_aids,measles,bmi,polio)|>
#enter other indicators here) |>
drop_na()
Principal Component Analysis (PCA) Setup:
Use PCA to reduce the dimensionality of the dataset while retaining as much variance as possible.
Ensure that the dataset is scaled before performing PCA.
Perform PCA on the numerical columns and extract the principal components (PC1 and PC2). What do the first two principal components explain about the data?
# Perform PCA on numeric variables
num_data<- life_clean|>
select(where(is.numeric))|>
scale()|>
prcomp()
# View PCA result
num_data
## Standard deviations (1, .., p=8):
## [1] 1.7353845 1.2120350 1.0098804 0.8749307 0.7658567 0.7116193 0.6597346
## [8] 0.4537195
##
## Rotation (n x k) = (8 x 8):
## PC1 PC2 PC3 PC4 PC5
## life_expectancy 0.5245613 -0.12619982 0.006163638 -0.000744236 0.04786014
## adult_mortality -0.4305274 0.28739667 -0.222014379 0.072160832 -0.02871336
## infant_deaths -0.2032930 -0.62063601 -0.184001722 0.095549278 -0.06726956
## alcohol 0.2767740 0.05327943 -0.706371985 -0.358106115 0.50206765
## hiv_aids -0.3481627 0.33093135 -0.485082357 0.097733555 -0.27829418
## measles -0.1707941 -0.62538390 -0.293585347 0.060223166 -0.14404595
## bmi 0.4066487 0.06995161 -0.209966562 -0.233074853 -0.80018228
## polio 0.3252671 0.08907971 -0.228627215 0.888774461 0.04241781
## PC6 PC7 PC8
## life_expectancy -0.02467778 0.06284337 -0.837867289
## adult_mortality -0.15942126 -0.71956008 -0.365438750
## infant_deaths -0.71727038 0.10675090 -0.009943118
## alcohol -0.05044121 -0.06542226 0.185633474
## hiv_aids 0.07133995 0.61870211 -0.243066101
## measles 0.66271914 -0.16411568 -0.055002865
## bmi -0.11187268 -0.21093312 0.184482455
## polio -0.01237782 -0.09159226 0.183668323
Visualize PCA Results:
Create a scatter plot of the first two principal components (PC1 and PC2) to visualize how countries cluster based on their health and development indicators.
Add color coding or labels based on the development status (e.g., developed vs. developing countries) to better understand how these groups are positioned in the PCA space.
Add the arrows of PC1 and PC2, make sure they are labeled.
library(broom)
num_data|>
augment(life_clean)|>
ggplot(aes(.fittedPC1, .fittedPC2, color = status)) +
geom_point()+
coord_fixed()
Create the Rotation Arrows:
# define an arrow style
arrow_style <- arrow(
angle = 20, length = grid::unit(8, "pt"),
ends = "first", type = "closed"
)
num_data |>
tidy(matrix = "rotation") |> # extract rotation matrix
pivot_wider(
names_from = "PC", values_from = "value",
names_prefix = "PC"
) |>
ggplot(aes(PC1, PC2)) +
geom_segment(
xend = 0, yend = 0,
arrow = arrow_style
) +
geom_text(aes(label = column,)) +
coord_fixed()
Summarize the findings of the PCA.
What does PC1 show? it shows countries with generailly more money and less mortality to have a higher PC1
What does PC2 show? countries that are less developed with higher PC2
What does the analysis reveal about the relationship between development level and health outcomes in different countries? Increase in development means better health
Plot the variance explained by each PC- bargraph
num_data |>
tidy(matrix = "eigenvalues") |>
ggplot(aes(PC, percent)) +
geom_col(color="red",fill="blue") +
scale_x_continuous(breaks = 1:6) +
scale_y_continuous(labels = scales::label_percent())
summary(num_data)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.7354 1.2120 1.0099 0.87493 0.76586 0.7116 0.65973
## Proportion of Variance 0.3764 0.1836 0.1275 0.09569 0.07332 0.0633 0.05441
## Cumulative Proportion 0.3764 0.5601 0.6876 0.78324 0.85656 0.9199 0.97427
## PC8
## Standard deviation 0.45372
## Proportion of Variance 0.02573
## Cumulative Proportion 1.00000
How much variation is explained by PC1 and PC2?
pc1 explains a little more than 40% variance and pc2 about 18% variance