-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifa.R
More file actions
337 lines (289 loc) · 13.2 KB
/
fifa.R
File metadata and controls
337 lines (289 loc) · 13.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
### Prepare ####
library(cluster)
library(fpc)
library(ggplot2)
library(foreign)
library(ggdendro)
library(reshape2)
set.seed(4444)
setwd("C:\\Users\\ddunn\\Dropbox\\DB Cloud-Only Files\\R\\fifa")
# setwd("Z:\\LAD\\CMG Direct Marketing\\Analytics\\R\\Examples\\fifa\\")
### Control panel for screen-scraping ####
sleep.time <- 0.01
pagecount <- 237
pc.ignore <- 0
names.page <- 50
names.lastpage <- 37
name.gaplines <- 41
namLine1 <- 541
posLine1 <- 545
RATLine1 <- 548
PACLine1 <- 557
SHOLine1 <- 560
PASLine1 <- 563
DRILine1 <- 566
DEFLine1 <- 569
HEALine1 <- 572
TopRAT <- 85
### Create custom urls to scrape ####
pageSeq <- seq(from=1,to=pagecount,by=1)
urls.df <- data.frame(pageSeq)
for(i in 1:length(urls.df$pageSeq)){
urls.df$url[i] <- paste0("http://www.futhead.com/14/?page=",
urls.df$pageSeq[i],
"&sort_direction=desc")
}
### Scrape html from custom urls ####
pages <- as.list("na")
for(j in 1:length(urls.df$pageSeq)){
pages[[j]] <- urls.df$pageSeq[j]
download.file(urls.df$url[j],destfile=paste0(urls.df$pageSeq[j],".txt"))
Sys.sleep(sleep.time)
}
### Identify which lines store player statistics ####
namSeq <- seq(from=namLine1,by=name.gaplines,length.out=names.page)
posSeq <- seq(from=posLine1,by=name.gaplines,length.out=names.page)
RATSeq <- seq(from=RATLine1,by=name.gaplines,length.out=names.page)
PACSeq <- seq(from=PACLine1,by=name.gaplines,length.out=names.page)
SHOSeq <- seq(from=SHOLine1,by=name.gaplines,length.out=names.page)
PASSeq <- seq(from=PASLine1,by=name.gaplines,length.out=names.page)
DRISeq <- seq(from=DRILine1,by=name.gaplines,length.out=names.page)
DEFSeq <- seq(from=DEFLine1,by=name.gaplines,length.out=names.page)
HEASeq <- seq(from=HEALine1,by=name.gaplines,length.out=names.page)
### Create empty dataframe for storing player stats
attribs <- data.frame(matrix(nrow=names.page*(pagecount-1)+names.lastpage,ncol=9))
colnames(attribs) <- c("Name","Position","RAT","PAC","SHO","PAS","DRI","DEF","HEA")
### Store lines from full pages containing player stats to dataframe ####
for(m in 1:(pagecount-1-pc.ignore)){
page <- readLines(paste0(urls.df$pageSeq[m],".txt"))
for(k in 1:names.page){
n <- (m-1)*names.page+k
attribs$Name[n] <- page[namSeq[k]]
attribs$Position[n] <- page[posSeq[k]]
attribs$RAT[n] <- page[RATSeq[k]]
attribs$PAC[n] <- page[PACSeq[k]]
attribs$SHO[n] <- page[SHOSeq[k]]
attribs$PAS[n] <- page[PASSeq[k]]
attribs$DRI[n] <- page[DRISeq[k]]
attribs$DEF[n] <- page[DEFSeq[k]]
attribs$HEA[n] <- page[HEASeq[k]]
}
}
### Store lines from partial last page containing player stats to dataframe ####
pagelast <- readLines(paste0(urls.df$pageSeq[pagecount],".txt"))
for(p in 1:names.lastpage){
q <- (pagecount-1)*names.page+p
attribs$Name[q] <- pagelast[namSeq[p]]
attribs$Position[q] <- pagelast[posSeq[p]]
attribs$RAT[q] <- pagelast[RATSeq[p]]
attribs$PAC[q] <- pagelast[PACSeq[p]]
attribs$SHO[q] <- pagelast[SHOSeq[p]]
attribs$PAS[q] <- pagelast[PASSeq[p]]
attribs$DRI[q] <- pagelast[DRISeq[p]]
attribs$DEF[q] <- pagelast[DEFSeq[p]]
attribs$HEA[q] <- pagelast[HEASeq[p]]
}
### Remove html wrapped around player stats in each line ####
attribs$Name <- gsub("^.*<span class=\"name\">","",attribs$Name)
attribs$Name <- gsub("</span>.*$","",attribs$Name)
attribs$Position <- gsub("^ *","",attribs$Position)
attribs$RAT <- gsub("^.*<span>","",attribs$RAT)
attribs$RAT <- gsub("</span>.*$","",attribs$RAT)
attribs$PAC <- gsub("^.*<span class=\"attribute\">","",attribs$PAC)
attribs$PAC <- gsub("</span>.*$","",attribs$PAC)
attribs$SHO <- gsub("^.*<span class=\"attribute\">","",attribs$SHO)
attribs$SHO <- gsub("</span>.*$","",attribs$SHO)
attribs$PAS <- gsub("^.*<span class=\"attribute\">","",attribs$PAS)
attribs$PAS <- gsub("</span>.*$","",attribs$PAS)
attribs$DRI <- gsub("^.*<span class=\"attribute\">","",attribs$DRI)
attribs$DRI <- gsub("</span>.*$","",attribs$DRI)
attribs$DEF <- gsub("^.*<span class=\"attribute\">","",attribs$DEF)
attribs$DEF <- gsub("</span>.*$","",attribs$DEF)
attribs$HEA <- gsub("^.*<span class=\"attribute\">","",attribs$HEA)
attribs$HEA <- gsub("</span>.*$","",attribs$HEA)
### Remove statisitcs from duplicated players ####
attribs <- attribs[!duplicated(attribs$Name),]
rownames(attribs) <- NULL
### Clean up foreign characters in names ####
Encoding(attribs$Name) <- "UTF-8"
attribs$Name <- iconv(attribs$Name,"UTF-8","UTF-8",sub='')
### Create general position type ####
attribs$Type[attribs$Position=="CF"] <- "Forward"
attribs$Type[attribs$Position=="ST"] <- "Forward"
attribs$Type[attribs$Position=="LM"] <- "Midfield"
attribs$Type[attribs$Position=="RM"] <- "Midfield"
attribs$Type[attribs$Position=="CDM"] <- "Midfield"
attribs$Type[attribs$Position=="CM"] <- "Midfield"
attribs$Type[attribs$Position=="CAM"] <- "Midfield"
attribs$Type[attribs$Position=="LW"] <- "Midfield"
attribs$Type[attribs$Position=="RW"] <- "Midfield"
attribs$Type[attribs$Position=="LB"] <- "Defense"
attribs$Type[attribs$Position=="RB"] <- "Defense"
attribs$Type[attribs$Position=="CB"] <- "Defense"
attribs$Type[attribs$Position=="LWB"] <- "Defense"
attribs$Type[attribs$Position=="RWB"] <- "Defense"
attribs$Type[attribs$Position=="GK"] <- "Keeper"
### Change each stat to the appropriate data type ####
attribs$Name <- as.character(attribs$Name)
attribs$Position <- as.factor(attribs$Position)
attribs$RAT <- as.integer(attribs$RAT)
attribs$PAC <- as.integer(attribs$PAC)
attribs$SHO <- as.integer(attribs$SHO)
attribs$PAS <- as.integer(attribs$PAS)
attribs$DRI <- as.integer(attribs$DRI)
attribs$DEF <- as.integer(attribs$DEF)
attribs$HEA <- as.integer(attribs$HEA)
attribs$Type <- ordered(attribs$Type,levels=c("Forward","Midfield","Defense","Keeper"))
### Boxcharts of player ratings by type and position ####
ggplot(attribs) +
aes(x=Type,y=RAT) +
geom_boxplot(aes(fill=Type)) + geom_jitter(size=0.5) +
theme(legend.position="none",
axis.title.x=element_blank()) +
ylab("Rating") +ggtitle("Player Ratings by Type")
ggplot(attribs) +
aes(x=reorder(Position,RAT,FUN=median),y=RAT) +
geom_boxplot(aes(fill=Position)) + geom_jitter(size=0.5) +
theme(legend.position="none",
axis.title.x=element_blank()) +
ylab("Rating") +ggtitle("Player Ratings by Position")
### Linear models for each type of position ####
for.lm <- lm(RAT~.,data=attribs[attribs$Type=="Forward",3:9])
mid.lm <- lm(RAT~.,data=attribs[attribs$Type=="Midfield",3:9])
def.lm <- lm(RAT~.,data=attribs[attribs$Type=="Defense",3:9])
kee.lm <- lm(RAT~.,data=attribs[attribs$Type=="Keeper",3:9])
summary(for.lm)
summary(mid.lm)
summary(def.lm)
summary(kee.lm)
attribs$For.Pred <- predict(for.lm,newdata=attribs[,3:9])
attribs$Mid.Pred <- predict(mid.lm,newdata=attribs[,3:9])
attribs$Def.Pred <- predict(def.lm,newdata=attribs[,3:9])
attribs$Kee.Pred <- predict(kee.lm,newdata=attribs[,3:9])
### Find best predicted type by rating ####
attribs$Best.Pred <- apply(attribs[,11:14],1,max)
attribs$Best <- names(attribs[,11:14])[max.col(attribs[,11:14])]
attribs$Best[attribs$Best=="For.Pred"] <- "Forward"
attribs$Best[attribs$Best=="Mid.Pred"] <- "Midfield"
attribs$Best[attribs$Best=="Def.Pred"] <- "Defense"
attribs$Best[attribs$Best=="Kee.Pred"] <- "Keeper"
attribs$Best <- ordered(attribs$Best,levels=c("Forward","Midfield","Defense","Keeper"))
table(attribs$Type,attribs$Best)
### Create attribute of predicted rating by actual type
attribs$Type.Pred[attribs$Type=="Forward"] <- attribs$For.Pred[attribs$Type=="Forward"]
attribs$Type.Pred[attribs$Type=="Midfield"] <- attribs$Mid.Pred[attribs$Type=="Midfield"]
attribs$Type.Pred[attribs$Type=="Defense"] <- attribs$Def.Pred[attribs$Type=="Defense"]
attribs$Type.Pred[attribs$Type=="Keeper"] <- attribs$Kee.Pred[attribs$Type=="Keeper"]
attribs$Type.xFactor <- attribs$Type.Pred - attribs$RAT
attribs$xFactor <- attribs$Best.Pred - attribs$RAT
### Create row number by Type ####
attribs$TypeRow[attribs$Type=="Forward"] <- seq(length=nrow(attribs[attribs$Type=="Forward",]))
attribs$TypeRow[attribs$Type=="Midfield"] <- seq(length=nrow(attribs[attribs$Type=="Midfield",]))
attribs$TypeRow[attribs$Type=="Defense"] <- seq(length=nrow(attribs[attribs$Type=="Defense",]))
attribs$TypeRow[attribs$Type=="Keeper"] <- seq(length=nrow(attribs[attribs$Type=="Keeper",]))
### Prediction and residual charts for each linear model ####
ggplot(attribs) +
aes(x=RAT,y=Type.Pred) +
geom_point(aes(color=Type),size=3,alpha=0.3) +
facet_grid(Type~.) +
theme(legend.position="none") +
xlab("Actual Rating") + ylab("Predicted Rating") + ggtitle("Model Performance")
ggplot(attribs) +
aes(x=Type.xFactor) +
geom_density(aes(color=Type,fill=Type),alpha=0.3) +
xlab("Residual") + ylab("Density") + ggtitle("Model Residuals") +
geom_vline(xintercept=0,color="red",size=1,linetype=2) +
coord_cartesian(xlim = c(-15,15))
### Dotplots of coefficient weights ####
ggplot() +
aes(x=for.lm$coefficients[2:7],
y=reorder(names(for.lm$coefficients[2:7]),for.lm$coefficients[2:7])) +
geom_point(color="red",size=10) +
theme(panel.background=element_rect(fill="darkgray")) +
theme(legend.position="none") +
xlab("Weight") + ylab("Attribute") + ggtitle("Attribute Weighting for Forward") +
geom_vline(xintercept=0,color="gold",size=2,linetype=2) +
coord_cartesian(xlim = c(-0.05,0.8))
ggplot() +
aes(x=mid.lm$coefficients[2:7],
y=reorder(names(mid.lm$coefficients[2:7]),mid.lm$coefficients[2:7])) +
geom_point(color="green",size=10) +
theme(panel.background=element_rect(fill="darkgray")) +
theme(legend.position="none") +
xlab("Weight") + ylab("Attribute") + ggtitle("Attribute Weighting for Midfield") +
geom_vline(xintercept=0,color="gold",size=2,linetype=2) +
coord_cartesian(xlim = c(-0.05,0.8))
ggplot() +
aes(x=def.lm$coefficients[2:7],
y=reorder(names(def.lm$coefficients[2:7]),def.lm$coefficients[2:7])) +
geom_point(color="blue",size=10) +
theme(panel.background=element_rect(fill="darkgray")) +
theme(legend.position="none") +
xlab("Weight") + ylab("Attribute") + ggtitle("Attribute Weighting for Defense") +
geom_vline(xintercept=0,color="gold",size=2,linetype=2) +
coord_cartesian(xlim = c(-0.05,0.8))
ggplot() +
aes(x=kee.lm$coefficients[2:7],
y=reorder(names(kee.lm$coefficients[2:7]),kee.lm$coefficients[2:7])) +
geom_point(color="purple",size=10) +
theme(panel.background=element_rect(fill="darkgray")) +
theme(legend.position="none") +
xlab("Weight") + ylab("Attribute") + ggtitle("Attribute Weighting for Keeper") +
geom_vline(xintercept=0,color="gold",size=2,linetype=2) +
coord_cartesian(xlim = c(-0.05,0.8))
### Find players who would be ranked higher in a different type ####
head(attribs[attribs$Type=="Forward"&attribs$Best=="Defense",],3) # F better as D
head(attribs[attribs$Type=="Forward"&attribs$Best=="Midfield",],3) # F better as M
head(attribs[attribs$Type=="Midfield"&attribs$Best=="Defense",],3) # M better as D
head(attribs[attribs$Type=="Midfield"&attribs$Best=="Forward",],3) # M better as F
head(attribs[attribs$Type=="Defense"&attribs$Best=="Forward",],3) # D better as F
head(attribs[attribs$Type=="Defense"&attribs$Best=="Midfield",],3) # D better as M
### Find outlier predicted players by
head(attribs[order(attribs$Type.xFactor),
c("Name","Position","RAT","Type.Pred","xFactor")],3) # > pred in type
tail(attribs[order(attribs$Type.xFactor),
c("Name","Position","RAT","Type.Pred","xFactor")],3) # < pred in type
head(attribs[order(attribs$xFactor),
c("Name","Position","RAT","Best.Pred","xFactor")],3) # > pred in best type
tail(attribs[order(attribs$xFactor),
c("Name","Position","RAT","Best.Pred","xFactor")],3) # < pred in best type
### k-means clustering ####
wss <- (nrow(attribs)-1)*sum(apply(attribs[,4:9],2,var))
for (w in 2:9){
wss[w] <- sum(kmeans(attribs[,4:9],centers=w)$withinss)
}
clusterNums <- seq(from=1,to=9,by=1)
ggplot() +
aes(x=clusterNums,y=wss) +
geom_point(color="brown",size=10) +
coord_cartesian(xlim = c(0,10)) +
scale_x_continuous(breaks=1:9) +
ylab("Weighted Sum of Squares") + xlab("Number of Clusters") +
ggtitle("WSS by Cluster Count")
fit3 <- kmeans(attribs[,4:9],3)
attribs$kM3 <- fit3$cluster
### Hierarchical clustering ####
Top <- attribs[attribs$RAT>=TopRAT&attribs$Type!="Keeper",c(1,4:10)]
rownames(Top) <- Top$Name
Top.hc <- hclust(dist(Top[2:7],method="euclidean"),method="ward")
Top.dhc <- as.dendrogram(Top.hc,hang=0.1)
Top.ddata <-dendro_data(Top.dhc,type="rectangle")
Top.ddata$labels$Type <- Top$Type[match(Top.ddata$labels$label,Top$Name)]
Type <- Top.ddata$labels$Type
ggplot(segment(Top.ddata)) +
geom_segment(aes(x=x,y=y,xend=xend,yend=yend)) +
geom_text(data=label(Top.ddata),
aes(x=x,y=y,label=label,hjust=0,color=Type)) +
coord_flip() + scale_y_reverse(expand=c(0.2,0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
axis.line.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.x=element_blank(),
axis.title.x=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank()) +
theme(legend.justification=c(0,0), legend.position=c(0.25,0.6)) +
ggtitle("Player Tree")