knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(ggplot2)
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(e1071)
library(plyr)
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ✓ purrr   0.3.3
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::arrange()   masks plyr::arrange()
## x purrr::compact()   masks plyr::compact()
## x dplyr::count()     masks plyr::count()
## x dplyr::failwith()  masks plyr::failwith()
## x dplyr::filter()    masks stats::filter()
## x dplyr::id()        masks plyr::id()
## x dplyr::lag()       masks stats::lag()
## x dplyr::mutate()    masks plyr::mutate()
## x dplyr::rename()    masks plyr::rename()
## x dplyr::summarise() masks plyr::summarise()
## x dplyr::summarize() masks plyr::summarize()
library(tidyr)
library(plotly)
## 
## Attaching package: 'plotly'
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(readxlsb)
library(readxl)
library(ez)
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
library(ggthemes)
library(dplyr)
library(MOTE)
library(mice)
## 
## Attaching package: 'mice'
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:plotly':
## 
##     subplot
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:plyr':
## 
##     is.discrete, summarize
## The following object is masked from 'package:e1071':
## 
##     impute
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(ppcor)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
## The following object is masked from 'package:dplyr':
## 
##     select
library(lattice)
library(moments)
## 
## Attaching package: 'moments'
## The following objects are masked from 'package:e1071':
## 
##     kurtosis, moment, skewness
library(MASS)
library (ggplot2movies)
ep1 <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 1)
ep1_t4 <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 2)
ep1_w <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 3)
ep1 <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 1)
ep1_t4 <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 2)
ep1_w <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 3)
ep1_n <- read_xlsx("/Users/na/Desktop/Shri R Projects/ANLY500 Projects/epl/epl1.xlsx", sheet = 4)
ep1$pos <- factor(ep1$pos)
ep1$season <- factor(ep1$season)

ep1_t4$pos <- factor(ep1_t4$pos)
ep1_t4$season <- factor(ep1_t4$season)

ep1_w$pos <- factor(ep1_w$pos)
ep1_w$season <- factor(ep1_w$season)

colnames(ep1)
##  [1] "season" "name"   "pos"    "team"   "p"      "w"      "d"      "l"     
##  [9] "f"      "a"      "gd"     "points"
colnames(ep1_t4)
##  [1] "season" "name"   "pos"    "team"   "p"      "w"      "d"      "l"     
##  [9] "f"      "a"      "gd"     "points"
colnames(ep1_w)
##  [1] "season" "name"   "pos"    "team"   "p"      "w"      "d"      "l"     
##  [9] "f"      "a"      "gd"     "points"
names(ep1) = c("Season", "Name", "Position", "Team", "Played", "Won", "Drawn", "Lost", "Goals.For", "Goals.Against", "Goal.Difference", "Points")
names(ep1_t4) = c("Season", "Name", "Position","Team", "Played", "Won", "Drawn", "Lost", "Goals.For", "Goals.Against", "Goal.Difference", "Points")
names(ep1_w) = c("Season", "Name", "Position","Team", "Played", "Won", "Drawn", "Lost", "Goals.For", "Goals.Against", "Goal.Difference", "Points")
names(ep1_n) = c("Season", "Name", "Position","Team", "Played", "Won", "Drawn", "Lost", "Goals.For", "Goals.Against", "Goal.Difference", "Points")
#pts - All
ggplot(data = ep1_t4, mapping = aes(x=Season, y= Points, group = Position, color = Position)) +
geom_line() +
  labs(title = "Points by Premier League Top 4 teams", subtitle = "English Premier League", x = "Season", y = "Points", caption = "Data includes 1968-2018") +
    theme(axis.text.x=element_text(angle=90, hjust=1))

ggplot(data = ep1_t4, mapping = aes(x=Season, y= Points, group = Position, color = Position)) +
geom_line() +
  facet_wrap(facets = vars(Position), nrow = 4) +
  labs(title = "Points by Premier League Top 4 teams", subtitle = "English Premier League", x = "Season", y = "Points", caption = "Data includes 1968-2018") +
    theme(axis.text.x=element_text(angle=90, hjust=1))

