-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFL.Rmd
108 lines (87 loc) · 2.49 KB
/
NFL.Rmd
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
---
title: "NFL"
author: "Alexander Ivanoff"
date: "10/10/2017"
output: html_document
---
```{r}
library(nflscrapR)
season_2016 = season_play_by_play(2016)
```
```{r}
season_2017 = season_play_by_play(2017)
```
```{r}
library(dplyr)
names(season_2017)
unique(season_2017$GameID)
receivers = season_2017 %>% group_by("Receiver")
tds_2017 = season_2017[season_2017$Touchdown == 1,]
td = receivers[receivers$Touchdown == 1,]
```
## Convert data into usable format
```{r}
clean = function(df) {
df$TwoPointConv[df$TwoPointConv != "Success" | is.na(df$TwoPointConv)] = 0
df$TwoPointConv[df$TwoPointConv == "Success"] = 1
df$TwoPointConv = as.numeric(df$TwoPointConv)
return(df)
}
## Creating scoring functions
points = function(df, penalized = FALSE) {
df$ScoringPlay = df$Accepted.Penalty != 1 & (df$PlayType == "Pass" | df$PlayType == "Run")
df$PassingScore = df$PassAttempt * (df$Touchdown * 4 + df$Yards.Gained * 0.04) +
df$InterceptionThrown * (-1) + df$TwoPointConv * 2
df$FScore = df$ScoringPlay * (df$Touchdown * 6 + df$Yards.Gained * 0.1 + df$Reception * 0.5 + df$TwoPointConv * 2)
return(df)
}
pipe = function(df) {
points(clean(df))
}
```
## Test the qb score metrics on Tom Brady
```{r}
bid = unique(season_2017$Passer_ID[season_2017$Passer == "T.Brady"])[2]
brady = szn_2017[szn_2017$Passer_ID == bid,]
brady[brady$Passer_ID == bid | brady$Rusher_ID == bid,]
qb_preds[qb_preds$Passer == "T.Brady",]
```
```{r}
szn_2017 = pipe(season_2017)
qb_preds = szn_2017 %>% group_by(Passer, GameID) %>% summarise(score = sum(PassingScore))
```
```{r}
score = function(df, playerID, gameID) {
game = df[df$GameID == gameID,]
return(sum(game$FScore[game$Receiver_ID == playerID]) +
sum(game$FScore[game$Rusher_ID == playerID]) +
sum(game$PassingScore[game$Passer_ID == playerID]))
}
in_game = function(df, playerID, gameID) {
game = df[df$GameID == gameID,]
IDs = union_all(game$Passer_ID, game$Receiver_ID, game$Rusher_ID)
return(playerID %in% IDs)
}
```
```{r}
playerID = bid
gameID = "2017090700"
score(szn_2017, bid, gameID)
PlayerIDs = union_all(unique(szn_2017$Passer_ID), unique(szn_2017$Receiver_ID), unique(szn_2017$Rusher_ID))
GameIDs = unique(szn_2017$GameID)
```
```{r}
x = c()
for (player in PlayerIDs){
for (game in GameIDs) {
if(in_game(szn_2017, player, game) & player != "None") {
x = append(x, list(player, score(szn_2017, player, game)))
}
}
}
prs = data.frame(x, ncol = 2)
```
```{r}
prs = data.frame(as.matrix(x, ncol = 2))
#prs %>% group_by("x")
```