library(ggiraphExtra)
library(knitr)
library(data.table)
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(caret)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
## Loading required package: lattice
library(ggplot2)
library(gbm)
## Loaded gbm 2.1.8.1
library(ggsci)
library(tree)
library(mlr3) #https://mlr3book.mlr-org.com/basics.html#learners
library(mlr3viz)
library(mlr3learners)
library(kknn)
## 
## Attaching package: 'kknn'
## The following object is masked from 'package:caret':
## 
##     contr.dummy

Load In Data

# data<-read_csv("_Projects\\Project3\\datasets\\NPBase.csv")
# 
# npData<-read_csv("NPBase.csv")
# npData<- as_tibble(npData)
# npData <- as.data.frame(npData)
# npData$win <- as.character(npData$win)
# #recode(npData$win, Yes = "1", No = "0") did not use
# #recode(npData$win, "Yes"="TRUE", "No" = "FALSE") did not use
# 
# npData
# 
# npData$win <- npData$win %>% mutate(win = ifelse(win == "No",0,1))
# 
# 
# npData <- data.frame(match_id = c(1234,5678,1245),
#                       start_time = c(1657339909,1657357190,1657366555),
#                       win = c('false','false','true'),
#                       hero_id = '53',
#                       account_id = c(2345438,259803,438689),
#                       leaguename = c('Titus','Destiny','Ultras'),
#                       gold_per_min = c(569,549,654),
#                       net_worth = c(15770,15317,16985),
#                       gold = c(1140,1559,3210),
#                       kills = c(4,12,7),
#                       tower_damage = c(1245,19823,4599),
#                       duration = c(1754,1759,1829),
#                       lane = c(1,3,3),
#                       lane_role = c(1, 3,3)
#                      )

    npData<-read_csv("NPBase.csv")
    npData$gold <- as.numeric(npData$gold)
    dataModel <- data.frame(npData)
    dataModel$win <- as.factor(dataModel$win)
    dataModel$lane <- as.factor(dataModel$lane)
    dataModel$lane_role <- as.factor(dataModel$lane_role)
    dataModel
