-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFL kicking data analysis October 9.R
189 lines (164 loc) · 6.43 KB
/
NFL kicking data analysis October 9.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
load("/Users/gregorymatthews/Dropbox/SPORTS/FOOTBALL/Kick_Database.RData")
kick.dat[1:10,]
#Some simple stuff
summary(kick.dat$yards)
table(kick.dat$good)
table(kick.dat$year)
table(kick.dat$good,kick.dat$year)
#Averages by year
table(kick.dat$good,kick.dat$year)
#Averages by year
tapply(kick.dat$good,kick.dat$year,mean)
#Average distance by year
tapply(kick.dat$yards,kick.dat$year,mean)
#Boxplots of distance by year
png("boxplot by year.png",w=1000,h=1000)
boxplot(kick.dat$yards~kick.dat$year,ylab="Distance",xlab="Year",main="Distribution of Field Goal Attempts by Year")
dev.off()
#Just fixed effects
summary(glm(good~yards,data=kick.dat,family="binomial"))
summary(glm(good~I(yards^0.5),data=kick.dat,family="binomial"))#Lower AIC
#Add in year
a<-glm(good~I(yards^0.5),data=kick.dat,family="binomial")
b<-glm(good~I(yards^0.5)+year,data=kick.dat,family="binomial")
anova(a,b)
1-pchisq(18.766,8)#0.01616243
#Adding in year as numeric
#Kickers getting better?
a<-glm(good~I(yards^0.5)+name,data=kick.dat,family="binomial")
b<-glm(good~I(yards^0.5)+name+as.numeric(year),data=kick.dat,family="binomial")
anova(a,b)
library(lme4)
#Use random effects for kicker
a<-glmer(good~I(yards^0.5)+(1|name),data=kick.dat,family="binomial")
summary(a)
#Do we need a random yard variable? Maybe
a<-glmer(good~I(yards^0.5)+(1|name),data=kick.dat,family="binomial")
b<-glmer(good~I(yards^0.5)+(1+yards|name),data=kick.dat,family="binomial")
anova(a,b)
#Are kickers different from year to year?
a<-glmer(good~I(yards^0.5)+year+(1+yards|name),data=kick.dat,family="binomial")
b<-glmer(good~I(yards^0.5)+year+(1+yards|name)+(1|name:year),data=kick.dat,family="binomial")
anova(a,b)
#Final Model without controlling for year
kickingMer<-glmer(good~I(yards^0.5)+(1|name)+(1|name:year),data=kick.dat,family="binomial")
b<-kickingMer
pdist<-function(dist){
logitp<-fixef(b)%*%c(1,sqrt(dist))+rnorm(1000,0,sqrt(VarCorr(b)[1][[1]][1]))
p<-exp(logitp)/(1+exp(logitp))
p
}
pdistList<-list()
for (i in 17:60){
pdistList[[i]]<-cbind(pdist(i),i)
}
pdist<-do.call(rbind,pdistList)
pdist<-as.data.frame(pdist)
names(pdist)<-c("y","x")
aaa<-tapply(pdist$y,pdist$x,quantile,c(0.05,0.95))
bbb<-do.call(rbind,aaa)
png("KickerAndYearNOFEyear.png",h=1000,w=1000)
plot(c(17:60),bbb[,1],type="l",xlab="Distance of Kick",ylab="Probabilty of making the kick",col="blue",main="Kicker and year variability",sub="Not Controlling for Year")
points(c(17:60),bbb[,2],type="l",col="red")
abline(h=seq(0,1,0.1),col="gray50",lty=3)
legend(20,0.6,legend=c("95th percentile","5th percentile"),col=c("red","blue"),lwd=3)
dev.off()
#Final FINAL model controlling for year and yards as well as random effects for players and player:year
kickingMerYear<-glmer(good~I(yards^0.5)+year+(1|name)+(1|name:year),data=kick.dat,family="binomial")
b<-kickingMerYear
#Fixed Effects
#Only fixed effects
logitp<-fixef(b)%*%c(1,sqrt(40),1,0,0,0,0,0,0,0)
p<-exp(logitp)/(1+exp(logitp))
#Random Effects: Player ability held constant
#40 Yards FG in 2010
logitp<-fixef(b)%*%c(1,sqrt(40),1,0,0,0,0,0,0,0)
p<-exp(logitp)/(1+exp(logitp))
pdist<-function(dist){
logitp<-fixef(b)%*%c(1,sqrt(dist),1,0,0,0,0,0,0,0)+rnorm(1000,0,sqrt(VarCorr(b)[1][[1]][1]))
p<-exp(logitp)/(1+exp(logitp))
p
}
pdistList<-list()
for (i in 17:60){
pdistList[[i]]<-cbind(pdist(i),i)
}
pdist<-do.call(rbind,pdistList)
pdist<-as.data.frame(pdist)
names(pdist)<-c("y","x")
aaa<-tapply(pdist$y,pdist$x,quantile,c(0.05,0.95))
bbb<-do.call(rbind,aaa)
png("KickerAndYearFEyear.png",h=1000,w=1000)
plot(c(17:60),bbb[,1],type="l",xlab="Distance of Kick",ylab="Probabilty of making the kick",col="blue",main="Kicker and year variability",sub="Controlling for Year")
points(c(17:60),bbb[,2],type="l",col="red")
abline(h=seq(0,1,0.1),col="gray50",lty=3)
legend(20,0.6,legend=c("95th percentile","5th percentile"),col=c("red","blue"),lwd=3)
dev.off()
#Final FINAL model controlling for year and yards as well as random effects for players and player:year
kickingMerYear<-glmer(good~I(yards^0.5)+year+(1|name)+(1|name:year),data=kick.dat,family="binomial")
b<-kickingMerYear
#Fixed Effects
#Only fixed effects
logitp<-fixef(b)%*%c(1,sqrt(40),1,0,0,0,0,0,0,0)
p<-exp(logitp)/(1+exp(logitp))
#Random Effects: Player ability held constant
#40 Yards FG in 2010
logitp<-fixef(b)%*%c(1,sqrt(40),1,0,0,0,0,0,0,0)
p<-exp(logitp)/(1+exp(logitp))
pdist<-function(dist){
logitp<-fixef(b)%*%c(1,sqrt(dist),1,0,0,0,0,0,0,0)+rnorm(1000,0,sqrt(VarCorr(b)[1][[1]][1]))+rnorm(1000,0,sqrt(VarCorr(b)[2][[1]][1]))
p<-exp(logitp)/(1+exp(logitp))
p
}
pdistList<-list()
for (i in 17:60){
pdistList[[i]]<-cbind(pdist(i),i)
}
pdist<-do.call(rbind,pdistList)
pdist<-as.data.frame(pdist)
names(pdist)<-c("y","x")
aaa<-tapply(pdist$y,pdist$x,quantile,c(0.05,0.95))
bbb<-do.call(rbind,aaa)
png("KickerAndYearFEyearVariabiltyBetweenKickers.png",h=1000,w=1000)
plot(c(17:60),bbb[,1],type="l",xlab="Distance of Kick",ylab="Probabilty of making the kick",col="blue",main="Kicker Variability",sub="Controlling for Year")
points(c(17:60),bbb[,2],type="l",col="red")
abline(h=seq(0,1,0.1),col="gray50",lty=3)
legend(20,0.6,legend=c("95th percentile","5th percentile"),col=c("red","blue"),lwd=3)
dev.off()
####################################
##By year. Are kickers getting better?
####################################
kickingMer<-glmer(good~I(yards^0.5)+year+(1|name)+(1|name:year),data=kick.dat,family="binomial")
b<-kickingMer
pdist<-function(dist,year){
yyy<-c(2010:2003)
logitp<-fixef(b)%*%c(1,sqrt(dist),yyy==year)
p<-exp(logitp)/(1+exp(logitp))
p
}
pdistList<-list()
for (year in 2003:2011){
pdistList[[year]]<-list()
for (i in 30:60){
pdistList[[year]][[i]]<-cbind(pdist(i,year),i)
}
}
pdist<-do.call(rbind,pdistList[[2003]])
pdist<-as.data.frame(pdist)
names(pdist)<-c("y","x")
aaa<-tapply(pdist$y,pdist$x,quantile,c(0.05,0.95))
bbb<-do.call(rbind,aaa)
png("AllKickersbyYear.png",h=1000,w=1000)
plot(c(30:60),bbb[,1],type="l",xlab="Distance of Kick",ylab="Probabilty of making the kick",col="red",main="Probabilty of average kicker making a field goal",lwd=3,sub="Kicker ability held constant")
cols<-c("red","red","red","blue","blue","blue","blue","blue")
for (year in 2004:2011){
pdist<-do.call(rbind,pdistList[[year]])
pdist<-as.data.frame(pdist)
names(pdist)<-c("y","x")
aaa<-tapply(pdist$y,pdist$x,quantile,c(0.05,0.95))
bbb<-do.call(rbind,aaa)
points(c(30:60),bbb[,1],type="l",col=cols[year-2003],lwd=3)
}
abline(h=seq(0,1,0.1),col="gray50",lty=3)
legend(30,0.6,legend=c("2003-2006","2007-2011"),col=c("red","blue"),lwd=3)
dev.off()