In this part we are going to analyze a Fifa19 dataset. It is a list of more than 18000 units in which are present all the football player in Fifa19 and all the stats regarding their characteristics.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## âś” ggplot2 3.4.2 âś” purrr 1.0.1
## âś” tibble 3.2.1 âś” dplyr 1.1.2
## âś” tidyr 1.3.0 âś” stringr 1.5.0
## âś” readr 2.1.3 âś” forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
library(datarium)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(nortest)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
data <- read.csv("~/Downloads/kl.csv", header=FALSE)
Here there is a clean to extract only the part of tha data that we need.
##Pre-processing data for clustering There are two thingh to do before starting clustering. First of all we have to normalize our data to avoid false result. Alter normalizing it is importat to delede the missing value.
library(BBmisc)
##
## Attaching package: 'BBmisc'
## The following objects are masked from 'package:dplyr':
##
## coalesce, collapse, symdiff
## The following object is masked from 'package:base':
##
## isFALSE
data_new <- as.data.frame(lapply(data_new, as.numeric))
data_new <-normalize(data_new)
data_new <- na.omit(data_new)
We don’t have any missing values and so we don not lost variability
##PCA As first we compute PCA and plot it
library(stats)
library(FactoMineR)
library(ggfortify)
res <- prcomp(data_new,scale. = TRUE)
autoplot(res)
PCA tell us that there are at least 2 well-defined clusters. The two
principal components explain 70% of the variability of our data.
##Cluster validity analysis This help us to understand which method of clustering is better for us
library(clValid)
## Loading required package: cluster
comp <- res$x
PC1 <- comp[,1]
PC2<- comp[,2]
data_new<-as.data.frame(data_new)
test <- clValid(data_new, 2:5, clMethods = c("hierarchical","kmeans","diana","pam"), validation = "internal",maxitems = 3001)
summary(test)
##
## Clustering Methods:
## hierarchical kmeans diana pam
##
## Cluster sizes:
## 2 3 4 5
##
## Validation Measures:
## 2 3 4 5
##
## hierarchical Connectivity 0.0000 219.3857 225.9476 268.6175
## Dunn 0.5781 0.1361 0.1361 0.1361
## Silhouette 0.6115 0.3185 0.2709 0.2301
## kmeans Connectivity 0.0000 416.6187 435.1782 632.1131
## Dunn 0.5781 0.1122 0.1275 0.1354
## Silhouette 0.6115 0.2962 0.2573 0.2502
## diana Connectivity 0.0000 423.0194 639.3258 985.0913
## Dunn 0.5781 0.1008 0.1067 0.1126
## Silhouette 0.6115 0.2887 0.2170 0.1974
## pam Connectivity 0.0000 393.5917 606.6845 741.5052
## Dunn 0.5781 0.1242 0.1268 0.1309
## Silhouette 0.6115 0.3048 0.2431 0.2399
##
## Optimal Scores:
##
## Score Method Clusters
## Connectivity 0.0000 hierarchical 2
## Dunn 0.5781 hierarchical 2
## Silhouette 0.6115 hierarchical 2
By the result we are going to run the hierarchical clustering and k=2.
##HIERARCHICAL CLUSTERING
pca <- data.frame(PC1,PC2)
dist <- dist(pca[,1:2],method = "euclidean")
clust <- diana(pca)
plot(clust)
We can see two clear cluster they very very probably represents
goalkeepers(small cluster) and outfield players.