##       match_id start_time   win hero_id account_id
## 1   6653637943 1657339909 FALSE      53  250544263
## 2   6653911377 1657357190 FALSE      53   76082922
## 3   6654090150 1657366555  TRUE      53 1312740399
## 4   6656721191 1657512120 FALSE      53  209165399
## 5   6657130147 1657540852  TRUE      53  132851371
## 6   6657972166 1657586272  TRUE      53   59752811
## 7   6658023411 1657591653 FALSE      53   85367644
## 8   6658141348 1657602904 FALSE      53 1312740399
## 9   6658904045 1657643886  TRUE      53  155370747
## 10  6659582275 1657695291  TRUE      53  412910858
## 11  6660871188 1657775009  TRUE      53  162539779
## 12  6660953769 1657782385  TRUE      53  176158708
## 13  6662754663 1657890925  TRUE      53  216284105
## 14  6663760635 1657948184 FALSE      53  254710104
## 15  6663831927 1657952923  TRUE      53  254710104
## 16  6663890419 1657956692 FALSE      53  128367019
## 17  6664002830 1657962564  TRUE      53  176158708
## 18  6664762074 1657994640 FALSE      53  191362875
## 19  6664870055 1657999696  TRUE      53 1261414529
## 20  6665353816 1658035345 FALSE      53  374150060
## 21  6665371502 1658036503  TRUE      53  247018516
## 22  6665457039 1658041546  TRUE      53  153727297
## 23  6665530799 1658045885 FALSE      53  923473386
## 24  6665530990 1658045359  TRUE      53  899495504
## 25  6665634983 1658050792 FALSE      53  330534326
## 26  6666225827 1658073951  TRUE      53  216284105
## 27  6666901923 1658120586  TRUE      53  254710104
## 28  6667145190 1658138752  TRUE      53  145993649
## 29  6667146750 1658138846  TRUE      53  153727297
## 30  6667177868 1658140739 FALSE      53 1170923497
## 31  6667234541 1658143841  TRUE      53 1173255449
## 32  6667305314 1658147179  TRUE      53 1173255449
## 33  6667411276 1658151435  TRUE      53  153727297
## 34  6668150103 1658193432  TRUE      53  202464276
## 35  6668150769 1658193513 FALSE      53  368668788
## 36  6668154335 1658193855  TRUE      53  249651648
## 37  6668214992 1658199924  TRUE      53  254710104
## 38  6668488644 1658223069  TRUE      53  886742476
## 39  6669118010 1658252002 FALSE      53  851178649
## 40  6669394757 1658268012 FALSE      53  116080282
## 41  6669455743 1658273257 FALSE      53  173971950
## 42  6669611769 1658289953 FALSE      53  202984325
## 43  6669704190 1658298434 FALSE      53  236322966
## 44  6669833934 1658308212  TRUE      53  360648679
## 45  6669894503 1658312143 FALSE      53  327800297
## 46  6669951485 1658315451 FALSE      53  218367241
## 47  6669958120 1658315961 FALSE      53 1045494300
## 48  6669999318 1658318413  TRUE      53   34505203
## 49  6670287920 1658331155 FALSE      53  177411785
## 50  6670482012 1658340005  TRUE      53   34505203
## 51  6670489387 1658340043 FALSE      53  206379758
## 52  6670781535 1658358674  TRUE      53  125455547
## 53  6671029299 1658383899 FALSE      53  399465090
## 54  6671588490 1658415604  TRUE      53  177411785
## 55  6671680767 1658419338  TRUE      53  177411785
## 56  6671827493 1658425735  TRUE      53  439345730
## 57  6671860500 1658427423  TRUE      53  106863163
## 58  6671949473 1658432182  TRUE      53   34505203
## 59  6672334898 1658466306  TRUE      53  254710104
## 60  6672361043 1658468671 FALSE      53  110142194
## 61  6672429701 1658474342 FALSE      53  104504914
## 62  6672487457 1658478590  TRUE      53  458287006
## 63  6672643482 1658488211 FALSE      53  458287006
## 64  6672675633 1658491198 FALSE      53  103910993
## 65  6672954942 1658502006  TRUE      53  311360822
## 66  6673081589 1658506714 FALSE      53   97658618
## 67  6673720237 1658549075 FALSE      53  171337923
## 68  6673734891 1658550038 FALSE      53  171337923
## 69  6673880837 1658559912  TRUE      53 1049956230
## 70  6673883278 1658559988 FALSE      53  101240880
## 71  6673952558 1658564159 FALSE      53  177061558
## 72  6674003905 1658566608  TRUE      53  254710104
## 73  6674108208 1658572065 FALSE      53  146372433
## 74  6674151935 1658574386 FALSE      53   94439767
## 75  6674445805 1658586499 FALSE      53   34505203
## 76  6674752354 1658598769  TRUE      53 1352080796
## 77  6674755794 1658599100 FALSE      53   94738847
## 78  6674843794 1658603146  TRUE      53  172099728
## 79  6674859295 1658604114  TRUE      53   94738847
## 80  6675282069 1658635714 FALSE      53  250544263
## 81  6675316855 1658638257 FALSE      53  209165399
## 82  6675382529 1658643230  TRUE      53  110142194
## 83  6675511055 1658649423 FALSE      53  247018516
## 84  6675654131 1658656280  TRUE      53  458287006
## 85  6675690081 1658658166  TRUE      53  101240880
## 86  6675726202 1658660380  TRUE      53  118134220
## 87  6676013166 1658671587 FALSE      53  130801103
## 88  6676023312 1658671904 FALSE      53  224981103
## 89  6676306088 1658683321  TRUE      53  172099728
## 90  6676358917 1658685847  TRUE      53 1414344010
## 91  6676488286 1658692615  TRUE      53  118134220
## 92  6676495794 1658693040  TRUE      53  202464276
## 93  6676555616 1658696390  TRUE      53  131303632
## 94  6676612110 1658700154 FALSE      53   41637292
## 95  6676724543 1658710926 FALSE      53  206379758
## 96  6676767202 1658715326 FALSE      53   96189126
## 97  6676953324 1658732736  TRUE      53  375507918
## 98  6677121365 1658743996 FALSE      53 1201927095
## 99  6678322841 1658815578  TRUE      53 1045494300
## 100 6678463629 1658826027  TRUE      53  156328257
## 101 6678511020 1658828928  TRUE      53 1102532597
## 102 6679342655 1658866858  TRUE      53  339235645
## 103 6679573632 1658887212  TRUE      53  339235645
## 104 6679657576 1658894888  TRUE      53  181339278
## 105 6679802770 1658907260  TRUE      53  130271765
## 106 6679903505 1658913978  TRUE      53  101240880
## 107 6680017014 1658920526 FALSE      53  285770518
## 108 6680145522 1658926655  TRUE      53   27178898
## 109 6680665928 1658948874  TRUE      53   41637292
## 110 6680878849 1658962972 FALSE      53  191362875
## 111 6680993748 1658975129 FALSE      53   94004717
## 112 6681062357 1658981652 FALSE      53  198631761
## 113 6681156803 1658990009 FALSE      53  164948665
## 114 6681230217 1658995283 FALSE      53  250547284
## 115 6681277222 1658998409 FALSE      53  177061558
## 116 6681396813 1659005681 FALSE      53 1125296112
## 117 6681478657 1659009834 FALSE      53  250547284
## 118 6681513765 1659011683  TRUE      53  152455523
## 119 6681867862 1659025385  TRUE      53   85367644
## 120 6681976191 1659030407  TRUE      53   98482878
## 121 6682066040 1659035217  TRUE      53   41637292
## 122 6682250660 1659044952  TRUE      53  126212866
## 123 6682277285 1659046216  TRUE      53  202464276
## 124 6682300306 1659047994  TRUE      53  191362875
## 125 6682481806 1659066192  TRUE      53  108452107
## 126 6682492162 1659067359 FALSE      53  353424606
## 127 6682513240 1659069183 FALSE      53  191362875
## 128 6682553675 1659072704  TRUE      53 1263892079
## 129 6682582314 1659075095  TRUE      53  177061558
## 130 6682601132 1659076596 FALSE      53  254710104
## 131 6682735589 1659085662 FALSE      53  345509021
## 132 6682792018 1659089174  TRUE      53  853928083
## 133 6682841003 1659092421  TRUE      53  399885807
## 134 6682857406 1659092750  TRUE      53  177061558
## 135 6682944177 1659096969 FALSE      53  287966898
## 136 6682960303 1659097801 FALSE      53  177411785
## 137 6683081104 1659102476  TRUE      53  177411785
## 138 6683177335 1659106632 FALSE      53   97590558
## 139 6683374759 1659114015 FALSE      53  177411785
## 140 6683769334 1659134442  TRUE      53  126212866
## 141 6683806784 1659138256  TRUE      53  126212866
## 142 6683976570 1659152412  TRUE      53  392277500
## 143 6684012176 1659155203 FALSE      53   86822085
## 144 6684155347 1659164477  TRUE      53   99573176
## 145 6684418241 1659178291  TRUE      53  162641196
## 146 6685073736 1659204694  TRUE      53  194521913
## 147 6685285522 1659215590  TRUE      53  191290281
## 148 6685452046 1659228631 FALSE      53   97658618
## 149 6685493723 1659232558 FALSE      53  126212866
## 150 6685545862 1659236652 FALSE      53  108452107
## 151 6685585153 1659239647  TRUE      53  247018516
## 152 6685749437 1659250478  TRUE      53  171337923
## 153 6685766100 1659251102 FALSE      53  176616331
## 154 6685893938 1659257496 FALSE      53  171337923
## 155 6686015749 1659263220  TRUE      53  458287006
## 156 6686253875 1659272730 FALSE      53 1247302157
## 157 6687078267 1659310579 FALSE      53  194521913
## 158 6687280868 1659330212  TRUE      53  162641196
## 159 6687357426 1659336758 FALSE      53  101240880
## 160 6687371700 1659337808  TRUE      53  113874152
## 161 6687466269 1659344230  TRUE      53  104504914
## 162 6687780002 1659359538 FALSE      53  355168766
## 163 6687956200 1659366013  TRUE      53   97590558
## 164 6688230252 1659376799  TRUE      53  177411785
## 165 6688311075 1659380419  TRUE      53  177411785
## 166 6688750995 1659414127 FALSE      53  209165399
## 167 6688926370 1659428275  TRUE      53  236322966
## 168 6689092149 1659438187 FALSE      53  104504914
## 169 6689161876 1659441607 FALSE      53   97590558
## 170 6689257142 1659445538  TRUE      53  171097887
## 171 6689476142 1659453490  TRUE      53  171097887
## 172 6690270489 1659506663 FALSE      53  162126721
## 173 6690566627 1659525943  TRUE      53  359068880
## 174 6690777541 1659535109  TRUE      53  177411785
## 175 6690857847 1659537994  TRUE      53 1050127319
## 176 6690905555 1659539714 FALSE      53 1050127319
## 177 6691619784 1659588724 FALSE      53 1200963940
## 178 6691624668 1659589154  TRUE      53  104504914
## 179 6691811669 1659603583 FALSE      53 1247011173
## 180 6691866371 1659607073  TRUE      53  219950601
## 181 6691953594 1659612048 FALSE      53  181339278
## 182 6692323200 1659627816  TRUE      53  387624608
## 183 6692728978 1659650902  TRUE      53   96189126
## 184 6692938319 1659672202 FALSE      53 1133046148
## 185 6692942529 1659672614  TRUE      53 1232094007
## 186 6692978872 1659675650 FALSE      53  460481806
## 187 6693057839 1659682484 FALSE      53 1119908658
## 188 6693722540 1659715425  TRUE      53  146744261
## 189 6693777829 1659717891 FALSE      53  237527926
## 190 6693934550 1659725523 FALSE      53  237527926
## 191 6694523000 1659769569  TRUE      53  110142194
## 192 6694583595 1659773285  TRUE      53 1068676453
## 193 6694689276 1659778583 FALSE      53 1220632105
## 194 6694863732 1659787175  TRUE      53  345509021
## 195 6696003659 1659850638 FALSE      53  138885864
## 196 6696012766 1659851203  TRUE      53  138885864
## 197 6696170524 1659859859  TRUE      53   27178898
## 198 6696706878 1659882037  TRUE      53 1251319262
##                                            leaguename gold_per_min net_worth
## 1                                 Titus Colosseum Cup          569     15770
## 2                                      Destiny league          549     15317
## 3                              Ultras Dota Pro League          654     16985
## 4                                 Titus Colosseum Cup          472     10399
## 5            Dota 2 Champions League 2021-2022 Season          894     40098
## 6                          Thunderpick Bitcoin Series          593     21647
## 7                          Thunderpick Bitcoin Series          442     11580
## 8                              Ultras Dota Pro League          434      9842
## 9            Dota 2 Champions League 2021-2022 Season          701     26720
## 10                                     Destiny league          752     24276
## 11                                  Asia Battle Arena          810     22700
## 12                                     Destiny league          483     15927
## 13             Meister Provider Dota 2 Indonesia 2022          676     10746
## 14                                  Asia Battle Arena          902     41321
## 15                                  Asia Battle Arena          721     18818
## 16                     TopClans2022 Summer Invitional          512     14369
## 17                                     Destiny league          412     12732
## 18                         Thunderpick Bitcoin Series          848     34584
## 19                         Thunderpick Bitcoin Series          559     15112
## 20                     Khaz Modan Cup Series Season 3          290      6108
## 21                                  Asia Battle Arena          485      9739
## 22                                  Asia Battle Arena          655     13060
## 23                     Khaz Modan Cup Series Season 3          453     11319
## 24                                Titus Colosseum Cup          689     26345
## 25                     TopClans2022 Summer Invitional          646     22515
## 26             Meister Provider Dota 2 Indonesia 2022          560     10202
## 27                                Titus Colosseum Cup          643     13318
## 28  ESL One Malaysia 2022 Qualifiers powered by Intel          677     21088
## 29  ESL One Malaysia 2022 Qualifiers powered by Intel          785     29306
## 30                                     Destiny league          588     16788
## 31                                     Destiny league          672     18876
## 32                                     Destiny league          613     16366
## 33  ESL One Malaysia 2022 Qualifiers powered by Intel          808     27482
## 34  ESL One Malaysia 2022 Qualifiers powered by Intel          785     33771
## 35  ESL One Malaysia 2022 Qualifiers powered by Intel          442      8841
## 36  ESL One Malaysia 2022 Qualifiers powered by Intel          723     26798
## 37                                Titus Colosseum Cup          580     13682
## 38                     Khaz Modan Cup Series Season 3          805     27183
## 39           Dota 2 Champions League 2021-2022 Season          626     19381
## 40               TodayPay Dota2 Invitational Season 1          480     12344
## 41  ESL One Malaysia 2022 Qualifiers powered by Intel          603     21299
## 42                                     Destiny league          533     16905
## 43                                Titus Colosseum Cup          455      8771
## 44                                     Asian Tigers 3          683     22392
## 45  ESL One Malaysia 2022 Qualifiers powered by Intel          777     29024
## 46  ESL One Malaysia 2022 Qualifiers powered by Intel          625     23730
## 47                                     Destiny league          578     20852
## 48                          Riyadh Masters by Gamers8          623     17109
## 49                          Riyadh Masters by Gamers8          624     21470
## 50                          Riyadh Masters by Gamers8          650     22308
## 51               TodayPay Dota2 Invitational Season 1          559     16363
## 52  ESL One Malaysia 2022 Qualifiers powered by Intel          820     17797
## 53                     Khaz Modan Cup Series Season 3          680     18160
## 54                          Riyadh Masters by Gamers8          699     18374
## 55                          Riyadh Masters by Gamers8          605     14825
## 56  ESL One Malaysia 2022 Qualifiers powered by Intel          773     21653
## 57                          Riyadh Masters by Gamers8          821     29931
## 58                          Riyadh Masters by Gamers8          543     15297
## 59                                  Asia Battle Arena          630     16061
## 60                                  Asia Battle Arena          584     11481
## 61                                     Asian Tigers 3          714     27986
## 62                                     Asian Tigers 3          756     22428
## 63                                     Asian Tigers 3          487     10988
## 64                          Riyadh Masters by Gamers8          441     11678
## 65                          Riyadh Masters by Gamers8          856     39130
## 66                          Riyadh Masters by Gamers8          560     13953
## 67                                     Asian Tigers 3          128      7808
## 68                                     Asian Tigers 3          442     13145
## 69                                     Destiny league          477     17071
## 70                     Khaz Modan Cup Series Season 3          493     11153
## 71                     Khaz Modan Cup Series Season 3          764     26141
## 72                                Titus Colosseum Cup          725     21680
## 73                                  Asia Battle Arena          672     23782
## 74                       European Pro League Season 2          539     17176
## 75                          Riyadh Masters by Gamers8          533     15389
## 76                             Ultras Dota Pro League          415      8209
## 77                          Riyadh Masters by Gamers8          659     28956
## 78  ESL One Malaysia 2022 Qualifiers powered by Intel          748     22375
## 79                          Riyadh Masters by Gamers8          617     19469
## 80                                  Asia Battle Arena          488     10869
## 81                                  Asia Battle Arena          554     13675
## 82                                     Asian Tigers 3          807     28348
## 83                                  Asia Battle Arena          552     13818
## 84                                     Asian Tigers 3          829     34622
## 85                     Khaz Modan Cup Series Season 3          581     13529
## 86                          Riyadh Masters by Gamers8          592     12822
## 87                       European Pro League Season 2          592     22001
## 88                  YouTube SIVVIT - Big Russian Show          577     18847
## 89  ESL One Malaysia 2022 Qualifiers powered by Intel          815     27546
## 90                             Ultras Dota Pro League          696     21007
## 91                          Riyadh Masters by Gamers8          639     19413
## 92               TodayPay Dota2 Invitational Season 1          948     32455
## 93               TodayPay Dota2 Invitational Season 1          729     27526
## 94                         Thunderpick Bitcoin Series          629     23854
## 95  ESL One Malaysia 2022 Qualifiers powered by Intel          657     19874
## 96  ESL One Malaysia 2022 Qualifiers powered by Intel          573     19265
## 97  ESL One Malaysia 2022 Qualifiers powered by Intel          813     25736
## 98                     Khaz Modan Cup Series Season 3          583     16306
## 99                                     Destiny league          644     21768
## 100 ESL One Malaysia 2022 Qualifiers powered by Intel          678     23821
## 101                    Khaz Modan Cup Series Season 3          671     13065
## 102 ESL One Malaysia 2022 Qualifiers powered by Intel          818     21247
## 103 ESL One Malaysia 2022 Qualifiers powered by Intel          759     22575
## 104                                    Destiny league          802     25566
## 105                                    Victorious Cup          756     17329
## 106                    Khaz Modan Cup Series Season 3          616     18119
## 107                    Khaz Modan Cup Series Season 3          694     21558
## 108          Dota 2 Champions League 2021-2022 Season          624     17438
## 109              TodayPay Dota2 Invitational Season 1          641     18588
## 110              TodayPay Dota2 Invitational Season 1          632     21294
## 111                        Thunderpick Bitcoin Series          832     25164
## 112                                    Victorious Cup          828     31286
## 113 ESL One Malaysia 2022 Qualifiers powered by Intel          938     37910
## 114 ESL One Malaysia 2022 Qualifiers powered by Intel          521     15167
## 115                    Khaz Modan Cup Series Season 3          698     24601
## 116                                    Asian Tigers 3          651     21659
## 117 ESL One Malaysia 2022 Qualifiers powered by Intel          780     29327
## 118 ESL One Malaysia 2022 Qualifiers powered by Intel          701     25954
## 119                        Thunderpick Bitcoin Series          842     32565
## 120     Соревнования по электронным играм «Е-динство»          558      8796
## 121              TodayPay Dota2 Invitational Season 1          495      6596
## 122 ESL One Malaysia 2022 Qualifiers powered by Intel          594     12938
## 123              TodayPay Dota2 Invitational Season 1          663     16437
## 124 ESL One Malaysia 2022 Qualifiers powered by Intel          714     32550
## 125 ESL One Malaysia 2022 Qualifiers powered by Intel          692     13663
## 126                                    Asian Tigers 3          777     30697
## 127 ESL One Malaysia 2022 Qualifiers powered by Intel          451     11790
## 128                                    Victorious Cup          915     23816
## 129                    Khaz Modan Cup Series Season 3          668     11438
## 130 ESL One Malaysia 2022 Qualifiers powered by Intel          332      7461
## 131                      European Pro League Season 2          712     23657
## 132                                    Victorious Cup          801     20299
## 133 ESL One Malaysia 2022 Qualifiers powered by Intel          660     28349
## 134                                 Asia Battle Arena          838     34085
## 135                                 Asia Battle Arena          833     38657
## 136 ESL One Malaysia 2022 Qualifiers powered by Intel          729     26248
## 137 ESL One Malaysia 2022 Qualifiers powered by Intel          856     37695
## 138 ESL One Malaysia 2022 Qualifiers powered by Intel          540     19767
## 139 ESL One Malaysia 2022 Qualifiers powered by Intel          610     20269
## 140 ESL One Malaysia 2022 Qualifiers powered by Intel          638     19220
## 141 ESL One Malaysia 2022 Qualifiers powered by Intel          621     18086
## 142                      Titus Colosseum Cup Season 2          727     23482
## 143 ESL One Malaysia 2022 Qualifiers powered by Intel          887     33534
## 144                      Titus Colosseum Cup Season 2          374      7934
## 145                                    Asian Tigers 3          764     31745
## 146              TodayPay Dota2 Invitational Season 1          631     18752
## 147              TodayPay Dota2 Invitational Season 1          689     20105
## 148 ESL One Malaysia 2022 Qualifiers powered by Intel          618     19472
## 149 ESL One Malaysia 2022 Qualifiers powered by Intel          542     17019
## 150 ESL One Malaysia 2022 Qualifiers powered by Intel          596     18836
## 151                      Titus Colosseum Cup Season 2          672     14803
## 152                                    Asian Tigers 3          561     15597
## 153                                    Victorious Cup          871     42703
## 154                                    Asian Tigers 3          492     10583
## 155                                    Asian Tigers 3          586     11254
## 156                            Ultras Dota Pro League          456      9801
## 157              TodayPay Dota2 Invitational Season 1          641     19453
## 158                                    Asian Tigers 3          648     15604
## 159                    Khaz Modan Cup Series Season 3          655     26640
## 160                                    Asian Tigers 3          563     15107
## 161                                    Asian Tigers 3          592     23429
## 162                                 Asia Battle Arena          824     35087
## 163          Dota 2 Champions League 2022-2023 Season          645     18321
## 164          Dota 2 Champions League 2022-2023 Season          775     24101
## 165          Dota 2 Champions League 2022-2023 Season          932     41313
## 166                      Titus Colosseum Cup Season 2          513     14353
## 167                                 Asia Battle Arena          751     28538
## 168                    Khaz Modan Cup Series Season 3          475     15255
## 169          Dota 2 Champions League 2022-2023 Season          570     15176
## 170          Dota 2 Champions League 2022-2023 Season          598     13113
## 171          Dota 2 Champions League 2022-2023 Season          678     16985
## 172                                          MInd Cup          877     31353
## 173                                          MInd Cup          831     23908
## 174          Dota 2 Champions League 2022-2023 Season          924     41743
## 175                            Ultras Dota Pro League          134      7790
## 176                            Ultras Dota Pro League          624     19285
## 177                                    Destiny league          664     19179
## 178                                    Asian Tigers 3          525     18069
## 179                                    Destiny league          591     15327
## 180                                          MInd Cup          919     41418
## 181                                    Destiny league          640     18230
## 182                                       Summer wave          777     22294
## 183              TodayPay Dota2 Invitational Season 1          516      9095
## 184                                    Destiny league          120      6875
## 185                                    Destiny league          659     19467
## 186                      Titus Colosseum Cup Season 2          432      8934
## 187                                    Destiny league          667     19039
## 188                            Predator Energy League          706     18046
## 189                                       Summer wave          722     24314
## 190                                       Summer wave          876     41076
## 191                                 Asia Battle Arena          792     16988
## 192                                    Destiny league          518     13922
## 193                                    Destiny league          431     12950
## 194                      European Pro League Season 2          838     30875
## 195                      Titus Colosseum Cup Season 2          129      7910
## 196                      Titus Colosseum Cup Season 2          567     11645
## 197                      European Pro League Season 2          612     15363
## 198                            Ultras Dota Pro League          760     27049
##     gold kills tower_damage duration lane lane_role
## 1   1140     3         1221     1754    1         3
## 2    892     3         1559     1794    1         3
## 3   3210    10        19823     1547    3         3
## 4    449     4         1264     1319    1         1
## 5   9648    10        28469     2938    3         1
## 6   6622     5        10388     2435    3         1
## 7   2130     0        11727     1846    1         1
## 8   1192     0         6657     1315    3         3
## 9   4570     9        18541     2332    1         3
## 10  2576    13         9039     2263    3         3
## 11  2700    10         7885     1701    3         1
## 12  2377     1        18953     1957    3         3
## 13  1641    11         9049      934    3         1
## 14  3596    16        11169     3349    1         1
## 15  2243    22         4646     1533    3         1
## 16  1144     4         2475     1891    1         3
## 17  4407     0         8942     2002    3         3
## 18  5309    18        14206     2977    3         3
## 19  2687     4        16908     1879    1         3
## 20   958     2            0     1297    5         4
## 21  1364     4          645     1215    3         3
## 22  3310     9        16466     1182    1         1
## 23   269     1          446     1613    1         1
## 24  2020    10        23219     2473    3         3
## 25  1215     5         5258     2200    1         1
## 26  2447     3         2572     1040    3         1
## 27  1968     4        14306     1363    3         1
## 28  1338     8         8893     1938    3         3
## 29  1281     8        23614     2310    3         1
## 30  1063     5         3822     1838    3         1
## 31   651    11         9172     1672    1         1
## 32  2316     6         8085     1641    3         1
## 33  3307     6        14326     2083    3         1
## 34  1721     7        16460     2207    1         1
## 35   716     2         1474     1240    1         1
## 36  1273     9        15706     2278    1         1
## 37  1082     4         8754     1373    1         1
## 38  4008    14        13960     2142    3         1
## 39  1831     3         7552     1989    1         1
## 40   444     1          292     1706    1         1
## 41  3074     3         6085     2230    1         1
## 42   905     2        10574     2045    3         3
## 43   521     8          714     1352    3         1
## 44  3417     5        10921     2063    3         1
## 45   424     8        10553     2947    3         1
## 46  1605     4          855     2425    1         1
## 47  1277     8        12741     2516    2         2
## 48  3959     9         9364     1740    1         3
## 49  2970     2        14037     2417    1         3
## 50  3333     8         8823     2088    3         1
## 51  1238     3         2867     1851    1         1
## 52  1372    13        19135     1279    3         1
## 53  2310     6         5307     1686    3         1
## 54  3619     9        19038     1795    1         3
## 55  1320     3        22129     1464    1         3
## 56  1628    11        13320     1692    2         2
## 57  3881    11        16538     2190    2         2
## 58  2167     4        15882     1717    1         3
## 59  4486     8         5379     1614    1         1
## 60     1     4         1336     1527    1         1
## 61   581     4        16584     2752    1         3
## 62  1653    12        16618     1893    1         1
## 63  1183     1         2294     1420    1         1
## 64   578     2         3675     1735    1         1
## 65  5955    15        17614     3023    3         1
## 66  1428     2         6477     1710    1         1
## 67  7708     0            0     3312    2         2
## 68   365     1         5661     1890    3         3
## 69  2346     2         6867     2246    4         4
## 70  1278     3         5387     1436    3         3
## 71  1891    11         5526     2140    3         1
## 72  2830    11        10106     1801    1         1
## 73   957     7        13573     2647    3         1
## 74   976     2         2289     2203    3         1
## 75   389     3         8324     2066    1         3
## 76  2084     2        12088     1370    3         1
## 77  3881     8         8553     3038    3         3
## 78  3875     7        14898     1840    3         1
## 79  2039     6        16105     2172    3         3
## 80   569     6         2906     1610    1         3
## 81  1945     2         6836     1528    3         1
## 82  2148     7         8907     2252    3         1
## 83  1743     1         7569     1586    1         3
## 84  3297    12        22697     3626    3         1
## 85  2599     4        10267     1392    3         3
## 86  3747     8         9246     1278    3         3
## 87  2921     7        10944     2419    1         3
## 88  1647     5         4707     2108    1         1
## 89  4646     9        15996     2063    1         1
## 90  2907     1        13848     1882    3         1
## 91  2038     6        21849     1824    1         3
## 92  5705    20        18030     2054    1         1
## 93  3001     9        15313     2421    2         2
## 94  5229     4        15903     2505    3         3
## 95   349     6         2622     1909    3         1
## 96  1165     3         5936     2182    1         1
## 97  2436    13        19047     1962    1         1
## 98   456     5         3334     1997    1         1
## 99  4268     4        17180     2157    2         2
## 100 2671     7        12074     2156    3         3
## 101 1340     9         3208     1112    3         1
## 102 1747    17        21430     1651    2         2
## 103 1275    12        18738     1819    1         1
## 104 2741     8        10292     1963    3         1
## 105 2079    12        12953     1382    3         1
## 106 1369     7        11799     1818    1         3
## 107 1033     2         4663     2162    3         1
## 108 1438     9         9004     1760    1         3
## 109 1108    10         7872     1850    3         3
## 110  394     2         7872     2373    3         3
## 111  589     8         8530     2606    1         1
## 112 4386    11        16196     2404    1         3
## 113 1885     9        15057     3504    1         1
## 114  317     2         3777     1893    1         1
## 115 1126     8         6679     2563    3         1
## 116  929    11         5158     2305    3         1
## 117 1302     5        11033     2633    1         1
## 118 2204     9        13424     2271    1         1
## 119 8615    11        16681     2405    3         1
## 120 1596     8         1858      909    3         3
## 121  746     5         1492      800    3         3
## 122  713    12         6280     1336    3         1
## 123 1262     9         4386     1464    1         1
## 124 5445     2        25778     2869    1         3
## 125 1113    10         9585     1173    1         1
## 126 1722     5         8765     2484    3         1
## 127  440     2         3163     2032    1         1
## 128  641    20        14435     1596    3         1
## 129  988     5         3828     1029    3         1
## 130  361     2          582     1359    1         1
## 131  482    11         7827     2092    3         1
## 132 1474     8        18974     1534    1         1
## 133  899     6        27940     2647    2         2
## 134 5235    12        27195     2690    3         1
## 135 4957     8        13468     3477    3         1
## 136 1498     8         6838     2558    3         3
## 137 7820     3        15473     2722    1         3
## 138 1217     0         7462     2249    3         3
## 139 1589     2         5696     2365    3         1
## 140 1945     8         9261     1859    1         3
## 141 2086     4        20104     1782    3         3
## 142  457     7        15069     2264    3         1
## 143 2359    10        14573     2954    1         3
## 144 1284     2         3822     1569    3         1
## 145  945    16         8973     2646    1         3
## 146 3502     5         9250     1834    1         3
## 147 2080     2        15605     1780    3         1
## 148 1872     3         2206     1923    1         1
## 149 1519     2         5704     2054    3         3
## 150  261     8         2933     2251    1         1
## 151 2053     9         3602     1311    3         3
## 152  392     7         9377     1737    3         3
## 153 3328    12        15210     3667    2         2
## 154 2233     6         1619     1414    3         3
## 155 1179     3         8821     1134    2         2
## 156  271     4          757     1451    3         3
## 157    3     7         4454     2452    1         3
## 158 1329    12         5930     1491    3         1
## 159  160     8         9129     2696    3         3
## 160 3357     7         3297     1676    1         3
## 161 5054     5        11500     2578    1         3
## 162 1512    12         9796     3144    1         1
## 163 3096     7        14920     1701    3         3
## 164 1576     4        14679     1896    3         1
## 165 6813    20        24147     2753    1         1
## 166  828     3         1670     1906    3         1
## 167 5113    10        16526     2272    3         1
## 168  575     5         6119     2178    1         3
## 169  726     5         4034     1671    1         3
## 170  963     6         7417     1285    3         1
## 171 2765     9         9096     1486    3         1
## 172 1853     9        18608     2650    3         1
## 173 3333    15        15104     1737    1         1
## 174 7768    10        22566     2924    3         3
## 175 7690     0            0     3157    2         2
## 176 3135     5         3411     1982    3         1
## 177  504     2         6142     1845    2         2
## 178 2089     3         4920     2154    1         3
## 179  552     5         2447     1616    2         2
## 180 4268    14        22774     3095    1         1
## 181 1930    11         4915     1789    3         1
## 182  769    13        13574     1793    1         1
## 183 1845     3         3897     1013    3         1
## 184 6775     0            0     3085    2         2
## 185 1192     1        12197     1851    2         2
## 186 1809     6         1608     1378    3         3
## 187  539     4         6142     1782    2         2
## 188 3421    13        10925     1574    1         1
## 189 2189     6        12682     2140    3         1
## 190 2451    15        15030     3254    1         1
## 191  913    12         7430     1278    1         1
## 192 1522     1        10554     1636    2         2
## 193  875     2         8594     2040    3         3
## 194 2100    10        22006     2577    3         1
## 195 7810     0            0     3345    2         2
## 196 1815     6         6639     1214    3         3
## 197 4888     4        14661     1643    3         3
## 198 2274     7        19911     2301    1         1
#npData$account_id <- as.factor(npData$account_id)

