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.
Even though most of these numbers look like they are measured in the same units, presumably meters, we need to scale this data first because some events are expected to have much higher meter values, so those would affect the output more than they should. It looks like unscaled X1500m has more than 100times more importance than something like highjump.
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?
cat("Variance explained by PC2:", round(variance_pc2 *100, 2), "%\n")
Variance explained by PC2: 19.28 %
cat("Total variance explained by first 2 PCs:", round(variance_first_two *100, 2), "%\n")
Total variance explained by first 2 PCs: 48.41 %
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.
# Interpretation for medalists:# Higher performance in most events means LOWER times (running) or HIGHER distances/heights# We need to check the sign and magnitude of loadings to determine quadrant
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.
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
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?
X Y
1 40 115
2 45 105
3 50 110
4 55 95
5 60 100
6 65 85
head(claudeB)
X Y
1 40 50
2 45 55
3 50 48
4 55 60
5 60 53
6 65 65
head(claudeC)
X Y
1 40 120
2 45 115
3 50 117
4 55 110
5 60 112
6 65 105
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_scaled <-as.data.frame(scale(claudeA))claudeB_scaled <-as.data.frame(scale(claudeB))claudeC_scaled <-as.data.frame(scale(claudeC))plotA <-ggplot(claudeA_scaled, aes(x = X, y = Y)) +geom_point(alpha =0.6, color ="steelblue") +coord_fixed(ratio =1) +theme_minimal() +labs(title ="Claude Data A (Scaled)",x ="X (scaled)", y ="Y (scaled)")plotB <-ggplot(claudeB_scaled, aes(x = X, y = Y)) +geom_point(alpha =0.6, color ="darkgreen") +coord_fixed(ratio =1) +theme_minimal() +labs(title ="Claude Data B (Scaled)",x ="X (scaled)", y ="Y (scaled)")plotC <-ggplot(claudeC_scaled, aes(x = X, y = Y)) +geom_point(alpha =0.6, color ="darkred") +coord_fixed(ratio =1) +theme_minimal() +labs(title ="Claude Data C (Scaled)",x ="X (scaled)", y ="Y (scaled)")plotA | plotB | plotC
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.
calculate_pc1_variance <-function(data_scaled) { svd_result <-svd(data_scaled) D <- svd_result$d total_variance <-sum(D^2) pc1_variance <- D[1]^2/ total_variancereturn(pc1_variance)}variance_A <-calculate_pc1_variance(claudeA_scaled)variance_B <-calculate_pc1_variance(claudeB_scaled)variance_C <-calculate_pc1_variance(claudeC_scaled)cat("Percent of total variability explained by PC1:\n")