R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: ## Analysis objective Determine if teams in the UEFA Champions League 2022 competition performed better in home or away games.

##Loading data

url<-'https://projects.fivethirtyeight.com/soccer-api/club/spi_matches_latest.csv'
ucl <- read.csv(url)

##View first 6 rows

head (ucl)
##   season       date league_id               league                team1
## 1   2019 2019-03-01      1979 Chinese Super League      Shandong Luneng
## 2   2019 2019-03-01      1979 Chinese Super League   Shanghai Greenland
## 3   2019 2019-03-01      1979 Chinese Super League Guangzhou Evergrande
## 4   2019 2019-03-01      1979 Chinese Super League           Wuhan Zall
## 5   2019 2019-03-01      1979 Chinese Super League      Chongqing Lifan
## 6   2019 2019-03-02      1979 Chinese Super League          Shenzhen FC
##                    team2  spi1  spi2  prob1  prob2 probtie proj_score1
## 1          Guizhou Renhe 48.22 37.83 0.5755 0.1740  0.2505        1.75
## 2          Shanghai SIPG 39.81 60.08 0.2387 0.5203  0.2410        1.22
## 3      Tianjin Quanujian 65.59 39.99 0.7832 0.0673  0.1495        2.58
## 4          Beijing Guoan 32.25 54.82 0.2276 0.5226  0.2498        1.10
## 5           Guangzhou RF 38.24 40.45 0.4403 0.2932  0.2665        1.57
## 6 Hebei China Fortune FC 31.99 38.75 0.3966 0.3252  0.2783        1.41
##   proj_score2 importance1 importance2 score1 score2  xg1  xg2 nsxg1 nsxg2
## 1        0.84        45.9        22.1      1      0 1.39 0.26  2.05  0.54
## 2        1.89        25.6        63.4      0      4 0.57 2.76  0.80  1.50
## 3        0.62        77.1        28.8      3      0 0.49 0.45  1.05  0.75
## 4        1.79        35.8        58.9      0      1 1.12 0.97  1.51  0.94
## 5        1.24        26.2        21.3      2      2 2.77 3.17  1.05  2.08
## 6        1.25        40.5        24.6      3      1 1.33 0.65  0.88  1.72
##   adj_score1 adj_score2
## 1       1.05       0.00
## 2       0.00       3.26
## 3       3.15       0.00
## 4       0.00       1.05
## 5       2.10       2.10
## 6       2.61       1.05

##Rename columns for better understanding

ucl <- ucl %>% rename(
    home_team = team1,
    away_team = team2,
    home_goals = score1,
    away_goals = score2
  )

##Filter for 2022 UCL Competition

filter_2022 <- filter(ucl, league == 'UEFA Champions League', season == 2022) 
ucl_2022 <- select (filter_2022, date, league, season, home_team, away_team, home_goals, away_goals)
head(ucl_2022)
##         date                league season           home_team        away_team
## 1 2022-09-06 UEFA Champions League   2022   Borussia Dortmund    FC Copenhagen
## 2 2022-09-06 UEFA Champions League   2022       Dinamo Zagreb          Chelsea
## 3 2022-09-06 UEFA Champions League   2022              Celtic      Real Madrid
## 4 2022-09-06 UEFA Champions League   2022          RB Leipzig Shakhtar Donetsk
## 5 2022-09-06 UEFA Champions League   2022          Sevilla FC  Manchester City
## 6 2022-09-06 UEFA Champions League   2022 Paris Saint-Germain         Juventus
##   home_goals away_goals
## 1          3          0
## 2          1          0
## 3          0          3
## 4          1          4
## 5          0          4
## 6          2          1

Distinguishing types of wins

ucl_2022 <- ucl_2022 %>%
  mutate(result = case_when(
    home_goals > away_goals ~ "Home Win",
    home_goals < away_goals ~ "Away Win",
    TRUE ~ "Draw"
  ))

outcome <- ucl_2022 %>%
  count(result)

head(outcome)
##     result  n
## 1 Away Win 38
## 2     Draw 26
## 3 Home Win 61

EDA

library (ggplot2)

outcome <- outcome %>% rename(
    count =n
  )

ggplot(outcome, aes(x = result, y = count)) +
  geom_bar(stat = "identity")

#Conclusion - More teams performed better at home than away in the UCL 2022 competition. - There were more wins for away teams, however, than there were draws. - While the home team had the advantage in most cases, away teams were still able to perform better than the home in more games than they were both on par (ending in a draw)