Para estudiar las propiedades del PCA vamos a utilizar un set de datos que contiene caracterĂsticas del juego FIFA (el famoso rival de Pro Evolution Soccer, una de las grietas mĂ¡s grandes de mi adolescencia).
Comenzamos leyendo los datos.
library(readr)
data <- read_csv("C:/Users/Nico/Desktop/Maestria/player_stats.csv")
library(ggplot2) #para visualizar
library(dplyr) #para dplyar, jsja
colnames(data)
## [1] "player" "country" "height" "weight"
## [5] "age" "club" "ball_control" "dribbling"
## [9] "marking" "slide_tackle" "stand_tackle" "aggression"
## [13] "reactions" "att_position" "interceptions" "vision"
## [17] "composure" "crossing" "short_pass" "long_pass"
## [21] "acceleration" "stamina" "strength" "balance"
## [25] "sprint_speed" "agility" "jumping" "heading"
## [29] "shot_power" "finishing" "long_shots" "curve"
## [33] "fk_acc" "penalties" "volleys" "gk_positioning"
## [37] "gk_diving" "gk_handling" "gk_kicking" "gk_reflexes"
## [41] "value"
ggplot(data, aes(x = acceleration, y = age))+
geom_point(size = 1, position = position_jitter(height = 0.2))+
theme_light()+
labs(x = "Agresividad", y = "Cu?n viejo es el jugador")
Bueno, la idea del PCA con este dataset es que hay un montĂ³n de dimensiones. Es decir, muchĂsimos atributos relacionados con cĂ³mo juega cada jugador. Y la idea inicial es ver si podemos reducir un poco esa cantidad de informaciĂ³n.
Vamos entonces a elegir quĂ© columnas del dataset nos interesan mĂ¡s.
library(dplyr)
pcaVariables <- c("ball_control","agility",
"jumping", "finishing", "penalties",
"reactions", "slide_tackle","stand_tackle",
"vision", "aggression", "acceleration",
"crossing", "short_pass", "long_pass",
"sprint_speed", "heading", "shot_power",
"curve","volleys","long_shots","composure")
#Las vamos a describir un poco. Primero las pasamos a n?mero y las escalamos. Aunque creo que ya est?n en una misma unidad de medida.
data <- data %>%
mutate(across(all_of(pcaVariables), ~ as.numeric(.)))%>%
mutate(across(all_of(pcaVariables), ~ as.vector(scale(.))))
Como son muchos datos vamos a reducir la cantidad.
set.seed(43)
data <- data %>%
sample_n(500)
pcaResultado <- prcomp(data[, pcaVariables], center = TRUE, scale. = FALSE)
summary(pcaResultado)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 3.431 1.6801 1.23701 1.08279 0.86929 0.66264 0.57607
## Proportion of Variance 0.573 0.1374 0.07447 0.05706 0.03677 0.02137 0.01615
## Cumulative Proportion 0.573 0.7104 0.78483 0.84188 0.87866 0.90003 0.91618
## PC8 PC9 PC10 PC11 PC12 PC13 PC14
## Standard deviation 0.53218 0.4924 0.43123 0.41071 0.39469 0.38529 0.34106
## Proportion of Variance 0.01378 0.0118 0.00905 0.00821 0.00758 0.00722 0.00566
## Cumulative Proportion 0.92996 0.9418 0.95081 0.95902 0.96660 0.97382 0.97948
## PC15 PC16 PC17 PC18 PC19 PC20 PC21
## Standard deviation 0.30893 0.29904 0.25956 0.2484 0.23390 0.18603 0.13547
## Proportion of Variance 0.00464 0.00435 0.00328 0.0030 0.00266 0.00168 0.00089
## Cumulative Proportion 0.98413 0.98848 0.99176 0.9948 0.99742 0.99911 1.00000
library(factoextra)
fviz_eig(pcaResultado,
addlabels = TRUE)
loadings <- round(pcaResultado$rotation[, 1:2],2)
knitr::kable(loadings)
| PC1 | PC2 | |
|---|---|---|
| ball_control | -0.27 | -0.02 |
| agility | -0.22 | 0.09 |
| jumping | -0.07 | -0.19 |
| finishing | -0.24 | 0.25 |
| penalties | -0.24 | 0.20 |
| reactions | -0.18 | -0.06 |
| slide_tackle | -0.13 | -0.49 |
| stand_tackle | -0.14 | -0.49 |
| vision | -0.23 | 0.13 |
| aggression | -0.18 | -0.35 |
| acceleration | -0.20 | 0.05 |
| crossing | -0.25 | -0.03 |
| short_pass | -0.27 | -0.11 |
| long_pass | -0.25 | -0.17 |
| sprint_speed | -0.20 | 0.03 |
| heading | -0.18 | -0.20 |
| shot_power | -0.21 | 0.22 |
| curve | -0.25 | 0.10 |
| volleys | -0.25 | 0.23 |
| long_shots | -0.26 | 0.17 |
| composure | -0.24 | -0.04 |
El biplot es una representaciĂ³n en 2D de cĂ³mo las variables originales se proyectan sobre los dos primeros ejes del PCA. Si las flechas estĂ¡n cerca entre sĂ esas variables se correlacionan positivamente. Si estĂ¡n en direcciones opuestas, estĂ¡n correlacionadas negativamente.
library(factoextra)
fviz_pca_var(pcaResultado, col.var = "contrib",
gradient.cols = c("lightblue", "steelblue", "darkblue"),
repel = TRUE)
Ahora vamos a proyectar a los jugadores sobre los dos componentes, lo que nos permite ver cĂ³mo se distribuyen los jugadores en el nuevo espacio reducido.
fviz_pca_ind(pcaResultado, geom_ind = "point", col.ind = "gray30",
pointshape = 21, addEllipses = FALSE, repel = FALSE) +
theme_minimal()