#npData$match_id <- as.factor(npData$match_id)

# gold      <- "gold"       %in% charvec
#   gold_per_min    <- "gold_per_min"     %in% charvec
#   kills <- "kills"  %in% charvec
#   tower_damage <- "tower_damage" %in% charvec
#   duration <- "duration" %in% charvec
#   lane <- "lane" %in% charvec
#   lane_role <- "lane_role" %in% charvec
#   net_worth <- "net_worth" %in% charvec

EDA

varname <- "gold"

varHist <- ggplot(npData, aes(x=gold)) +  
  theme_bw() +                                                                     #Set classic bw plot theme
  geom_histogram(color="black", fill = "#34495E", alpha = 0.8, binwidth = 100) +   #Color options, binwidth set to 100 shares
  labs(x = "Win Or Lose", y = "Count", title = paste0("Counts of ", varname))                                  

varHist

boxPlot <- ggplot(npData, aes(x=win,y=gold), fill = win) +  
  geom_hline(yintercept = median(npData$gold), size = 0.8) +             #Add line for overall median shares
  geom_point(size = 0.8) +                                                  #Add points
  geom_boxplot(lwd = 0.5, width = 0.5, outlier.size = 0.8, alpha = 0.7) +   #Create boxplot
  xlab("") + ylab("Amount of Gold") +                                             #Label axis
  #theme(legend.position = "none") +                                         #Remove legend
  ggtitle(paste0("Wins vs ", varname))

#Display plot
boxPlot

  ggplot(npData, aes(x = gold, y = net_worth, color = win, fill = win) ) +
          geom_jitter() +          
          scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
          scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
          labs(title = paste0("Networth over Gold"))+
          theme_classic()

# Numerical Summaries

