Skip to content
Snippets Groups Projects
Commit 0be63053 authored by Mario Garrido Tapias's avatar Mario Garrido Tapias
Browse files

3D PCA and Var Biplot

parent 30cbf0b7
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,8 @@
# LIBRERIAS #
#############
library(stats) # Calculos
library(factoextra) # Representaciones
library(factoextra) # PCAs representations
library(plotly) # 3D PCA representations
############################# ####################
# CONJUNTO de ENTRENAMIENTO # # CONJUNTO de TEST #
......@@ -17,16 +18,18 @@ library(rsample)
jugadoresRepes <- laLigaPlayersStudy[rowsPIn2C[,1], ]
jugadoresRepes <- rbind(jugadoresRepes, laLigaPlayersStudy[rowsPIn2C[, 2], ])
rowsQuit <- c(rowsPIn2C[,1], rowsPIn2C[,2])
laLigaPlayersSplit <- laLigaPlayersStudy[-rowsQuit, ]
laLigaPlayersSplit$position.1920 <- laLigaPlayers[-rowsQuit,]$position.1920
laLigaPlayersSplit$position <- laLigaPlayers[-rowsQuit,]$position.1920
laLiga_split <- initial_split(laLigaPlayersSplit, prop = 8/10, strata = "position.1920")
laLiga_split <- initial_split(laLigaPlayersSplit, prop = 8/10, strata = "position")
train <- training(laLiga_split)
train <- train[,-39]
train <- rbind(train, jugadoresRepes)
test <- testing(laLiga_split)
test <- test[,-39]
# ToDo
plotFor3SeasonsForPos(laLigaPlayers, "position")
plotFor3SeasonsForPos(train, "position")
plotFor3SeasonsForPos(test, "position")
......@@ -54,12 +57,25 @@ rownames(pca_laLiga$x) <- train$player
# Con 48 PC -> Suma de los 2 primeros = 57.44, Suma de los 3 primeros = 33.925
# Con 35 PC -> Suma de los 2 primeros = 19.96, Suma de los 3 primeros = 24.15
# REPRESENTACIONES
repeatedPlayers <- which(duplicated(train$player) == TRUE)
for (player in repeatedPlayers){
train$player[player] <- paste(train$player[player], 2, sep = "")
}
rownames(pca_laLiga$x) <- train$player
# REPRESENTATIONS
# 2D
fviz_pca_ind(pca_laLiga, geom.ind = "point",
col.ind = "#FC4E07",
col.ind = "aquamarine3",
axes = c(1, 2),
pointsize = 1.5)
# 3D
data <- data.frame(pca_laLiga$x)
fig <- plot_ly(data, x = ~PC1, y = ~PC2, z = ~PC3, colors = c('aquamarine3') ) %>%
add_markers(size = 12)
fig
# CREACION del vector POSICIONES
colores <- function(vec){
# la función rainbow() devuelve un vector que contiene el número de colores distintos
......@@ -82,13 +98,17 @@ plot(pca_laLiga$x[,1:2], col = colores(unique(laLigaPlayers$position.1718)),
legend("topright", legend=unique(laLigaPlayers$position.1718),
col = colores(unique(laLigaPlayers$position.1718)),pch=19, cex=0.8)
table(laLigaPlayers$position.1718)
fviz_pca_var(pca_laLiga, col.var = "cos2",
geom.var = "arrow",
labelsize = 2,
repel = FALSE)
# BIPLOT
biplot(pca_laLiga, scale = 0, cex = 0.5, col = c("dodgerblue3", "deeppink3"))
# SELECCIÓN DEL NUMERO DE PC
fviz_screeplot(pca_laLiga, addlabels = TRUE, ylim = c(0, 40))
# BIPLOT (toSave)
biplot(pca_laLiga, scale = 0, cex = 0.5, col = c("khaki4", "darkorchid3"))
fviz_pca_biplot(pca_laLiga)
fviz_pca_var(pca_laLiga,
col.var = "contrib",
gradient.cols = c("#FC4E07", "#E7B800", "#006600"),
labelsize = 3,
repel = TRUE)
# SELECCIÓN DEL NUMERO DE PC (700x550)
fviz_screeplot(pca_laLiga, addlabels = TRUE, ylim = c(0, 40),
xlab = "Dimensiones", ylab = "Porcentaje de variabilidad explicada")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment