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 set because the scores of different events are of different magnitudes. For example, X1500m has a score in the hundreds while high jump is in the ones.

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.4.3
Warning: package 'purrr' was built under R version 4.4.3
── 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.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── 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
mean_centered_decathlon <- scale(decathlon %>% select(5:14))

X <- svd(mean_centered_decathlon)

first_two_PCs <- X$u[,1:2] %*% diag(X$d[1:2])

(first_two_PCs
  %>% data.frame()
  %>% summarize(across(everything(), var))
  %>% mutate(across(everything(), \(x) x/10))
  )
         X1        X2
1 0.2913494 0.1928006
0.2913494 + 0.1928006
[1] 0.48415
loadings <- X$v
rownames(loadings) <- colnames(decathlon[5:14])

The first two PCs explain about 48.4% of the overall 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[,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

The medalists would probably be in the second quadrant. Most of the 2nd PC loadings are positive, and the most important variables like longjump and shotput are negative in the 1st PC.

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 = first_two_PCs[,1],
                         PC2 = first_two_PCs[,2])
                )

(ggplot(data = decathlon)
  + geom_point(aes(x=PC1, y=PC2, color = Medal))
  + geom_hline(yintercept = 0, linetype = "dashed", color = "black")
  + geom_vline(xintercept = 0, linetype = "dashed", color = "black")
  + theme_classic()
    )

All medalists are located in the second quadrant along with two other competitors.

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 <- c(mean(decathlon$X100m), mean(decathlon$LongJump), mean(decathlon$ShotPut), mean(decathlon$HighJump), mean(decathlon$X400m), mean(decathlon$X110mHurdle), mean(decathlon$Discus), mean(decathlon$PoleVault), mean(decathlon$Javelin), mean(decathlon$X1500m))
)
 [1]  10.7465   7.3780  14.8630   1.9975  48.1100  14.3640  46.1160   4.3600
 [9]  61.6270 275.8450
(sd_vec_24 <- c(sd(decathlon$X100m), sd(decathlon$LongJump), sd(decathlon$ShotPut), sd(decathlon$HighJump), sd(decathlon$X400m), sd(decathlon$X110mHurdle), sd(decathlon$Discus), sd(decathlon$PoleVault), sd(decathlon$Javelin), sd(decathlon$X1500m))
)
 [1]  0.25192470  0.35983329  0.86639665  0.06873864  1.22200439  0.37591152
 [7]  3.82933882  1.50486929  7.10415453 12.15537460
warner_pc <- as.vector(((warner - mean_vec_24)/sd_vec_24)) %*% loadings

(ggplot(data = decathlon)
  + geom_point(aes(x=PC1, y=PC2, color = Medal))
  + geom_point(aes(x=warner_pc[1], y=warner_pc[2]), color = 'red')
  + geom_hline(yintercept = 0, linetype = "dashed", color = "black")
  + geom_vline(xintercept = 0, linetype = "dashed", color = "black")
  + theme_classic()
    )
Warning in geom_point(aes(x = warner_pc[1], y = warner_pc[2]), color = "red"): All aesthetics have length 1, but the data has 20 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
  a single row.

All of the medalists from 2024 are in the second quadrant while Warner is down in the third quadrant. This tells us that he probably would not have won a medal in 2024 with his 2020 performance.

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?

scaled_claudeA <- scale(claudeA)
scaled_claudeB <- scale(claudeB)
scaled_claudeC <- scale(claudeC)

library(patchwork)

p1 <- (ggplot(data = scaled_claudeA) + geom_point(aes(x=X, y=Y))) + labs(title = 'ClaudeA')

p2 <- (ggplot(data = scaled_claudeB) + geom_point(aes(x=X, y=Y))) + labs(title = 'ClaudeB')

p3 <- (ggplot(data = scaled_claudeC) + geom_point(aes(x=X, y=Y))) + labs(title = 'ClaudeC')

combined_plot <- p1 + p2 + p3 + plot_layout(ncol = 3) + coord_fixed()

combined_plot

I believe you were skeptical because in all data sets, the first PC is going to be explaining a majority of the variance. This is because the data looks highly correlated, so information about one x can tell you a lot about the y, and vice-versa. All PC1s are going to be much higher than requested, besides maybe the 90% PC1 request.

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.

svdA <- svd(scaled_claudeA)

(pcA <- ((svdA$u %*% diag(svdA$d))
         %>% data.frame()
         %>% summarize(across(everything(), var))
         %>% mutate(across(everything(), \(x) x/2)))
)
         X1         X2
1 0.9756201 0.02437991
svdB <- svd(scaled_claudeB)

(pcB <- ((svdB$u %*% diag(svdB$d))
         %>% data.frame()
         %>% summarize(across(everything(), var))
         %>% mutate(across(everything(), \(x) x/2)))
)
         X1         X2
1 0.9591807 0.04081932
svdC <- svd(scaled_claudeC)

(pcC <- ((svdC$u %*% diag(svdC$d))
         %>% data.frame()
         %>% summarize(across(everything(), var))
         %>% mutate(across(everything(), \(x) x/2)))
)
         X1          X2
1 0.9949096 0.005090442

The PC1 in set A explains 97.5% of the total variability, 96% in set B, and 99.5% in set C. All of these are much greater than what we asked for. This is the result that we expected.