Activity 3.2 - PC concepts

SUBMISSION INSTRUCTIONS

  1. Render to html
  2. Publish your html to RPubs
  3. Submit a link to your published solutions

Question 1

The data set we will analyze for this question are on the 10 events in the Men’s 2024 Olympic Decathlon in Paris.

decathlon <- read.csv('Data/mens_decathlon_paris2024.csv')
head(decathlon)
         Athlete  Medal      Nation Overall X100m LongJump ShotPut HighJump
1   Markus Rooth   Gold      Norway    8796 10.71     7.80   15.25     1.99
2 Leo Neugebauer Silver     Germany    8748 10.67     7.98   16.55     2.05
3  Lindon Victor Bronze     Grenada    8711 10.56     7.48   15.71     2.02
4    Sven Roosen   None Netherlands    8607 10.52     7.56   15.10     1.87
5  Janek Õiglane   None     Estonia    8572 10.89     7.25   14.58     1.99
6   Johannes Erm   None     Estonia    8569 10.64     7.66   14.61     2.08
  X400m X110mHurdle Discus PoleVault Javelin X1500m
1 47.69       14.25  49.80       5.3   66.87  279.6
2 47.70       14.51  53.33       5.0   56.64  284.7
3 47.84       14.62  53.91       4.9   68.22  283.5
4 46.40       13.99  46.88       4.7   63.72  258.5
5 48.02       14.45  43.49       5.3   71.89  265.6
6 47.19       14.35  46.29       4.6   59.58  259.7

For the purposes of this question, assume we have 10-dimensional data - that is, ignore the Overall column.

A)

Explain why we need to scale this data set before performing PCA.

We always want to scale so our values are equally valued while performing SVD testing.

B)

Use svd() to find the first 2 principal component scores and their loadings. Full credit will only be granted if you use the svd() ingredients u, d, and v. What percent of the overall variability do the first two PCs explain?

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
decathlon_numeric <- (decathlon
                      %>%select(X100m:X1500m)
)
decathlon_scaled <- scale(decathlon_numeric)
decathlon_svd <- svd(decathlon_scaled)
U <- decathlon_svd$u
D <- diag(decathlon_svd$d)
V <- decathlon_svd$v
PCs <- U %*% D
head(PCs, 2)
           [,1]      [,2]      [,3]       [,4]       [,5]       [,6]
[1,] -0.9696335 0.9075397 0.7205553 -0.1637611 -0.7854975 -0.7506910
[2,] -2.1338950 2.2401416 0.4899618 -1.0902256  0.7897618 -0.2423639
             [,7]      [,8]      [,9]       [,10]
[1,] 6.474979e-05 0.4802080 0.2288616 -0.01855389
[2,] 1.877641e-01 0.6018405 0.4109560  0.18266992
(PCs
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/10))
)
         X1        X2       X3        X4        X5         X6         X7
1 0.2913494 0.1928006 0.146543 0.1404117 0.0952215 0.05535841 0.04806271
          X8         X9         X10
1 0.01403932 0.01222556 0.003987768

The First 2 PCs explain around 48.4% of the total variability.

C)

Find and print the loadings. Based on the loadings alone, if the first two PCs are plotted in a 2D plane as shown below, which of the four quadrants will the medalists be in? Explain your reasoning.

loadings <- V
rownames(loadings) <- colnames(decathlon_numeric)
decathlon_loadings <- loadings[,1:2]
decathlon_loadings  
                    [,1]        [,2]
X100m        0.446612410  0.01678592
LongJump    -0.493037935 -0.06565628
ShotPut     -0.309431542  0.53134100
HighJump    -0.150386291 -0.05665757
X400m        0.485973869  0.14847674
X110mHurdle  0.345270647  0.31685991
Discus      -0.139145881  0.59570522
PoleVault    0.019829831  0.48083261
Javelin      0.252821246  0.03137029
X1500m       0.005588625 -0.01948870

Based on the loadings alone if the first two PCs are plotted in a 2D plane as shown below of the four quadrants we would expect the medalists to be in the second quadrant as there are greater negatives for PC1 and greater positives for PC2.

D)

Add the PCs to the decathlon data set and create a scatterplot of these PCs, with the points labeled by the athletes’ names. Color-code the points on whether or not the athlete was a medalist. Use the ggrepel package for better labeling. Verify that your intuition from C) is correct.

decathlon <- decathlon %>%
  mutate(PC1 = PCs[, 1],
         PC2 = PCs[, 2]) 

library(ggrepel)
ggplot(decathlon, aes(x = PC1, y = PC2, 
                      label = Athlete, 
                      color = Medal)) +
  geom_point(size = 3) +
  geom_text_repel(show.legend = FALSE, max.overlaps = Inf) +
  labs(x = "PC1",
       y = "PC2",
       color = "Medalist Status") +
  theme_minimal() 

The 3 medalist finishes can be found in the Quadrant 2 so my intutition was correct.

E)