npData
## # A tibble: 198 × 14
##      match_id start_time win   hero_id account_id leaguename        gold_per_min
##         <dbl>      <dbl> <lgl>   <dbl>      <dbl> <chr>                    <dbl>
##  1 6653637943 1657339909 FALSE      53  250544263 Titus Colosseum …          569
##  2 6653911377 1657357190 FALSE      53   76082922 Destiny league             549
##  3 6654090150 1657366555 TRUE       53 1312740399 Ultras Dota Pro …          654
##  4 6656721191 1657512120 FALSE      53  209165399 Titus Colosseum …          472
##  5 6657130147 1657540852 TRUE       53  132851371 Dota 2 Champions…          894
##  6 6657972166 1657586272 TRUE       53   59752811 Thunderpick Bitc…          593
##  7 6658023411 1657591653 FALSE      53   85367644 Thunderpick Bitc…          442
##  8 6658141348 1657602904 FALSE      53 1312740399 Ultras Dota Pro …          434
##  9 6658904045 1657643886 TRUE       53  155370747 Dota 2 Champions…          701
## 10 6659582275 1657695291 TRUE       53  412910858 Destiny league             752
## # ℹ 188 more rows
## # ℹ 7 more variables: net_worth <dbl>, gold <dbl>, kills <dbl>,
## #   tower_damage <dbl>, duration <dbl>, lane <dbl>, lane_role <dbl>
playerMatches <- npData %>% select(win,account_id,match_id)
  
playerMatches
## # A tibble: 198 × 3
##    win   account_id   match_id
##    <lgl>      <dbl>      <dbl>
##  1 FALSE  250544263 6653637943
##  2 FALSE   76082922 6653911377
##  3 TRUE  1312740399 6654090150
##  4 FALSE  209165399 6656721191
##  5 TRUE   132851371 6657130147
##  6 TRUE    59752811 6657972166
##  7 FALSE   85367644 6658023411
##  8 FALSE 1312740399 6658141348
##  9 TRUE   155370747 6658904045
## 10 TRUE   412910858 6659582275
## # ℹ 188 more rows
playerWins <- select(playerMatches, -match_id)

#Total Wins by a Player
playerWins <- playerWins %>% group_by(account_id) %>% summarise(TotalWins = sum(win==1))

playerWins <- as.data.table(playerWins)

#Total Matches by a Player

playerMatches <- as.data.table(playerMatches)
playerMatches[, .(rowCount =.N), by=account_id]
##      account_id rowCount
##   1:  250544263        2
##   2:   76082922        1
##   3: 1312740399        2
##   4:  209165399        3
##   5:  132851371        1
##  ---                    
## 116:  237527926        2
## 117: 1068676453        1
## 118: 1220632105        1
## 119:  138885864        2
## 120: 1251319262        1
playerMatches <- as.data.table(playerMatches)
sumMatches <- playerMatches[, .(totalMatches =.N), by=account_id]

sumMatches <- playerMatches[sumMatches, on =.(account_id =account_id)]

sumMatches
##        win account_id   match_id totalMatches
##   1: FALSE  250544263 6653637943            2
##   2: FALSE  250544263 6675282069            2
##   3: FALSE   76082922 6653911377            1
##   4:  TRUE 1312740399 6654090150            2
##   5: FALSE 1312740399 6658141348            2
##  ---                                         
## 194:  TRUE 1068676453 6694583595            1
## 195: FALSE 1220632105 6694689276            1
## 196: FALSE  138885864 6696003659            2
## 197:  TRUE  138885864 6696012766            2
## 198:  TRUE 1251319262 6696706878            1
#Player Frame comprising of the player matches and wins
playerFrame <- sumMatches[playerWins, on =.(account_id =account_id)]

playerFrame <- playerFrame %>% mutate(win_perc = TotalWins/totalMatches) %>% arrange(desc(totalMatches))

playerFrame
##        win account_id   match_id totalMatches TotalWins  win_perc
##   1: FALSE  177411785 6670287920            9         6 0.6666667
##   2:  TRUE  177411785 6671588490            9         6 0.6666667
##   3:  TRUE  177411785 6671680767            9         6 0.6666667
##   4: FALSE  177411785 6682960303            9         6 0.6666667
##   5:  TRUE  177411785 6683081104            9         6 0.6666667
##  ---                                                             
## 194:  TRUE 1251319262 6696706878            1         1 1.0000000
## 195:  TRUE 1261414529 6664870055            1         1 1.0000000
## 196:  TRUE 1263892079 6682553675            1         1 1.0000000
## 197:  TRUE 1352080796 6674752354            1         1 1.0000000
## 198:  TRUE 1414344010 6676358917            1         1 1.0000000
#League Matches
leagueMatches <- npData %>% select(win,account_id,match_id,leaguename)
leagueMatches <- as.data.table(leagueMatches)
sumLeagueMatches <- leagueMatches[,.(totalLeagueMatches =.N), by=leaguename]

leagueMatches
##        win account_id   match_id                               leaguename
##   1: FALSE  250544263 6653637943                      Titus Colosseum Cup
##   2: FALSE   76082922 6653911377                           Destiny league
##   3:  TRUE 1312740399 6654090150                   Ultras Dota Pro League
##   4: FALSE  209165399 6656721191                      Titus Colosseum Cup
##   5:  TRUE  132851371 6657130147 Dota 2 Champions League 2021-2022 Season
##  ---                                                                     
## 194:  TRUE  345509021 6694863732             European Pro League Season 2
## 195: FALSE  138885864 6696003659             Titus Colosseum Cup Season 2
## 196:  TRUE  138885864 6696012766             Titus Colosseum Cup Season 2
## 197:  TRUE   27178898 6696170524             European Pro League Season 2
## 198:  TRUE 1251319262 6696706878                   Ultras Dota Pro League
#League Wins
leagueWins <- leagueMatches %>% group_by(leaguename) %>% summarise(leagueTotalWins = sum(win==1))
leagueWins
## # A tibble: 22 × 2
##    leaguename                                        leagueTotalWins
##    <chr>                                                       <int>
##  1 Asia Battle Arena                                               8
##  2 Asian Tigers 3                                                 11
##  3 Destiny league                                                 10
##  4 Dota 2 Champions League 2021-2022 Season                        3
##  5 Dota 2 Champions League 2022-2023 Season                        6
##  6 ESL One Malaysia 2022 Qualifiers powered by Intel              21
##  7 European Pro League Season 2                                    2
##  8 Khaz Modan Cup Series Season 3                                  5
##  9 MInd Cup                                                        2
## 10 Meister Provider Dota 2 Indonesia 2022                          2
## # ℹ 12 more rows
leagueWins <- as.data.table(leagueWins)

#League Frame
leagueFrame <- sumLeagueMatches[leagueWins, on =.(leaguename =leaguename)]

leagueFrame <- leagueFrame %>% mutate(win_perc = leagueTotalWins/totalLeagueMatches) %>% arrange(desc(totalLeagueMatches))


#Numerical

avgTable <- table(avgKills=round(mean(npData$kills),digits=2),avgDur=round(mean(npData$duration),digits=2),avgGPM=round(mean(npData$gold_per_min),digits=2),avgNetWorth = round(mean(npData$net_worth),digits=2), avgTowerDmg=round(mean(npData$tower_damage),digits=2))
avgTable
## , , avgGPM = 644.11, avgNetWorth = 20283.04, avgTowerDmg = 10066.26
## 
##         avgDur
## avgKills 2011.8
##     6.75      1
kable(avgTable)
avgKills avgDur avgGPM avgNetWorth avgTowerDmg Freq
6.75 2011.8 644.11 20283.04 10066.26 1
#Categorical

#Create new categorical variables to classify game as high/normal/low GPM, networth, tower damage, kills,duration
table(npData$win,npData$lane,npData$lane_role)
## , ,  = 1
## 
##        
##          1  2  3  4  5
##   FALSE 28  0 21  0  0
##   TRUE  23  0 38  0  0
## 
## , ,  = 2
## 
##        
##          1  2  3  4  5
##   FALSE  0  8  0  0  0
##   TRUE   0 10  0  0  0
## 
## , ,  = 3
## 
##        
##          1  2  3  4  5
##   FALSE 14  0 16  0  0
##   TRUE  17  0 21  0  0
## 
## , ,  = 4
## 
##        
##          1  2  3  4  5
##   FALSE  0  0  0  0  1
##   TRUE   0  0  0  1  0
#note that lane role determined by networth


categoryTable <- table(win=npData$win,lane=npData$lane,lanerole=npData$lane_role)
kable(categoryTable)
win lane lanerole Freq
FALSE 1 1 28
TRUE 1 1 23
FALSE 2 1 0
TRUE 2 1 0
FALSE 3 1 21
TRUE 3 1 38
FALSE 4 1 0
TRUE 4 1 0
FALSE 5 1 0
TRUE 5 1 0
FALSE 1 2 0
TRUE 1 2 0
FALSE 2 2 8
TRUE 2 2 10
FALSE 3 2 0
TRUE 3 2 0
FALSE 4 2 0
TRUE 4 2 0
FALSE 5 2 0
TRUE 5 2 0
FALSE 1 3 14
TRUE 1 3 17
FALSE 2 3 0
TRUE 2 3 0
FALSE 3 3 16
TRUE 3 3 21
FALSE 4 3 0
TRUE 4 3 0
FALSE 5 3 0
TRUE 5 3 0
FALSE 1 4 0
TRUE 1 4 0
FALSE 2 4 0
TRUE 2 4 0
FALSE 3 4 0
TRUE 3 4 0
FALSE 4 4 0
TRUE 4 4 1
FALSE 5 4 1
TRUE 5 4 0

#Modeling Page

Model Fitting

#Glm Fit # Change p = user input for proportion of data # Change var used to user input

#dataModel <- data.frame(npData)
#dataModel$win <- as.factor(dataModel$win)

if ("account_id" %in% colnames(dataModel)){
 dataModel <- subset(dataModel, select = -account_id) 
}

if ("match_id" %in% colnames(dataModel)){
dataModel <- subset(dataModel, select = -match_id)
}

if ("hero_id" %in% colnames(dataModel)){
dataModel <- subset(dataModel, select = -hero_id)
}

if ("leaguename" %in% colnames(dataModel)){
dataModel <- subset(dataModel, select = -leaguename)
}

