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 need to scale this data since the numbers are in both seconds and distances which are not easily comparable, so scaling will help with this issue

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?

decathlon_raw <- decathlon[, sapply(decathlon, is.numeric)]
decathlon_raw <- subset(decathlon_raw, select = -Overall)

decathlon_scaled <- scale(decathlon_raw)

svd_result <- svd(decathlon_scaled)
u <- svd_result$u
d <- svd_result$d
v <- svd_result$v

pc_scores <- u %*% diag(d)

var_explained <- d^2 / sum(d^2)
percent_var <- round(100 * var_explained[1:2], 2)

percent_var
[1] 29.13 19.28

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 <- svd_result$v[, 1:2]
rownames(loadings) <- colnames(decathlon_raw)
colnames(loadings) <- c("PC1", "PC2")
loadings
                     PC1         PC2
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

From what I found most of the medalists will be in quadrant 1 with a few in 2 and 3

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.

library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.4.3
library(ggrepel)
Warning: package 'ggrepel' was built under R version 4.4.3
decathlon$PC1 <- svd_result$u[, 1] * svd_result$d[1]
decathlon$PC2 <- svd_result$u[, 2] * svd_result$d[2]

top3_cutoff <- sort(decathlon$Overall, decreasing = TRUE)[3]
decathlon$Medalist <- decathlon$Overall >= top3_cutoff

ggplot(decathlon, aes(x = PC1, y = PC2, color = Medalist, label = Athlete)) +
  geom_point(size = 3) +
  geom_text_repel(max.overlaps = Inf) +
  labs(
    title = "Decathlon 2024: PCA Scatterplot",
    x = "PC1",
    y = "PC2"
  ) +
  theme_minimal()

Based off of my plot I am able to see that the medalists are in Quadrant 2

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_raw)
sd_vec_24 <- apply(decathlon_raw, 2, sd)

warner_std <- (warner - mean_vec_24) / sd_vec_24
warner_pc <- warner_std %*% svd_result$v[, 1:2]

warner_df <- data.frame(
  PC1 = warner_pc[1],
  PC2 = warner_pc[2],
  Athlete = "Warner2020",
  Medalist = TRUE
)

combined_df <- rbind(
  decathlon[, c("PC1", "PC2", "Athlete", "Medalist")],
  warner_df
)

library(ggplot2)
library(ggrepel)

ggplot(combined_df, aes(x = PC1, y = PC2, color = Medalist, label = Athlete)) +
  geom_point(size = 3) +
  geom_text_repel(max.overlaps = Inf) +
  theme_minimal() +
  labs(
    title = "Decathlon 2024 + Warner 2020",
    x = "PC1",
    y = "PC2"
  )

Based off of my graphs I would say that Warner’s 2020 performance would not have won a medal in 2024 since he fell short of the quadrant that contains the 3 medalists

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)

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

dfA <- as.data.frame(claudeA)
dfB <- as.data.frame(claudeB)
dfC <- as.data.frame(claudeC)

plot_scaled <- function(df, title) {
  ggplot(df, aes(x = X, y = Y)) +
    geom_point(alpha = 0.6) +
    coord_fixed() +
    ggtitle(title) +
    theme_minimal()
}

pA <- plot_scaled(dfA, "Claude A")
pB <- plot_scaled(dfB, "Claude B")
pC <- plot_scaled(dfC, "Claude C")

(pA | pB | pC)

The reason you were probably skeptical is because all of the graphs seem to have clear trends in them with minimal scattering of points. They all look like they would 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.

pc1_variance <- function(df) {
  svd_result <- svd(df)
  var_explained <- svd_result$d^2 / sum(svd_result$d^2)
  return(var_explained[1])
}

varA <- pc1_variance(dfA)
varB <- pc1_variance(dfB)
varC <- pc1_variance(dfC)

percent_variance <- data.frame(
  Dataset = c("Claude A", "Claude B", "Claude C"),
  PC1_Variance = round(100 * c(varA, varB, varC), 2)
)

print(percent_variance)
   Dataset PC1_Variance
1 Claude A        97.56
2 Claude B        95.92
3 Claude C        99.49