-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_effect.R
158 lines (135 loc) · 7.93 KB
/
home_effect.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
158
#_______________________________________________________________________________
# About: Analysis of home effect in football matches
#_______________________________________________________________________________
#-------------------------------------------------------------------------------
# Import dataset and libraries
serieA_2122<- read.csv("data/serieA_21-22.csv")
teams_list<- names(table(serieA_2122[,"HomeTeam"]))
library(ggplot2)
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Define variables for goals scored/conceeded Home/Away
TeamHomeGoalsScored<- vector(mode="numeric", length=length(teams_list))
TeamAwayGoalsScored<- vector(mode="numeric", length=length(teams_list))
TeamHomeGoalsConceeded<- vector(mode="numeric", length=length(teams_list))
TeamAwayGoalsConceeded<- vector(mode="numeric", length=length(teams_list))
for (i in 1:length(teams_list)){
TeamHomeGoalsScored[i]<- sum(serieA_2122[serieA_2122$HomeTeam== teams_list[i],"FTHG"])
TeamAwayGoalsScored[i]<- sum(serieA_2122[serieA_2122$AwayTeam==teams_list[i],"FTAG"])
TeamHomeGoalsConceeded[i]<- sum(serieA_2122[serieA_2122$HomeTeam== teams_list[i],"FTAG"])
TeamAwayGoalsConceeded[i]<- sum(serieA_2122[serieA_2122$AwayTeam==teams_list[i],"FTHG"])
}
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# (1) GOAL SCORED
#-------------------------------------------------------------------------------
# Dataframe for ggplot
goalscored_df <- data.frame(Stadium=rep(c("Home", "Away"),each=20),
Teams=rep(teams_list,2),
Goals=c(TeamHomeGoalsScored,TeamAwayGoalsScored)
)
# Plot
ggplot(goalscored_df, aes(x=Teams, y=Goals, fill=Stadium)) +
geom_bar(stat="identity", width=0.6, position=position_dodge(),colour="black")+
scale_fill_manual(values = c("#145A32", "#52BE80"))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
ggtitle("Goals scored Serie A 2021-22") +
theme(plot.title = element_text(hjust = 0.5,face="bold"))
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# (2) GOAL CONCEEDED
#-------------------------------------------------------------------------------
# Dataframe
goalconceeded_df <- data.frame(Stadium=rep(c("Home", "Away"),each=20),
Teams=rep(teams_list,2),
Goals=c(TeamHomeGoalsConceeded,TeamAwayGoalsConceeded)
)
# Plot
ggplot(goalconceeded_df, aes(x=Teams, y=Goals, fill=Stadium)) +
geom_bar(stat="identity", width=0.6, position=position_dodge(),colour="black")+
scale_fill_manual( values=c("#641E16", "#EC7063"))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
ggtitle("Goals conceeded Serie A 2021-22") +
theme(plot.title = element_text(hjust = 0.5,face="bold"))
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# (3) GOAL SCORED/CONCEEDED DIFFERENCE HOME VS AWAY
#-------------------------------------------------------------------------------
# Dataframes
goal_scored_difference<- TeamHomeGoalsScored- TeamAwayGoalsScored
goal_conceeded_difference<- TeamHomeGoalsConceeded- TeamAwayGoalsConceeded
goal_scored_difference_df <- data.frame(teams_list,
goal_scored_difference
)
goal_conceeded_difference_df <- data.frame(teams_list,
goal_conceeded_difference
)
# Variable to color bars (green= GOOD ; red= BAD)
goal_scored_difference_df$color <- ifelse(goal_scored_difference_df$goal_scored_difference >= 0,
"#52BE80", "#EC7063")
goal_conceeded_difference_df$color <- ifelse(goal_conceeded_difference_df$goal_conceeded_difference >= 0,
"#EC7063", "#52BE80")
# Plots
ggplot(goal_scored_difference_df, aes(x = teams_list,
y = goal_scored_difference,
fill = color)) +
geom_bar(stat = "identity", width=0.6, position=position_dodge(),colour="black") +
scale_fill_identity() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
labs(title = "Goals scored difference Home vs Away",
x = "Teams",
y = "Goals scored difference")+
theme(plot.title = element_text(hjust = 0.5,face="bold"))
# Goal conceeded difference Home vs Away
ggplot(goal_conceeded_difference_df, aes(x = teams_list,
y = goal_conceeded_difference,
fill = color)) +
geom_bar(stat = "identity", width=0.6, position=position_dodge(),colour="black") +
scale_fill_identity() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
labs(title = "Goals conceeded difference Home vs Away",
x = "Teams",
y = "Goals conceeded difference")+
theme(plot.title = element_text(hjust = 0.5,face="bold"))
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# (4) USEFUL RESULTS
#-------------------------------------------------------------------------------
serieA_2122$useful<- (serieA_2122$FTR=="H") | (serieA_2122$FTR=="D")
useful_results_home<- vector(mode="numeric", length=length(teams_list))
useful_results_away<- vector(mode="numeric", length=length(teams_list))
for (i in 1:length(teams_list)){
useful_results_home[i]<- sum(serieA_2122[serieA_2122$HomeTeam== teams_list[i],"useful"])
useful_results_away[i]<- sum(!serieA_2122[serieA_2122$AwayTeam== teams_list[i],"useful"])
}
# Useful results home and away
useful_results_df <- data.frame(Stadium=rep(c("Home", "Away"),each=20),
Teams=rep(teams_list,2),
Results=c(useful_results_home,useful_results_away)
)
ggplot(useful_results_df, aes(x=Teams, y=Results, fill=Stadium)) +
geom_bar(stat="identity", width=0.6, position=position_dodge(),colour="black")+
scale_fill_manual(values = c("#EC7063", "#52BE80"))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
ggtitle("Useful results Serie A 2021-22") +
theme(plot.title = element_text(hjust = 0.5,face="bold"))
# Useful results difference home vs away
# Dataframe
useful_results_difference<- useful_results_home- useful_results_away
useful_results_difference_df <- data.frame(teams_list,
useful_results_difference
)
useful_results_difference_df$Better<- ifelse(useful_results_difference_df$useful_results_difference > 0,
"Home", "Away")
# Plot
ggplot(useful_result_difference_df, aes(x = teams_list,
y = useful_results_difference,
fill = Better)) +
geom_bar(stat = "identity", width=0.6, position=position_dodge(),colour="black") +
scale_fill_manual(values = c("#EC7063", "#52BE80"))+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,face="bold"))+
labs(title = "Useful results difference Home vs Away",
x = "Teams",
y = "Useful results difference")+
theme(plot.title = element_text(hjust = 0.5,face="bold"))
#-------------------------------------------------------------------------------