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

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, gdp, hiv_aids) |> 
  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?

pca_fit <- life_clean |>
  select(where(is.numeric)) |>
  scale() |>
  prcomp()
  
   



# View PCA result
pca_fit
## Standard deviations (1, .., p=7):
## [1] 1.7230878 1.1009225 0.9733849 0.8192065 0.7499235 0.6498108 0.4644580
## 
## Rotation (n x k) = (7 x 7):
##                        PC1         PC2          PC3         PC4          PC5
## life_expectancy  0.5309883 -0.08868276 -0.005494813 -0.01209921 -0.007927003
## adult_mortality -0.4450675  0.34634954  0.020396129 -0.05038388  0.018712734
## infant_deaths   -0.1476524 -0.41604695  0.836357277 -0.20485847  0.243377748
## bmi              0.4096364  0.20610689 -0.130639513 -0.47479776  0.671209445
## alcohol          0.2919266  0.50461224  0.384723938 -0.39471014 -0.579956517
## gdp              0.3394688  0.31398772  0.348715118  0.75750323  0.193343218
## hiv_aids        -0.3604205  0.55079983  0.115667137 -0.01754785  0.340735950
##                         PC6         PC7
## life_expectancy  0.01401269  0.84246720
## adult_mortality -0.75520281  0.32912152
## infant_deaths   -0.03766732  0.05469602
## bmi             -0.20428204 -0.23444632
## alcohol          0.02079256 -0.13983875
## gdp             -0.16351917 -0.16321510
## hiv_aids         0.59929253  0.27888576

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

Create the Rotation Arrows:

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

pca_fit |>
  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 that countries with generally more money and less mortality tend to have a higher PC1 score.

What does PC2 show? It shows that lessser developed countries with higher infant deaths, lower life expectancy and higher diseases tend to have a higher PC2 score.

What does the analysis reveal about the relationship between development level and health outcomes in different countries? It shows there is a positive relationship between development level and health outcomes in different countries.

Plot the variance explained by each PC- bargraph

pca_fit |>
  tidy(matrix = "eigenvalues") |>
  ggplot(aes(PC, percent)) +
  geom_col(color= "red", fill= "purple"  ) +
  scale_x_continuous(breaks = 1:6) +
  scale_y_continuous(labels = scales::label_percent())

summary(pca_fit)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     1.7231 1.1009 0.9734 0.81921 0.74992 0.64981 0.46446
## Proportion of Variance 0.4242 0.1731 0.1353 0.09587 0.08034 0.06032 0.03082
## Cumulative Proportion  0.4242 0.5973 0.7327 0.82852 0.90886 0.96918 1.00000

How much variation is explained by PC1 and PC2? Pc1 explains a little more than 40% of the variance while PC2 explains about 17% of the variance.