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("~/Data 110/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)) 

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, 
         hepatitis_b,measles,bmi,polio,diphtheria,hiv_aids,schooling,population,gdp,percentage_expenditure) |> 
  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 <- life_clean |>
  select(where(is.numeric))|>
  scale()|>
  prcomp()


# View PCA result
PCA
## Standard deviations (1, .., p=13):
##  [1] 2.0218394 1.4417220 1.3139185 1.1941898 0.9242781 0.8280877 0.7298797
##  [8] 0.6508811 0.6237076 0.5731514 0.5120532 0.3663901 0.2660605
## 
## Rotation (n x k) = (13 x 13):
##                                PC1         PC2         PC3         PC4
## life_expectancy         0.43018191  0.16069899  0.01508981  0.22573723
## adult_mortality        -0.31827922 -0.21292657 -0.05038099 -0.37014162
## infant_deaths          -0.17591837  0.52572925  0.25227180 -0.12294174
## hepatitis_b             0.20977024 -0.27983059  0.41564368 -0.17640061
## measles                -0.10685253  0.41421866  0.26215052 -0.11082470
## bmi                     0.30801298  0.01114807 -0.09702309  0.17029163
## polio                   0.26381560 -0.15457056  0.38726306 -0.22685580
## diphtheria              0.27258362 -0.17507977  0.43392534 -0.25416137
## hiv_aids               -0.23091046 -0.19278616 -0.10435968 -0.48382986
## schooling               0.39397313  0.06272555 -0.04236094 -0.02364028
## population             -0.08005265  0.48268604  0.25271222 -0.13502182
## gdp                     0.30780545  0.19085506 -0.35636849 -0.41419396
## percentage_expenditure  0.28073828  0.19334654 -0.37534746 -0.42898183
##                                PC5         PC6         PC7         PC8
## life_expectancy        -0.01694755  0.02790500 -0.06273631  0.12799484
## adult_mortality        -0.19598774 -0.02142017  0.15668108 -0.45937234
## infant_deaths          -0.05403600 -0.09297837  0.03773031 -0.02479186
## hepatitis_b             0.10765903 -0.06106414  0.63943098  0.13499571
## measles                 0.05482282  0.78684089  0.10269741 -0.04465411
## bmi                    -0.65019505  0.11370386  0.28767990 -0.45322332
## polio                   0.02104675  0.02109653 -0.63982072 -0.41240338
## diphtheria              0.07062934 -0.03446186  0.02988258  0.11277155
## hiv_aids               -0.45128521  0.14750572 -0.16677563  0.47350090
## schooling              -0.39299573  0.09163920 -0.12997095  0.35266418
## population             -0.25736495 -0.56639674  0.01794680  0.02713224
## gdp                     0.18697113 -0.02292377  0.07395801 -0.06824121
## percentage_expenditure  0.23727981 -0.03496294  0.08456482 -0.09792042
##                                PC9         PC10         PC11         PC12
## life_expectancy        -0.20384673  0.032078883 -0.003358996 -0.820800035
## adult_mortality        -0.61663486  0.029520599 -0.045465408 -0.237057783
## infant_deaths           0.08616594  0.031590986 -0.767977016 -0.044464012
## hepatitis_b             0.12514193  0.463149765 -0.012937157 -0.030959695
## measles                -0.05999169  0.009961664  0.311526021  0.024263014
## bmi                     0.33385326 -0.168044602 -0.033267293  0.043893594
## polio                   0.17052826  0.309058262  0.006651050  0.007182240
## diphtheria             -0.11667922 -0.776362275 -0.050548064  0.058322742
## hiv_aids                0.33339551  0.006009693  0.007974422 -0.275256867
## schooling              -0.52406577  0.233131181 -0.131371964  0.425878180
## population             -0.02280855  0.024338009  0.537669138  0.013492385
## gdp                     0.03963153  0.018545938 -0.003560394  0.061878460
## percentage_expenditure  0.09738362 -0.026031033  0.028072505 -0.006110221
##                                 PC13
## life_expectancy        -0.0156013158
## adult_mortality         0.0014489584
## infant_deaths           0.0106943323
## hepatitis_b             0.0102592136
## measles                -0.0004143368
## bmi                     0.0027174839
## polio                   0.0067225861
## diphtheria             -0.0077753122
## hiv_aids               -0.0089145179
## schooling               0.0822411629
## population             -0.0053350373
## gdp                    -0.7203529966
## percentage_expenditure  0.6882163137

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 |>
  augment(life_clean) |>
  ggplot(aes(.fittedPC1,.fittedPC2)) +
  geom_point(aes(color=status))

Create the Rotation Arrows:

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

PCA |>
  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, aes(color = column)
  ) +
  scale_color_manual(values = c("darkred","red","darkgoldenrod","orange","yellow","darkgreen","green","navy","blue","skyblue","purple","maroon","pink"))

# I choose to color instead of labels because the labels where really clutered and hard to read.

Summarize the findings of the PCA.

What does PC1 show? PC1 captures information in regards to how developed a country is we can see this as the developed contries tend to the right and less developed countries tend to the left.

What does PC2 show? the diffrence or ratio between infant deaths and hepatitis b cases.

What does the analysis reveal about the relationship between development level and health outcomes in different countries? a lower level of development tends to have higher mortality rates for infants and adults as well as a gretter susceptabilty to certian deseases like hiv and measles countries with higher development still have some health problems like high BMI and diphetheria

Plot the variance explained by each PC- bargraph

PCA |>
  tidy(matrix = "eigenvalues")|>
  ggplot(aes(PC, percent))+
  geom_col(aes(fill = as.factor(PC)))+
  theme(legend.position="none")+
  scale_x_continuous(breaks = 1:13) +
  scale_y_continuous(labels = scales::label_percent())+
  scale_fill_manual(values = c("darkred","red","darkgoldenrod","orange","yellow","darkgreen","green","navy","blue","skyblue","purple","maroon","pink"))

How much variation is explained by PC1 and PC2?

~45%