if ("start_time" %in% colnames(dataModel)) {
  dataModel <- subset(dataModel, select = -start_time)
}
dataModel
##       win gold_per_min net_worth gold kills tower_damage duration lane
## 1   FALSE          569     15770 1140     3         1221     1754    1
## 2   FALSE          549     15317  892     3         1559     1794    1
## 3    TRUE          654     16985 3210    10        19823     1547    3
## 4   FALSE          472     10399  449     4         1264     1319    1
## 5    TRUE          894     40098 9648    10        28469     2938    3
## 6    TRUE          593     21647 6622     5        10388     2435    3
## 7   FALSE          442     11580 2130     0        11727     1846    1
## 8   FALSE          434      9842 1192     0         6657     1315    3
## 9    TRUE          701     26720 4570     9        18541     2332    1
## 10   TRUE          752     24276 2576    13         9039     2263    3
## 11   TRUE          810     22700 2700    10         7885     1701    3
## 12   TRUE          483     15927 2377     1        18953     1957    3
## 13   TRUE          676     10746 1641    11         9049      934    3
## 14  FALSE          902     41321 3596    16        11169     3349    1
## 15   TRUE          721     18818 2243    22         4646     1533    3
## 16  FALSE          512     14369 1144     4         2475     1891    1
## 17   TRUE          412     12732 4407     0         8942     2002    3
## 18  FALSE          848     34584 5309    18        14206     2977    3
## 19   TRUE          559     15112 2687     4        16908     1879    1
## 20  FALSE          290      6108  958     2            0     1297    5
## 21   TRUE          485      9739 1364     4          645     1215    3
## 22   TRUE          655     13060 3310     9        16466     1182    1
## 23  FALSE          453     11319  269     1          446     1613    1
## 24   TRUE          689     26345 2020    10        23219     2473    3
## 25  FALSE          646     22515 1215     5         5258     2200    1
## 26   TRUE          560     10202 2447     3         2572     1040    3
## 27   TRUE          643     13318 1968     4        14306     1363    3
## 28   TRUE          677     21088 1338     8         8893     1938    3
## 29   TRUE          785     29306 1281     8        23614     2310    3
## 30  FALSE          588     16788 1063     5         3822     1838    3
## 31   TRUE          672     18876  651    11         9172     1672    1
## 32   TRUE          613     16366 2316     6         8085     1641    3
## 33   TRUE          808     27482 3307     6        14326     2083    3
## 34   TRUE          785     33771 1721     7        16460     2207    1
## 35  FALSE          442      8841  716     2         1474     1240    1
## 36   TRUE          723     26798 1273     9        15706     2278    1
## 37   TRUE          580     13682 1082     4         8754     1373    1
## 38   TRUE          805     27183 4008    14        13960     2142    3
## 39  FALSE          626     19381 1831     3         7552     1989    1
## 40  FALSE          480     12344  444     1          292     1706    1
## 41  FALSE          603     21299 3074     3         6085     2230    1
## 42  FALSE          533     16905  905     2        10574     2045    3
## 43  FALSE          455      8771  521     8          714     1352    3
## 44   TRUE          683     22392 3417     5        10921     2063    3
## 45  FALSE          777     29024  424     8        10553     2947    3
## 46  FALSE          625     23730 1605     4          855     2425    1
## 47  FALSE          578     20852 1277     8        12741     2516    2
## 48   TRUE          623     17109 3959     9         9364     1740    1
## 49  FALSE          624     21470 2970     2        14037     2417    1
## 50   TRUE          650     22308 3333     8         8823     2088    3
## 51  FALSE          559     16363 1238     3         2867     1851    1
## 52   TRUE          820     17797 1372    13        19135     1279    3
## 53  FALSE          680     18160 2310     6         5307     1686    3
## 54   TRUE          699     18374 3619     9        19038     1795    1
## 55   TRUE          605     14825 1320     3        22129     1464    1
## 56   TRUE          773     21653 1628    11        13320     1692    2
## 57   TRUE          821     29931 3881    11        16538     2190    2
## 58   TRUE          543     15297 2167     4        15882     1717    1
## 59   TRUE          630     16061 4486     8         5379     1614    1
## 60  FALSE          584     11481    1     4         1336     1527    1
## 61  FALSE          714     27986  581     4        16584     2752    1
## 62   TRUE          756     22428 1653    12        16618     1893    1
## 63  FALSE          487     10988 1183     1         2294     1420    1
## 64  FALSE          441     11678  578     2         3675     1735    1
## 65   TRUE          856     39130 5955    15        17614     3023    3
## 66  FALSE          560     13953 1428     2         6477     1710    1
## 67  FALSE          128      7808 7708     0            0     3312    2
## 68  FALSE          442     13145  365     1         5661     1890    3
## 69   TRUE          477     17071 2346     2         6867     2246    4
## 70  FALSE          493     11153 1278     3         5387     1436    3
## 71  FALSE          764     26141 1891    11         5526     2140    3
## 72   TRUE          725     21680 2830    11        10106     1801    1
## 73  FALSE          672     23782  957     7        13573     2647    3
## 74  FALSE          539     17176  976     2         2289     2203    3
## 75  FALSE          533     15389  389     3         8324     2066    1
## 76   TRUE          415      8209 2084     2        12088     1370    3
## 77  FALSE          659     28956 3881     8         8553     3038    3
## 78   TRUE          748     22375 3875     7        14898     1840    3
## 79   TRUE          617     19469 2039     6        16105     2172    3
## 80  FALSE          488     10869  569     6         2906     1610    1
## 81  FALSE          554     13675 1945     2         6836     1528    3
## 82   TRUE          807     28348 2148     7         8907     2252    3
## 83  FALSE          552     13818 1743     1         7569     1586    1
## 84   TRUE          829     34622 3297    12        22697     3626    3
## 85   TRUE          581     13529 2599     4        10267     1392    3
## 86   TRUE          592     12822 3747     8         9246     1278    3
## 87  FALSE          592     22001 2921     7        10944     2419    1
## 88  FALSE          577     18847 1647     5         4707     2108    1
## 89   TRUE          815     27546 4646     9        15996     2063    1
## 90   TRUE          696     21007 2907     1        13848     1882    3
## 91   TRUE          639     19413 2038     6        21849     1824    1
## 92   TRUE          948     32455 5705    20        18030     2054    1
## 93   TRUE          729     27526 3001     9        15313     2421    2
## 94  FALSE          629     23854 5229     4        15903     2505    3
## 95  FALSE          657     19874  349     6         2622     1909    3
## 96  FALSE          573     19265 1165     3         5936     2182    1
## 97   TRUE          813     25736 2436    13        19047     1962    1
## 98  FALSE          583     16306  456     5         3334     1997    1
## 99   TRUE          644     21768 4268     4        17180     2157    2
## 100  TRUE          678     23821 2671     7        12074     2156    3
## 101  TRUE          671     13065 1340     9         3208     1112    3
## 102  TRUE          818     21247 1747    17        21430     1651    2
## 103  TRUE          759     22575 1275    12        18738     1819    1
## 104  TRUE          802     25566 2741     8        10292     1963    3
## 105  TRUE          756     17329 2079    12        12953     1382    3
## 106  TRUE          616     18119 1369     7        11799     1818    1
## 107 FALSE          694     21558 1033     2         4663     2162    3
## 108  TRUE          624     17438 1438     9         9004     1760    1
## 109  TRUE          641     18588 1108    10         7872     1850    3
## 110 FALSE          632     21294  394     2         7872     2373    3
## 111 FALSE          832     25164  589     8         8530     2606    1
## 112 FALSE          828     31286 4386    11        16196     2404    1
## 113 FALSE          938     37910 1885     9        15057     3504    1
## 114 FALSE          521     15167  317     2         3777     1893    1
## 115 FALSE          698     24601 1126     8         6679     2563    3
## 116 FALSE          651     21659  929    11         5158     2305    3
## 117 FALSE          780     29327 1302     5        11033     2633    1
## 118  TRUE          701     25954 2204     9        13424     2271    1
## 119  TRUE          842     32565 8615    11        16681     2405    3
## 120  TRUE          558      8796 1596     8         1858      909    3
## 121  TRUE          495      6596  746     5         1492      800    3
## 122  TRUE          594     12938  713    12         6280     1336    3
## 123  TRUE          663     16437 1262     9         4386     1464    1
## 124  TRUE          714     32550 5445     2        25778     2869    1
## 125  TRUE          692     13663 1113    10         9585     1173    1
## 126 FALSE          777     30697 1722     5         8765     2484    3
## 127 FALSE          451     11790  440     2         3163     2032    1
## 128  TRUE          915     23816  641    20        14435     1596    3
## 129  TRUE          668     11438  988     5         3828     1029    3
## 130 FALSE          332      7461  361     2          582     1359    1
## 131 FALSE          712     23657  482    11         7827     2092    3
## 132  TRUE          801     20299 1474     8        18974     1534    1
## 133  TRUE          660     28349  899     6        27940     2647    2
## 134  TRUE          838     34085 5235    12        27195     2690    3
## 135 FALSE          833     38657 4957     8        13468     3477    3
## 136 FALSE          729     26248 1498     8         6838     2558    3
## 137  TRUE          856     37695 7820     3        15473     2722    1
## 138 FALSE          540     19767 1217     0         7462     2249    3
## 139 FALSE          610     20269 1589     2         5696     2365    3
## 140  TRUE          638     19220 1945     8         9261     1859    1
## 141  TRUE          621     18086 2086     4        20104     1782    3
## 142  TRUE          727     23482  457     7        15069     2264    3
## 143 FALSE          887     33534 2359    10        14573     2954    1
## 144  TRUE          374      7934 1284     2         3822     1569    3
## 145  TRUE          764     31745  945    16         8973     2646    1
## 146  TRUE          631     18752 3502     5         9250     1834    1
## 147  TRUE          689     20105 2080     2        15605     1780    3
## 148 FALSE          618     19472 1872     3         2206     1923    1
## 149 FALSE          542     17019 1519     2         5704     2054    3
## 150 FALSE          596     18836  261     8         2933     2251    1
## 151  TRUE          672     14803 2053     9         3602     1311    3
## 152  TRUE          561     15597  392     7         9377     1737    3
## 153 FALSE          871     42703 3328    12        15210     3667    2
## 154 FALSE          492     10583 2233     6         1619     1414    3
## 155  TRUE          586     11254 1179     3         8821     1134    2
## 156 FALSE          456      9801  271     4          757     1451    3
## 157 FALSE          641     19453    3     7         4454     2452    1
## 158  TRUE          648     15604 1329    12         5930     1491    3
## 159 FALSE          655     26640  160     8         9129     2696    3
## 160  TRUE          563     15107 3357     7         3297     1676    1
## 161  TRUE          592     23429 5054     5        11500     2578    1
## 162 FALSE          824     35087 1512    12         9796     3144    1
## 163  TRUE          645     18321 3096     7        14920     1701    3
## 164  TRUE          775     24101 1576     4        14679     1896    3
## 165  TRUE          932     41313 6813    20        24147     2753    1
## 166 FALSE          513     14353  828     3         1670     1906    3
## 167  TRUE          751     28538 5113    10        16526     2272    3
## 168 FALSE          475     15255  575     5         6119     2178    1
## 169 FALSE          570     15176  726     5         4034     1671    1
## 170  TRUE          598     13113  963     6         7417     1285    3
## 171  TRUE          678     16985 2765     9         9096     1486    3
## 172 FALSE          877     31353 1853     9        18608     2650    3
## 173  TRUE          831     23908 3333    15        15104     1737    1
## 174  TRUE          924     41743 7768    10        22566     2924    3
## 175  TRUE          134      7790 7690     0            0     3157    2
## 176 FALSE          624     19285 3135     5         3411     1982    3
## 177 FALSE          664     19179  504     2         6142     1845    2
## 178  TRUE          525     18069 2089     3         4920     2154    1
## 179 FALSE          591     15327  552     5         2447     1616    2
## 180  TRUE          919     41418 4268    14        22774     3095    1
## 181 FALSE          640     18230 1930    11         4915     1789    3
## 182  TRUE          777     22294  769    13        13574     1793    1
## 183  TRUE          516      9095 1845     3         3897     1013    3
## 184 FALSE          120      6875 6775     0            0     3085    2
## 185  TRUE          659     19467 1192     1        12197     1851    2
## 186 FALSE          432      8934 1809     6         1608     1378    3
## 187 FALSE          667     19039  539     4         6142     1782    2
## 188  TRUE          706     18046 3421    13        10925     1574    1
## 189 FALSE          722     24314 2189     6        12682     2140    3
## 190 FALSE          876     41076 2451    15        15030     3254    1
## 191  TRUE          792     16988  913    12         7430     1278    1
## 192  TRUE          518     13922 1522     1        10554     1636    2
## 193 FALSE          431     12950  875     2         8594     2040    3
## 194  TRUE          838     30875 2100    10        22006     2577    3
## 195 FALSE          129      7910 7810     0            0     3345    2
## 196  TRUE          567     11645 1815     6         6639     1214    3
## 197  TRUE          612     15363 4888     4        14661     1643    3
## 198  TRUE          760     27049 2274     7        19911     2301    1
##     lane_role
## 1           3
## 2           3
## 3           3
## 4           1
## 5           1
## 6           1
## 7           1
## 8           3
## 9           3
## 10          3
## 11          1
## 12          3
## 13          1
## 14          1
## 15          1
## 16          3
## 17          3
## 18          3
## 19          3
## 20          4
## 21          3
## 22          1
## 23          1
## 24          3
## 25          1
## 26          1
## 27          1
## 28          3
## 29          1
## 30          1
## 31          1
## 32          1
## 33          1
## 34          1
## 35          1
## 36          1
## 37          1
## 38          1
## 39          1
## 40          1
## 41          1
## 42          3
## 43          1
## 44          1
## 45          1
## 46          1
## 47          2
## 48          3
## 49          3
## 50          1
## 51          1
## 52          1
## 53          1
## 54          3
## 55          3
## 56          2
## 57          2
## 58          3
## 59          1
## 60          1
## 61          3
## 62          1
## 63          1
## 64          1
## 65          1
## 66          1
## 67          2
## 68          3
## 69          4
## 70          3
## 71          1
## 72          1
## 73          1
## 74          1
## 75          3
## 76          1
## 77          3
## 78          1
## 79          3
## 80          3
## 81          1
## 82          1
## 83          3
## 84          1
## 85          3
## 86          3
## 87          3
## 88          1
## 89          1
## 90          1
## 91          3
## 92          1
## 93          2
## 94          3
## 95          1
## 96          1
## 97          1
## 98          1
## 99          2
## 100         3
## 101         1
## 102         2
## 103         1
## 104         1
## 105         1
## 106         3
## 107         1
## 108         3
## 109         3
## 110         3
## 111         1
## 112         3
## 113         1
## 114         1
## 115         1
## 116         1
## 117         1
## 118         1
## 119         1
## 120         3
## 121         3
## 122         1
## 123         1
## 124         3
## 125         1
## 126         1
## 127         1
## 128         1
## 129         1
## 130         1
## 131         1
## 132         1
## 133         2
## 134         1
## 135         1
## 136         3
## 137         3
## 138         3
## 139         1
## 140         3
## 141         3
## 142         1
## 143         3
## 144         1
## 145         3
## 146         3
## 147         1
## 148         1
## 149         3
## 150         1
## 151         3
## 152         3
## 153         2
## 154         3
## 155         2
## 156         3
## 157         3
## 158         1
## 159         3
## 160         3
## 161         3
## 162         1
## 163         3
## 164         1
## 165         1
## 166         1
## 167         1
## 168         3
## 169         3
## 170         1
## 171         1
## 172         1
## 173         1
## 174         3
## 175         2
## 176         1
## 177         2
## 178         3
## 179         2
## 180         1
## 181         1
## 182         1
## 183         1
## 184         2
## 185         2
## 186         3
## 187         2
## 188         1
## 189         1
## 190         1
## 191         1
## 192         2
## 193         3
## 194         1
## 195         2
## 196         3
## 197         3
## 198         1
# class(dataModel$win)
# class(dataModel$gold_per_min)
# class(dataModel$net_worth)
# class(dataModel$gold)
# class(dataModel$kills)
# class(dataModel$tower_damage)
# class(dataModel$duration)
# class(dataModel$lane)
# class(dataModel$lane_role)
keepVars <- c("gold_per_min","kills")
keepVars <- c(keepVars, "win")
dataModel <- dataModel[, names(dataModel) %in% keepVars]
dataModel
##       win gold_per_min kills
## 1   FALSE          569     3
## 2   FALSE          549     3
## 3    TRUE          654    10
## 4   FALSE          472     4
## 5    TRUE          894    10
## 6    TRUE          593     5
## 7   FALSE          442     0
## 8   FALSE          434     0
## 9    TRUE          701     9
## 10   TRUE          752    13
## 11   TRUE          810    10
## 12   TRUE          483     1
## 13   TRUE          676    11
## 14  FALSE          902    16
## 15   TRUE          721    22
## 16  FALSE          512     4
## 17   TRUE          412     0
## 18  FALSE          848    18
## 19   TRUE          559     4
## 20  FALSE          290     2
## 21   TRUE          485     4
## 22   TRUE          655     9
## 23  FALSE          453     1
## 24   TRUE          689    10
## 25  FALSE          646     5
## 26   TRUE          560     3
## 27   TRUE          643     4
## 28   TRUE          677     8
## 29   TRUE          785     8
## 30  FALSE          588     5
## 31   TRUE          672    11
## 32   TRUE          613     6
## 33   TRUE          808     6
## 34   TRUE          785     7
## 35  FALSE          442     2
## 36   TRUE          723     9
## 37   TRUE          580     4
## 38   TRUE          805    14
## 39  FALSE          626     3
## 40  FALSE          480     1
## 41  FALSE          603     3
## 42  FALSE          533     2
## 43  FALSE          455     8
## 44   TRUE          683     5
## 45  FALSE          777     8
## 46  FALSE          625     4
## 47  FALSE          578     8
## 48   TRUE          623     9
## 49  FALSE          624     2
## 50   TRUE          650     8
## 51  FALSE          559     3
## 52   TRUE          820    13
## 53  FALSE          680     6
## 54   TRUE          699     9
## 55   TRUE          605     3
## 56   TRUE          773    11
## 57   TRUE          821    11
## 58   TRUE          543     4
## 59   TRUE          630     8
## 60  FALSE          584     4
## 61  FALSE          714     4
## 62   TRUE          756    12
## 63  FALSE          487     1
## 64  FALSE          441     2
## 65   TRUE          856    15
## 66  FALSE          560     2
## 67  FALSE          128     0
## 68  FALSE          442     1
## 69   TRUE          477     2
## 70  FALSE          493     3
## 71  FALSE          764    11
## 72   TRUE          725    11
## 73  FALSE          672     7
## 74  FALSE          539     2
## 75  FALSE          533     3
## 76   TRUE          415     2
## 77  FALSE          659     8
## 78   TRUE          748     7
## 79   TRUE          617     6
## 80  FALSE          488     6
## 81  FALSE          554     2
## 82   TRUE          807     7
## 83  FALSE          552     1
## 84   TRUE          829    12
## 85   TRUE          581     4
## 86   TRUE          592     8
## 87  FALSE          592     7
## 88  FALSE          577     5
## 89   TRUE          815     9
## 90   TRUE          696     1
## 91   TRUE          639     6
## 92   TRUE          948    20
## 93   TRUE          729     9
## 94  FALSE          629     4
## 95  FALSE          657     6
## 96  FALSE          573     3
## 97   TRUE          813    13
## 98  FALSE          583     5
## 99   TRUE          644     4
## 100  TRUE          678     7
## 101  TRUE          671     9
## 102  TRUE          818    17
## 103  TRUE          759    12
## 104  TRUE          802     8
## 105  TRUE          756    12
## 106  TRUE          616     7
## 107 FALSE          694     2
## 108  TRUE          624     9
## 109  TRUE          641    10
## 110 FALSE          632     2
## 111 FALSE          832     8
## 112 FALSE          828    11
## 113 FALSE          938     9
## 114 FALSE          521     2
## 115 FALSE          698     8
## 116 FALSE          651    11
## 117 FALSE          780     5
## 118  TRUE          701     9
## 119  TRUE          842    11
## 120  TRUE          558     8
## 121  TRUE          495     5
## 122  TRUE          594    12
## 123  TRUE          663     9
## 124  TRUE          714     2
## 125  TRUE          692    10
## 126 FALSE          777     5
## 127 FALSE          451     2
## 128  TRUE          915    20
## 129  TRUE          668     5
## 130 FALSE          332     2
## 131 FALSE          712    11
## 132  TRUE          801     8
## 133  TRUE          660     6
## 134  TRUE          838    12
## 135 FALSE          833     8
## 136 FALSE          729     8
## 137  TRUE          856     3
## 138 FALSE          540     0
## 139 FALSE          610     2
## 140  TRUE          638     8
## 141  TRUE          621     4
## 142  TRUE          727     7
## 143 FALSE          887    10
## 144  TRUE          374     2
## 145  TRUE          764    16
## 146  TRUE          631     5
## 147  TRUE          689     2
## 148 FALSE          618     3
## 149 FALSE          542     2
## 150 FALSE          596     8
## 151  TRUE          672     9
## 152  TRUE          561     7
## 153 FALSE          871    12
## 154 FALSE          492     6
## 155  TRUE          586     3
## 156 FALSE          456     4
## 157 FALSE          641     7
## 158  TRUE          648    12
## 159 FALSE          655     8
## 160  TRUE          563     7
## 161  TRUE          592     5
## 162 FALSE          824    12
## 163  TRUE          645     7
## 164  TRUE          775     4
## 165  TRUE          932    20
## 166 FALSE          513     3
## 167  TRUE          751    10
## 168 FALSE          475     5
## 169 FALSE          570     5
## 170  TRUE          598     6
## 171  TRUE          678     9
## 172 FALSE          877     9
## 173  TRUE          831    15
## 174  TRUE          924    10
## 175  TRUE          134     0
## 176 FALSE          624     5
## 177 FALSE          664     2
## 178  TRUE          525     3
## 179 FALSE          591     5
## 180  TRUE          919    14
## 181 FALSE          640    11
## 182  TRUE          777    13
## 183  TRUE          516     3
## 184 FALSE          120     0
## 185  TRUE          659     1
## 186 FALSE          432     6
## 187 FALSE          667     4
## 188  TRUE          706    13
## 189 FALSE          722     6
## 190 FALSE          876    15
## 191  TRUE          792    12
## 192  TRUE          518     1
## 193 FALSE          431     2
## 194  TRUE          838    10
## 195 FALSE          129     0
## 196  TRUE          567     6
## 197  TRUE          612     4
## 198  TRUE          760     7
dataSubset <- dataModel[, names(dataModel) %in% keepVars]
dataIndex <- createDataPartition(dataModel$win, p = 0.75, list = FALSE)
class(dataIndex)
## [1] "matrix" "array"
dataTrain <- dataModel[dataIndex, ]
dataTest <- dataModel[-dataIndex, ]
glmFit <- train(win ~ ., data = dataTrain, 
         method = "glm", 
         family = "binomial",
         preProcess = c("center", "scale"),
         trControl = trainControl(method = "cv", number = 10))

