-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.R
157 lines (97 loc) · 3.99 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
author: "Pawe³ Pechta"
title: "Multinomial LR Model for predicting the outcomes of football matches"
---
install.packages("tidyverse")
install.packages("mice")
install.packages("caTools")
install.packages("corrplot")
# Source data import to create dataset
library(tidyverse)
dataset <- dir("data", full.names = T) %>% map_df(read_csv)
# Processing and Exploring Data (limiting number of variables and removing NA)
dataset <- dataset[c(2:10, 12:23)]
library(mice)
md.pattern(dataset)
dataset <- na.omit(dataset)
summary(dataset)
write.csv(dataset, "./dataset.csv", row.names=FALSE)
# Input Data preparation (removing multicollinear variables)
str(dataset)
df_corr <- dataset[,sapply(dataset, is.numeric)]
df_corr.cor = cor(df_corr, method = c("pearson"))
library(corrplot)
palette = colorRampPalette(c("green", "white", "red")) (20)
heatmap(x = df_corr.cor, col = palette, symm = TRUE)
df_corr <- df_corr[c(7:16)]
df_corr.cor = cor(df_corr, method = c("pearson"))
heatmap(x = df_corr.cor, col = palette, symm = TRUE)
input_data <- dataset[c(1:3, 6, 9, 12:21)]
# Splitting data into training and test sets to build and evaluate model
library(caTools)
set.seed(123)
sample = sample.split(input_data, SplitRatio = 0.70)
train = subset(input_data,sample == TRUE)
test = subset(input_data, sample == FALSE)
write.csv(train, "./training/training.csv", row.names = FALSE)
write.csv(test, "./test/test.csv", row.names = FALSE)
# Building and Comparising Predictive Models (by AIC value)
train$FTR <- relevel(factor(train$FTR), ref = "D")
require(nnet)
# with all variables
multinom.model_1 <- multinom(FTR ~HST + AST + HF + AF + HC + AC + HY + AY
+ HR + AR +1, data = train)
summary(multinom.model_1) # coefficients and standard errors
summary <- summary(multinom.model_1)$coefficients/summary(multinom.model_1)$standard.errors
p <- (1 - pnorm(abs(summary), 0, 1)) * 2
head(p) # p-values
# with only relevant variables
multinom.model_2 <- multinom(FTR ~AST + HST + AF + HC + AC + HY + HR + AR +1,
data = train)
summary(multinom.model_2)
# model with only relevant variables has lower AIC value so should be better
summary <- summary(multinom.model_2)$coefficients/summary(multinom.model_2)$standard.errors
p <- (1 - pnorm(abs(summary), 0, 1)) * 2
head(p) # p-values
# 'HR' variables are relevant only for 'A' category of dependent variable,
# so can either be left or deleted from the model
exp(coef(multinom.model_2)) # odds ratio
multinom.model <- multinom.model_2 # rename model
# Predicting on Test Data
output <- test
output$Predicted <- predict(multinom.model, newdata = output, "class")
output <- output[c(1:4, 16)]
names(output)[4] <- "Actual"
write.csv(output, "./test/output.csv", row.names = FALSE)
# Evaluating the Model
install.packages('e1071', dependencies = TRUE)
observations <- factor(output$Actual,
levels = c("H", "A", "D"))
predicted <- factor(output$Predicted,
levels = c("H", "A", "D"))
conf <- table(predicted, observations)
library(caret)
f.conf <- confusionMatrix(conf)
install.packages('yardstick')
library(yardstick)
library(ggplot2)
set.seed(123)
mat_conf <- data.frame(
FTR = sample(0:1,1325, replace = T),
predicted = sample(0:1,1325, replace = T)
)
mat_conf$FTR <- observations
mat_conf$predicted <- predicted
cm <- conf_mat(mat_conf, FTR, predicted)
# Confusion matrix to assess the quality of classification on a test set
autoplot(cm, type = "heatmap") +
scale_fill_gradient(low = "firebrick1", high = "aquamarine1")
print(f.conf)
# Future matches prediction (an example of the practical use of the model)
library("readxl")
average_stats <- read_excel("./future_matches_prediction/average_stats.xlsx")
predictions <- average_stats
predictions$Prediction <- predict(multinom.model, newdata = predictions,
"class")
predictions <- predictions[c(1:4, 13)]
write.csv(predictions, "./future_matches_prediction/predictions.csv", row.names = FALSE)