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)
library(broom)

setwd("C:/Users/wjcor/Downloads/Intro2r")

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, polio,hiv_aids,diphtheria,measles, hepatitis_b,gdp) |> 
  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 |>
  tidy(matrix = "scores")
## # A tibble: 19,970 × 3
##      row    PC   value
##    <int> <dbl>   <dbl>
##  1     1     1 -2.76  
##  2     1     2  1.12  
##  3     1     3 -1.22  
##  4     1     4  0.774 
##  5     1     5 -0.544 
##  6     1     6  2.08  
##  7     1     7 -0.0923
##  8     1     8  0.163 
##  9     1     9  1.01  
## 10     1    10  0.359 
## # ℹ 19,960 more rows

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.

pca_fit |>
  augment(life_clean) |>
  ggplot(aes(.fittedPC1, .fittedPC2, color = status)) +
  geom_point() +
  geom_segment(aes(x = -7.5, y = 0, xend = 5, yend = 0), 
               arrow = arrow(type = "closed", length = unit(.1, "inches")),
               color = "black") +
  geom_segment(aes(x = 0, y = -6, xend = 0, yend = 9), 
               arrow = arrow(type = "closed", length = unit(.1, "inches")),
               color = "black")+
  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")+
  xlab("PC1") +
  ylab("PC2")
## Warning in geom_segment(aes(x = -7.5, y = 0, xend = 5, yend = 0), arrow = arrow(type = "closed", : All aesthetics have length 1, but the data has 1997 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
## Warning in geom_segment(aes(x = 0, y = -6, xend = 0, yend = 9), arrow = arrow(type = "closed", : All aesthetics have length 1, but the data has 1997 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
## Warning in geom_text(aes(x = 5, y = 0, label = "PC1"), vjust = -0.5, color = "black"): All aesthetics have length 1, but the data has 1997 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
## Warning in geom_text(aes(x = 0, y = 4, label = "PC2"), hjust = -0.5, color = "black"): All aesthetics have length 1, but the data has 1997 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.

Create the Rotation Arrows:

arrow_style <- arrow(
  angle = 20, length = grid::unit(8, "pt"),
  ends = "first", type = "closed"
)

pca_fit |>
  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
  ) +
  xlim(-.5,.4)+
  geom_text(aes(label = column), hjust = 1) +
  coord_fixed()

Summarize the findings of the PCA.

What does PC1 show? PC1 shows the development status of the country

What does PC2 show? Countries with lower adult mortality rates also have lower hiv_aids cases. Countries with High infant deaths also have higher measles rates. Hepatitis b, diphtheria, and polio are all related gdp and bmi are related

What does the analysis reveal about the relationship between development level and health outcomes in different countries? Developed countries have lower death rates than developing countries, additionally, they have lower aids and measles rates.

Plot the variance explained by each PC- bargraph

pca_fit |>
  tidy(matrix = "eigenvalues") |>
  ggplot(aes(PC, percent)) + 
  geom_col(fill= "lightblue") + 
  scale_x_continuous(
    # create one axis tick per PC
    breaks = 1:10
  ) +
  scale_y_continuous(
    name = "variance explained",
    breaks = seq(0, 1, by = 0.1), 
    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: 10 × 4
##       PC std.dev percent cumulative
##    <dbl>   <dbl>   <dbl>      <dbl>
##  1     1   1.83   0.336       0.336
##  2     2   1.30   0.170       0.505
##  3     3   1.20   0.144       0.649
##  4     4   0.960  0.0922      0.741
##  5     5   0.832  0.0692      0.810
##  6     6   0.715  0.0512      0.861
##  7     7   0.662  0.0438      0.905
##  8     8   0.639  0.0408      0.946
##  9     9   0.582  0.0338      0.980
## 10    10   0.448  0.0201      1

~50%