ggplot(data = ep1_w, mapping = aes(x=Season, y= Points, group = Position, color = Position)) +
geom_line() +
  labs(title = "Points by Premier League Top 4 teams", subtitle = "English Premier League", x = "Season", y = "Points", caption = "Data includes 1968-2018") +
    theme(axis.text.x=element_text(angle=90, hjust=1))

#Wins - All- Winners

ggplot(data = ep1_w, mapping = aes(x=Season, y= Points, color = Team)) +
  geom_point() + 
  geom_smooth() +
      labs(title = "Premier League Winning teams", subtitle = "English Premier League", x = "Season", y = "Winners", caption = "Data includes 1968-2018") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ep1wptsall <- ggplot(data = ep1_w, mapping = aes(x=Season, y= Points, color = Team)) +
  geom_point() + 
      labs(title = "Premier League Winning teams", subtitle = "English Premier League", x = "Season", y = "Winners", caption = "Data includes 1968-2018") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1wpts_new <- ggplotly(ep1wptsall)
ep1wpts_new
ep1t4 <- ggplot(data = ep1_t4, mapping = aes(x=Season, y= Points, fill=Team)) +
  geom_bar(stat = "identity") +
          labs(title = "Points by Premier League Top 4 teams", subtitle = "English Premier League", x = "Season", y = "Points", caption = "Data includes 1968-2018") +
    theme(axis.text.x=element_text(angle=90, hjust=1))
ep1t4teams <- ggplotly(ep1t4)
ep1t4teams
##Top 4 = 1995- 2018
##env_u6ld_bos <- env_u6ld %>% filter(Value < 100, Place =="Boston, MA")

ep1_n_t4 <-ep1_n %>% filter(Position < 5)

#ggplot(data = ep1_n_t4, mapping = aes(x=Season, y= Points, shape = Season)) +
 # geom_point(alpha= 0.5) +
  #theme(axis.text.x=element_text(angle=90, hjust=1))

#ggplot(data = ep1_n_t4, mapping = aes(x=Season, y= Points, color = Position)) +
 # geom_point() + 
  #geom_smooth() +
   #   labs(title = "Premier League Winning teams", subtitle = "English Premier League", x = "Season", y = "Winners", caption = "Data includes 1968-2018") +
  #theme(axis.text.x=element_text(angle=90, hjust=1))


#ggplot(data = ep1_n_t4, mapping = aes(x=Season, y= Goals.For, group = Position, color = Position)) +
 # geom_line(method= "lm") +
  #    labs(title = "Goals scored by Top 4 Premier League teams", subtitle = "English Premier League", x = "Season", y = "Goals", caption = "Data includes 1968-2018") +
   #   theme(axis.text.x=element_text(angle=90, hjust=1))

#losses - Increasing order
ep1nloss <- ggplot(data = ep1_n_t4, mapping = aes(x=Position, y= Lost, color= Team)) +
  geom_boxplot(aes(group=Position), alpha=0) +
  geom_jitter(alpha=0.8) +
      labs(title = "Losses by Top 4 Premier League teams", subtitle = "English Premier League", x = "Position", y = "Games Lost", caption = "Data includes 1995-2018")
ep1nloss1 <- ggplotly(ep1nloss)
ep1nloss1
#Wins - Decreasing order
ep1nwin <- ggplot(data = ep1_n_t4, mapping = aes(x=Position, y= Won, color=Team)) +
  geom_boxplot(aes(group=Position), alpha=0) +
  geom_jitter(alpha=0.8)+
      labs(title = "Wins by Top 4 Premier League teams", subtitle = "English Premier League", x = "Position", y = "Games Won", caption = "Data includes 1995-2018")