predictions <- predict(glmFit, dataTrain)

devia<-summary(glmFit)$deviance

coef <-summary(glmFit)$coefficients

predictions
##   [1] FALSE TRUE  TRUE  FALSE FALSE FALSE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE 
##  [13] FALSE FALSE FALSE FALSE TRUE  FALSE TRUE  FALSE FALSE TRUE  TRUE  FALSE
##  [25] TRUE  TRUE  TRUE  FALSE TRUE  FALSE TRUE  FALSE FALSE FALSE FALSE TRUE 
##  [37] TRUE  FALSE TRUE  TRUE  FALSE TRUE  TRUE  TRUE  FALSE TRUE  FALSE FALSE
##  [49] TRUE  FALSE FALSE TRUE  FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE FALSE
##  [61] TRUE  TRUE  TRUE  FALSE TRUE  FALSE TRUE  TRUE  TRUE  FALSE TRUE  FALSE
##  [73] FALSE TRUE  TRUE  TRUE  TRUE  FALSE TRUE  FALSE TRUE  TRUE  FALSE TRUE 
##  [85] TRUE  FALSE TRUE  TRUE  TRUE  TRUE  TRUE  FALSE FALSE FALSE TRUE  FALSE
##  [97] TRUE  TRUE  TRUE  TRUE  TRUE  FALSE FALSE FALSE TRUE  TRUE  FALSE FALSE
## [109] FALSE FALSE TRUE  TRUE  TRUE  FALSE FALSE TRUE  TRUE  TRUE  TRUE  TRUE 
## [121] FALSE TRUE  FALSE TRUE  TRUE  TRUE  TRUE  FALSE FALSE FALSE FALSE FALSE
## [133] TRUE  TRUE  TRUE  FALSE FALSE TRUE  FALSE TRUE  TRUE  TRUE  TRUE  FALSE
## [145] FALSE TRUE  FALSE TRUE  FALSE
## Levels: FALSE TRUE
devia
## [1] 183.5477
coef
##                 Estimate Std. Error    z value   Pr(>|z|)
## (Intercept)   0.29374756  0.1799485  1.6323981 0.10259566
## gold_per_min -0.09331071  0.2594704 -0.3596198 0.71913145
## kills         0.93775334  0.2940190  3.1894313 0.00142553
confusionMatrix(data = dataTest$win, reference = predict(glmFit, newdata = dataTest))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction FALSE TRUE
##      FALSE    11   11
##      TRUE      9   18
##                                         
##                Accuracy : 0.5918        
##                  95% CI : (0.4421, 0.73)
##     No Information Rate : 0.5918        
##     P-Value [Acc > NIR] : 0.5611        
##                                         
##                   Kappa : 0.1681        
##                                         
##  Mcnemar's Test P-Value : 0.8231        
##                                         
##             Sensitivity : 0.5500        
##             Specificity : 0.6207        
##          Pos Pred Value : 0.5000        
##          Neg Pred Value : 0.6667        
##              Prevalence : 0.4082        
##          Detection Rate : 0.2245        
##    Detection Prevalence : 0.4490        
##       Balanced Accuracy : 0.5853        
##                                         
##        'Positive' Class : FALSE         
## 

Classification Tree

Change . to user input

classTreeFit <- tree(win ~ ., data = dataTrain) # The '.' means all variables to be used as explanatory variables
summary(classTreeFit)
## 
## Classification tree:
## tree(formula = win ~ ., data = dataTrain)
## Number of terminal nodes:  9 
## Residual mean deviance:  0.9625 = 134.7 / 140 
## Misclassification error rate: 0.2617 = 39 / 149
class(classTreeFit)
## [1] "tree"

Confusion Matrix of Accuracy

df <- data.frame(matrix(rnorm(16), nrow = 4))
colnames(df) <- paste0("Col", 1:4)
rownames(df) <- paste0("Row", 1:4)

###Pruning

pruneFit <- cv.tree(classTreeFit, FUN = prune.misclass)
summary(pruneFit)
##        Length Class  Mode     
## size   6      -none- numeric  
## dev    6      -none- numeric  
## k      6      -none- numeric  
## method 1      -none- character
plot(pruneFit$size, pruneFit$dev, type = "b")

pruneFit
## $size
## [1] 9 8 5 3 2 1
## 
## $dev
## [1] 54 56 56 55 47 68
## 
## $k
## [1]       -Inf  0.0000000  0.6666667  1.0000000  3.0000000 20.0000000
## 
## $method
## [1] "misclass"
## 
## attr(,"class")
## [1] "prune"         "tree.sequence"
dfPruneFit <- cbind(size=pruneFit$size,dev=pruneFit$dev)
dfPruneFit <- data.frame(dfPruneFit)
dfPruneFit <- dfPruneFit %>% group_by(size)%>%arrange(size)%>%arrange(dev)
dfPruneFit
## # A tibble: 6 × 2
## # Groups:   size [6]
##    size   dev
##   <dbl> <dbl>
## 1     2    47
## 2     9    54
## 3     3    55
## 4     5    56
## 5     8    56
## 6     1    68
bestVal <- dfPruneFit$size[1]
bestVal
## [1] 2

