-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.R
897 lines (769 loc) · 34.7 KB
/
functions.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
library(png)
library(grid)
library(doSNOW)
library(plyr)
library(dplyr)
library(rvest)
library(ggplot2)
library(data.table)
library(XML)
library(lubridate)
library(lpSolve)
library(zoo)
library(MASS)
library(stringi)
options(stringsAsFactors = F, scipen =999)
simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), tolower(substring(s, 2)),
sep="", collapse=" ")
}
####ABBREVIATIONS###
abbrev<-read.csv("Misc Data/Abbrev.txt", sep="\t", header=TRUE, row.names = NULL)
abbrev$Abbrev2<-unlist(lapply(strsplit(abbrev$FullName, " "), function(x) paste(x[-length(x)], collapse=" ")))
abbrev$Abbrev2[abbrev$Abbrev=="NYJ"]<-"N.Y. Jets"
abbrev$Abbrev2[abbrev$Abbrev=="NYG"]<-"N.Y. Giants"
abbrev$Abbrev<-sapply(abbrev$Abbrev, simpleCap)
abbrev$Abbrev[abbrev$Abbrev%in% c("Gb", "Kc", "La", "Ne", "No", "Sd","Sf", "Tb")]<-
c("Gnb", "Kan", "Lar", "Nwe", "Nor", "Sdg", "Sfo", "Tam")
abbrev$Abbrev3<-tolower(abbrev$Abbrev)
abbrev$Abbrev3[abbrev$Abbrev3=="ari"]<-"crd"
abbrev$Abbrev3[abbrev$Abbrev3=="bal"]<-"rav"
abbrev$Abbrev3[abbrev$Abbrev3=="hou"]<-"htx"
abbrev$Abbrev3[abbrev$Abbrev3=="ind"]<-"clt"
abbrev$Abbrev3[abbrev$Abbrev3=="oak"]<-"rai"
abbrev$Abbrev3[abbrev$Abbrev3=="ten"]<-"oti"
abbrev$Abbrev3[abbrev$Abbrev3=="stl"|abbrev$Abbrev3=="lar"|abbrev$Abbrev3=="la"]<-"ram"
#https://stackoverflow.com/questions/20495598/replace-accented-characters-in-r-with-non-accented-counterpart-utf-8-encoding
unwanted_array <- list( 'Š'='S', 'š'='s', 'Ž'='Z', 'ž'='z', 'À'='A', 'Á'='A', 'Â'='A', 'Ã'='A', 'Ä'='A', 'Å'='A', 'Æ'='A', 'Ç'='C', 'È'='E', 'É'='E',
'Ê'='E', 'Ë'='E', 'Ì'='I', 'Í'='I', 'Î'='I', 'Ï'='I', 'Ñ'='N', 'Ò'='O', 'Ó'='O', 'Ô'='O', 'Õ'='O', 'Ö'='O', 'Ø'='O', 'Ù'='U',
'Ú'='U', 'Û'='U', 'Ü'='U', 'Ý'='Y', 'Þ'='B', 'ß'='Ss', 'à'='a', 'á'='a', 'â'='a', 'ã'='a', 'ä'='a', 'å'='a', 'æ'='a', 'ç'='c',
'è'='e', 'é'='e', 'ê'='e', 'ë'='e', 'ì'='i', 'í'='i', 'î'='i', 'ï'='i', 'ð'='o', 'ñ'='n', 'ò'='o', 'ó'='o', 'ô'='o', 'õ'='o',
'ö'='o', 'ø'='o', 'ù'='u', 'ú'='u', 'û'='u', 'ý'='y', 'ý'='y', 'é'='e', '«'='' )
coordName<-function(x) {
x<-chartr(paste(names(unwanted_array), collapse=''),
paste(unwanted_array, collapse=''),
x)
x[x%in% abbrev$Abbrev]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev], abbrev$Abbrev)]
x[x%in% abbrev$Abbrev2]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev2], abbrev$Abbrev2)]
x[x%in% abbrev$Abbrev3]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev3], abbrev$Abbrev3)]
x[x%in% abbrev$FullName]<-abbrev$Abbrev[match(x[x%in% abbrev$FullName], abbrev$FullName)]
x<-gsub("^\\s+|\\s+$", "", x)
x<-gsub(" Jr.| Sr.","",x)
x<-gsub("*|+|`", "",x)
x<-gsub("[*]|[`]", "",x)
x<-gsub("[+]", "",x)
x<-gsub(", Jr.", " Jr.", x)
x<-gsub("[.]|[']|[,]", "", x)
x<-gsub(" Jr", "", x)
x<-gsub(" Sr", "", x)
x<-gsub(" III| Iii", "", x)
x<-gsub(" IV", "", x)
x<-gsub(" II| Ii", "", x)
x<-gsub("-", " ", x)
x<-gsub(" ", " ", x)
x<-gsub("R J ", "RJ ", x)
x<-gsub("D J ", "DJ ", x)
x<-gsub("E J ", "EJ ", x)
x<-gsub("T J ", "TJ ", x)
x<-gsub("D J ", "DJ ", x)
x<-gsub("De'Andre", "DeAndre", x)
x<-gsub("Le'Ron", "LeRon", x)
x<-sapply(x,simpleCap)
x<-gsub("Zac |Zack |Zachary |Zackary ", "Zach ", x)
x<-gsub("Johnathan |Jon |John ", "Jonathan ", x)
x<-gsub("Christopher ", "Chris ", x)
x<-gsub("Billy ", "Bill ", x)
x<-gsub("Deshone", "DeShone", x)
x<-gsub("Khalif", "Kalif", x)
x<-gsub("Gerell", "Gerrell", x)
x<-gsub("Mitch ", "Mitchell ", x)
x<-gsub("William ", "Will ", x)
x<-gsub("Pat ", "Patrick ", x)
x<-gsub("Alex ", "Alexander ", x)
x<-gsub("Jimmy ", "Jim ", x)
x<-gsub("Thad ", "Thaddeus ", x)
x<-gsub("Tim |Timothhy |Timmy ", "Timothy ", x)
x<-gsub("Matt ", "Matthew ", x)
x<-gsub("Nathan |Nathaniel ", "Nate ", x)
x<-gsub("Joe ", "Joseph ", x)
x<-gsub("Shaquelle |Shaquille |Shaquil ", "Shaq ", x)
x<-gsub("Johnnie Lee ", "Johnnielee ", x)
x<-gsub("Josh ", "Joshua ", x)
x<-gsub("Rich ", "Richard ", x)
x<-gsub("Mike |mike ", "Michael ", x)
x<-gsub("Rob ", "Robert ", x)
x<-gsub("Samuel ", "Sam ", x)
x<-gsub("Nicholas |Nicolas |Nic ", "Nick ", x)
x<-gsub("Daniel |Danny ", "Dan ", x)
x<-gsub("Stevie ", "Steve ", x)
x<-gsub("Brad ", "Bradley ", x)
x<-gsub("Walt ", "Walter ", x)
x<-gsub("Trenton ", "Trent ", x)
x<-gsub("Edward ", "Ed ", x)
x<-gsub("Ronald ", "Ron ", x)
x<-gsub("Isreal ", "Israel ", x)
x<-gsub("Vincent ", "Vince ", x)
x<-gsub("Dave ", "David ", x)
x<-gsub("Rodrick |Roderick ", "Rod ", x)
x<-gsub("Ben |Benny ", "Benjamin ", x)
x<-gsub("Brenden ", "Brendan ", x)
x[ grepl("Odell", x) & grepl("Beckham", x)]<-"Odell Beckham"
x[x== "Evan Dietrich Smith"]<-"Evan Smith"
x[x== "Chris Kirksey"]<-"Christian Kirksey"
x[x== "Antwon Blake"]<-"Valentino Blake"
x[x== "Joseph Lefeged"]<-"Joseph Young"
x[x== "Lac Edwards"]<-"Lachlan Edwards"
x[x== "Cb Bryant"]<-"Christian Bryant"
x[x== "Leterrius Walton"]<-"Lt Walton"
x[x== "Nordly Capi"]<-"Cap Capi"
x[x== "Jacob Schum"]<-"Jake Schum"
x[x== "Owamagbe Odighizuwa"]<-"Owa Odighizuwa"
x[x== "Ricky Wagner"]<-"Rick Wagner"
x[x== "Gator Hoskins"]<-"Harold Hoskins"
x[x== "Vladimir Ducasse"]<-"Vlad Ducasse"
x[x== "Dejonathan Gomes"]<-"Dejon Gomes"
x[x== "Dajonathan Harris"|x=="Dajon Harris"|x=="DaJohnathan Harris"|x=="DaJohn Harris"]<-"Dajohn Harris"
x[x== "Jeffrey Linkenbach"]<-"Jeff Linkenbach"
x[x== "Manuel Ramirez"]<-"Manny Ramirez"
x[x== "Herbert Taylor"]<-"Herb Taylor"
x[x== "Ike Ndukwe"]<-"Ikechuku Ndukwe"
x[x== "Nate Arthur Byham"]<-"Nate Byham"
x[x== "Vic Worsley"]<-"Victor Worsley"
x[x== "Baba Oshinowo"]<-"Babatunde Oshinowo"
x[x== "Bam Childress"]<-"Brandon Childress"
x[x== "Steve Cheek"]<-"Stephen Cheek"
x[x== "Steve Neal"]<-"Stephen Neal"
x[x== "Renaud Williams"]<-"Renauld Williams"
x[x== "Woody Dantzler"]<-"Woodrow Dantzler"
x[x== "Raymond Perryman"]<-"Ray Perryman"
x[x== "Jj Huggins"]<-"Johnny Huggins"
x[x== "Abdul Karim Al Jabbar"]<-"Karim Abdul Jabbar"
x[x== "Maugaula Tuitele"]<-"Ula Tuitele"
x[x== "Rocket Ismail"]<-"Raghib Ismail"
x[x== "Michael A Jones"]<-"Michael Jones"
x[x== "T Marcus Spriggs"]<-"Marcus Spriggs"
x[x== "Tyrone M Williams"]<-"Tyrone Williams"
x[x=="Eman Sanders"]<-"Emmanuel Sanders"
x[x=="Bishop Stankey"]<-"Bishop Sankey"
x[x=="Jarret Boykin"]<-"Jarrett Boykin"
x[x=="Kadem Carrey"]<-"Kadeem Carey"
x[x=="Brendan Lefell"]<-"Brandon Lafell"
x[x=="Ezekial Ansah"]<-"Ezekiel Ansah"
x[x=="Jarius Byrd"]<-"Jairus Byrd"
x[x=="Devis Harris"]<-"David Harris"
x[x=="Ryan Matthews"]<-"Ryan Mathews"
x[x=="Levonte David"]<-"Lavonte David"
x[x=="Gio Bernard"]<-"Giovani Bernard"
x[x=="Steven Haushka"]<-"Steven Hauschka"
x[x=="Cordarelle Patterson"]<-"Cordarrelle Patterson"
x[x=="Luke Kueckly"]<-"Luke Kuechly"
x[x=="Paul Worrliow"]<-"Paul Worrilow"
x[x=="Micheal Floyd"]<-"Michael Floyd"
x[x=="Odell Beckam"]<-"Odell Beckham"
x[x=="Kennan Allen"]<-"Keenan Allen"
x[x=="Ryan Tannehil"]<-"Ryan Tannehill"
x[x== "Greg K Jones"]<-"Greg Jones"
x[x== "Todd F Collins"]<-"Todd Collins"
x[x== "Kevin R Williams"]<-"Kevin Williams"
x[x== "Charles L Johnson"]<-"Charles Johnson"
x[x== "Michael L Lewis"]<-"Michael Lewis"
x[x== "Andre Hal"]<-"Andre Hall"
x[x== "R Jay Soward"]<-"Rjay Soward"
x[x== "Dede Dorsey"|x=="DeDe Dorsey"]<-"De Dorsey"
x[x== "Scott Vines"]<-"Scottie Vines"
x[x== "N D Kalu"]<-"ND Kalu"
x[x== "Will Peterson"]<-"Will James"
x[x== "AD Denham"]<-"Anthony Denham"
x[x== "Joseph Davenport"]<-"Joseph Dean Davenport"
x[x== "Derek M Smith"]<-"Derek Smith"
x[x== "Stephen Hauschka"]<-"Steven Hauschka"
x[x== "Roosevelt Williams"]<-"Roe Williams"
x[x== "Oshiomogho Atogwe"]<-"Oj Atogwe"
x[x== "Jolonn Dunbar"]<-"Jo Lonn Dunbar"
x[x== "Standford Keglar"]<-"Stanford Keglar"
x[x== "Chinedum Ndukwe"]<-"Nedu Ndukwe"
x[x== "Dwight Bentley"]<-"Bill Bentley"
x[x== "Ray Ventrone"]<-"Raymond Ventrone"
x[x== "Jay Elliott"]<-"Jayrone Elliott"
x[x== "Sammie Hill"]<-"Sammie Lee Hill"
x[x== "Cameron Cleeland"]<-"Cam Cleeland"
x[x== "Tony Hargrove"]<-"Anthony Hargrove"
x[x== "Ed Hartwell"]<-"Edgerton Hartwell"
x[x== "Junior Siav"]<-"Junior Siavii"
x[x== "Jerry Attaochu"]<-"Jeremiah Attaochu"
x[x== "Jalen Tabor"]<-"Teez Tabor"
x[x== "Albert Fincher"]<-"Alfred Fincher"
x[x== "Pep Levingston"]<-"Lazarius Levingston"
x[x== "Hebron Fangupo"]<-"Loni Fangupo"
x[x== "Jay Ratliff"]<-"Jeremiah Ratliff"
x[x== "Macho Harris"]<-"Victor Harris"
x[x== "Juqua Thomas"]<-"Juqua Parker"
x[x== "Christion Jones"]<-"Christian Jones"
x[x== "Mo Alexander"]<-"Maurice Alexander"
x[x== "Malcolm Floyd"]<-"Malcom Floyd"
x[x== "Joseph Unga"]<-"Jj Unga"
x[x== "Jean Phillipe Darche"]<-"Jp Darche"
x[x== "Christian Mohr"]<-"Chris Mohr"
x[x== "Corderrelle Patterson"]<-"Cordarrelle Patterson"
x[grepl( "Dom", x)& grepl("Rodgers", x)& grepl("romartie", x)]<-"Dominique Rodgers Cromartie"
x[ grepl("Stallworth", x)& grepl("Donte", x)]<-"Donte Stallworth"
x[ grepl("Priest", x)& grepl("Holmes", x)]<-"Priest Holmes"
x[ grepl("David", x)& grepl("Givens", x)]<-"David Givens"
x[ grepl("Chad", x)& grepl("Ocho", x)]<-"Chad Johnson"
x[x== "M White"]<-"Myles White"
x[x== "Nickell Robey"]<-"Nickell Robey Coleman"
x[x== "Deji Olatoye"]<-"Ayodeji Olatoye"
x[x== "Saverio Rocca"]<-"Sav Rocca"
x[x== "Travis Carrie"]<-"Tj Carrie"
x[x== "Jordan Dizon"]<-"Jordon Dizon"
x[x== "Dj Ware"]<-"Dan Ware"
x[x== "Donald Drer"]<-"Donald Driver"
x[x== "Ziggy Hood"]<-"Evander Hood"
x[x== "Ziggy Ansah"]<-"Ezekiel Ansah"
x[x== "Chris Dwightstone Jones"]<-"Chris Jones"
x[x== "Tony Dewayne Mcdaniel"]<-"Tony Mcdaniel"
x[x== "Don Juan Carey"]<-"Don Carey"
x[x== "Brian Jeffrey Mihalik"]<-"Brian Mihalik"
x[x== "Robert Chevis Nelson"]<-"Robert Nelson"
x[x== "Doug Oneal Middleton"]<-"Doug Middleton"
x[x== "Mickey Charles Shuler"]<-"Mickey Shuler"
x[x== "Lequan Letrell Lewis"]<-"Lequan Lewis"
x[x== "Andre Phillip Smith"]<-"Andre Smith"
x[x== "Jacque Cesaire"]<-"Jacques Cesaire"
x[x== "Boobie Dixon"]<-"Anthony Dixon"
x[x== "Andre Jerome Caldwell"]<-"Andre Caldwell"
x[x== "Devin Breaux"]<-"Delvin Breaux"
x[x== "Tyron Crawford"]<-"Tyrone Crawford"
x[x== "Cornellius Carradine"]<-"Tank Carradine"
x[x== "Hasean Clinton Dix"]<-"Ha Ha Clinton Dix"
x[x== "Ramon Humbar"]<-"Ramon Humber"
x[x== "Chad Ochocinco"]<-"Chad Johnson"
x[x== "Philip Rers"]<-"Philip Rivers"
x[x== "Tani Tupou"]<-"Taniela Tupou"
x[x== "Bobo Wilson"]<-"Jesus Wilson"
x[x== "John Paul Foschi"]<-"Jp Foschi"
x[x== "Danny Ware"]<-"Dj Ware"
x[x== "Jonathan Baldwin"]<-"Jon Baldwin"
x[x== "Chartric Darby"]<-"Chuck Darby"
x[x== "Trevor Graham"]<-"Tj Graham"
x[x== "J Talley"]<-"Julian Talley"
x[x== "Charles D Johnson"]<-"Charles Johnson"
x[x== "Seantavious Jones"]<-"Seantavius Jones"
x[x== "Will Fuller V"]<- "Will Fuller"
x[x== "Pacman Jones"]<- "Adam Jones"
x[x== "Broderick Bunkley"]<- "Brodrick Bunkley"
x[x== "Evan Dietrich-Smith" ]<-"Evan Smith"
x[x=="Philly Brown" ]<-"Corey Brown"
x[x=="Zachdiles" ]<-"Zach Diles"
x[x=="Leighton Vander" ]<-"Leighton Vander Esch"
x[x=="Laurinaitis"]<-"James Laurinaitis"
x[x=="Bradshaw"]<-"Ahmad Bradshaw"
x[x== "Samajae Perine"]<-"Samaje Perine"
x[x== "Phillip Rivers"]<-"Philip Rivers"
x[x== "Tedd Ginn"]<-"Ted Ginn"
x[x== "Alexander Ogletree"]<-"Alec Ogletree"
x[x== "Tony Jeffersom"]<-"Tony Jefferson"
x[x== "Vinateri"|x=="vinateri"|x=="Adam Vinateri"]<-"Adam Vinatieri"
x[x== "Khali Mack"|x=="Kahlil Mack"]<-"Khalil Mack"
x[x== "Wendall Smallwood"]<-"Wendell Smallwood"
x[x== "Alshon Jeffrey"]<-"Alshon Jeffery"
x[x== "Will Lutz"]<-"Wil Lutz"
x[x== "Risshard Matthews"]<-"Rishard Matthews"
x[x== "Joey Brosa"]<-"Joey Bosa"
x[x== "Giovanni Bernard"]<-"Giovani Bernard"
x[x== "Jim Grahm"]<-"Jim Graham"
x[x== "Hassan Reddick"]<-"Haason Reddick"
x[x== "Dein Jones"]<-"Deion Jones"
x[x== "James Connor"]<-"James Conner"
x[x== "Robbie Anderson"]<-"Robby Anderson"
x[x== "Terrence West"]<-"Terrance West"
x[x== "Jay Ajayii"]<-"Jay Ajayi"
x[x== "Isiah Crowell"]<-"Isaiah Crowell"
x[x== "Bernard Mckinney"]<-"Benardrick Mckinney"
x[x== "Paul Puz"|x=="Paul Posluzsny"|x=="Paul Posluzny"]<-"Paul Posluszny"
x[x== "Lagarette Blount"|x=="Legarette Blount"]<-"Legarrette Blount"
x[x== "Erik Decker"]<-"Eric Decker"
x[x== "Bilall Powell"]<-"Bilal Powell"
x[x== "Jedeveon Clowdney"|grepl("eon Clowney", x)]<-"Jadeveon Clowney"
x[x== "Mathew Stafford"]<-"Matthew Stafford"
x[x== "Chandler Carazano"]<-"Chandler Catanzaro"
x[x== "Navarro Bowman"]<-"Navorro Bowman"
x[x== "Emanuel Sanders"]<-"Emmanuel Sanders"
x[x== "Darren Mccfadden"]<-"Darren Mcfadden"
x[x== "James Laurinatis"]<-"James Laurinaitis"
x[x== "Gore"]<-"Frank Gore"
x[x== "Russel Wilson"]<-"Russell Wilson"
x[x== "Everson Griffin"]<-"Everson Griffen"
x[x== "Buck Allen"]<-"Javorius Allen"
x[x== "Reuben Randle"]<-"Rueben Randle"
x[x== "Derrick Jonhson"]<-"Derrick Johnson"
x[x== "Mo Wilkerson"]<-"Muhammad Wilkerson"
x[x== "Mcmanus"]<-"Brandon Mcmanus"
x[x== "Ryan Shaziwer"]<-"Ryan Shazier"
x[x== "Larry Donnel"]<-"Larry Donnell"
x[x== "Martellus Bennet"]<-"Martellus Bennett"
x[x== "Daniel Herron"]<-"Dan Herron"
x[x== "Juju Smithschuster"]<-"Juju Smith Schuster"
x[x== "Maurice Jonesdrew"]<-"Maurice Jones Drew"
x[x== "Steve Smith Sr"]<-"Steve Smith"
x[x== "Austin Seferianjenkins"]<-"Austin Seferian Jenkins"
x[x== "Jermain Kearse"]<-"Jermaine Kearse"
x[endsWith( x," Sr")]<-gsub(" Sr", "", x[endsWith( x," Sr")])
#madden data has lots of erors
x[x=='Carnell Williams']<-"Cadillac Williams"
x[x=='Benjamin Roethlisbergr']<-"Benjamin Roethlisberger"
x[x=='Maurice Drew Jones']<-"Maurice Jones Drew"
x[x=='Domanick Davis']<-"Domanick Williams"
x[x=='Tj Houshmandz']<-"Tj Houshmandzadeh"
x[x=='Chris Wells']<-"Tj Houshmandzadeh"
x[x%in% c("London Fletcher Baker", "Londom Fletcher")]<-"London Fletcher"
x[x=="Chis Long"]<-"Chris Long"
x[x=='Arenas Williams']<-"Aeneas Williams"
x[x=='Roosevelt Colvin']<-"Rosevelt Colvin"
x[x=='Antonie Winfield']<-"Antoine Winfield"
x[x=='Aquib Talib']<-"Aqib Talib"
x[x=='Michael Barrow']<-"Micheal Barrow"
x[x=='Freddie Keiaho']<-"Freddy Keiaho"
x[x=='Lethon Flowers']<-"Lee Flowers"
x[x=='Marcus Polland']<-"Marcus Pollard"
x[x=='Andry Goodman']<-"Andre Goodman"
x[x=='Al Singleton']<-"Alshermond Singleton"
x[x=='Kimo Voelhoffen']<-"Kimo Von Oelhoffen"
x[x=='Johnny Morton']<-"Johnnie Morton"
x[x=='Greg White'|x=="Stylez White"]<-"Stylez G White"
x[x=='Ndukwe Kalu']<-"Nd Kalu"
x[x=='Ryan Lindell']<-"Rian Lindell"
x[x=='Benard Pollard']<-"Bernard Pollard"
x[x=='Amos Zereque']<-"Amos Zereoue"
x[x=='Ryan Lindell']<-"Rian Lindell"
x[x=='Donovan Darius']<-"Donovin Darius"
x[x=='Willie Green']<-"Will Green"
x[x=='Daymeion Hughes']<-"Dante Hughes"
x[x=='Gregory Toler']<-"Greg Toler"
x[x=='Darney Scott']<-"Darnay Scott"
x[x=='Chris Fuamatu Mafala']<-"Chris Fuamatu Maafala"
x[x=='Demetrius Bell']<-"Demetress Bell"
x[x=='Matthew Mcbriar'|x=='Matt Mcbriar']<-"Mat Mcbriar"
x[x=='Willie Henderson']<-"Will Henderson"
x[x=='Vonte Leach']<-"Vonta Leach"
x[x=='Phillip Wheeler']<-"Philip Wheeler"
x[x=='Siitupe Peko']<-"Tupe Peko"
x[x=='Chris Fuamatu Ma']<-"Chris Fuamatu Maafala"
x[x=='Greg R Randall']<-"Greg Randall"
x[x=='Barrett Robbins']<-"Barret Robbins"
x[x=='Greg R Randall']<-"Greg Randall"
x[x=='Owin Schmitt']<-"Owen Schmitt"
x[x=='Jermaine Mayberry']<-"Jermane Mayberry"
x[x=='Greg R Randall']<-"Greg Randall"
x[x=='Clifton Ryan']<-"Cliff Ryan"
x[x=='Marvin Smith']<-"Marvel Smith"
x[x== "David Alexander Gettis"]<-"David Gettis"
x[x== "Caleb Jeffrey Hanie"]<-"Caleb Hanie"
x[x=="Willie Snead Iv"|x=="Willie Snead IV"]<-"Willie Snead"
x[x=="Ne"]<-"NWE"
x<-gsub(" Defense", "", x)
x[x=="Ne"| grepl("Patriots", x)|x=="Nep"|x=="NEP"]<-"NWE"
x[x=="Kc"| grepl("Chiefs", x)|x=="Kcc"|x=="KCC"]<-"KAN"
x[x=="Gb" | grepl("Packers", x)|x=="GBP"|x=="Gbp"]<-"GNB"
x[x=="Sd"| x=="Lac"|x=="LAC"| grepl("Chargers", x)| (grepl("ngeles", x)& grepl("LAC|lac|Lac", x))]<-"SDG"
x[x=="No" |grepl("Saints",x)|x=="Nos"|x=="NOS"]<-"NOR"
x[x=="Tb" | grepl("Buccaneers", x)|x=="Tbb"|x=="TBB"]<-"TAM"
x[x=="Sf"| grepl("49ers", x)| grepl("San Francisco", x)]<-"SFO"
x[x=="La"]<-"LAR"
x[x%in% c("Rams", "St Louis", "Stl Rams", "Saint Louis Rams", "St Louis Rams", "La Rams", "Los Angeles Rams", "STL", "Stl")|
(grepl("ngeles", x)& grepl("lar|Lar|LAR", x))]<-"Lar"
x[x=="Jac"|x=="Jacksonville Jagaurs"]<-"JAX"
x[grepl("Cardinals", x)]<-"ARI"
x[grepl("Falcons", x)]<-"ATL"
x[grepl("Ravens", x)|x%in% c("Blt", "BLT")]<-"BAL"
x[grepl("Bills", x)]<-"BUF"
x[grepl("Carolina", x)]<-"CAR"
x[grepl("Bears", x)]<-"CHI"
x[grepl("Bengals", x)]<-"CIN"
x[grepl("Browns", x)|x%in% c("CLV", "Clv")]<-"CLE"
x[grepl("Cowboys", x)]<-"DAL"
x[grepl("Broncos", x)]<-"DEN"
x[grepl("Lions", x)]<-"DET"
x[grepl("Packers", x)]<-"GNB"
x[grepl("Texans", x)|x=="Hst"|x=="HST"]<-"HOU"
x[grepl("Colts", x)]<-"IND"
x[grepl("Jaguars", x)| x=="JAC"]<-"JAX"
x[grepl("Dolphins", x)]<-"MIA"
x[grepl("Vikings", x)]<-"MIN"
x[grepl("Giants", x)| grepl("Nyg", x)| grepl("nyg", x)]<-"NYG"
x[grepl("Jets", x)| grepl("Nyj", x)| grepl("nyj", x)]<-"NYJ"
x[grepl("Raiders", x)]<-"OAK"
x[grepl("Panthers", x)]<-"CAR"
x[grepl("Eagles", x)]<-"PHI"
x[grepl("Steelers", x)]<-"PIT"
x[grepl("Seahawks", x)]<-"SEA"
x[grepl("Titans", x)]<-"TEN"
x[grepl("Redskins", x)|x=='wsh'|x=="Wsh"]<-"WAS"
x[x%in% c("Arz", "ARZ")]<-"Ari"
x[x%in% abbrev$Abbrev]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev], abbrev$Abbrev)]
x[x%in% abbrev$Abbrev2]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev2], abbrev$Abbrev2)]
x[x%in% abbrev$Abbrev3]<-abbrev$Abbrev[match(x[x%in% abbrev$Abbrev3], abbrev$Abbrev3)]
x[x%in% abbrev$FullName]<-abbrev$Abbrev[match(x[x%in% abbrev$FullName], abbrev$FullName)]
x<-sapply(x,simpleCap)
x
}
moving<-function(x, length, operation="mean", include.current=F) {
if(include.current){
x<-c(NA, x)
}
#x<-c(2, 4, 1,2,4,1,2,4 ,4,4, 10, 1, NA, 1, 1, 1, 2 , NA,3, NA, 2);length<-10
#x<-c(NA, NA)
#if(operation%in% c())
x2<-x
if(operation%in% c("harmonic", "geometric")) {
x[x<=0]<-NA
}
if(operation=="mean" & length(x)>=1) {
y<-rep(NA, length(x))
if(is.na(x[1])) {
y[1]<-sum(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])/
length(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling mean of previous values
y[!is.na(x)]<-append(rollmean(x[!is.na(x)], length,na.pad=TRUE, align="left" )[-1], NA)
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))/
rev(seq_along(x[is.na(y) & !is.na(x)])-1)
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
} else{
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))/
rev(seq_along(x[is.na(y) & !is.na(x)])-1)
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
}
} else if (operation=="median" & length(x)>=1) {
y<-rep(NA, length(x))
if(is.na(x[1])) {
y[1]<-median(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling median of previous values
y[!is.na(x)]<-append(rollmedian(x[!is.na(x)], length,na.pad=TRUE, align="left" )[-1], NA)
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))/
rev(seq_along(x[is.na(y) & !is.na(x)])-1)
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
} else{
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))/
rev(seq_along(x[is.na(y) & !is.na(x)])-1)
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
}
} else if (operation=="max" & length(x)>=1) {
#x<-c(2, 4, 1,2,4,1,2,4 ,4,4, 10, 1, NA, 1, 1, 1, 2 , NA,3, NA, 2);length<-4
y<-rep(NA, length(x))
if(is.na(x[1])) {
y[1]<-max(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling median of previous values
y[!is.na(x)]<-append(rollmax(x[!is.na(x)], length,na.pad=TRUE, align="left" )[-1], NA)
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rev(cummax(rev(x[is.na(y) & !is.na(x)]))[-length(cummax(rev(x[is.na(y) & !is.na(x)])))])
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
} else{
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rev(cummax(rev(x[is.na(y) & !is.na(x)]))[-length(cummax(rev(x[is.na(y) & !is.na(x)])))])
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
}
} else if (operation=="min" & length(x)>=1) {
x<-(-x)
y<-rep(NA, length(x))
if(is.na(x[1])) {
y[1]<-max(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling median of previous values
y[!is.na(x)]<-append(rollmax(x[!is.na(x)], length,na.pad=TRUE, align="left" )[-1], NA)
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rev(cummax(rev(x[is.na(y) & !is.na(x)]))[-length(cummax(rev(x[is.na(y) & !is.na(x)])))])
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
} else{
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rev(cummax(rev(x[is.na(y) & !is.na(x)]))[-length(cummax(rev(x[is.na(y) & !is.na(x)])))])
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
}
y<-(-y)
} else if (operation =="sum" & length(x)>=1) {
y<-rep(NA, length(x))
if(is.na(x[1])) {
y[1]<-sum(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling sum of previous values
y[!is.na(x)]<-append(rollsum(x[!is.na(x)], length,na.pad=TRUE, align="left" )[-1], NA)
#fill in first few games with cumulative moving sum
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
#fill in first game with next game if NA
y<-na.locf(y, fromLast=TRUE,na.rm=FALSE)
} else {
#rolling mean of previous values
#fill in first few games with cumulative moving sum
y[is.na(y) & !is.na(x)]<-rev(cumsum(rev(x[is.na(y) & !is.na(x)]))-rev(x[is.na(y) & !is.na(x)]))
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
#fill in first game with next game
y<-na.locf(y, fromLast=TRUE,na.rm=FALSE)
}
}else if (operation =="lag" & length(x)>=1) {
y<-rep(NA, length(x))
if(length(x[!is.na(x)])>length) {
#rolling sum of previous values
y[!is.na(x)]<-lead(x[!is.na(x)], length)
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
toNA<-which(!is.na(x))[length(which(!is.na(x)))-length+1]
y[toNA:length(y)]<-NA
}
} else if( length(x)>=1) {
y<-rep(NA, length(x))
operationFunc<-get(operation)
if(is.na(x[1])) {
y[1]<-operationFunc(x[!is.na(x)][1:min(length, length(x[!is.na(x)]))])
}
if(length(x[!is.na(x)])>length) {
#rolling median of previous values
y[!is.na(x)]<-c(rollapply(x[!is.na(x)], width=length,FUN=operationFunc, fill=NA, align="left" )[-1], NA)
#fill in first few games with cumulative moving ave, rollapplyr is slower than cumsum, cummean, etc
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rollapplyr(x[is.na(y) & !is.na(x)], rev(seq_along(x[is.na(y) & !is.na(x)])), operationFunc, align="left")[-1]
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
} else if(!all(is.na(x))){
#fill in first few games with cumulative moving ave
y[is.na(y) & !is.na(x)][-length( y[is.na(y) & !is.na(x)])]<-
rollapplyr(x[is.na(y) & !is.na(x)], rev(seq_along(x[is.na(y) & !is.na(x)])), operationFunc, align="left")[-1]
#fill in missing games with previous game
y<-na.locf(y, fromLast=FALSE,na.rm=FALSE)
}
y[is.na(y) & !is.na(x2)]<-0
}
if(length(x)<1) {
y<-NA
}
#adjust beginning games, sometimes errors if multiple NAs to start off
NA_vec<-which(is.na(x2));
if(operation%in% c("nonZero", "geometric", "harmonic")){
NA_vec<-which(is.na(x2)| x2==0);
}
still_NA<-TRUE;
y[length(x2)]<-NA
adjust<-length(x2)
#first 2 should be NA for var-variables
if(operation %in% c("var", "sd", "kurtosis", "skewness")){
y[length(x2)-1]<-NA
adjust<-length(x2)-1
}
while(still_NA) {
y[adjust]<-NA
if((adjust) %in% NA_vec) {adjust<-adjust-1} else{ still_NA<-FALSE}
}
if(include.current){
y<-y[-length(y)]
y[length(y)]<-x[length(x)]
if(is.na(x[1])){
y[1]<-NA
}
}
y
}
harmonic<-function(x) {
harm<-1/mean(1/x[x>0], na.rm=TRUE)
if(is.nan(harm)){harm<-NA}
harm
}
geometric<- function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
trimmed<-function(x) {
mean(x[-c(which.min(x),which.max(x))], na.rm=T)
}
trimmed2<-function(x) {
if(sum(!is.na(x))>1){
mean(x[-c(which.max(x))], na.rm=T)
} else{
x
}
}
quartile75<-function(x) {
unname(quantile(x, .75, na.rm=TRUE))
}
quartile25<-function(x) {
unname(quantile(x, .25, na.rm=TRUE))
}
nonZero<-function(x) {
mean(x[x!=0], na.rm=TRUE)
}
Evaluation.NDCG = function(EstimatedRank, RelevanceLevel) {
Ordered.RelevanceLevel <- RelevanceLevel[order(-EstimatedRank)]
Actual.RelevanceLevel <- RelevanceLevel[order(-RelevanceLevel)]
dcg <- 0
normalization.constant <- 0
for(i in 1:length(Ordered.RelevanceLevel)) dcg <- dcg + (2^Ordered.RelevanceLevel[i] - 1) / log2(i+1)
for(i in 1:length(Actual.RelevanceLevel)) normalization.constant <- normalization.constant + (2^Actual.RelevanceLevel[i] - 1) / log2(i+1)
dcg / normalization.constant
}
makeSlotPlot<-function(Title="Simulation Results for All Draft Slots",
Parameters=c("Case 1", "Case 1 + ZeroRB in R1", "Case1 + ZeroWR in R1")){
Slot<-rep( paste0("Slot", 1:12), 3)
Sims<-unlist(c(simScores_allSlots, simScores_allSlots_zeroRB, simScores_allSlots_zeroWR))
Sims<-data.frame(Sim=Sims, Slot=rep(Slot, each=25000), Parameter=rep(Parameters, each=length(Sims)/3))
Sims$Slot<-factor(Sims$Slot, levels=unique(Sims$Slot))
Sims<-ddply(Sims, .(Slot, Parameter), summarize,
N = length(Sim),
mean = mean(Sim),
sd = sd(Sim),
se = sd / sqrt(N) )
library(ggplot2)
head(Sims)
p<-ggplot(Sims, aes(x=Slot, y=mean, fill=Parameter)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,
size=.3) + # Thinner lines
# theme(axis.text.x=element_blank(),
# axis.title.x = element_blank()
# )+
coord_cartesian(ylim=c(min(Sims$mean)-10, max(Sims$mean+20)))+
geom_errorbar(aes(ymin=mean-1.96*se, ymax=mean+1.96*se), #add confidence interval (+/-1.96*SE)
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
xlab("Slot") +
ylab("Mean-Simulated Starting Lineup (25,000 sims)") +
ggtitle(Title)
p
}
makeParamPlot<-function(Parameters=c("1. RBx5,WRx5,QBx2,K/DST/TEx1 (default)", "2. default, shift=.25","3. default, shift=-.15",
"4. zero RB in R1, shift=0", "5. zero WR in R1, shift=0", "6. \u2264 1QB in R1-11, shift=0",
"7. RBx4, WRx6, shift=0", "8. RBx6, WRx4, \u2264 1QB in R1-11, shift=0",
"9. RBx5,WRx5,TEx2,QB/DST/Kx1, shift=0", "10. RBx4,WRx5,QB/TEx2,DST/Kx1, shift=0",
"11. Zero RB in R1-4, \u2264 1QB in R1-11, shift=0" ),
Title="Simulation Results for Different Draft Parameters"){
Sims<-list(simScores,simScores2, simScores3, simScores4, simScores5,
simScores6, simScores7, simScores8, simScores9, simScores10, simScores11, simScores12)[1:length(Parameters)] %>% unlist(recursive = T)
Sims<-data.frame(Sim=Sims, Parameter=rep(as.character(Parameters), each=25000))
Sims$Parameter<-factor(Sims$Parameter, levels=unique(Sims$Parameter))
Sims<-ddply(Sims, .(Parameter), summarize,
N = length(Sim),
mean = mean(Sim),
sd = sd(Sim),
se = sd / sqrt(N) )
library(ggplot2)
head(Sims)
p<-ggplot(Sims, aes(x=Parameter, y=mean, fill=Parameter)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,
size=.3) + # Thinner lines
theme(axis.text.x=element_blank(),
axis.title.x = element_blank()
)+
coord_cartesian(ylim=c(min(Sims$mean)-10, max(Sims$mean+20)))+
geom_errorbar(aes(ymin=mean-1.96*se, ymax=mean+1.96*se), #add confidence interval (+/-1.96*SE)
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
xlab("Parameter") +
ylab("Mean-Simulated Starting Lineup (25,000 sims)") +
ggtitle(Title)
p
}
makeActualSlotPlot<-function(Title="Actual Results for All Draft Slots",
Parameters=c("Case 1", "Case 1 + ZeroRB in R1", "Case1 + ZeroWR in R1")){
Slot<-rep( paste0("Slot", 1:12), 3)
Actuals<-unlist(c(actualScores_allSlots, actualScores_allSlots_zeroRB, actualScores_allSlots_zeroWR))
Actuals<-data.frame(Actuals=Actuals, Slot=rep(Slot, each=250), Parameter=rep(Parameters, each=length(Actuals)/3))
Actuals$Slot<-factor(Actuals$Slot, levels=unique(Actuals$Slot))
Actuals<-ddply(Actuals, .(Slot, Parameter), summarize,
N = length(Actuals),
mean = mean(Actuals),
sd = sd(Actuals),
se = sd / sqrt(N) )
library(ggplot2)
head(Actuals)
p<-ggplot(Actuals, aes(x=Slot, y=mean, fill=Parameter)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,
size=.3) + # Thinner lines
# theme(axis.text.x=element_blank(),
# axis.title.x = element_blank()
# )+
coord_cartesian(ylim=c(min(Actuals$mean)-10, max(Actuals$mean+20)))+
geom_errorbar(aes(ymin=mean-1.96*se, ymax=mean+1.96*se), #add confidence interval (+/-1.96*SE)
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
xlab("Slot") +
ylab("Mean-Actual Starting Lineup (25,000 sims)") +
ggtitle(Title)
p
}
makeActualParamPlot<-function(Parameters=c("1. RBx5,WRx5,QBx2,K/DST/TEx1 (default)", "2. default, shift=.25","3. default, shift=-.15",
"4. zero RB in R1, shift=0", "5. zero WR in R1, shift=0", "6. \u2264 1QB in R1-11, shift=0",
"7. RBx4, WRx6, shift=0", "8. RBx6, WRx4, \u2264 1QB in R1-11, shift=0",
"9. RBx5,WRx5,TEx2,QB/DST/Kx1, shift=0", "10. RBx4,WRx5,QB/TEx2,DST/Kx1, shift=0",
"11. Zero RB in R1-4, \u2264 1QB in R1-11, shift=0" ),
Title="Actual Results for Different Draft Parameters"){
Sims<-list(simScores,simScores2, simScores3, simScores4, simScores5,
simScores6, simScores7, simScores8, simScores9, simScores10, simScores11, simScores12)[1:length(Parameters)] %>% unlist(recursive = T)
Sims<-data.frame(Sim=Sims, Parameter=rep(as.character(Parameters), each=25000))
Sims$Parameter<-factor(Sims$Parameter, levels=unique(Sims$Parameter))
Sims<-ddply(Sims, .(Parameter), summarize,
N = length(Sim),
mean = mean(Sim),
sd = sd(Sim),
se = sd / sqrt(N) )
Actuals<-list(actualScores,actualScores2, actualScores3, actualScores4, actualScores5,
actualScores6, actualScores7, actualScores8, actualScores9, actualScores10, actualScores11, actualScores12)[1:length(Parameters)] %>% unlist(recursive = T)
Actuals<-data.frame(Actuals=Actuals, Parameter=rep(as.character(Parameters), each=length(actualScores)))
Actuals$Parameter<-factor(Actuals$Parameter, levels=unique(Actuals$Parameter))
Actuals<-ddply(Actuals, .(Parameter), summarize,
N = length(Actuals),
mean = mean(Actuals),
sd = sd(Actuals),
se = sd / sqrt(N) )
Sims$Type<-"Sims"
Actuals$Type<-"Actuals"
Sims<-rbind(Sims, Actuals)
library(ggplot2)
head(Sims)
p<-ggplot(Sims, aes(x=Type, y=mean, fill=Parameter)) +
geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,
size=.3) + # Thinner lines
coord_cartesian(ylim=c(min(Sims$mean)-10, max(Sims$mean+20)))+
geom_errorbar(aes(ymin=mean-1.96*se, ymax=mean+1.96*se), #add confidence interval (+/-1.96*SE)
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
xlab("Type") +
ylab("Mean-Simulated Starting Lineup (25,000 sims)") +
ggtitle(Title)
p
}
Metrics<-function(y, x) {
Rsq<-1-sum((y-x)^2)/sum((y-mean(y))^2) #Rsq
RMSE<-sqrt(mean((y-x)^2)) #RMSE
bias<-mean(y-x) #RMSE
paste(c(paste("RSq:", Rsq), paste("RMSE:", RMSE),paste("bias:", bias) ))
}