Canadian Damian Warner won the gold medal in the decathlon in the 2020 Tokyo games. He began the 2024 decathlon but bowed out after three straight missed pole vault attempts.

These are his results in the 10 events in 2020:

warner <- c(10.12, 8.24, 14.8, 2.02, 47.48, 13.46, 48.67, 4.9, 63.44, 271.08)

Would this have won a medal if it had happened in 2024? To answer this, we will compute his PCs with respect to the 2024 athletes and add it to the plot to see where his 2020 gold-medal performance compares to the 2024 athletes. To do this:

  • Find the mean vector from the 2024 athletes. Call it mean_vec_24.
  • Find the standard deviation vector from the 2024 athletes. Call it sd_vec_24.
  • Standardize Warner’s 2020 results with respect to the 2024 athletes: (warner-mean_vec_24)/sd_vec_24
  • Find Warner’s PC coordinates using the 2024 loadings.
  • Add his point to the scatterplot.

Do you think his 2020 performance would have won a medal if it had happened in 2024?

mean_vec_24 <- colMeans(decathlon_numeric)
sd_vec_24 <- apply(decathlon_numeric, 2, sd)
warner_24 <- (warner - mean_vec_24)/sd_vec_24
warner_pc <- as.numeric(warner_24 %*% V[, 1:2])
warner_pc
[1] -3.4226989 -0.5092531
ggplot(decathlon, aes(x = PC1, y = PC2, 
                      label = Athlete, 
                      color = Medal)) +
  geom_point(size = 3) +
  geom_text_repel(show.legend = FALSE, max.overlaps = Inf) +
  labs(x = "PC1",
       y = "PC2",
       color = "Status") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "top") +
  # Add Warner's 2020 point
  geom_point(aes(x = warner_pc[1], y = warner_pc[2]), 
             color = "blue", size = 4, shape = 17) +
  annotate("text", 
           x = warner_pc[1], 
           y = warner_pc[2], 
           label = "Damian Warner (2020)", 
           color = "blue", 
           hjust = -0.1, 
           vjust = -0.5)

Question 2

Below is a screenshot of a conversation between me and chatbot Claude:

After looking at the graphs, I grew skeptical. So I said:

Behold, Claude’s three data sets which I’ve called claudeA, claudeB, and claudeC:

claudeA <- read.csv('Data/claude_dataA.csv')
claudeB <- read.csv('Data/claude_dataB.csv')
claudeC <- read.csv('Data/claude_dataC.csv')

Each data set has an X and a Y column which represent 2-dimensional variables that we need to rotate.

A)

Scale each data set and plot them side-by-side using the patchwork package. Make sure the aspect ratio of each graph is 1 (i.e., make the height and width of each graph equal). At this point, explain why you think I was skeptical. Specifically, do you think the percent variability explained by the first PC of each data set appears to exceed or fall short of the variability I asked it to?

library(ggplot2)
library(patchwork)

# Scale each dataset
claudeA_scaled <- as.data.frame(scale(claudeA))
claudeB_scaled <- as.data.frame(scale(claudeB))
claudeC_scaled <- as.data.frame(scale(claudeC))

# Create base plots (equal aspect ratio)
pA <- ggplot(claudeA_scaled, aes(x = X, y = Y)) +
  geom_point() +
  coord_equal() +  # ensures aspect ratio = 1
  ggtitle("claudeA (scaled)")

pB <- ggplot(claudeB_scaled, aes(x = X, y = Y)) +
  geom_point() +
  coord_equal() +
  ggtitle("claudeB (scaled)")

pC <- ggplot(claudeC_scaled, aes(x = X, y = Y)) +
  geom_point() +
  coord_equal() +
  ggtitle("claudeC (scaled)")

# Combine side-by-side using patchwork
pA + pB + pC

B)

Use SVD to find the first PC for each data set, and find the actual percent of total variability explained by each PC using aggregation methods.

claudeA_svd <- svd(claudeA_scaled)
UA <- claudeA_svd$u
DA <- diag(claudeA_svd$d)
VA <- claudeA_svd$v
PCsA <- UA %*% DA
head(PCsA, 3)
          [,1]        [,2]
[1,] -2.290018  0.06649290
[2,] -1.920272 -0.08310223
[3,] -1.940032  0.15680860
(PCsA
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1         X2
1 0.9756201 0.02437991
claudeB_svd <- svd(claudeB_scaled)
UB <- claudeB_svd$u
DB <- diag(claudeB_svd$d)
VB <- claudeB_svd$v
PCsB <- UB %*% DB
(PCsB
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1         X2
1 0.9591807 0.04081932
claudeC_svd <- svd(claudeC_scaled)
UC <- claudeC_svd$u
DC <- diag(claudeC_svd$d)
VC <- claudeC_svd$v
PCsC <- UC %*% DC
(PCsC
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1          X2
1 0.9949096 0.005090442

We can see that our percent variabilities are 97.6% for claudeA, 95.9% for claudeB, and 99.5% for claudeC.