Pruned Predictions

pruneFitFinal <- prune.misclass(classTreeFit, best = bestVal)
summary(pruneFitFinal)
## 
## Classification tree:
## snip.tree(tree = classTreeFit, nodes = 3:2)
## Variables actually used in tree construction:
## [1] "kills"
## Number of terminal nodes:  2 
## Residual mean deviance:  1.232 = 181.1 / 147 
## Misclassification error rate: 0.3087 = 46 / 149
fullPred <- predict(classTreeFit, dplyr::select(dataTest, -"win"), type = "class")

prunePred <- predict(pruneFitFinal, dplyr::select(dataTest, -"win"), type = "class")

prunePred
##  [1] FALSE FALSE FALSE TRUE  FALSE FALSE TRUE  FALSE TRUE  FALSE TRUE  TRUE 
## [13] FALSE FALSE TRUE  FALSE TRUE  FALSE FALSE FALSE TRUE  TRUE  TRUE  FALSE
## [25] FALSE FALSE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  FALSE TRUE  FALSE
## [37] TRUE  TRUE  FALSE TRUE  TRUE  TRUE  FALSE FALSE TRUE  FALSE FALSE FALSE
## [49] TRUE 
## Levels: FALSE TRUE

Comparison Full Fit

cm <- confusionMatrix(fullPred,dataTest$win)

cmFull <- cm$table

cmFull
##           Reference
## Prediction FALSE TRUE
##      FALSE    12   18
##      TRUE     10    9
accFull <- cm$overall[1]

list(cmFull,accFull)
## [[1]]
##           Reference
## Prediction FALSE TRUE
##      FALSE    12   18
##      TRUE     10    9
## 
## [[2]]
##  Accuracy 
## 0.4285714

Comparison Pruned Fit

pruneTbl <- table(data.frame(prunePred, dataTest[, "win"]))
kable(pruneTbl)
FALSE TRUE
FALSE 13 11
TRUE 9 16
accPrune<-sum(diag(pruneTbl)/sum(pruneTbl))
print(accPrune)
## [1] 0.5918367

Random Forest

Change the . on 254 to user input

Here we change our mtry to be ncol(diamondsTrain)/3 since this example was regression

trainRFModel <- train(win ~ ., data = dataTrain,
method = "rf",
trControl = trainControl(method = "repeatedcv", number = 5, repeats = 3),
tuneGrid = data.frame(mtry = sqrt(ncol(dataTrain) - 1)))

trainConMat <- confusionMatrix(trainRFModel, newdata = dataTest)
#testConMat <- confusionMatrix(data = dataTest$win, reference = predict(trainRFModel, newdata = dataTest))
trainConMat
## Cross-Validated (5 fold, repeated 3 times) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction FALSE TRUE
##      FALSE  25.5 18.3
##      TRUE   18.8 37.4
##                             
##  Accuracy (average) : 0.6286
#testConMat
summary(trainRFModel)
##                 Length Class      Mode     
## call               4   -none-     call     
## type               1   -none-     character
## predicted        149   factor     numeric  
## err.rate        1500   -none-     numeric  
## confusion          6   -none-     numeric  
## votes            298   matrix     numeric  
## oob.times        149   -none-     numeric  
## classes            2   -none-     character
## importance         2   -none-     numeric  
## importanceSD       0   -none-     NULL     
## localImportance    0   -none-     NULL     
## proximity          0   -none-     NULL     
## ntree              1   -none-     numeric  
## mtry               1   -none-     numeric  
## forest            14   -none-     list     
## y                149   factor     numeric  
## test               0   -none-     NULL     
## inbag              0   -none-     NULL     
## xNames             2   -none-     character
## problemType        1   -none-     character
## tuneValue          1   data.frame list     
## obsLevels          2   -none-     character
## param              0   -none-     list
predict(trainRFModel,newdata=dataTest)
##  [1] FALSE TRUE  FALSE TRUE  TRUE  TRUE  TRUE  FALSE FALSE TRUE  FALSE FALSE
## [13] TRUE  FALSE TRUE  FALSE FALSE TRUE  FALSE TRUE  TRUE  TRUE  TRUE  TRUE 
## [25] TRUE  FALSE TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  FALSE
## [37] TRUE  TRUE  FALSE TRUE  TRUE  TRUE  FALSE FALSE TRUE  TRUE  TRUE  FALSE
## [49] TRUE 
## Levels: FALSE TRUE

#Classification Using mlr3

#https://mlr3book.mlr-org.com/basics.html#learners


#npTask <- tsk("npData")
data("npData", package = "datasets")
str(npData)
## spc_tbl_ [198 × 14] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ match_id    : num [1:198] 6.65e+09 6.65e+09 6.65e+09 6.66e+09 6.66e+09 ...
##  $ start_time  : num [1:198] 1.66e+09 1.66e+09 1.66e+09 1.66e+09 1.66e+09 ...
##  $ win         : logi [1:198] FALSE FALSE TRUE FALSE TRUE TRUE ...
##  $ hero_id     : num [1:198] 53 53 53 53 53 53 53 53 53 53 ...
##  $ account_id  : num [1:198] 2.51e+08 7.61e+07 1.31e+09 2.09e+08 1.33e+08 ...
##  $ leaguename  : chr [1:198] "Titus Colosseum Cup" "Destiny league" "Ultras Dota Pro League" "Titus Colosseum Cup" ...
##  $ gold_per_min: num [1:198] 569 549 654 472 894 593 442 434 701 752 ...
##  $ net_worth   : num [1:198] 15770 15317 16985 10399 40098 ...
##  $ gold        : num [1:198] 1140 892 3210 449 9648 ...
##  $ kills       : num [1:198] 3 3 10 4 10 5 0 0 9 13 ...
##  $ tower_damage: num [1:198] 1221 1559 19823 1264 28469 ...
##  $ duration    : num [1:198] 1754 1794 1547 1319 2938 ...
##  $ lane        : num [1:198] 1 1 3 1 3 3 1 3 1 3 ...
##  $ lane_role   : num [1:198] 3 3 3 1 1 1 1 3 3 3 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   match_id = col_double(),
##   ..   start_time = col_double(),
##   ..   win = col_logical(),
##   ..   hero_id = col_double(),
##   ..   account_id = col_double(),
##   ..   leaguename = col_character(),
##   ..   gold_per_min = col_double(),
##   ..   net_worth = col_double(),
##   ..   gold = col_double(),
##   ..   kills = col_double(),
##   ..   tower_damage = col_double(),
##   ..   duration = col_double(),
##   ..   lane = col_double(),
##   ..   lane_role = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
npSubset = subset(npData, select = c("win","gold_per_min","net_worth","duration"))

taskNP = as_task_classif(npSubset, target = "win", id = "wins", positive = "TRUE") #We can set the variable of interest here

taskNP
## <TaskClassif:wins> (198 x 4)
## * Target: win
## * Properties: twoclass
## * Features (3):
##   - dbl (3): duration, gold_per_min, net_worth
autoplot(taskNP, type = "pairs")

#Checking Elements of TaskNP
c(taskNP$nrow, taskNP$ncol) #Get size of data object
## [1] 198   4
list(taskNP$feature_names, taskNP$target_names) #we can see which variables are features vs targets from our earlier as_task_clasif
## [[1]]
## [1] "duration"     "gold_per_min" "net_worth"   
## 
## [[2]]
## [1] "win"
head(taskNP$row_ids) #Pull indexes of the task
## [1] 1 2 3 4 5 6
taskNP$data() #Look at data with this
##        win duration gold_per_min net_worth
##   1: FALSE     1754          569     15770
##   2: FALSE     1794          549     15317
##   3:  TRUE     1547          654     16985
##   4: FALSE     1319          472     10399
##   5:  TRUE     2938          894     40098
##  ---                                      
## 194:  TRUE     2577          838     30875
## 195: FALSE     3345          129      7910
## 196:  TRUE     1214          567     11645
## 197:  TRUE     1643          612     15363
## 198:  TRUE     2301          760     27049
taskNP$data(rows = c(1, 3, 10), cols = "duration") #Pull specific data with this
##    duration
## 1:     1754
## 2:     1547
## 3:     2263
summary(as.data.table(taskNP)) #Pull everything out
##     win         duration     gold_per_min     net_worth    
##  TRUE :110   Min.   : 800   Min.   :120.0   Min.   : 6108  
##  FALSE: 88   1st Qu.:1614   1st Qu.:560.0   1st Qu.:14357  
##              Median :1908   Median :644.5   Median :19109  
##              Mean   :2012   Mean   :644.1   Mean   :20283  
##              3rd Qu.:2326   3rd Qu.:755.0   3rd Qu.:24529  
##              Max.   :3667   Max.   :948.0   Max.   :42703

Task Mutators with mlr3

taskNPSmall = taskNP$clone() # use clone method to create copy
taskNPSmall$select(c("gold_per_min", "duration")) # keep only these features
taskNPSmall$filter(2:4) # keep only these rows
taskNPSmall$data()
##      win duration gold_per_min
## 1: FALSE     1794          549
## 2:  TRUE     1547          654
## 3: FALSE     1319          472
#Lets make it even smaller

taskNPSmaller = taskNPSmall$clone()
taskNPSmaller$filter(2)   #keep only the 2nd row
taskNPSmaller$data()
##      win duration gold_per_min
## 1: FALSE     1794          549
#Create a learner
classifLrn <- lrn("classif.rpart")

#Partition our Data (default uses 33/67)
splits = partition(taskNP)
splits
## $train
##   [1]   1   4   7   8  14  20  25  30  42  47  49  51  53  60  61  64  66  67
##  [19]  68  73  74  75  77  80  81  87  95  96  98 107 110 111 112 113 114 115
##  [37] 116 117 130 131 136 150 153 154 157 159 162 166 169 172 176 181 184 186
##  [55] 187 189 190 193 195   3   9  11  12  13  15  19  21  27  28  31  32  33
##  [73]  34  36  38  48  52  54  55  56  57  58  59  72  76  78  82  85  89  91
##  [91]  92  97 100 102 105 106 109 118 119 120 121 122 123 124 125 128 132 133
## [109] 134 137 144 145 146 147 151 155 158 160 161 167 171 173 174 175 178 180
## [127] 182 183 185 188 191 192 197
## 
## $test
##  [1]   2  16  18  23  35  39  40  41  43  45  46  63  70  71  83  88  94 126 127
## [20] 135 138 139 143 148 149 156 168 177 179   5   6  10  17  22  24  26  29  37
## [39]  44  50  62  65  69  79  84  86  90  93  99 101 103 104 108 129 140 141 142
## [58] 152 163 164 165 170 194 196 198
classifLrn$train(taskNP, splits$train)
classifLrn$model
## n= 133 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 133 59 TRUE (0.55639098 0.44360902)  
##    2) duration< 1884.5 67 19 TRUE (0.71641791 0.28358209)  
##      4) gold_per_min>=591 39  3 TRUE (0.92307692 0.07692308) *
##      5) gold_per_min< 591 28 12 FALSE (0.42857143 0.57142857)  
##       10) duration< 1317 7  2 TRUE (0.71428571 0.28571429) *
##       11) duration>=1317 21  7 FALSE (0.33333333 0.66666667) *
##    3) duration>=1884.5 66 26 FALSE (0.39393939 0.60606061)  
##      6) net_worth>=25450 32 12 TRUE (0.62500000 0.37500000)  
##       12) duration< 2368 12  0 TRUE (1.00000000 0.00000000) *
##       13) duration>=2368 20  8 FALSE (0.40000000 0.60000000)  
##         26) net_worth>=31549 12  5 TRUE (0.58333333 0.41666667) *
##         27) net_worth< 31549 8  1 FALSE (0.12500000 0.87500000) *
##      7) net_worth< 25450 34  6 FALSE (0.17647059 0.82352941) *
classifLrn$param_set #to view hyper parameters, current settings are stored in the value column
## <ParamSet>
##                 id    class lower upper nlevels        default value
##  1:             cp ParamDbl     0     1     Inf           0.01      
##  2:     keep_model ParamLgl    NA    NA       2          FALSE      
##  3:     maxcompete ParamInt     0   Inf     Inf              4      
##  4:       maxdepth ParamInt     1    30      30             30      
##  5:   maxsurrogate ParamInt     0   Inf     Inf              5      
##  6:      minbucket ParamInt     1   Inf     Inf <NoDefault[3]>      
##  7:       minsplit ParamInt     1   Inf     Inf             20      
##  8: surrogatestyle ParamInt     0     1       2              0      
##  9:   usesurrogate ParamInt     0     2       3              2      
## 10:           xval ParamInt     0   Inf     Inf             10     0
#classifLrn$param_set$set_values(minsplit = 10) if you want to set the parameters