ep1nwin1 <- ggplotly(ep1nwin)
ep1nwin1
##Wins and Losses - Very Imp Graphs
ep1_nw <- ep1_n %>% filter (Position == 1)
ep1wins <- ggplot(data = ep1_nw, mapping = aes(x=Season, y= Won, color= Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
  geom_jitter(alpha=0.8)+
      labs(title = "Wins by Premier League Winners", subtitle = "English Premier League", x = "Season", y = "Games Won", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1wwins <- ggplotly(ep1wins)
ep1wwins
ep1loss <- ggplot(data = ep1_nw, mapping = aes(x=Season, y= Lost, color= Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Losses by Premier League Winners", subtitle = "English Premier League", x = "Position", y = "Games Lost", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1wloss <- ggplotly(ep1loss)
ep1wloss
#geom_jitter(alpha=0.8)+

ep1pts <- ggplot(data = ep1_nw, mapping = aes(x=Season, y= Points, color= Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Points by Premier League Winners", subtitle = "English Premier League", x = "Position", y = "Points", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1wpts <- ggplotly(ep1pts)
ep1wpts
ep1gd <- ggplot(data = ep1_nw, mapping = aes(x=Season, y= Goal.Difference, color= Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Goal Differnce for Premier League Winners", subtitle = "English Premier League", x = "Position", y = "Goal Differnce", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1wgd <- ggplotly(ep1gd)
ep1wgd
##Bottom 3 teams - Points
ep1_b3 <- ep1_n %>% filter(Position >17)
ep1_b <- ep1_n %>% filter(Position ==20)
ep1_19 <- ep1_n %>% filter(Position == 19)
ep1_18 <- ep1_n %>% filter(Position == 18)

ep1bpts <- ggplot(data = ep1_b, mapping = aes(x=Season, y= Points, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Points by PL team Ranked #20 every Season", subtitle = "English Premier League", x = "Position", y = "Points", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1rbpts <- ggplotly(ep1bpts)
ep1rbpts
ep1bgd <- ggplot(data = ep1_b, mapping = aes(x=Season, y= Goal.Difference, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Goal Difference for PL team Ranked #20 every Season", subtitle = "English Premier League", x = "Position", y = "Points", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1rbgd <- ggplotly(ep1bgd)
ep1rbgd
ep119pts <- ggplot(data = ep1_19, mapping = aes(x=Season, y= Points, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Points by PL team Ranked #19 every Season", subtitle = "English Premier League", x = "Position", y = "Points", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r19pts <- ggplotly(ep119pts)
ep1r19pts
ep118pts <- ggplot(data = ep1_18, mapping = aes(x=Season, y= Points, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Points by PL team Ranked #18 every Season", subtitle = "English Premier League", x = "Position", y = "Points", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r18pts <- ggplotly(ep118pts)
ep1r18pts
##Wins by Bottom 3

ep1bw <- ggplot(data = ep1_b, mapping = aes(x=Season, y= Won, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Won by Pl team Ranked #20 every Season", subtitle = "English Premier League", x = "Position", y = "Games Won", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1rbw <- ggplotly(ep1bw)
ep1rbw
ep119w <- ggplot(data = ep1_19, mapping = aes(x=Season, y= Won, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Won by PL team Ranked #19 every Season", subtitle = "English Premier League", x = "Position", y = "Games Won", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r19w <- ggplotly(ep119w)
ep1r19w
ep118w <- ggplot(data = ep1_18, mapping = aes(x=Season, y= Won, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Won by PL team Ranked #18 every Season", subtitle = "English Premier League", x = "Position", y = "Games Won", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r18w <- ggplotly(ep118w)
ep1r18w
##Losses by Bottom 3

ep1bl <- ggplot(data = ep1_b, mapping = aes(x=Season, y= Lost, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Lost by Pl team Ranked #20 every Season", subtitle = "English Premier League", x = "Position", y = "Games Lost", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1rbl <- ggplotly(ep1bl)
ep1rbl
ep119l <- ggplot(data = ep1_19, mapping = aes(x=Season, y= Lost, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Lost by PL team Ranked #19 every Season", subtitle = "English Premier League", x = "Position", y = "Games Lost", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r19l <- ggplotly(ep119l)
ep1r19l
ep118l <- ggplot(data = ep1_18, mapping = aes(x=Season, y= Lost, color=Team)) +
  geom_smooth(method = "lm", aes(group=Position, color = Team), alpha=0) +
geom_jitter(alpha=0.8)+
        labs(title = "Games Lost by PL team Ranked #18 every Season", subtitle = "English Premier League", x = "Position", y = "Games Lost", caption = "Data includes 1995/1996-2018/2019 Seasons") +
  theme(axis.text.x=element_text(angle=90, hjust=1))
ep1r18l <- ggplotly(ep118l)
ep1r18l