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("~/Library/CloudStorage/OneDrive-montgomerycollege.edu/DATA 110 - Mais Alraee/week 10 4:7 - 4:13")
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, alcohol, bmi, polio, diphtheria) |>
  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
library(tidyverse)
pca_fit <- life_clean |> 
  select(where(is.numeric)) |> 
  scale() |>                   
  prcomp()                    


# View PCA result
pca_fit
## Standard deviations (1, .., p=7):
## [1] 1.7469099 1.0282981 0.9785835 0.9052239 0.7456229 0.5755126 0.4761144
## 
## Rotation (n x k) = (7 x 7):
##                        PC1        PC2        PC3         PC4         PC5
## life_expectancy  0.4995055  0.2355275 -0.1023446  0.12666521  0.12185292
## adult_mortality -0.3834494 -0.4145092  0.2696917 -0.43562742 -0.38694022
## infant_deaths   -0.1915309  0.2272880 -0.8624516 -0.32681808 -0.24495160
## alcohol          0.2977791  0.2527548  0.2179340 -0.82393001  0.31473677
## bmi              0.3962503  0.2992498  0.2135144  0.01072959 -0.82104887
## polio            0.3991364 -0.5347707 -0.2006611 -0.06884535 -0.04420005
## diphtheria       0.4022190 -0.5306920 -0.1990494 -0.06049086 -0.01713828
##                           PC6         PC7
## life_expectancy  5.545479e-02  0.80658487
## adult_mortality  5.025902e-02  0.51613472
## infant_deaths    9.659266e-03  0.03047387
## alcohol         -1.381912e-02 -0.14777196
## bmi              3.548014e-05 -0.18333165
## polio           -7.108021e-01 -0.05012570
## diphtheria       6.991958e-01 -0.15536250

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))+
  xlim(-5,5)+
  ylim(-4,4)+
  xlab("PC1")+
  ylab("PC2")+
  guides(color = guide_legend(title = NULL))+
  theme_minimal()
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

Create the Rotation Arrows:

  pca_fit |>
  # add PCs to the original dataset
  augment(life_clean) |>
  ggplot(aes(.fittedPC1, .fittedPC2)) +
  geom_point(aes(color = status)) +
  
  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

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(-5, 5) +
  ylim(-4, 4) +
  xlab("PC1") +
  ylab("PC2") +
  guides(color = guide_legend(title = NULL)) +  # No title for the legend
  scale_color_manual(values = c("Developed" = "darkorange", "Developing" = "blue")) +
  theme_minimal()

Summarize the findings of the PCA.

What does PC1 show? Developed countries separate along PC1 in the PCA plot. Since PCA centers the data, this separation happens around zero. Countries with positive PC1 values tend to be Developed, and those with negative values are usually Developing There’s some overlap, but the trend is clear.

What does PC2 show? We also notice that along PC2, there’s no clear separation between Developed and Developing Countries of either status can appear anywhere along the axis.

What does the analysis reveal about the relationship between development level and health outcomes in different countries?

Plot the variance explained by each PC- bargraph

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

pca_fit |>
  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? Together, PC1 + PC2 explain more than 50% of all the variation in the dataset.

pca_fit |>
  tidy(matrix = "eigenvalues")
## # A tibble: 7 × 4
##      PC std.dev percent cumulative
##   <dbl>   <dbl>   <dbl>      <dbl>
## 1     1   1.75   0.436       0.436
## 2     2   1.03   0.151       0.587
## 3     3   0.979  0.137       0.724
## 4     4   0.905  0.117       0.841
## 5     5   0.746  0.0794      0.920
## 6     6   0.576  0.0473      0.968
## 7     7   0.476  0.0324      1

More specifically, together, PC1 + PC2 explain more than 59% of all the variation in the dataset.