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("~/Downloads/Spring Semester 2025/DATA 110/Week 10")
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))
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, bmi, alcohol, polio, hiv_aids) |>
#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
pca_fit <- life_clean |>
select(where(is.numeric)) |>
scale() |>
prcomp()
# View PCA result
pca_fit
## Standard deviations (1, .., p=7):
## [1] 1.7188097 1.0814913 0.9408417 0.8729050 0.7632887 0.6625661 0.4553239
##
## Rotation (n x k) = (7 x 7):
## PC1 PC2 PC3 PC4 PC5
## life_expectancy 0.5346596 0.07517617 -0.03271557 0.006019871 -0.04096427
## adult_mortality -0.4477902 -0.33875835 -0.04760090 0.072332373 0.04201606
## infant_deaths -0.1687787 0.54409935 -0.74092500 0.246128002 0.24993358
## bmi 0.4084058 -0.22343417 -0.06947910 -0.247077096 0.79787486
## alcohol 0.2827975 -0.42447124 -0.63358621 -0.279103515 -0.46978076
## polio 0.3261621 -0.25640383 0.01511181 0.885500981 -0.03082647
## hiv_aids -0.3632979 -0.53638199 -0.20299458 0.105332543 0.27539292
## PC6 PC7
## life_expectancy 0.05131513 -0.83849254
## adult_mortality -0.73844955 -0.36077119
## infant_deaths -0.04023820 -0.04283588
## bmi -0.21199731 0.18936818
## alcohol -0.07943126 0.18307432
## polio -0.08746492 0.18690803
## hiv_aids 0.62572966 -0.24622829
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)
pca_fit |>
# add PCs to the original dataset
augment(life_clean) |>
ggplot(aes(.fittedPC1, .fittedPC2)) +
geom_point(aes(color = status)) +
# Add the PCA1 and PCA2 arrows
geom_segment(aes(x = -4.9, y = 0, xend = 5, yend = 0),
arrow = arrow(type = "closed", length = unit(0.1, "inches")),
color = "black") + # PC1 arrow
geom_segment(aes(x = 0, y = -2.5, xend = 0, yend = 4),
arrow = arrow(type = "closed", length = unit(0.1, "inches")),
color = "black") + # PC2 arrow
# Add text labels- Positioning and color of the label
geom_text(aes(x = 5, y = 0, label = "PC1"),
vjust = -0.5, color = "black") +
geom_text(aes(x = 0, y = 4, label = "PC2"),
hjust = -0.5, color = "black") +
xlim(-8, 5) +
ylim(-7, 7) +
xlab("PC1") +
ylab("PC2") +
theme_minimal()
Create the Rotation Arrows:
#Rotation Matrix
arrow_style <- arrow(
angle = 20, length = grid::unit(8, "pt"),
ends = "first", type = "closed"
)
pca_fit |>
# extract rotation matrix
tidy(matrix = "rotation") |>
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), hjust = 1) +
xlim(-1.5, 0.5) + ylim(-1, 1) +
coord_fixed()+
theme_minimal()
Summarize the findings of the PCA.
What does PC1 show? PC1 have information related to the countries development status. Countries with positive PC1 values tend to be developed, and those with negative values are usually developing. This is strongly influenced by health indicators (infant deaths, adult mortality, and hiv) which are more common in developing nations.
What does PC2 show? There is no clear separation between developed and developing countries. For instance, adult mortality is heavily correlated to HIV. Infants mortality contributes positively to PC2. Other variables like alcohol, polio, and bmi are close together and don’t have a strong influence in any direction.
What does the analysis reveal about the relationship between development level and health outcomes in different countries? Developed countries shows better health outcomes. They are associated with lower infant and adult mortality, lower hiv, and higher vaccination rates for polio.
Plot the variance explained by each PC- bargraph
#Plot the variance explained by each PC- bargraph
pca_fit |>
# extract eigenvalues
tidy(matrix = "eigenvalues") |>
ggplot(aes(PC, percent)) +
geom_col(fill= "lightblue") +
scale_x_continuous(
# create one axis tick per PC
breaks = 1:7
) +
scale_y_continuous(
name = "variance explained",
breaks = seq(0, 1, by = 0.1),
# format y axis ticks as percent values
label = scales::label_percent(accuracy = 1)
)+
xlab("Principal Component (PC)") +
theme_minimal()
How much variation is explained by PC1 and PC2?
pca_fit |>
tidy(matrix = "eigenvalues")
## # A tibble: 7 × 4
## PC std.dev percent cumulative
## <dbl> <dbl> <dbl> <dbl>
## 1 1 1.72 0.422 0.422
## 2 2 1.08 0.167 0.589
## 3 3 0.941 0.126 0.716
## 4 4 0.873 0.109 0.824
## 5 5 0.763 0.0832 0.908
## 6 6 0.663 0.0627 0.970
## 7 7 0.455 0.0296 1
PC1 and PC2 explain around 59% of the total variation, which is good for visual interpretation.