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 will need to scale this data because variables with larger values will be the one driving the total variability

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)
Warning: package 'ggplot2' was built under R version 4.5.2
── 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   4.0.0     ✔ 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(ggplot2)
library(ggrepel)
decathlon_numeric <- decathlon %>% 
                      select(-c('Athlete','Medal','Nation', 'Overall'))

decathlon_scaled <- scale(decathlon_numeric)

D_SVD <- svd(decathlon_scaled)

U <- D_SVD$u
D <- D_SVD$d
V <- D_SVD$v

first2scores <- U[, 1:2] %*% diag(D[1:2])
head(first2scores)
           [,1]        [,2]
[1,] -0.9696335  0.90753969
[2,] -2.1338950  2.24014162
[3,] -0.7323012  2.05468273
[4,] -1.4369409 -0.05634227
[5,]  1.0587944 -0.11932168
[6,] -1.1274914 -0.28469476
V[,1:2]
              [,1]        [,2]
 [1,]  0.446612410  0.01678592
 [2,] -0.493037935 -0.06565628
 [3,] -0.309431542  0.53134100
 [4,] -0.150386291 -0.05665757
 [5,]  0.485973869  0.14847674
 [6,]  0.345270647  0.31685991
 [7,] -0.139145881  0.59570522
 [8,]  0.019829831  0.48083261
 [9,]  0.252821246  0.03137029
[10,]  0.005588625 -0.01948870
(first2scores
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/10))
)
         X1        X2
1 0.2913494 0.1928006

PC1 alone explains 29.13% of the total variability in these 10 variables, PC1 and PC1 together explain 48.41% of the total variability of the ten variables

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 <- D_SVD$v
rownames(loadings) <- colnames(decathlon_numeric)
loadings[,1:2]
                    [,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

Medalists will most likely be in quadrant 2 because when looking at the loading and each variable, the events you want the larger amounts are weighted negatively and the events where you want the smaller amounts is weighted positively for PC1 making them more likely to be on the left side. When you add PC2 into the mix, the loading that are weighted the most are events that you want larger scores so putting you higher into quadrant 2.

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_with_pcs <- decathlon %>%
  mutate(PC1 = first2scores[, 1],
         PC2 = first2scores[, 2])
ggplot(decathlon_with_pcs, aes(x = PC1, y = PC2, color = Medal, label = Athlete)) +
  geom_point(size = 3, alpha = 0.8) +
  geom_text_repel(size = 3.5, max.overlaps = 30, show.legend = FALSE) +
  labs(
    title = "Decathlon Athletes: PC1 vs PC2",
    x = "PC1",
    y = "PC2"
  ) +
  theme_classic(base_size = 14) +
  theme(legend.position = "right")

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 <- apply(decathlon_numeric, 2, mean)
sd_vec_24   <- apply(decathlon_numeric, 2, sd)

warner_scaled <- (warner-mean_vec_24)/sd_vec_24

warner_pc <- as.numeric(warner_scaled %*% V[, 1:2])
warner_pc
[1] -3.4226989 -0.5092531
warner_df <- data.frame(
  PC1 = warner_pc[1],
  PC2 = warner_pc[2],
  Athlete = "Damian Warner (2020)",
  Medal = "2020 Gold"
)

ggplot(decathlon_with_pcs, aes(x = PC1, y = PC2, color = Medal, label = Athlete)) +
  geom_point(size = 3, alpha = 0.8) +
  geom_text_repel(size = 3.5, max.overlaps = 30, show.legend = FALSE) +
  geom_point(
    data = warner_df, 
    aes(x = PC1, y = PC2),
    color = "red", shape = 8, size = 5
  ) +
  geom_text_repel(
    data = warner_df, 
    aes(label = Athlete),
    color = "red",
    size = 4
  ) +
  labs(
    title = "Decathlon Athletes: PC1 vs PC2 with Warner)",
    x = "PC1",
    y = "PC2"
  ) +
  theme_classic(base_size = 14) +
  theme(legend.position = "top")

I do not think he would have won a metal if completed his pole vaults

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(patchwork)
A_scaled <- as.data.frame(scale(claudeA))
B_scaled <- as.data.frame(scale(claudeB))
C_scaled <- as.data.frame(scale(claudeC))

pA <- ggplot(A_scaled, aes(x = X, y = Y)) +
  geom_point(alpha = 0.7) +
  coord_equal() +
  ggtitle("Claude A")

pB <- ggplot(B_scaled, aes(x = X, y = Y)) +
  geom_point(alpha = 0.7) +
  coord_equal() +
  ggtitle("Claude B")

pC <- ggplot(C_scaled, aes(x = X, y = Y)) +
  geom_point(alpha = 0.7) +
  coord_equal() +
  ggtitle("Claude C")

pA + pB + pC

I think you were skeptical because theses are all graphs that are a strong directions and the first PC of each data set seems to exceed the variability you asked for.

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.

A_svd <- svd(A_scaled)
Ua <- A_svd$u
Da <- A_svd$d
Va <- A_svd$v

PCa <- Ua %*% diag(Da)

(PCa
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1         X2
1 0.9756201 0.02437991
B_svd <- svd(B_scaled)
Ub <- B_svd$u
Db <- B_svd$d
Vb <- B_svd$v

PCb <- Ub %*% diag(Db)

(PCb
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1         X2
1 0.9591807 0.04081932
C_svd <- svd(C_scaled)
Uc <- C_svd$u
Dc <- C_svd$d
Vc <- C_svd$v

PCc <- Uc %*% diag(Dc)

(PCc
 %>% data.frame()
 %>% summarize(across(everything(), var))
 %>% mutate(across(everything(), \(x) x/2))
)
         X1          X2
1 0.9949096 0.005090442

PC1 accounts for over 90% of the variance in each one of these graphs. So you were right to be skeptical of the output that Claude gave you.