-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasetPrep.R
More file actions
131 lines (101 loc) · 3.86 KB
/
Copy pathdatasetPrep.R
File metadata and controls
131 lines (101 loc) · 3.86 KB
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
## This script stores some functions used for cleaning and homogeneizing the datasets
# clean function removes the betting attributes, removes Time attribute (not useful) and
# homogeneizes the date format in order to have the same date format in all datasets.
# It receives the dataframe.
# It returns the cleaned dataframe.
prepDataset <- function(df) {
# Removing betting attributes from dataset
df <- df[, - c(which(colnames(df) == "B365H"):ncol(df))]
# Removing Time attribute in case it exists
if (is.null(df$Time) == FALSE){
df <- df[, - which(colnames(df) == "Time")]
}
# Homogeneizing Date attribute: dd/mm/yy
date <- as.Date(df$Date, format = "%d/%m/%Y")
date.df <- data.frame("dd" = as.numeric(format(date, format = "%d")),
"mm" = as.numeric(format(date, format = "%m")),
"yy" = as.numeric(format(date, format = "%y")))
df <- data.frame(date.df, df[, c(3:23)])
# Defining F/S Attibute (Fall or Spring):
s.f <- rep("Spring", times=length(df$mm))
s.f[which(df$mm >= 8 & df$mm <= 12)] <- "Fall"
df <- data.frame("SF"=s.f, df)
# Defining Season Attribute:
if(df$yy[1]==15){
df <- data.frame("Season"=rep("15/16", times = length(df$yy)), df)
}
if(df$yy[1]==16){
df <- data.frame("Season"=rep("16/17", times = length(df$yy)), df)
}
if(df$yy[1]==17){
df <- data.frame("Season"=rep("17/18", times = length(df$yy)), df)
}
if(df$yy[1]==18){
df <- data.frame("Season"=rep("18/19", times = length(df$yy)), df)
}
if(df$yy[1]==19){
df <- data.frame("Season"=rep("19/20", times = length(df$yy)), df)
}
return(df)
}
# wld function classifies the football game in win, loss or draw for a given team.
# It adds a column called WLD with class values: win, loss, draw (factor).
# It recieves two values: a season dataframe and the name of a team.
# It returns a dataframe containing football games for the given team and the new attibute WLD.
wld <- function(df, team) {
df.team <- df[which(df$HomeTeam == team | df$AwayTeam == team), ]
team.home <- which(df.team$HomeTeam == team)
team.away <- which(df.team$AwayTeam == team)
team.wld <- c()
for (i in team.home) {
if(df.team[i,]$FTHG > df.team[i,]$FTAG){
team.wld[i] <- "win"
}
if(df.team[i,]$FTHG == df.team[i,]$FTAG){
team.wld[i] <- "draw"
}
if(df.team[i,]$FTHG < df.team[i,]$FTAG){
team.wld[i] <- "loss"
}
}
for (i in team.away) {
if(df.team[i,]$FTHG > df.team[i,]$FTAG){
team.wld[i] <- "loss"
}
if(df.team[i,]$FTHG == df.team[i,]$FTAG){
team.wld[i] <- "draw"
}
if(df.team[i,]$FTHG < df.team[i,]$FTAG){
team.wld[i] <- "win"
}
}
df.wld <- data.frame("WLD" = team.wld)
df.team <- data.frame(df.team, df.wld)
return(df.team)
}
# selectPred function selects predictors that can be known previously to the game
# It receives a dataframe and the target response
# It returns a new dataframe that contains the columns which would be used to predict
# plus target response
selectPred <- function(df, targ){
pred.cols <- c("Season", "SF", "HomeTeam", "AwayTeam", "Referee", targ)
return(df[,pred.cols])
}
# homeTeam function selects rows in a dataframe for a given home team
# It receives a dataframe and the team name as a string
# It returns a new dataframe that contains rows with the given home team
homeTeam <- function(df, home.team){
df <- df[c(which(df$HomeTeam == home.team)), ]
return(df)
}
# awayTeam function selects rows in a dataframe for a given away team
# It receives a dataframe and the team name as a string
# It returns a new dataframe that contains rows with the given away team
awayTeam <- function(df, away.team){
df <- df[c(which(df$AwayTeam == away.team)), ]
return(df)
}
team <- function(df, team.name){
df <- df[c(which(df$AwayTeam == team.name | df$HomeTeam == team.name)), ]
return(df)
}