#classifLrn = lrn("classif.rpart", minsplit =10) hyperparameters can be done at creation of learner

#Predicting
predsClass = classifLrn$predict(taskNP,splits$test)
predsClass #truth is what actually happened, response is the prediction
## <PredictionClassif> for 65 observations:
##     row_ids truth response
##           2 FALSE    FALSE
##          16 FALSE    FALSE
##          18 FALSE     TRUE
## ---                       
##         194  TRUE    FALSE
##         196  TRUE     TRUE
##         198  TRUE     TRUE
as.data.table(predsClass)
##     row_ids truth response
##  1:       2 FALSE    FALSE
##  2:      16 FALSE    FALSE
##  3:      18 FALSE     TRUE
##  4:      23 FALSE    FALSE
##  5:      35 FALSE     TRUE
##  6:      39 FALSE    FALSE
##  7:      40 FALSE    FALSE
##  8:      41 FALSE    FALSE
##  9:      43 FALSE    FALSE
## 10:      45 FALSE    FALSE
## 11:      46 FALSE    FALSE
## 12:      63 FALSE    FALSE
## 13:      70 FALSE    FALSE
## 14:      71 FALSE     TRUE
## 15:      83 FALSE    FALSE
## 16:      88 FALSE    FALSE
## 17:      94 FALSE    FALSE
## 18:     126 FALSE    FALSE
## 19:     127 FALSE    FALSE
## 20:     135 FALSE     TRUE
## 21:     138 FALSE    FALSE
## 22:     139 FALSE    FALSE
## 23:     143 FALSE     TRUE
## 24:     148 FALSE    FALSE
## 25:     149 FALSE    FALSE
## 26:     156 FALSE    FALSE
## 27:     168 FALSE    FALSE
## 28:     177 FALSE     TRUE
## 29:     179 FALSE     TRUE
## 30:       5  TRUE     TRUE
## 31:       6  TRUE    FALSE
## 32:      10  TRUE    FALSE
## 33:      17  TRUE    FALSE
## 34:      22  TRUE     TRUE
## 35:      24  TRUE    FALSE
## 36:      26  TRUE     TRUE
## 37:      29  TRUE     TRUE
## 38:      37  TRUE    FALSE
## 39:      44  TRUE    FALSE
## 40:      50  TRUE    FALSE
## 41:      62  TRUE    FALSE
## 42:      65  TRUE     TRUE
## 43:      69  TRUE    FALSE
## 44:      79  TRUE    FALSE
## 45:      84  TRUE     TRUE
## 46:      86  TRUE     TRUE
## 47:      90  TRUE     TRUE
## 48:      93  TRUE    FALSE
## 49:      99  TRUE    FALSE
## 50:     101  TRUE     TRUE
## 51:     103  TRUE     TRUE
## 52:     104  TRUE     TRUE
## 53:     108  TRUE     TRUE
## 54:     129  TRUE     TRUE
## 55:     140  TRUE     TRUE
## 56:     141  TRUE     TRUE
## 57:     142  TRUE    FALSE
## 58:     152  TRUE    FALSE
## 59:     163  TRUE     TRUE
## 60:     164  TRUE    FALSE
## 61:     165  TRUE     TRUE
## 62:     170  TRUE     TRUE
## 63:     194  TRUE    FALSE
## 64:     196  TRUE     TRUE
## 65:     198  TRUE     TRUE
##     row_ids truth response
#This learner's model can then be applied to other data
newPreds = classifLrn$predict_newdata(npData)
newPreds
## <PredictionClassif> for 198 observations:
##     row_ids truth response
##           1 FALSE    FALSE
##           2 FALSE    FALSE
##           3  TRUE     TRUE
## ---                       
##         196  TRUE     TRUE
##         197  TRUE     TRUE
##         198  TRUE     TRUE
autoplot(newPreds)

#Evaluating Accuracy of MLR3 Model

#First we create the measure
msrAcc = msr("classif.acc")
msrAUC = msr("classif.auc")
msrFP = msr("classif.fp")
msrFPR = msr("classif.fpr")
msrFN = msr("classif.fn")
msrFNR = msr("classif.fnr")
measures = c(msrAcc,msrAUC,msrFP,msrFPR,msrFN,msrFNR)

predsClass$score(measures)
## classif.acc classif.auc  classif.fp classif.fpr  classif.fn classif.fnr 
##   0.6461538         NaN   7.0000000   0.2413793  16.0000000   0.4444444
predsClass$confusion
##         truth
## response TRUE FALSE
##    TRUE    20     7
##    FALSE   16    22

#Thresholding Can adjust the threshold of log odds at which it would be predicted as a win as below. Recall that if the log odds are above .5, it will be predicted a win. Sometimes you may want to adjust this for accuracy.

#predsClass$set_threshold(??)
predsClass$confusion
##         truth
## response TRUE FALSE
##    TRUE    20     7
##    FALSE   16    22

#kNN

npSubset = subset(npData, select = c("win","gold_per_min","net_worth","duration"))

taskNP = as_task_classif(npSubset, target = "win", id = "wins", positive = "TRUE")

classifLrn <- lrn("classif.rpart")

#Partition our Data (default uses 33/67)
splits = partition(taskNP)
splits
## $train
##   [1]   1   2   4   7  16  18  20  23  25  35  39  40  43  46  47  51  60  61
##  [19]  63  66  67  70  71  73  75  77  87  88  94  95  96  98 107 110 112 116
##  [37] 126 131 135 136 138 143 148 149 150 153 154 156 157 159 168 172 179 181
##  [55] 186 187 190 193 195   5   6  11  15  21  24  26  27  28  29  32  33  38
##  [73]  44  50  52  54  55  58  59  69  72  76  85  90  91  92  93  97 100 101
##  [91] 102 103 104 105 106 118 120 121 122 125 128 129 132 133 134 137 140 141
## [109] 142 145 146 147 151 152 155 158 160 163 165 170 173 175 178 180 182 183
## [127] 188 191 192 194 196 197 198
## 
## $test
##  [1]   8  14  30  41  42  45  49  53  64  68  74  80  81  83 111 113 114 115 117
## [20] 127 130 139 162 166 169 176 177 184 189   3   9  10  12  13  17  19  22  31
## [39]  34  36  37  48  56  57  62  65  78  79  82  84  86  89  99 108 109 119 123
## [58] 124 144 161 164 167 171 174 185
classifLrn$train(taskNP, splits$train)
classifLrn$model
## n= 133 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 133 59 TRUE (0.5563910 0.4436090)  
##    2) gold_per_min>=592.5 86 28 TRUE (0.6744186 0.3255814)  
##      4) duration< 2090 46  5 TRUE (0.8913043 0.1086957) *
##      5) duration>=2090 40 17 FALSE (0.4250000 0.5750000)  
##       10) gold_per_min>=659.5 30 14 TRUE (0.5333333 0.4666667)  
##         20) duration< 2845.5 23  9 TRUE (0.6086957 0.3913043)  
##           40) net_worth>=26296.5 15  4 TRUE (0.7333333 0.2666667) *
##           41) net_worth< 26296.5 8  3 FALSE (0.3750000 0.6250000) *
##         21) duration>=2845.5 7  2 FALSE (0.2857143 0.7142857) *
##       11) gold_per_min< 659.5 10  1 FALSE (0.1000000 0.9000000) *
##    3) gold_per_min< 592.5 47 16 FALSE (0.3404255 0.6595745)  
##      6) duration< 1227.5 7  0 TRUE (1.0000000 0.0000000) *
##      7) duration>=1227.5 40  9 FALSE (0.2250000 0.7750000)  
##       14) net_worth>=13239.5 22  7 FALSE (0.3181818 0.6818182)  
##         28) duration< 1745.5 7  2 TRUE (0.7142857 0.2857143) *
##         29) duration>=1745.5 15  2 FALSE (0.1333333 0.8666667) *
##       15) net_worth< 13239.5 18  2 FALSE (0.1111111 0.8888889) *
#Predicting data on our testing split
predsClass = classifLrn$predict(taskNP,splits$test)
predsClass
## <PredictionClassif> for 65 observations:
##     row_ids truth response
##           8 FALSE    FALSE
##          14 FALSE    FALSE
##          30 FALSE    FALSE
## ---                       
##         171  TRUE     TRUE
##         174  TRUE    FALSE
##         185  TRUE     TRUE
msrAcc = msr("classif.acc")
msrAUC = msr("classif.auc")
msrFP = msr("classif.fp")
msrFPR = msr("classif.fpr")
msrFN = msr("classif.fn")
msrFNR = msr("classif.fnr")
measures = c(msrAcc,msrAUC,msrFP,msrFPR,msrFN,msrFNR)

predsClass$score(measures)
## classif.acc classif.auc  classif.fp classif.fpr  classif.fn classif.fnr 
##   0.6923077         NaN   7.0000000   0.2413793  13.0000000   0.3611111
predsClass$confusion
##         truth
## response TRUE FALSE
##    TRUE    23     7
##    FALSE   13    22
#######

knnLrn <- lrn("classif.kknn")

taskNPKnn = as_task_classif(npSubset, target = "win", id = "wins", positive = "TRUE", backend =npSubset)

splitsKNN = partition(taskNPKnn)

knnLrn$train(taskNPKnn, splitsKNN$train)
knnLrn$model
## $formula
## win ~ .
## NULL
## 
## $data
##        win duration gold_per_min net_worth
##   1: FALSE     1754          569     15770
##   2: FALSE     1794          549     15317
##   3: FALSE     1846          442     11580
##   4: FALSE     1315          434      9842
##   5: FALSE     3349          902     41321
##  ---                                      
## 129:  TRUE     1851          659     19467
## 130:  TRUE     1278          792     16988
## 131:  TRUE     1636          518     13922
## 132:  TRUE     2577          838     30875
## 133:  TRUE     1214          567     11645
## 
## $pv
## $pv$k
## [1] 7
## 
## 
## $kknn
## NULL
knnPreds <- knnLrn$predict(taskNPKnn,splits$test)

#Measuring Accuracy
confMat <- knnPreds$confusion
confMat
##         truth
## response TRUE FALSE
##    TRUE    27     3
##    FALSE    9    26
#Creating the measures
msrAcc = msr("classif.acc")
msrFPR = msr("classif.fpr")
msrFNR = msr("classif.fnr")
measures = c(msrAcc,msrFPR,msrFNR)

knnPreds$score(measures)
## classif.acc classif.fpr classif.fnr 
##   0.8153846   0.1034483   0.2500000

#kNN Caret

knnNP <- train(win ~ ., data = dataTrain, method = "knn", 
               trControl = trainControl(method = "cv", number = 10),
               tuneGrid = expand.grid(k = 1:10)
)

knnPreds <- predict(knnNP, newdata = dataTest)

knnAcc <- confusionMatrix(knnPreds, dataTest$win)
knnAcc$table
##           Reference
## Prediction FALSE TRUE
##      FALSE     9   11
##      TRUE     13   16

kNN Caret F1

truePositives <- knnAcc$table[2, 2]
falsePositives <- knnAcc$table[2, 1]
falseNegatives <- knnAcc$table[1, 2]

precision <- truePositives / (truePositives + falsePositives)
recall <- truePositives / (truePositives + falseNegatives)
F1_score <- 2 * (precision * recall) / (precision + recall)

Prediction Page Using the GLM

gold_per_min,net_worth,gold,kills,tower_damage,duration,lane, lane_role

dataModel

predictions <- predict(glmFit, data = c(input\(gpm,input\)networth,input\(gold,input\)kills,input\(tower_damage,input\)duration,input\(lane,input\)lane_role))

dataIndex <- createDataPartition(dataModel$win, p = 0.75, list = FALSE)
dataTrain <- dataModel[dataIndex, ]
dataTest <- dataModel[-dataIndex, ]

glmFit <- train(win ~ ., data = dataTrain, 
         method = "glm", 
         family = "binomial",
         preProcess = c("center", "scale"),
         trControl = trainControl(method = "cv", number = 10))


#Change to be reactive to user data

userData <- data.frame(gold_per_min=600,net_worth=17000,gold=400,kills=11,tower_damage=9000,duration=2500,lane=3,lane_role=3)

userPred<-predict(glmFit, newdata= userData)

userPred
## [1] TRUE
## Levels: FALSE TRUE