-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
509 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
----#carga de librerias#----- | ||
|
||
library(FactoMineR) | ||
library(foreign) | ||
library(psych) | ||
library(knitr) | ||
library(xtable) | ||
suppressPackageStartupMessages(library(dendextend)) | ||
suppressPackageStartupMessages(library(dendextendRcpp)) | ||
suppressPackageStartupMessages(library(ggplot2)) | ||
suppressPackageStartupMessages(library(ggfortify)) | ||
library(corrplot) | ||
#library(Stuff) | ||
suppressPackageStartupMessages(library(dplyr)) | ||
library(broom) | ||
library(plotly) | ||
library(reshape) | ||
library(GGally) | ||
library(lubridate) | ||
library(SportsAnalytics) | ||
library(archetypes) | ||
library("RColorBrewer") | ||
|
||
----#carga de sets de datos#----- | ||
|
||
DatosPremiumArgentina2013 <- read.csv("DatosPremiumArgentina2013.csv", sep=";") | ||
DatosPremiumArgentina2014 <- read.csv("DatosPremiumArgentina2014.csv", sep=";") | ||
DatosPremiumArgentina2015 <- read.csv("DatosPremiumArgentina2015.csv", sep=";") | ||
DatosPremiumArgentina2016 <- read.csv("DatosPremiumArgentina2016.csv", sep=";") | ||
|
||
DatosPremiumArgentina2013[,c("local","visitante","perso_nombre","perso_apellido","team")] <- NULL | ||
DatosPremiumArgentina2014[,c("local","visitante","perso_nombre","perso_apellido","team")] <- NULL | ||
DatosPremiumArgentina2015[,c("local","visitante","perso_nombre","perso_apellido","team")] <- NULL | ||
DatosPremiumArgentina2016[,c("local","visitante","perso_nombre","perso_apellido","team")] <- NULL | ||
|
||
' | ||
DatosPremiumArgentina2016$rol_id_rol <- factor(DatosPremiumArgentina2016$rol_id_rol) | ||
summary(DatosPremiumArgentina2016) | ||
DatosPremiumArgentina2016$local.1 <- as.character(DatosPremiumArgentina2016$local.1) | ||
DatosPremiumArgentina2016$team.1 <- as.character(DatosPremiumArgentina2016$team.1) | ||
DatosPremiumArgentina2016$J_local <- ifelse(DatosPremiumArgentina2016$team.1==DatosPremiumArgentina2016$local.1,"L","V") | ||
ggplot(data = DatosPremiumArgentina2016, aes(x = J_local,y=disparo_afuera, fill = J_local)) + geom_boxplot() | ||
ggplot(data = DatosPremiumArgentina2016, aes(x = J_local,y=pase_correcto, fill = J_local)) + geom_boxplot() | ||
datos2016 <- DatosPremiumArgentina2016[DatosPremiumArgentina2016$team.1! | ||
' | ||
|
||
-----#unificacion de bases------ | ||
|
||
Basetotal <- rbind(DatosPremiumArgentina2013,DatosPremiumArgentina2014,DatosPremiumArgentina2015,DatosPremiumArgentina2016) | ||
|
||
---#correccion tipo de datos------- | ||
|
||
Basetotal$rol_id_rol <- factor(Basetotal$rol_id_rol) | ||
|
||
Basetotal$local.1 <- as.character(Basetotal$local.1) | ||
|
||
Basetotal$team.1 <- as.character(Basetotal$team.1) | ||
|
||
----# creacion variable local visitante----- | ||
Basetotal$J_local <- ifelse(Basetotal$team.1==Basetotal$local.1,"L","V") | ||
|
||
-----#se saca la seleccion y el apodo---- | ||
Basetotal <- Basetotal[Basetotal$team.1!='Argentina',] | ||
Basetotal$perso_apodo <- NULL | ||
Basetotal <- Basetotal[Basetotal$rol_id_rol %in% c('1','2','3','4'),] # se sacan tecnicos y arbitros | ||
|
||
summary(Basetotal) | ||
|
||
-----#transformacion datos---- | ||
|
||
dat.m <- melt(Basetotal,id.vars='J_local', measure.vars=c("goles_convertidos","goles_encontra","asistencias","disparo_afuera","disparo_palo","disparo_atajado","penal_errado","faltas","offsides","amarillas","doble_amarilla","rojas","pase_correcto","pase_incorrecto","despejes","quites","atajadas","atajada_penal")) | ||
|
||
----#multboxplot---- | ||
p <- ggplot(dat.m) + geom_boxplot(aes(x=J_local, y=value, color=variable)) | ||
|
||
p+facet_wrap( ~ variable, scales="free") | ||
|
||
----#grafico de barras local vs visitante de rol id y titular----- | ||
t1 <- count(Basetotal,vars = c("J_local","rol_id_rol")) | ||
|
||
p1 <- ggplot(t1,aes(x=rol_id_rol,y=freq,fill=J_local))+geom_bar(stat="identity",position="dodge") | ||
|
||
t2 <- count(Basetotal,vars = c("J_local","titular")) | ||
|
||
p2 <- ggplot(t2,aes(x=titular,y=freq,fill=J_local))+geom_bar(stat="identity",position="dodge") | ||
|
||
multiplot(p1,p2) | ||
|
||
----#Cambio a tipo fecha----- | ||
Basetotal$fecha <- as.Date(Basetotal$fecha,"%d/%m/%Y") | ||
|
||
l1 <- ggplot(Basetotal,aes(x=fecha,y=goles_convertidos))+geom_line() | ||
|
||
l1 | ||
|
||
----#datos por jugador---- | ||
|
||
jugadores <- Basetotal %>% select(perso_nombre.1,perso_apellido.1,minutos_jugados:atajada_penal)%>%group_by(perso_nombre.1,perso_apellido.1) | ||
|
||
jugadores_agr <- jugadores %>%summarise_each(funs(sum)) | ||
|
||
#se sacan jugadores con menos de 90 minutos en los 3 torneos | ||
|
||
jugadores_agr <- jugadores_agr %>% filter(minutos_jugados>=90) | ||
|
||
jugadores_agr <- jugadores_agr %>% mutate(pr_goles_convertidos=goles_convertidos/minutos_jugados,pr_asistencias=asistencias/minutos_jugados, | ||
pr_disparo_afuera=disparo_afuera/minutos_jugados,pr_disparo_atajado=disparo_atajado/minutos_jugados, | ||
pr_faltas=faltas/minutos_jugados,pr_faltas_recibidos=faltas_recibidas/minutos_jugados,pr_offsides=offsides/minutos_jugados, | ||
pr_amarillas=(amarillas+doble_amarilla)/minutos_jugados,pr_expulsados=(doble_amarilla+rojas)/minutos_jugados,pr_pase_correcto=pase_correcto/minutos_jugados, | ||
pr_incorrecto=pase_incorrecto/minutos_jugados,pr_despejes=despejes/minutos_jugados,pr_quites=quites/minutos_jugados,pr_atajadas=atajadas/minutos_jugados,jugador=paste(perso_apellido.1,perso_nombre.1,sep=",")) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
----#carga de librerias#----- | ||
|
||
library(ggplot2) | ||
library(dplyr) | ||
library(plyr) | ||
library(reshape) | ||
library(GGally) | ||
library(lubridate) | ||
library(SportsAnalytics) | ||
library(archetypes) | ||
library("RColorBrewer") | ||
library(lattice) | ||
|
||
-----#Analisis de datos#----- | ||
|
||
summary(datos2016) | ||
|
||
head(datos2016[datos2016$rol_id_rol=='5',]) # tecnicos rol 5 | ||
|
||
datos2016 <- datos2016[datos2016$rol_id_rol!='5',] | ||
|
||
-----#transformacion datos#----- | ||
|
||
dat.m <- melt(datos2016,id.vars='J_local', measure.vars=c("goles_convertidos","goles_encontra","asistencias","disparo_afuera","disparo_palo","disparo_atajado","penal_errado","faltas","offsides","amarillas","doble_amarilla","rojas","pase_correcto","pase_incorrecto","despejes","quites","atajadas","atajada_penal")) | ||
|
||
|
||
-------#multboxplot#-------- | ||
p <- ggplot(dat.m) + geom_boxplot(aes(x=J_local, y=value, color=variable)) | ||
|
||
p+facet_wrap( ~ variable, scales="free") | ||
|
||
(gg <- ggplotly(p)) | ||
|
||
-----#correlaciones pases#------- | ||
ggpairs(datos2016,mapping = ggplot2::aes(color=J_local),columns = c("goles_convertidos","asistencias","disparo_afuera","disparo_atajado","pase_correcto","despejes","quites")) | ||
|
||
-----#grafico de barras local vs visitante de rol id y titular#---- | ||
t1 <- count(datos2016,vars = c("J_local","rol_id_rol")) | ||
|
||
p1 <- ggplot(t1,aes(x=rol_id_rol,y=freq,fill=J_local))+geom_bar(stat="identity",position="dodge") | ||
|
||
t2 <- count(datos2016,vars = c("J_local","titular")) | ||
|
||
p2 <- ggplot(t2,aes(x=titular,y=freq,fill=J_local))+geom_bar(stat="identity",position="dodge") | ||
|
||
(ggp2 <- ggplotly(p2)) | ||
|
||
multiplot(p1,p2) | ||
#head(as.Date(datos2016$fecha,"%d/%m/%Y")) | ||
datos2016$fecha <- as.Date(datos2016$fecha,"%d/%m/%Y") | ||
|
||
l1 <- ggplot(datos2016,aes(x=fecha,y=goles_convertidos))+geom_line(stat="identity",position="dodge") | ||
|
||
#head(month(datos2016$fecha)) funcion de lubridate | ||
|
||
l1 <- ggplot(datos2016,aes(x=month(fecha),y=goles_convertidos))+ geom_point() + | ||
xlab("") + ylab("goles convertidos") | ||
l1 | ||
|
||
----#datos por jugador#------ | ||
|
||
jugadores <- datos2016 %>% select(perso_nombre.1,perso_apellido.1,rol_id_rol,team.1,minutos_jugados:atajada_penal)%>%group_by(perso_nombre.1,perso_apellido.1,rol_id_rol,team.1) | ||
|
||
jugadores_agr <- jugadores %>%summarise_each(funs(sum)) %>% mutate(pr_goles_convertidos=goles_convertidos/minutos_jugados,pr_asistencias=asistencias/minutos_jugados, | ||
pr_disparo_afuera=disparo_afuera/minutos_jugados,pr_disparo_atajado=disparo_atajado/minutos_jugados, | ||
pr_faltas=faltas/minutos_jugados,pr_faltas_recibidos=faltas_recibidas/minutos_jugados,pr_offsides=offsides/minutos_jugados, | ||
pr_amarillas=(amarillas+doble_amarilla)/minutos_jugados,pr_expulsados=(doble_amarilla+rojas)/minutos_jugados,pr_pase_correcto=pase_correcto/minutos_jugados, | ||
pr_incorrecto=pase_incorrecto/minutos_jugados,pr_despejes=despejes/minutos_jugados,pr_quites=quites/minutos_jugados,pr_atajadas=atajadas/minutos_jugados,jugador=paste(perso_apellido.1,perso_nombre.1,sep=",")) | ||
|
||
|
||
----#archetypes of players------ | ||
col_pal <- brewer.pal(7, "Set1") | ||
|
||
col_black <- rgb(0, 0, 0, 0.2) | ||
#mat <- as.matrix(subset(dat, select = -c(Team, Name, Number, Nationality,WeakFootAccuracy, WeakFootFrequency))) | ||
|
||
rownames(matjugadores) <- NULL | ||
|
||
pcplot(matjugadores, col = col_black, las = 2) | ||
|
||
set.seed(1234) | ||
|
||
as <- stepArchetypes(matjugadores, k = 1:14) | ||
|
||
screeplot(as) | ||
|
||
a4 <- bestModel(as[[5]]) | ||
|
||
parameters(a4) | ||
|
||
barplot(a4, matjugadores, percentiles = TRUE) | ||
|
||
pcplot(a4, matjugadores, data.col = col_black, atypes.col = col_pal[1:5]) | ||
|
||
legend("topleft", legend = sprintf("A%s", 1:5),col = col_pal[1:5], lwd = 1, bg = "white") | ||
|
||
--------### Alpha coefficients:------ | ||
|
||
coef <- coef(a4, "alphas") | ||
|
||
pcplot(coef, col = c(NA, NA, col_black),rx = matrix(c(0, 1), ncol = 5, nrow = 2), var.label = FALSE) | ||
|
||
|
||
coef <- coef(a4, "alphas") | ||
|
||
|
||
## The best player is a combination of Archetyp 1 and Archetype 2 with | ||
## Archetype 1 contributing more than Archetype 2: | ||
which <- which(coef[, 3] == 0 & coef[, 4] == 0 & | ||
coef[, 1] > 0 & coef[, 2] > 0 & | ||
coef[, 1] < coef[, 2]) | ||
|
||
cbind(jugadores_agr[which, c("jugador")], | ||
coef[which, ]) | ||
|
||
## ... in relation to player position: | ||
pos <- as.character(jugadores$rol_id_rol) | ||
|
||
cols <- rep("gray", length(pos)) | ||
|
||
cols[pos == "Defender"] <- col_pal[1] | ||
|
||
|
||
pcplot(coef, col = c(NA, NA, cols), | ||
rx = matrix(c(0, 1), ncol = 4, nrow = 2), var.label = FALSE) | ||
|
||
#Good players: | ||
|
||
good_players <- function(atype, threshold) { | ||
which <- which(coef(a4, "alphas")[, atype] > threshold) | ||
good_coef <- coef(a4, "alphas")[which, ] | ||
good_dat <- subset(jugadores_agr[which, ], select = c(jugador)) | ||
good_dat <- cbind(good_dat, good_coef) | ||
good_dat <- good_dat[order(-good_coef[, atype]), ] | ||
good_dat | ||
} | ||
|
||
good_threshold <- 0.95 | ||
|
||
players <- lapply(1:5, good_players, good_threshold) | ||
|
||
players | ||
|
||
|
||
myColours <- brewer.pal(6,"Blues") | ||
my.settings <- list( | ||
superpose.polygon=list(col=myColours[2:5], border="transparent"), | ||
strip.background=list(col=myColours[6]), | ||
strip.border=list(col="black") | ||
) | ||
|
||
parallelplot(matjugadores,horizontal.axis=FALSE, par.settings=my.settings) | ||
|
||
|
||
ggparcoord(matjugadores, columns = c(1:3),scale = "centerObs")+geom_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
----#carga de librerias#----- | ||
|
||
library(FactoMineR) | ||
library(foreign) | ||
library(psych) | ||
library(knitr) | ||
library(xtable) | ||
suppressPackageStartupMessages(library(dendextend)) | ||
suppressPackageStartupMessages(library(dendextendRcpp)) | ||
suppressPackageStartupMessages(library(ggplot2)) | ||
suppressPackageStartupMessages(library(ggfortify)) | ||
library(corrplot) | ||
#library(Stuff) | ||
suppressPackageStartupMessages(library(dplyr)) | ||
library(broom) | ||
library(plotly) | ||
|
||
-----#a- cluster jerárquico----- | ||
|
||
matjugadores <- as.matrix(jugadores_agr[,c(23:36)]) | ||
#nombres <- iconv(jugadores_agr$jugador,to='ASCII//TRANSLIT') #por tildes en ubuntu | ||
nombres <- jugadores_agr$jugador | ||
rownames(matjugadores) <- nombres | ||
|
||
|
||
d2 = dist(matjugadores,method = "euclidean") | ||
jug.clust.sin = as.dendrogram(hclust(d2, method = "single")) %>% set("branches_lwd", 2) | ||
jug.clust.com = as.dendrogram(hclust(d2, method = "complete")) %>% set("branches_lwd", 2) | ||
jug.clust.avg = as.dendrogram(hclust(d2, method = "average")) %>% set("branches_lwd", 2) | ||
jug.clust.ward = as.dendrogram(hclust(d2, method = "ward.D")) %>% set("branches_lwd", 2) | ||
jug.dend = dendlist("Cercano" = jug.clust.sin, "Lejano" = jug.clust.com, "Promedio" = jug.clust.avg,"Ward"=jug.clust.ward) | ||
|
||
corrplot(cor.dendlist(jug.dend), "pie", "lower") | ||
|
||
plot(jug.clust.avg %>% set("branches_k_color", k=3) %>% set("branches_lwd", 2), main = "Average") | ||
jug3 <- cutree(jug.clust.com,5) | ||
jugadores_agr$clust <- factor(jug3) | ||
|
||
qplot(jugadores_agr$clust,xlab="Cluster") | ||
|
||
plot(jugadores_agr$clust) | ||
|
||
qplot() | ||
ggplot(jugadores_agr,aes()) | ||
|
||
p1 <- ggplot(t1,aes(x=rol_id_rol,y=freq,fill=J_local))+geom_bar(stat="identity",position="dodge") | ||
|
||
|
||
Tabla <- describeBy(jugadores_agr[,c(23:36)], group = jugadores_agr$clust, mat = T)[,c("group1", "mean")] | ||
Tabla <- cbind(Tabla,"promedio gral"=rep(colMeans(jugadores_agr[,c(23:36)]), each = 5)) | ||
|
||
Tabla | ||
|
||
----#b- Idem para un cluster no jerárquico.----- | ||
|
||
pca.jug <- PCA(jugadores_agr[,c(23:36)]) | ||
PCA.1.2 = pca.jug$ind$coord[,c(1:2)] | ||
PCA.1.3 = pca.jug$ind$coord[,c(1:3)] | ||
|
||
|
||
kclusts <- data.frame(k=1:5) %>% group_by(k) %>% do(kclust=kmeans(jugadores_agr[,c(23:36)], .$k)) | ||
clusters <- kclusts %>% group_by(k) %>% do(tidy(.$kclust[[1]])) | ||
|
||
|
||
assignments <- kclusts %>% group_by(k) %>% do(augment(.$kclust[[1]], PCA.1.2)) | ||
|
||
clusterings <- kclusts %>% group_by(k) %>% do(glance(.$kclust[[1]])) | ||
|
||
scrpl <- ggplot(clusterings, aes(k, tot.withinss)) + geom_line() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme_bw() | ||
|
||
(ggscrpl <- ggplotly(scrpl)) | ||
|
||
#4 grupos parece ser lo mejor por screeplot | ||
|
||
k4 <- ggplot(PCA.1.2, aes(x = Dim.1, y = Dim.2), theme = NULL) + geom_point(size = 3, color = subset(assignments, k == 4)$.cluster) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme_bw() + ggtitle("Kmeans") | ||
plot(k4) | ||
|
||
(ggdim2 <- ggplotly(k4)) | ||
|
||
k4_dim3 = ggplot(PCA.1.3, aes(x = Dim.1, y = Dim.3), theme = NULL) + geom_point(size = 3, color = subset(assignments, k == 4)$.cluster) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme_bw() + ggtitle("Kmeans") | ||
plot(k4_dim3) | ||
|
||
(ggdim3 <- ggplotly(k4_dim3)) | ||
|
||
grid.arrange(k4,k4_dim3,ncol=2) | ||
|
||
subplot(ggdim2,ggdim3) | ||
|
||
|
||
multiplot(k4,k4_dim3) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
----#carga librerias---- | ||
|
||
library(dplyr) | ||
|
||
---#agrupar por equipo--- | ||
|
||
#Idea_1: sumar valores y ponderarlos por 90 min. Que es la duracion de un partido. | ||
|
||
----#preparacion de datos---- | ||
|
||
equipos_tot <- Basetotal %>% dplyr::select(fecha,team.1,minutos_jugados:atajada_penal) %>% group_by(fecha,team.1) | ||
|
||
equipos_tot <- equipos_tot %>%summarise_each(funs(sum,n_distinct(fecha,team.1)))%>%dplyr::select(fecha:minutos_jugados_n_distinct) | ||
|
||
equipos_tot <- equipos_tot %>% mutate(partidos=minutos_jugados_n_distinct) | ||
|
||
equipos_tot$minutos_jugados_n_distinct <- NULL | ||
|
||
equipos_tot_df <- as.data.frame(equipos_tot) | ||
|
||
equipos_tot_df <- equipos_tot_df %>% dplyr::select(-fecha)%>% group_by(team.1) %>%summarise_each(funs(sum)) | ||
|
Oops, something went wrong.