Dataset is the same of Clustering and our aim in this case is to classify outfield players and goalkeepers. Having all the stats it is very easy, but this this project needs to have an example for future working and for understand very well all the procedure.
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(tree)
library(ISLR)
library(ipred)
library(rpart)
kl <- read.csv("~/Downloads/kl.csv", header=FALSE)
data <- kl
Now we take only variables that we are going to use. After this we assign factor 1 to goalkeepers and 0 to outfield players.
I split in two equal part my dataset, I take randomly 9079 (half of my nrow original dataset) rows in train set and the rest in test set.
train = sample(1:nrow(data_new), 9079)
class(train)
## [1] "integer"
test = data_new[-train,]
dim(train)
## NULL
dim(test)
## [1] 9079 35
I built a classification tree on train dataset.
classif_tree <- tree(as.factor(data_new$`data[, 21]`) ~., data_new, split="gini", subset=train)
classif_tree
## node), split, n, deviance, yval, (yprob)
## * denotes terminal node
##
## 1) root 9079 6405.000 0 ( 0.8869920 0.1130080 )
## 2) GKDiving < 35.5 8052 0.000 0 ( 1.0000000 0.0000000 ) *
## 3) GKDiving > 35.5 1027 15.870 1 ( 0.0009737 0.9990263 )
## 6) Strength < 33.5 12 6.884 1 ( 0.0833333 0.9166667 )
## 12) Crossing < 14.5 7 0.000 1 ( 0.0000000 1.0000000 ) *
## 13) Crossing > 14.5 5 5.004 1 ( 0.2000000 0.8000000 ) *
## 7) Strength > 33.5 1015 0.000 1 ( 0.0000000 1.0000000 ) *
summary(classif_tree) # error rate on the training set
##
## Classification tree:
## tree(formula = as.factor(data_new$`data[, 21]`) ~ ., data = data_new,
## subset = train, split = "gini")
## Variables actually used in tree construction:
## [1] "GKDiving" "Strength" "Crossing"
## Number of terminal nodes: 4
## Residual mean deviance: 0.0005514 = 5.004 / 9075
## Misclassification error rate: 0.0001101 = 1 / 9079
plot(classif_tree)
text(classif_tree, pretty=0)
tree.pred = predict(classif_tree, test, type="class") # Use DT on test data
table(tree.pred, test$`data[, 21]`) # Confusion Matrix of test set
##
## tree.pred 0 1
## 0 8079 0
## 1 1 999
How I sad before in this case is very easy to classify rightly the units. Our misclassification error rate is zero because I have many predictors and the distance between the two “type” of position is very high.
conf = table(tree.pred, test$`data[, 21]`)
summary(conf)
## Number of cases in table: 9079
## Number of factors: 2
## Test for independence of all factors:
## Chisq = 9069, df = 1, p-value = 0
accuracy=sum(diag(conf))/sum(conf)
round(accuracy*100,2)
## [1] 99.99
error=1-accuracy
round(error*100, 2)
## [1] 0.01
The cv.tree() function reports the number of terminal nodes of each tree considered
cv.data = cv.tree(classif_tree, FUN=prune.misclass)
par(mfrow=c(1,2))
plot(cv.data$size, cv.data$dev,type="b")
plot(cv.data$k, cv.data$dev,type="b")
We now apply the prune.misclass() function in order to prune the tree and have a good visualization.
prune.data = prune.misclass(classif_tree,best = 2) # best is the optimal number of terminal nodes
plot(prune.data)
text(prune.data,pretty=0)
## Prediction on Test
tree.pred = predict(prune.data, test, type="class")
table(tree.pred, test$`data[, 21]`) # confusion Matrix of test set using the pruned tree
##
## tree.pred 0 1
## 0 8079 0
## 1 1 999
As we can see this is a very simple model, but I hope this project could help you to understand clearly every step. See you soon with a more complex classification problem!