>train = read.csv("train.csv")

>test = read.csv("test.csv")

# After reading the given sets, we need to check whether the train set has any NA values
> table(is.na(train))

FALSE   TRUE
100813   1463

# Since there are many true values that indicate the presence of NA values, lets check which columns have NA values
> colSums(is.na(train))
Item_Identifier               Item_Weight          Item_Fat_Content
                        0                      1463                         0
          Item_Visibility                 Item_Type                  Item_MRP
                        0                         0                         0
        Outlet_Identifier Outlet_Establishment_Year               Outlet_Size
                        0                         0                         0
     Outlet_Location_Type               Outlet_Type         Item_Outlet_Sales
                        0                         0                         0

# The column Item_Weight has NA values. So using the median function lets eliminate those NA values and replace it by the median of the column values

> train$Item_Weight[is.na(train$Item_Weight)] <- median(train$Item_Weight, na.rm = TRUE)

# Checking if we have any NA values now left in the column Item_Weight
> table(is.na(train$Item_Weight))

FALSE
14204

> #itemfatcontent
> prop.table(table(train$Item_Fat_Content))

LF    low fat    Low Fat        reg    Regular
0.03675021 0.01253168 0.59736694 0.01372853 0.33962264

 >train$Item_Fat_Content <-gsub("low fat", "LF" , train$Item_Fat_Content)
 > train$Item_Fat_Content <-gsub("LOW FAT", "LF" , train$Item_Fat_Content)
 >train$Item_Fat_Content <-gsub("reg", "Regular" , train$Item_Fat_Content)

 > prop.table(table(train$Item_Fat_Content))
LF   Regular
0.6466488 0.3533512

#find mean of item_visibility

summary(train)
 Item_Identifier  Item_Weight     Item_Fat_Content Item_Visibility
 FDG33  :  10    Min.   : 4.555   LF     : 316     Min.   :0.00000
 FDW13  :  10    1st Qu.: 8.774   low fat: 112     1st Qu.:0.02699
 DRE49  :   9    Median :12.600   Low Fat:5089     Median :0.05393
 DRN47  :   9    Mean   :12.858   reg    : 117     Mean   :0.06613
 FDD38  :   9    3rd Qu.:16.850   Regular:2889     3rd Qu.:0.09459
 FDF52  :   9    Max.   :21.350                    Max.   :0.32839
 (Other):8467
                 Item_Type       Item_MRP      Outlet_Identifier
 Fruits and Vegetables:1232   Min.   : 31.29   OUT027 : 935
 Snack Foods          :1200   1st Qu.: 93.83   OUT013 : 932
 Household            : 910   Median :143.01   OUT035 : 930
 Frozen Foods         : 856   Mean   :140.99   OUT046 : 930
 Dairy                : 682   3rd Qu.:185.64   OUT049 : 930
 Canned               : 649   Max.   :266.89   OUT045 : 929
 (Other)              :2994                    (Other):2937
 Outlet_Establishment_Year Outlet_Size   Outlet_Location_Type
 Min.   :1985                    :2410   Tier 1:2388
 1st Qu.:1987              High  : 932   Tier 2:2785
 Median :1999              Medium:2793   Tier 3:3350
 Mean   :1998              Small :2388
 3rd Qu.:2004
 Max.   :2009

            Outlet_Type   Item_Outlet_Sales
 Grocery Store    :1083   Min.   :   33.29
 Supermarket Type1:5577   1st Qu.:  834.25
 Supermarket Type2: 928   Median : 1794.33
 Supermarket Type3: 935   Mean   : 2181.29
                          3rd Qu.: 3101.30
                          Max.   :13086.97

#Check the column Item_Visibility

 train$Item_Visibility
   [1] 0.016047301 0.019278216 0.016760075 0.000000000 0.000000000 0.000000000
   [7] 0.012741089 0.127469857 0.016687114 0.094449590 0.000000000 0.045463773
  [13] 0.100013500 0.047257328 0.068024300 0.069088961 0.008596051 0.069196376
  [19] 0.034237682 0.102492120 0.138190277 0.035399923 0.025698134 0.057556998
  [25] 0.025896485 0.099887103 0.066693437 0.019356132 0.161466534 0.072221801
  [31] 0.170348551 0.080829372 0.000000000 0.000000000 0.097904029 0.089291137
  [37] 0.059511812 0.113123893 0.123111453 0.026480954 0.102511504 0.026818430
  [43] 0.117818348 0.069102831 0.042277867 0.089498926 0.000000000 0.025879577
  [49] 0.121632721 0.255394896 0.067779712 0.159803853 0.028696932 0.065041581
  [55] 0.028760013 0.010053105 0.048157338 0.071958197 0.173481304 0.061082177
  [61] 0.000000000 0.040071131 0.012090074 0.150285990 0.026015519 0.195068226
  [67] 0.033082215 0.078946455 0.041700756 0.143422643 0.105296072 0.121531501
  [73] 0.000000000 0.011324862 0.186357148 0.024205661 0.033230816 0.113720344
  [79] 0.112556507 0.000000000 0.059352241 0.014087057 0.045230944 0.293417759
  [85] 0.031382044 0.126398860 0.025403898 0.039057677 0.123115764 0.039624006
  [91] 0.040421193 0.105274111 0.033970195 0.053212652 0.024378706 0.009714595
  [97] 0.131383762 0.074613090 0.028459761 0.074627201 0.063695084 0.045797829
 [103] 0.115857223 0.024832806 0.034027909 0.046769600 0.116576702 0.000000000
 [109] 0.278974075 0.035913805 0.079806266 0.005505481 0.064625342 0.071636937
 [115] 0.000000000 0.032677678 0.160760054 0.069110650 0.041177505 0.040081193
 [121] 0.035307322 0.063017847 0.171938781 0.040197932 0.049058014 0.011610960
 [127] 0.183507295 0.102349519 0.086708987 0.032835147 0.033809913 0.072669120
 [133] 0.144338493 0.009163216 0.011307038 0.076855628 0.059956876 0.038532062
 [139] 0.043209581 0.056816465 0.019866705 0.048331717 0.091221855 0.027622076
 [145] 0.000000000 0.039355947 0.060836524 0.082316506 0.155350299 0.076841095
 [151] 0.151419185 0.026001976 0.073209447 0.041683481 0.042560252 0.014081156
 [157] 0.000000000 0.152918384 0.030969274 0.060376776 0.041922850 0.077367431
 [163] 0.045838211 0.071077939 0.094659871 0.077608838 0.021327477 0.042485444
 [169] 0.025578526 0.048980800 0.019471688 0.122918852 0.057373797 0.044155597
 [175] 0.291865402 0.000000000 0.064750025 0.098312421 0.191013663 0.176573035
 [181] 0.060134441 0.007294652 0.128478462 0.075107656 0.049381406 0.045165796
 [187] 0.094153750 0.028907832 0.018059621 0.090436094 0.146399712 0.074264356
 [193] 0.050186726 0.020631654 0.015611080 0.041395445 0.097313129 0.059582996
 [199] 0.098663652 0.101338651 0.052146456 0.041764487 0.054344186 0.078362484
 [205] 0.052339069 0.042464962 0.065732883 0.023625114 0.051643608 0.165694219
 [211] 0.041878397 0.058091482 0.000000000 0.026903714 0.000000000 0.150554711
 [217] 0.000000000 0.134975628 0.156168790 0.000000000 0.133700752 0.115119905
 [223] 0.110030997 0.051038045 0.062837968 0.017116983 0.024129332 0.049754975
 [229] 0.000000000 0.101561568 0.005949644 0.139561160 0.126035694 0.027164679
 [235] 0.099325278 0.043573660 0.082741482 0.064565202 0.084554569 0.058451805
 [241] 0.113748685 0.020603511 0.052044976 0.062764429 0.087977262 0.010010425
 [247] 0.033946163 0.058414678 0.027106459 0.027022883 0.058153409 0.128937661
 [253] 0.088846306 0.093577790 0.165694678 0.148392623 0.098438394 0.143303291
 [259] 0.076276208 0.175103435 0.023511371 0.061052760 0.049559042 0.119418124
 [265] 0.072545601 0.090486828 0.056245075 0.014618973 0.043479126 0.045166237
 [271] 0.057827101 0.089394766 0.105509440 0.005234153 0.036390174 0.032928240
 [277] 0.081252534 0.076552407 0.016758569 0.044248175 0.038288087 0.186032678
 [283] 0.031867464 0.021370201 0.193772568 0.009866049 0.082551042 0.099729888
 [289] 0.018801549 0.119721451 0.071222087 0.060371962 0.033287541 0.058584620
 [295] 0.116792920 0.031452265 0.007213950 0.081196619 0.049916364 0.046270950
 [301] 0.027477160 0.104704537 0.012540003 0.147023834 0.159657596 0.141398626
 [307] 0.148821808 0.000000000 0.029691762 0.096715523 0.014790559 0.054378253
 [313] 0.123428594 0.029943463 0.052368061 0.093861144 0.033968647 0.173154079
 [319] 0.139735226 0.043351896 0.042086652 0.128482690 0.049357077 0.008326735
 [325] 0.021362955 0.151420934 0.032511818 0.022323658 0.019292355 0.048124443
 [331] 0.092613070 0.094697274 0.035984104 0.056596985 0.204700000 0.020618324
 [337] 0.047098210 0.087584126 0.038078899 0.095102092 0.011953902 0.062799379
 [343] 0.031125709 0.026005891 0.092463920 0.107765165 0.010647477 0.015749341
 [349] 0.077628053 0.000000000 0.056695731 0.063993068 0.069529262 0.034452949
 [355] 0.066700680 0.034699737 0.058346939 0.065203103 0.078929571 0.182514881
 [361] 0.016895293 0.073379745 0.080757580 0.112271498 0.153741385 0.127929521
 [367] 0.078385640 0.078502143 0.047684831 0.123159656 0.000000000 0.053392944
 [373] 0.036715907 0.065236932 0.041947547 0.082888496 0.055390121 0.012194556
 [379] 0.060495243 0.000000000 0.014753811 0.160707489 0.085622362 0.046350909
 [385] 0.102920886 0.026604781 0.000000000 0.031860325 0.000000000 0.021250002
 [391] 0.159936948 0.129503146 0.142688846 0.179484411 0.089163056 0.105761491
 [397] 0.056960814 0.007040980 0.011026594 0.093203257 0.026916794 0.035264298
 [403] 0.015908424 0.017719277 0.035334201 0.065652494 0.056919038 0.048143292
 [409] 0.022047313 0.090427268 0.027779254 0.000000000 0.008033611 0.033088472
 [415] 0.160663021 0.178975721 0.017627888 0.019415800 0.016993225 0.027139013
 [421] 0.037772490 0.036746479 0.022460102 0.000000000 0.052135750 0.062508438
 [427] 0.007309544 0.015623754 0.142978223 0.017191055 0.143592939 0.011995271
 [433] 0.157172360 0.061393095 0.264124669 0.020310046 0.054630834 0.052517969
 [439] 0.047565208 0.060797393 0.041291169 0.022604051 0.056580228 0.164196823
 [445] 0.022976497 0.027310252 0.141975462 0.000000000 0.016198660 0.014205168
 [451] 0.093444950 0.019199733 0.154120252 0.089004389 0.011117041 0.103422709
 [457] 0.089035961 0.102723919 0.024733134 0.044871033 0.029760052 0.078999287
 [463] 0.020870744 0.031599715 0.042413704 0.072284689 0.009984847 0.052852621
 [469] 0.014660821 0.076354362 0.076709639 0.005677876 0.000000000 0.012282366
 [475] 0.026681262 0.127541722 0.119513002 0.054463442 0.000000000 0.015756784
 [481] 0.010907967 0.075435560 0.000000000 0.115194717 0.066922802 0.064636203
 [487] 0.103195401 0.131210320 0.035747522 0.192650072 0.098031771 0.030118338
 [493] 0.016201845 0.058373080 0.087652908 0.065999008 0.088750333 0.100277876
 [499] 0.099252438 0.000000000 0.000000000 0.026384672 0.228993134 0.065431917
 [505] 0.026749991 0.000000000 0.043333912 0.089656812 0.039954281 0.087854925
 [511] 0.149404057 0.026895200 0.065857092 0.161817014 0.082197959 0.015335349
 [517] 0.005990720 0.013898123 0.078912473 0.059776237 0.000000000 0.297883712
 [523] 0.022059594 0.035324940 0.124967595 0.062411404 0.032754431 0.160178832
 [529] 0.104035148 0.063900190 0.062648111 0.111685868 0.233039817 0.178245750
 [535] 0.140241213 0.000000000 0.033935576 0.016516275 0.026537206 0.015673267
 [541] 0.089083914 0.131152799 0.083109455 0.045818773 0.044869794 0.000000000
 [547] 0.046057810 0.030815427 0.018060550 0.035205866 0.076705948 0.089677773
 [553] 0.132327406 0.065520388 0.012518707 0.050353695 0.110762642 0.045050674
 [559] 0.083016832 0.000000000 0.023983258 0.075836522 0.100330684 0.051778173
 [565] 0.022970468 0.066014596 0.074299450 0.131128467 0.014816356 0.049318315
 [571] 0.017960492 0.035801238 0.038606365 0.008871687 0.125793938 0.058369013
 [577] 0.000000000 0.073269336 0.049677651 0.031470832 0.024579432 0.131665479
 [583] 0.015523707 0.122475364 0.107891398 0.128065918 0.045361705 0.090390357
 [589] 0.087029712 0.052054819 0.051952573 0.050786366 0.067141355 0.121485195
 [595] 0.080640478 0.037699254 0.000000000 0.034501405 0.053795154 0.044004675
 [601] 0.000000000 0.121250374 0.000000000 0.100230114 0.167383061 0.034404732
 [607] 0.082719632 0.041913535 0.013204300 0.041105789 0.141106202 0.036463975
 [613] 0.019464180 0.000000000 0.123086812 0.105646853 0.118149377 0.046545786
 [619] 0.065009745 0.016630303 0.134532392 0.083133128 0.039428167 0.000000000
 [625] 0.034823127 0.148492521 0.065901299 0.041622264 0.064933297 0.094549300
 [631] 0.107076832 0.128674983 0.188322664 0.000000000 0.052025392 0.065316099
 [637] 0.015162650 0.053584207 0.020989132 0.144017405 0.115978122 0.081465335
 [643] 0.041177505 0.042352822 0.074541867 0.184541210 0.114160573 0.175352413
 [649] 0.083590755 0.029471408 0.028282832 0.000000000 0.008208342 0.033349290
 [655] 0.076060037 0.014301326 0.074931201 0.080131363 0.167444431 0.030337480
 [661] 0.048749690 0.014788594 0.111298111 0.052685260 0.018485511 0.184689756
 [667] 0.064153845 0.092516598 0.104245198 0.041779181 0.072269850 0.179806961
 [673] 0.044995631 0.053523765 0.041459372 0.036234918 0.105445801 0.000000000
 [679] 0.059971330 0.075595405 0.210375806 0.059341019 0.185209440 0.098566832
 [685] 0.110844170 0.035015200 0.023567538 0.000000000 0.047927220 0.036207786
 [691] 0.044377096 0.034742632 0.116915909 0.062979148 0.068604503 0.032948610
 [697] 0.007450258 0.093027718 0.164864915 0.083498083 0.014522363 0.025963795
 [703] 0.019012262 0.059393919 0.027212058 0.013263968 0.079113947 0.030208510
 [709] 0.049634817 0.047187038 0.066681333 0.185218446 0.162462044 0.031273559
 [715] 0.042836778 0.114127418 0.146895866 0.013420879 0.026181893 0.012184999
 [721] 0.026428924 0.045243613 0.000000000 0.076790922 0.045950475 0.133673087
 [727] 0.060252433 0.082529704 0.082552214 0.089272253 0.072541731 0.017536671
 [733] 0.072088552 0.092781435 0.008644894 0.025342692 0.031145743 0.000000000
 [739] 0.030108283 0.044391149 0.022639471 0.000000000 0.139419417 0.015278899
 [745] 0.039329257 0.008579863 0.028164527 0.000000000 0.054367971 0.027346700
 [751] 0.016001434 0.165328057 0.013529884 0.030510526 0.027699863 0.096467330
 [757] 0.011636708 0.141642219 0.044767801 0.028009382 0.116445500 0.013584408
 [763] 0.026950104 0.054751689 0.076121831 0.058516562 0.037923509 0.058365706
 [769] 0.031346387 0.015663185 0.063750302 0.072524760 0.038040837 0.049823902
 [775] 0.014560297 0.026408698 0.069446588 0.032171320 0.026046138 0.121848436
 [781] 0.108470504 0.000000000 0.038494623 0.122830885 0.038029746 0.065207559
 [787] 0.037014587 0.133281968 0.045299563 0.121149472 0.094083303 0.000000000
 [793] 0.000000000 0.035022503 0.016060186 0.009014978 0.096733815 0.102607233
 [799] 0.024521191 0.160624116 0.052165855 0.000000000 0.000000000 0.077536540
 [805] 0.024928351 0.080711180 0.080968973 0.057485328 0.149669322 0.024776026
 [811] 0.061165512 0.041718456 0.061008860 0.167799329 0.167351411 0.071832909
 [817] 0.041886576 0.150341867 0.020542737 0.045047439 0.091008572 0.075493480
 [823] 0.156286566 0.033735909 0.078547351 0.093653464 0.036107198 0.109003832
 [829] 0.073480266 0.113234914 0.037711338 0.016054884 0.021170542 0.065188619
 [835] 0.117537563 0.014303240 0.137697119 0.047008497 0.000000000 0.060863167
 [841] 0.000000000 0.045105407 0.076855207 0.024482644 0.000000000 0.020474913
 [847] 0.062044506 0.220225608 0.079794329 0.114916546 0.005973620 0.146973462
 [853] 0.073766310 0.049349121 0.328390948 0.132670580 0.012455787 0.058142797
 [859] 0.006289291 0.049295020 0.054362695 0.110356797 0.085274988 0.164542555
 [865] 0.160292264 0.112859454 0.021569566 0.073055148 0.119345629 0.072334667
 [871] 0.108148913 0.075853370 0.062655235 0.022278477 0.020961192 0.025993423
 [877] 0.018838681 0.105577348 0.129086113 0.113125627 0.070699313 0.151467821
 [883] 0.036164160 0.062606583 0.070437800 0.079844043 0.120139206 0.032028115
 [889] 0.120497266 0.023492524 0.122657336 0.070588037 0.073024401 0.159587755
 [895] 0.088821765 0.136810742 0.128682722 0.109459733 0.096644015 0.013120028
 [901] 0.000000000 0.048339408 0.081658450 0.049753390 0.030379382 0.065810045
 [907] 0.000000000 0.025272781 0.019314961 0.071282168 0.184843579 0.122896411
 [913] 0.082935004 0.161350176 0.106968096 0.159582185 0.144538270 0.038685802
 [919] 0.172309903 0.037690731 0.057870080 0.026409147 0.064367627 0.173516063
 [925] 0.128980880 0.048115543 0.077899109 0.130932750 0.098464979 0.037372847
 [931] 0.032230526 0.040357315 0.074613090 0.160936178 0.019502354 0.065577449
 [937] 0.016944719 0.065576228 0.025795770 0.010008699 0.053362086 0.064621927
 [943] 0.018126724 0.000000000 0.049263979 0.021343732 0.110681931 0.061955439
 [949] 0.076483451 0.024701262 0.026718210 0.016597651 0.051411100 0.112985849
 [955] 0.114117090 0.000000000 0.175033524 0.020760673 0.061405051 0.026676216
 [961] 0.029380407 0.025715562 0.065026434 0.066315023 0.023664054 0.154627247
 [967] 0.205294827 0.152261999 0.000000000 0.136275173 0.174621343 0.038597077
 [973] 0.029799965 0.011314423 0.072410764 0.028118435 0.163350221 0.093307713
 [979] 0.105994654 0.067055339 0.107376743 0.043810028 0.118672537 0.126897925
 [985] 0.014076503 0.088305478 0.090153756 0.182501773 0.108710162 0.058446424
 [991] 0.054990009 0.155288310 0.018021361 0.081655900 0.010755465 0.050111641
 [997] 0.048931174 0.065960909 0.036319195 0.039055756

# Since, Item_Visibility has values such as O..which is ridiculous..and hence finding values with 0 in item_visibility and replacing it with the mean found above in item_visibility

rapply(train,function(x) ifelse(x==0,0.06613 ,x), how = "replace")
      Item_Identifier Item_Weight Item_Fat_Content Item_Visibility Item_Type Item_MRP
1                157       9.300                3     0.016047301         5 249.8092
2                  9       5.920                5     0.019278216        15  48.2692
3                663      17.500                3     0.016760075        11 141.6180
4               1122      19.200                5     0.066130000         7 182.0950
5               1298       8.930                3     0.066130000        10  53.8614
6                759      10.395                5     0.066130000         1  51.4008
7                697      13.650                5     0.012741089        14  57.6588
8                739          NA                3     0.127469857        14 107.7622
9                441      16.200                5     0.016687114         6  96.9726
10               991      19.200                5     0.094449590         6 187.8214
11              1172      11.800                3     0.066130000         7  45.5402
12               148      18.500                5     0.045463773         5 144.1102
13              1143      15.100                5     0.100013500         7 145.4786
14               905      17.600                5     0.047257328        14 119.6782
15               376      16.350                3     0.068024300         7 196.4426
16               768       9.000                5     0.069088961         3  56.3614
17              1281      11.800                3     0.008596051         9 115.3492
18               768       9.000                5     0.069196376         3  54.3614
19                80          NA                3     0.034237682         8 113.2834
20               969      13.350                3     0.102492120         5 230.5352
21               667      18.850                5     0.138190277        14 250.8724
22              1077          NA                5     0.035399923         1 144.5444
23              1279      14.600                3     0.025698134        10 196.5084
24               260          NA                3     0.057556998         1 107.6938
25               844      13.850                5     0.025896485         6 165.0210
26              1295      13.000                3     0.099887103        10  45.9060
27              1025       7.645                5     0.066693437        14  42.3112
28               102      11.650                2     0.019356132         8  39.1164
29               351       5.925                5     0.161466534         5  45.5086
30               245          NA                5     0.072221801         4  43.6454
31              1049      19.250                3     0.170348551         5  55.7956
32              1481      18.600                3     0.080829372         9  96.4436
33               757      18.700                3     0.066130000        14 256.6672
34               707      17.850                3     0.066130000         2  93.1436
35                65      17.500                3     0.097904029        15 174.8738
36              1534      10.000                3     0.089291137         9 146.7102
37              1033          NA                5     0.059511812         7 128.0678
38               144       8.850                5     0.113123893        15 122.5388
39              1125          NA                5     0.123111453        14  36.9874
40               215          NA                3     0.026480954        14  87.6198
41               969      13.350                3     0.102511504         5 230.6352
42               568       9.800                3     0.026818430        11 126.0020
43               181      13.600                3     0.117818348        14 192.9136
44               237      21.350                3     0.069102831         4 259.9278
45               612      12.150                5     0.042277867         4 126.5046
46               642       6.420                1     0.089498926         5 178.1002
47              1439      19.600                3     0.066130000         9 153.3024
48              1058      10.000                3     0.025879577         4 265.2226
49               587      15.850                5     0.121632721         1  60.6220
50               870          NA                5     0.255394896         5 196.8794
51              1379       7.390                3     0.067779712         9 143.8812
52               643      10.195                3     0.159803853         6 141.5154
53               831       9.895                5     0.028696932         4 117.0492
54               178      10.895                3     0.065041581         7 196.3794
55              1443      12.150                3     0.028760013        10 151.4708
56               554       7.905                3     0.010053105        14 249.0408
57              1539       9.195                3     0.048157338        10 106.1622
58               108       8.365                3     0.071958197         8  38.0506
59              1184      15.100                3     0.173481304        14 194.5110
60               486          NA                3     0.061082177         4 180.0344
61               630      10.000                3     0.066130000         7 246.9144
62              1040       7.970                5     0.040071131        11  87.3514
63               362          NA                3     0.012090074         7  38.2848
64              1200          NA                5     0.150285990         6  51.0692
65              1203          NA                3     0.026015519        14 255.8356
66               267      17.700                1     0.195068226        14 185.4266
67               442      19.350                3     0.033082215        11 172.5738
68              1219      13.650                5     0.078946455         5 186.0240
69                73      17.600                3     0.041700756        15 164.8526
70              1344       8.645                3     0.143422643         9  95.8410
71               542      15.600                5     0.105296072        14 170.1764
72               587      15.850                5     0.121531501         1  59.2220
73               454      18.250                3     0.066130000        16 164.7526
74               395       7.855                3     0.011324862         4 189.6188
75              1553       7.825                2     0.186357148        10 254.3698
76               255       8.390                5     0.024205661         6 114.0176
77               782      12.850                3     0.033230816        14 172.3422
78               683          NA                3     0.113720344         1  89.9804
79               583      19.000                3     0.112556507         6 104.9622
80              1038       5.905                3     0.066130000         4 222.5456
81               317       7.760                3     0.059352241        14 101.8700
82               657      11.800                4     0.014087057         6 180.3344
83              1054      16.750                2     0.045230944        14 187.9556
     Outlet_Identifier Outlet_Establishment_Year Outlet_Size Outlet_Location_Type
1                   10                      1999           3                    1
2                    4                      2009           3                    3
3                   10                      1999           3                    1
4                    1                      1998           1                    3
5                    2                      1987           2                    3
6                    4                      2009           3                    3
7                    2                      1987           2                    3
8                    6                      1985           3                    3
9                    8                      2002           1                    2
10                   3                      2007           1                    2
11                  10                      1999           3                    1
12                   9                      1997           4                    1
13                  10                      1999           3                    1
14                   9                      1997           4                    1
15                   2                      1987           2                    3
16                   9                      1997           4                    1
17                   4                      2009           3                    3
18                  10                      1999           3                    1
19                   6                      1985           3                    3
20                   7                      2004           4                    2
21                   2                      1987           2                    3
22                   6                      1985           3                    3
23                   7                      2004           4                    2
24                   5                      1985           4                    1
25                   9                      1997           4                    1
26                   3                      2007           1                    2
27                   7                      2004           4                    2
28                   2                      1987           2                    3
29                   1                      1998           1                    3
30                   5                      1985           4                    1
31                   1                      1998           1                    3
32                   4                      2009           3                    3
33                   4                      2009           3                    3
34                   8                      2002           1                    2
35                   9                      1997           4                    1
36                  10                      1999           3                    1
37                   6                      1985           3                    3
38                   4                      2009           3                    3
39                   6                      1985           3                    3
40                   6                      1985           3                    3
41                   9                      1997           4                    1
42                   2                      1987           2                    3
43                  10                      1999           3                    1
44                   4                      2009           3                    3
45                   2                      1987           2                    3
46                   1                      1998           1                    3
47                   8                      2002           1                    2
48                   8                      2002           1                    2
49                   9                      1997           4                    1
50                   5                      1985           4                    1
51                   9                      1997           4                    1
52                   2                      1987           2                    3
53                   2                      1987           2                    3
54                   3                      2007           1                    2
55                   3                      2007           1                    2
56                   4                      2009           3                    3
57                   8                      2002           1                    2
58                  10                      1999           3                    1
59                   9                      1997           4                    1
60                   5                      1985           4                    1
61                   4                      2009           3                    3
62                   8                      2002           1                    2
63                   6                      1985           3                    3
64                   5                      1985           4                    1
65                   6                      1985           3                    3
66                   1                      1998           1                    3
67                   7                      2004           4                    2
68                   8                      2002           1                    2
69                   8                      2002           1                    2
70                   9                      1997           4                    1
71                   9                      1997           4                    1
72                   2                      1987           2                    3
73                   8                      2002           1                    2
74                   3                      2007           1                    2
75                  10                      1999           3                    1
76                   9                      1997           4                    1
77                  10                      1999           3                    1
78                   5                      1985           4                    1
79                   3                      2007           1                    2
80                   8                      2002           1                    2
81                   9                      1997           4                    1
82                   9                      1997           4                    1
83                   4                      2009           3                    3
     Outlet_Type Item_Outlet_Sales
1              2         3735.1380
2              3          443.4228
3              2         2097.2700
4              1          732.3800
5              2          994.7052
6              3          556.6088
7              2          343.5528
8              4         4022.7636
9              2         1076.5986
10             2         4710.5350
11             2         1516.0266
12             2         2187.1530
13             2         1589.2646
14             2         2145.2076
15             2         1977.4260
16             2         1547.3192
17             3         1621.8888
18             2          718.3982
19             4         2303.6680
20             2         2748.4224
21             2         3775.0860
22             4         4064.0432
23             2         1587.2672
24             1          214.3876
25             2         4078.0250
26             2          838.9080
27             2         1065.2800
28             2          308.9312
29             1          178.4344
30             1          125.8362
31             1          163.7868
32             3         2741.7644
33             3         3068.0064
34             2         2174.5028
35             2         2085.2856
36             2         3791.0652
37             4         2797.6916
38             3         1609.9044
39             4          388.1614
40             4         2180.4950
41             2         3435.5280
42             2         2150.5340
43             2         2527.3768
44             3         6768.5228
45             2          373.5138
46             1          358.2004
47             2         2428.8384
48             2         5815.0972
49             2         2576.6460
50             1          780.3176
51             2         3134.5864
52             2          850.8924
53             2          810.9444
54             2         3121.2704
55             2         4815.0656
56             3         6258.5200
57             2         2117.2440
58             2          796.9626
59             2         4910.2750
60             1          892.1720
61             3         3185.1872
62             2         1062.6168
63             4          484.7024
64             1          147.8076
65             4         2543.3560
66             1          184.4266
67             2         4865.6664
68             2         1118.5440
69             2         2302.3364
70             2         2027.3610
71             2         3435.5280
72             2          599.2200
73             2         4604.6728
74             2         2285.0256
75             2         5580.7356
76             2         2290.3520
77             2         1207.0954
78             1          643.1628
79             2         1587.9330
80             2         5305.0944
81             2         1697.7900
82             2         1427.4752
83             3         4693.8900
 [ reached getOption("max.print") -- omitted 8440 rows ]

#seperating the first two characters of item_identifier and storing in another variable and giving its count..then renaming it

 train$Item_Identifier_new <- substr(train$Item_Identifier, 1 , 2)
 train$Item_Identifier_new
   [1] "FD" "DR" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
  [15] "FD" "FD" "NC" "FD" "DR" "FD" "FD" "FD" "NC" "FD" "FD" "NC" "FD" "DR"
  [29] "FD" "FD" "FD" "NC" "FD" "FD" "DR" "NC" "FD" "DR" "FD" "FD" "FD" "FD"
  [43] "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD"
  [57] "NC" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "NC"
  [71] "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC"
  [85] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "DR" "FD" "NC" "FD" "FD" "FD"
  [99] "NC" "FD" "FD" "FD" "FD" "DR" "FD" "FD" "FD" "FD" "DR" "DR" "FD" "FD"
 [113] "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD" "NC" "DR" "FD" "FD" "FD" "FD"
 [127] "DR" "FD" "DR" "NC" "FD" "NC" "NC" "FD" "FD" "NC" "FD" "FD" "FD" "NC"
 [141] "NC" "NC" "NC" "NC" "FD" "FD" "FD" "NC" "FD" "NC" "DR" "FD" "FD" "DR"
 [155] "FD" "NC" "FD" "FD" "DR" "FD" "FD" "FD" "FD" "DR" "FD" "DR" "FD" "FD"
 [169] "NC" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "DR" "FD" "FD" "FD"
 [183] "NC" "FD" "FD" "FD" "FD" "NC" "FD" "NC" "FD" "FD" "FD" "FD" "NC" "FD"
 [197] "FD" "NC" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD" "FD"
 [211] "DR" "DR" "FD" "NC" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [225] "DR" "NC" "FD" "DR" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "DR" "FD" "FD"
 [239] "FD" "NC" "NC" "NC" "NC" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "DR" "FD"
 [253] "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "DR" "FD" "FD" "FD" "NC"
 [267] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [281] "FD" "NC" "NC" "DR" "FD" "FD" "FD" "NC" "FD" "NC" "DR" "FD" "FD" "FD"
 [295] "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "NC" "FD" "DR" "DR" "FD" "FD"
 [309] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD"
 [323] "DR" "FD" "FD" "NC" "FD" "FD" "NC" "FD" "DR" "FD" "FD" "FD" "FD" "FD"
 [337] "NC" "FD" "FD" "FD" "FD" "FD" "NC" "NC" "FD" "DR" "FD" "NC" "NC" "FD"
 [351] "FD" "FD" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "NC" "FD" "FD"
 [365] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "NC" "FD" "FD" "FD" "FD" "FD"
 [379] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD" "FD"
 [393] "FD" "FD" "NC" "FD" "FD" "FD" "FD" "NC" "NC" "NC" "FD" "NC" "FD" "FD"
 [407] "FD" "FD" "FD" "DR" "FD" "NC" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "NC"
 [421] "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR"
 [435] "FD" "DR" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "DR"
 [449] "NC" "NC" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "DR" "NC" "FD" "FD"
 [463] "FD" "NC" "DR" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD"
 [477] "NC" "FD" "FD" "NC" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [491] "NC" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "NC" "FD" "FD" "FD"
 [505] "FD" "FD" "FD" "NC" "NC" "DR" "FD" "FD" "FD" "FD" "FD" "DR" "NC" "DR"
 [519] "FD" "NC" "FD" "FD" "NC" "FD" "NC" "FD" "NC" "NC" "FD" "FD" "FD" "NC"
 [533] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "DR" "FD"
 [547] "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD"
 [561] "DR" "DR" "FD" "FD" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD"
 [575] "DR" "FD" "DR" "DR" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC"
 [589] "FD" "NC" "NC" "FD" "NC" "FD" "NC" "FD" "FD" "NC" "FD" "DR" "FD" "FD"
 [603] "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD"
 [617] "FD" "DR" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [631] "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR"
 [645] "NC" "FD" "NC" "DR" "FD" "FD" "FD" "FD" "FD" "DR" "FD" "NC" "FD" "FD"
 [659] "NC" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "DR" "FD"
 [673] "FD" "FD" "NC" "NC" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [687] "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "NC" "NC" "NC" "FD"
 [701] "DR" "NC" "NC" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "NC" "NC" "DR" "FD"
 [715] "FD" "FD" "FD" "NC" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD"
 [729] "FD" "FD" "FD" "FD" "DR" "FD" "NC" "FD" "NC" "FD" "FD" "FD" "FD" "FD"
 [743] "FD" "FD" "NC" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD"
 [757] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "NC"
 [771] "FD" "FD" "DR" "DR" "FD" "FD" "DR" "FD" "FD" "FD" "NC" "FD" "FD" "FD"
 [785] "FD" "FD" "FD" "FD" "DR" "FD" "NC" "FD" "FD" "NC" "FD" "FD" "FD" "FD"
 [799] "FD" "FD" "NC" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD"
 [813] "FD" "DR" "NC" "DR" "FD" "FD" "NC" "FD" "DR" "FD" "FD" "NC" "FD" "FD"
 [827] "FD" "FD" "FD" "DR" "NC" "FD" "FD" "DR" "FD" "NC" "FD" "NC" "FD" "FD"
 [841] "FD" "FD" "DR" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD"
 [855] "FD" "DR" "FD" "FD" "DR" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD"
 [869] "NC" "FD" "FD" "NC" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "DR" "FD" "FD"
 [883] "NC" "FD" "DR" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD" "FD"
 [897] "FD" "FD" "FD" "NC" "FD" "FD" "NC" "FD" "NC" "FD" "DR" "NC" "DR" "NC"
 [911] "NC" "FD" "FD" "DR" "FD" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD"
 [925] "FD" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "FD"
 [939] "FD" "NC" "DR" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD"
 [953] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "NC" "FD" "FD" "FD"
 [967] "DR" "FD" "NC" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "FD" "NC" "FD" "FD"
 [981] "FD" "FD" "FD" "FD" "FD" "FD" "FD" "NC" "FD" "FD" "NC" "FD" "FD" "NC"
 [995] "DR" "FD" "NC" "FD" "DR" "NC"
 [ reached getOption("max.print") -- omitted 7523 entries ]

> table(train$Item_Identifier_new)

  DR   FD   NC
 799 6125 1599
>

#Replacing with better identifiable values using the function gsub
                                                                                t>train$Item_Identifier_new <-gsub("FD", "Food" , train$Item_Identifier_new)
>train$Item_Identifier_new <-gsub("NC", "Non consumable" , train$Item_Identifier_new)
>train$Item_Identifier_new <-gsub("DR", "Drinks" , train$Item_Identifier_new)
>train$Item_Identifier_new
   [1] "Food"           "Drinks"         "Food"           "Food"
   [5] "Non consumable" "Food"           "Food"           "Food"
   [9] "Food"           "Food"           "Food"           "Food"
  [13] "Food"           "Food"           "Food"           "Food"
  [17] "Non consumable" "Food"           "Drinks"         "Food"
  [21] "Food"           "Food"           "Non consumable" "Food"
  [25] "Food"           "Non consumable" "Food"           "Drinks"
  [29] "Food"           "Food"           "Food"           "Non consumable"
  [33] "Food"           "Food"           "Drinks"         "Non consumable"
  [37] "Food"           "Drinks"         "Food"           "Food"
  [41] "Food"           "Food"           "Food"           "Food"
  [45] "Food"           "Food"           "Non consumable" "Food"
  [49] "Food"           "Food"           "Non consumable" "Food"
  [53] "Food"           "Food"           "Non consumable" "Food"
  [57] "Non consumable" "Drinks"         "Food"           "Food"
  [61] "Food"           "Food"           "Food"           "Food"
  [65] "Food"           "Food"           "Food"           "Food"
  [69] "Drinks"         "Non consumable" "Food"           "Food"
  [73] "Food"           "Food"           "Non consumable" "Food"
  [77] "Food"           "Food"           "Food"           "Food"
  [81] "Food"           "Food"           "Food"           "Non consumable"
  [85] "Food"           "Food"           "Food"           "Food"
  [89] "Food"           "Food"           "Food"           "Drinks"
  [93] "Drinks"         "Food"           "Non consumable" "Food"
  [97] "Food"           "Food"           "Non consumable" "Food"
 [101] "Food"           "Food"           "Food"           "Drinks"
 [105] "Food"           "Food"           "Food"           "Food"
 [109] "Drinks"         "Drinks"         "Food"           "Food"
 [113] "Food"           "Food"           "Food"           "Food"
 [117] "Food"           "Food"           "Drinks"         "Food"
 [121] "Non consumable" "Drinks"         "Food"           "Food"
 [125] "Food"           "Food"           "Drinks"         "Food"
 [129] "Drinks"         "Non consumable" "Food"           "Non consumable"
 [133] "Non consumable" "Food"           "Food"           "Non consumable"
 [137] "Food"           "Food"           "Food"           "Non consumable"
 [141] "Non consumable" "Non consumable" "Non consumable" "Non consumable"
 [145] "Food"           "Food"           "Food"           "Non consumable"
 [149] "Food"           "Non consumable" "Drinks"         "Food"
 [153] "Food"           "Drinks"         "Food"           "Non consumable"
 [157] "Food"           "Food"           "Drinks"         "Food"
 [161] "Food"           "Food"           "Food"           "Drinks"
 [165] "Food"           "Drinks"         "Food"           "Food"
 [169] "Non consumable" "Food"           "Non consumable" "Food"
 [173] "Food"           "Food"           "Food"           "Food"
 [177] "Food"           "Non consumable" "Drinks"         "Food"
 [181] "Food"           "Food"           "Non consumable" "Food"
 [185] "Food"           "Food"           "Food"           "Non consumable"
 [189] "Food"           "Non consumable" "Food"           "Food"
 [193] "Food"           "Food"           "Non consumable" "Food"
 [197] "Food"           "Non consumable" "Food"           "Food"
 [201] "Non consumable" "Food"           "Food"           "Food"
 [205] "Food"           "Food"           "Food"           "Drinks"
 [209] "Food"           "Food"           "Drinks"         "Drinks"
 [213] "Food"           "Non consumable" "Non consumable" "Food"
 [217] "Food"           "Food"           "Food"           "Food"
 [221] "Food"           "Food"           "Food"           "Food"
 [225] "Drinks"         "Non consumable" "Food"           "Drinks"
 [229] "Food"           "Food"           "Non consumable" "Food"
 [233] "Food"           "Food"           "Non consumable" "Drinks"
 [237] "Food"           "Food"           "Food"           "Non consumable"
 [241] "Non consumable" "Non consumable" "Non consumable" "Food"
 [245] "Drinks"         "Food"           "Food"           "Food"
 [249] "Food"           "Food"           "Drinks"         "Food"
 [253] "Food"           "Food"           "Food"           "Food"
 [257] "Food"           "Non consumable" "Food"           "Food"
 [261] "Food"           "Drinks"         "Food"           "Food"
 [265] "Food"           "Non consumable" "Food"           "Food"
 [269] "Food"           "Food"           "Food"           "Food"
 [273] "Food"           "Food"           "Food"           "Food"
 [277] "Food"           "Food"           "Food"           "Food"
 [281] "Food"           "Non consumable" "Non consumable" "Drinks"
 [285] "Food"           "Food"           "Food"           "Non consumable"
 [289] "Food"           "Non consumable" "Drinks"         "Food"
 [293] "Food"           "Food"           "Food"           "Food"
 [297] "Food"           "Non consumable" "Food"           "Food"
 [301] "Food"           "Food"           "Non consumable" "Food"
 [305] "Drinks"         "Drinks"         "Food"           "Food"
 [309] "Food"           "Food"           "Food"           "Food"
 [313] "Food"           "Food"           "Food"           "Food"
 [317] "Food"           "Food"           "Food"           "Food"
 [321] "Drinks"         "Food"           "Drinks"         "Food"
 [325] "Food"           "Non consumable" "Food"           "Food"
 [329] "Non consumable" "Food"           "Drinks"         "Food"
 [333] "Food"           "Food"           "Food"           "Food"
 [337] "Non consumable" "Food"           "Food"           "Food"
 [341] "Food"           "Food"           "Non consumable" "Non consumable"
 [345] "Food"           "Drinks"         "Food"           "Non consumable"
 [349] "Non consumable" "Food"           "Food"           "Food"
 [353] "Food"           "Drinks"         "Food"           "Food"
 [357] "Food"           "Food"           "Food"           "Food"
 [361] "Drinks"         "Non consumable" "Food"           "Food"
 [365] "Food"           "Food"           "Food"           "Food"
 [369] "Food"           "Food"           "Food"           "Non consumable"
 [373] "Non consumable" "Food"           "Food"           "Food"
 [377] "Food"           "Food"           "Food"           "Food"
 [381] "Food"           "Food"           "Food"           "Food"
 [385] "Food"           "Food"           "Food"           "Food"
 [389] "Food"           "Drinks"         "Food"           "Food"
 [393] "Food"           "Food"           "Non consumable" "Food"
 [397] "Food"           "Food"           "Food"           "Non consumable"
 [401] "Non consumable" "Non consumable" "Food"           "Non consumable"
 [405] "Food"           "Food"           "Food"           "Food"
 [409] "Food"           "Drinks"         "Food"           "Non consumable"
 [413] "Food"           "Food"           "Non consumable" "Food"
 [417] "Food"           "Food"           "Food"           "Non consumable"
 [421] "Food"           "Food"           "Food"           "Food"
 [425] "Non consumable" "Food"           "Food"           "Food"
 [429] "Food"           "Food"           "Food"           "Food"
 [433] "Food"           "Drinks"         "Food"           "Drinks"
 [437] "Non consumable" "Food"           "Food"           "Food"
 [441] "Food"           "Food"           "Food"           "Non consumable"
 [445] "Food"           "Food"           "Food"           "Drinks"
 [449] "Non consumable" "Non consumable" "Food"           "Food"
 [453] "Food"           "Food"           "Food"           "Non consumable"
 [457] "Food"           "Food"           "Drinks"         "Non consumable"
 [461] "Food"           "Food"           "Food"           "Non consumable"
 [465] "Drinks"         "Food"           "Non consumable" "Food"
 [469] "Food"           "Food"           "Non consumable" "Food"
 [473] "Food"           "Food"           "Food"           "Food"
 [477] "Non consumable" "Food"           "Food"           "Non consumable"
 [481] "Food"           "Drinks"         "Food"           "Food"
 [485] "Food"           "Food"           "Food"           "Food"
 [489] "Food"           "Food"           "Non consumable" "Food"
 [493] "Non consumable" "Food"           "Food"           "Food"
 [497] "Food"           "Food"           "Food"           "Non consumable"
 [501] "Non consumable" "Food"           "Food"           "Food"
 [505] "Food"           "Food"           "Food"           "Non consumable"
 [509] "Non consumable" "Drinks"         "Food"           "Food"
 [513] "Food"           "Food"           "Food"           "Drinks"
 [517] "Non consumable" "Drinks"         "Food"           "Non consumable"
 [521] "Food"           "Food"           "Non consumable" "Food"
 [525] "Non consumable" "Food"           "Non consumable" "Non consumable"
 [529] "Food"           "Food"           "Food"           "Non consumable"
 [533] "Food"           "Food"           "Food"           "Food"
 [537] "Food"           "Food"           "Food"           "Non consumable"
 [541] "Food"           "Food"           "Food"           "Food"
 [545] "Drinks"         "Food"           "Food"           "Food"
 [549] "Non consumable" "Food"           "Food"           "Food"
 [553] "Food"           "Food"           "Food"           "Food"
 [557] "Non consumable" "Food"           "Food"           "Food"
 [561] "Drinks"         "Drinks"         "Food"           "Food"
 [565] "Food"           "Drinks"         "Food"           "Food"
 [569] "Food"           "Food"           "Food"           "Non consumable"
 [573] "Food"           "Food"           "Drinks"         "Food"
 [577] "Drinks"         "Drinks"         "Food"           "Non consumable"
 [581] "Food"           "Food"           "Food"           "Food"
 [585] "Food"           "Food"           "Food"           "Non consumable"
 [589] "Food"           "Non consumable" "Non consumable" "Food"
 [593] "Non consumable" "Food"           "Non consumable" "Food"
 [597] "Food"           "Non consumable" "Food"           "Drinks"
 [601] "Food"           "Food"           "Food"           "Food"
 [605] "Non consumable" "Food"           "Food"           "Food"
 [609] "Non consumable" "Food"           "Food"           "Food"
 [613] "Non consumable" "Food"           "Food"           "Food"
 [617] "Food"           "Drinks"         "Food"           "Food"
 [621] "Food"           "Food"           "Non consumable" "Food"
 [625] "Food"           "Food"           "Food"           "Food"
 [629] "Food"           "Food"           "Drinks"         "Food"
 [633] "Food"           "Food"           "Food"           "Food"
 [637] "Food"           "Food"           "Food"           "Food"
 [641] "Food"           "Food"           "Food"           "Drinks"
 [645] "Non consumable" "Food"           "Non consumable" "Drinks"
 [649] "Food"           "Food"           "Food"           "Food"
 [653] "Food"           "Drinks"         "Food"           "Non consumable"
 [657] "Food"           "Food"           "Non consumable" "Food"
 [661] "Food"           "Food"           "Food"           "Food"
 [665] "Food"           "Non consumable" "Food"           "Food"
 [669] "Food"           "Food"           "Drinks"         "Food"
 [673] "Food"           "Food"           "Non consumable" "Non consumable"
 [677] "Food"           "Non consumable" "Food"           "Food"
 [681] "Food"           "Food"           "Food"           "Food"
 [685] "Food"           "Food"           "Food"           "Food"
 [689] "Food"           "Food"           "Food"           "Non consumable"
 [693] "Food"           "Food"           "Food"           "Non consumable"
 [697] "Non consumable" "Non consumable" "Non consumable" "Food"
 [701] "Drinks"         "Non consumable" "Non consumable" "Non consumable"
 [705] "Food"           "Food"           "Food"           "Non consumable"
 [709] "Food"           "Food"           "Non consumable" "Non consumable"
 [713] "Drinks"         "Food"           "Food"           "Food"
 [717] "Food"           "Non consumable" "Food"           "Non consumable"
 [721] "Food"           "Food"           "Food"           "Food"
 [725] "Food"           "Food"           "Food"           "Food"
 [729] "Food"           "Food"           "Food"           "Food"
 [733] "Drinks"         "Food"           "Non consumable" "Food"
 [737] "Non consumable" "Food"           "Food"           "Food"
 [741] "Food"           "Food"           "Food"           "Food"
 [745] "Non consumable" "Food"           "Drinks"         "Food"
 [749] "Food"           "Food"           "Food"           "Food"
 [753] "Food"           "Food"           "Drinks"         "Food"
 [757] "Food"           "Food"           "Food"           "Food"
 [761] "Food"           "Food"           "Food"           "Non consumable"
 [765] "Food"           "Food"           "Food"           "Food"
 [769] "Food"           "Non consumable" "Food"           "Food"
 [773] "Drinks"         "Drinks"         "Food"           "Food"
 [777] "Drinks"         "Food"           "Food"           "Food"
 [781] "Non consumable" "Food"           "Food"           "Food"
 [785] "Food"           "Food"           "Food"           "Food"
 [789] "Drinks"         "Food"           "Non consumable" "Food"
 [793] "Food"           "Non consumable" "Food"           "Food"
 [797] "Food"           "Food"           "Food"           "Food"
 [801] "Non consumable" "Food"           "Non consumable" "Food"
 [805] "Food"           "Food"           "Non consumable" "Food"
 [809] "Food"           "Food"           "Food"           "Food"
 [813] "Food"           "Drinks"         "Non consumable" "Drinks"
 [817] "Food"           "Food"           "Non consumable" "Food"
 [821] "Drinks"         "Food"           "Food"           "Non consumable"
 [825] "Food"           "Food"           "Food"           "Food"
 [829] "Food"           "Drinks"         "Non consumable" "Food"
 [833] "Food"           "Drinks"         "Food"           "Non consumable"
 [837] "Food"           "Non consumable" "Food"           "Food"
 [841] "Food"           "Food"           "Drinks"         "Non consumable"
 [845] "Food"           "Food"           "Food"           "Food"
 [849] "Food"           "Food"           "Non consumable" "Food"
 [853] "Food"           "Food"           "Food"           "Drinks"
 [857] "Food"           "Food"           "Drinks"         "Food"
 [861] "Food"           "Non consumable" "Food"           "Food"
 [865] "Food"           "Food"           "Food"           "Food"
 [869] "Non consumable" "Food"           "Food"           "Non consumable"
 [873] "Food"           "Food"           "Food"           "Food"
 [877] "Food"           "Food"           "Food"           "Drinks"
 [881] "Food"           "Food"           "Non consumable" "Food"
 [885] "Drinks"         "Food"           "Food"           "Food"
 [889] "Non consumable" "Food"           "Food"           "Food"
 [893] "Non consumable" "Food"           "Food"           "Food"
 [897] "Food"           "Food"           "Food"           "Non consumable"
 [901] "Food"           "Food"           "Non consumable" "Food"
 [905] "Non consumable" "Food"           "Drinks"         "Non consumable"
 [909] "Drinks"         "Non consumable" "Non consumable" "Food"
 [913] "Food"           "Drinks"         "Food"           "Drinks"
 [917] "Food"           "Food"           "Food"           "Food"
 [921] "Food"           "Food"           "Non consumable" "Food"
 [925] "Food"           "Drinks"         "Food"           "Food"
 [929] "Food"           "Food"           "Food"           "Food"
 [933] "Food"           "Non consumable" "Food"           "Food"
 [937] "Food"           "Food"           "Food"           "Non consumable"
 [941] "Drinks"         "Food"           "Food"           "Food"
 [945] "Food"           "Food"           "Food"           "Food"
 [949] "Food"           "Non consumable" "Food"           "Food"
 [953] "Food"           "Food"           "Food"           "Food"
 [957] "Food"           "Food"           "Food"           "Food"
 [961] "Non consumable" "Food"           "Non consumable" "Food"
 [965] "Food"           "Food"           "Drinks"         "Food"
 [969] "Non consumable" "Food"           "Food"           "Food"
 [973] "Food"           "Non consumable" "Food"           "Food"
 [977] "Food"           "Non consumable" "Food"           "Food"
 [981] "Food"           "Food"           "Food"           "Food"
 [985] "Food"           "Food"           "Food"           "Non consumable"
 [989] "Food"           "Food"           "Non consumable" "Food"
 [993] "Food"           "Non consumable" "Drinks"         "Food"
 [997] "Non consumable" "Food"           "Drinks"         "Non consumable"
 [ reached getOption("max.print") -- omitted 7523 entries ]

   #calculate outlet_establishment years( to check how old it is with reference to its outlet sales)
   #PS. Since in the given problem..the year upto 2013 is to be considered. hence.

   train$outlet_years <- 2013 - train$Outlet_Establishment_Year
> summary(train$outlet_years)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   4.00    9.00   14.00   15.17   26.00   28.00

# adding non edible in fat content wherever it is non consumable in item identifer new

> train$Item_Fat_Content[train$Item_Identifier_new=="Non consumable"]<- "Non edible"
> train$Item_Fat_Content
   [1] "LF"         "Regular"    "LF"         "Regular"    "Non edible" "Regular"
   [7] "Regular"    "LF"         "Regular"    "Regular"    "LF"         "Regular"
  [13] "Regular"    "Regular"    "LF"         "Regular"    "Non edible" "Regular"
  [19] "LF"         "LF"         "Regular"    "Regular"    "Non edible" "LF"
  [25] "Regular"    "Non edible" "Regular"    "LF"         "Regular"    "Regular"
  [31] "LF"         "Non edible" "LF"         "LF"         "LF"         "Non edible"
  [37] "Regular"    "Regular"    "Regular"    "LF"         "LF"         "LF"
  [43] "LF"         "LF"         "Regular"    "LF"         "Non edible" "LF"
  [49] "Regular"    "Regular"    "Non edible" "LF"         "Regular"    "LF"
  [55] "Non edible" "LF"         "Non edible" "LF"         "LF"         "LF"
  [61] "LF"         "Regular"    "LF"         "Regular"    "LF"         "LF"
  [67] "LF"         "Regular"    "LF"         "Non edible" "Regular"    "Regular"
  [73] "LF"         "LF"         "Non edible" "Regular"    "LF"         "LF"
  [79] "LF"         "LF"         "LF"         "Regular"    "LF"         "Non edible"
  [85] "Regular"    "Regular"    "LF"         "Regular"    "Regular"    "LF"
  [91] "LF"         "LF"         "LF"         "LF"         "Non edible" "LF"
  [97] "LF"         "Regular"    "Non edible" "Regular"    "LF"         "Regular"
 [103] "Regular"    "Regular"    "LF"         "Regular"    "Regular"    "Regular"
 [109] "LF"         "LF"         "LF"         "LF"         "Regular"    "LF"
 [115] "Regular"    "LF"         "Regular"    "LF"         "LF"         "LF"
 [121] "Non edible" "LF"         "Regular"    "Regular"    "LF"         "LF"
 [127] "LF"         "LF"         "LF"         "Non edible" "LF"         "Non edible"
 [133] "Non edible" "LF"         "LF"         "Non edible" "LF"         "LF"
 [139] "LF"         "Non edible" "Non edible" "Non edible" "Non edible" "Non edible"
 [145] "Regular"    "LF"         "LF"         "Non edible" "LF"         "Non edible"
 [151] "LF"         "Regular"    "Regular"    "LF"         "LF"         "Non edible"
 [157] "Regular"    "LF"         "LF"         "Regular"    "Regular"    "LF"
 [163] "LF"         "LF"         "Regular"    "Regular"    "LF"         "Regular"
 [169] "Non edible" "LF"         "Non edible" "LF"         "Regular"    "Regular"
 [175] "LF"         "LF"         "Regular"    "Non edible" "Regular"    "LF"
 [181] "Regular"    "LF"         "Non edible" "Regular"    "Regular"    "Regular"
 [187] "Regular"    "Non edible" "Regular"    "Non edible" "LF"         "Regular"
 [193] "Regular"    "LF"         "Non edible" "LF"         "Regular"    "Non edible"
 [199] "Regular"    "Regular"    "Non edible" "Regular"    "LF"         "LF"
 [205] "LF"         "Regular"    "LF"         "LF"         "Regular"    "LF"
 [211] "LF"         "LF"         "Regular"    "Non edible" "Non edible" "Regular"
 [217] "Regular"    "Regular"    "LF"         "LF"         "Regular"    "Regular"
 [223] "Regular"    "LF"         "LF"         "Non edible" "LF"         "LF"
 [229] "LF"         "LF"         "Non edible" "Regular"    "LF"         "LF"
 [235] "Non edible" "LF"         "Regular"    "Regular"    "LF"         "Non edible"
 [241] "Non edible" "Non edible" "Non edible" "Regular"    "LF"         "LF"
 [247] "LF"         "Regular"    "LF"         "LF"         "LF"         "LF"
 [253] "LF"         "Regular"    "Regular"    "LF"         "LF"         "Non edible"
 [259] "Regular"    "LF"         "Regular"    "LF"         "Regular"    "Regular"
 [265] "LF"         "Non edible" "LF"         "LF"         "LF"         "LF"
 [271] "Regular"    "LF"         "LF"         "LF"         "LF"         "LF"
 [277] "LF"         "Regular"    "LF"         "LF"         "Regular"    "Non edible"
 [283] "Non edible" "LF"         "LF"         "Regular"    "LF"         "Non edible"
 [289] "Regular"    "Non edible" "LF"         "Regular"    "LF"         "LF"
 [295] "Regular"    "LF"         "LF"         "Non edible" "Regular"    "LF"
 [301] "Regular"    "Regular"    "Non edible" "LF"         "LF"         "LF"
 [307] "LF"         "LF"         "LF"         "LF"         "Regular"    "Regular"
 [313] "Regular"    "Regular"    "Regular"    "LF"         "LF"         "Regular"
 [319] "LF"         "LF"         "LF"         "Regular"    "LF"         "Regular"
 [325] "LF"         "Non edible" "LF"         "Regular"    "Non edible" "LF"
 [331] "LF"         "LF"         "LF"         "Regular"    "LF"         "Regular"
 [337] "Non edible" "Regular"    "Regular"    "Regular"    "Regular"    "Regular"
 [343] "Non edible" "Non edible" "LF"         "LF"         "LF"         "Non edible"
 [349] "Non edible" "LF"         "Regular"    "Regular"    "Regular"    "LF"
 [355] "Regular"    "Regular"    "LF"         "Regular"    "Regular"    "LF"
 [361] "LF"         "Non edible" "LF"         "LF"         "LF"         "LF"
 [367] "Regular"    "Regular"    "Regular"    "Regular"    "Regular"    "Non edible"
 [373] "Non edible" "LF"         "Regular"    "LF"         "LF"         "Regular"
 [379] "LF"         "Regular"    "Regular"    "LF"         "Regular"    "Regular"
 [385] "Regular"    "LF"         "LF"         "LF"         "Regular"    "LF"
 [391] "LF"         "LF"         "Regular"    "LF"         "Non edible" "LF"
 [397] "Regular"    "Regular"    "Regular"    "Non edible" "Non edible" "Non edible"
 [403] "LF"         "Non edible" "Regular"    "LF"         "LF"         "Regular"
 [409] "LF"         "LF"         "LF"         "Non edible" "LF"         "LF"
 [415] "Non edible" "LF"         "Regular"    "LF"         "LF"         "Non edible"
 [421] "LF"         "LF"         "Regular"    "Regular"    "Non edible" "Regular"
 [427] "LF"         "LF"         "Regular"    "LF"         "LF"         "Regular"
 [433] "Regular"    "LF"         "Regular"    "LF"         "Non edible" "Regular"
 [439] "Regular"    "LF"         "LF"         "LF"         "Regular"    "Non edible"
 [445] "LF"         "LF"         "Regular"    "LF"         "Non edible" "Non edible"
 [451] "LF"         "Regular"    "Regular"    "LF"         "Regular"    "Non edible"
 [457] "LF"         "Regular"    "Regular"    "Non edible" "LF"         "LF"
 [463] "LF"         "Non edible" "LF"         "LF"         "Non edible" "LF"
 [469] "LF"         "Regular"    "Non edible" "LF"         "LF"         "Regular"
 [475] "LF"         "LF"         "Non edible" "LF"         "Regular"    "Non edible"
 [481] "Regular"    "LF"         "LF"         "LF"         "LF"         "LF"
 [487] "LF"         "LF"         "LF"         "Regular"    "Non edible" "Regular"
 [493] "Non edible" "LF"         "LF"         "Regular"    "LF"         "Regular"
 [499] "Regular"    "Non edible" "Non edible" "LF"         "LF"         "Regular"
 [505] "LF"         "LF"         "Regular"    "Non edible" "Non edible" "LF"
 [511] "LF"         "LF"         "LF"         "LF"         "Regular"    "LF"
 [517] "Non edible" "LF"         "Regular"    "Non edible" "Regular"    "LF"
 [523] "Non edible" "LF"         "Non edible" "Regular"    "Non edible" "Non edible"
 [529] "Regular"    "LF"         "Regular"    "Non edible" "LF"         "LF"
 [535] "LF"         "LF"         "LF"         "Regular"    "LF"         "Non edible"
 [541] "LF"         "LF"         "Regular"    "LF"         "LF"         "Regular"
 [547] "Regular"    "LF"         "Non edible" "Regular"    "Regular"    "LF"
 [553] "LF"         "Regular"    "LF"         "Regular"    "Non edible" "LF"
 [559] "LF"         "Regular"    "LF"         "LF"         "Regular"    "LF"
 [565] "Regular"    "LF"         "Regular"    "Regular"    "Regular"    "LF"
 [571] "Regular"    "Non edible" "Regular"    "LF"         "LF"         "LF"
 [577] "Regular"    "LF"         "LF"         "Non edible" "Regular"    "Regular"
 [583] "LF"         "LF"         "LF"         "LF"         "LF"         "Non edible"
 [589] "LF"         "Non edible" "Non edible" "Regular"    "Non edible" "Regular"
 [595] "Non edible" "LF"         "LF"         "Non edible" "LF"         "LF"
 [601] "LF"         "LF"         "LF"         "LF"         "Non edible" "Regular"
 [607] "LF"         "LF"         "Non edible" "LF"         "LF"         "LF"
 [613] "Non edible" "Regular"    "LF"         "Regular"    "Regular"    "LF"
 [619] "Regular"    "Regular"    "Regular"    "LF"         "Non edible" "LF"
 [625] "LF"         "LF"         "Regular"    "Regular"    "LF"         "Regular"
 [631] "LF"         "LF"         "LF"         "Regular"    "LF"         "Regular"
 [637] "Regular"    "Regular"    "Regular"    "Regular"    "LF"         "LF"
 [643] "LF"         "LF"         "Non edible" "Regular"    "Non edible" "LF"
 [649] "Regular"    "Regular"    "Regular"    "LF"         "Regular"    "LF"
 [655] "LF"         "Non edible" "Regular"    "LF"         "Non edible" "Regular"
 [661] "LF"         "LF"         "LF"         "Regular"    "LF"         "Non edible"
 [667] "Regular"    "LF"         "Regular"    "Regular"    "LF"         "Regular"
 [673] "LF"         "LF"         "Non edible" "Non edible" "Regular"    "Non edible"
 [679] "Regular"    "Regular"    "Regular"    "LF"         "Regular"    "LF"
 [685] "LF"         "LF"         "Regular"    "LF"         "Regular"    "LF"
 [691] "LF"         "Non edible" "Regular"    "Regular"    "Regular"    "Non edible"
 [697] "Non edible" "Non edible" "Non edible" "Regular"    "LF"         "Non edible"
 [703] "Non edible" "Non edible" "LF"         "LF"         "Regular"    "Non edible"
 [709] "Regular"    "LF"         "Non edible" "Non edible" "LF"         "LF"
 [715] "LF"         "LF"         "Regular"    "Non edible" "LF"         "Non edible"
 [721] "LF"         "Regular"    "Regular"    "Regular"    "Regular"    "Regular"
 [727] "LF"         "Regular"    "LF"         "LF"         "Regular"    "Regular"
 [733] "LF"         "LF"         "Non edible" "LF"         "Non edible" "LF"
 [739] "LF"         "LF"         "Regular"    "Regular"    "LF"         "LF"
 [745] "Non edible" "Regular"    "LF"         "Regular"    "Regular"    "Regular"
 [751] "LF"         "Regular"    "Regular"    "Regular"    "LF"         "Regular"
 [757] "LF"         "LF"         "LF"         "LF"         "LF"         "Regular"
 [763] "LF"         "Non edible" "Regular"    "Regular"    "LF"         "Regular"
 [769] "LF"         "Non edible" "LF"         "Regular"    "LF"         "LF"
 [775] "LF"         "LF"         "LF"         "Regular"    "LF"         "LF"
 [781] "Non edible" "Regular"    "LF"         "Regular"    "Regular"    "Regular"
 [787] "LF"         "LF"         "LF"         "LF"         "Non edible" "LF"
 [793] "LF"         "Non edible" "Regular"    "Regular"    "LF"         "LF"
 [799] "LF"         "Regular"    "Non edible" "Regular"    "Non edible" "LF"
 [805] "Regular"    "LF"         "Non edible" "LF"         "Regular"    "Regular"
 [811] "Regular"    "LF"         "LF"         "LF"         "Non edible" "LF"
 [817] "LF"         "Regular"    "Non edible" "LF"         "LF"         "Regular"
 [823] "Regular"    "Non edible" "Regular"    "LF"         "LF"         "Regular"
 [829] "Regular"    "LF"         "Non edible" "LF"         "LF"         "LF"
 [835] "LF"         "Non edible" "Regular"    "Non edible" "Regular"    "Regular"
 [841] "Regular"    "LF"         "LF"         "Non edible" "Regular"    "LF"
 [847] "Regular"    "LF"         "LF"         "Regular"    "Non edible" "LF"
 [853] "LF"         "Regular"    "LF"         "LF"         "Regular"    "Regular"
 [859] "LF"         "LF"         "LF"         "Non edible" "Regular"    "Regular"
 [865] "LF"         "LF"         "Regular"    "LF"         "Non edible" "Regular"
 [871] "Regular"    "Non edible" "Regular"    "Regular"    "Regular"    "LF"
 [877] "Regular"    "LF"         "LF"         "LF"         "Regular"    "Regular"
 [883] "Non edible" "LF"         "LF"         "Regular"    "LF"         "LF"
 [889] "Non edible" "Regular"    "Regular"    "Regular"    "Non edible" "Regular"
 [895] "Regular"    "LF"         "Regular"    "Regular"    "Regular"    "Non edible"
 [901] "LF"         "Regular"    "Non edible" "Regular"    "Non edible" "LF"
 [907] "LF"         "Non edible" "LF"         "Non edible" "Non edible" "LF"
 [913] "LF"         "LF"         "Regular"    "LF"         "LF"         "LF"
 [919] "LF"         "LF"         "Regular"    "Regular"    "Non edible" "LF"
 [925] "LF"         "LF"         "LF"         "Regular"    "Regular"    "LF"
 [931] "LF"         "LF"         "LF"         "Non edible" "LF"         "Regular"
 [937] "LF"         "LF"         "Regular"    "Non edible" "LF"         "LF"
 [943] "Regular"    "LF"         "LF"         "Regular"    "LF"         "Regular"
 [949] "LF"         "Non edible" "LF"         "LF"         "LF"         "Regular"
 [955] "Regular"    "LF"         "LF"         "Regular"    "LF"         "LF"
 [961] "Non edible" "LF"         "Non edible" "Regular"    "Regular"    "LF"
 [967] "LF"         "LF"         "Non edible" "LF"         "LF"         "LF"
 [973] "LF"         "Non edible" "LF"         "Regular"    "LF"         "Non edible"
 [979] "Regular"    "LF"         "LF"         "Regular"    "LF"         "LF"
 [985] "Regular"    "Regular"    "LF"         "Non edible" "LF"         "Regular"
 [991] "Non edible" "Regular"    "Regular"    "Non edible" "LF"         "LF"
 [997] "Non edible" "LF"         "LF"         "Non edible"
 [ reached getOption("max.print") -- omitted 7523 entries ]

> table(train$Item_Fat_Content)

        LF Non edible    Regular
      3918       1599       3006

#Applying RandomForest Algorithm for predicting RMSE factor and the importance order of variables

library(randomForest)
randomForest 4.6-12
Type rfNews() to see new features/changes/bug fixes.
> set.seed(123)

#Converting all character data variables into factors..since this is a regression classification in RandomForest

> train$Item_Type <- as.factor(train$Item_Type)
>train$Outlet_Identifier <- as.factor(train$Outlet_Identifier)
> train$Outlet_Size <- as.factor(train$Outlet_Size)
> train$Outlet_Location_Type <- as.factor(train$Outlet_Location_Type)
> train$Outlet_Type <- as.factor(train$Outlet_Type)
> train$Item_Identifier_new <- as.factor(train$Item_Identifier_new)
> train$Item_Fat_Content <- as.factor(train$Item_Fat_Content)

> rf <- randomForest(train$Item_Outlet_Sales ~ train$Item_Identifier_new + train$Item_Weight + train$Item_Fat_Content + train$Item_Visibility + train$Item_Type + train$Item_MRP + train$Outlet_Identifier + train$Outlet_Establishment_Year + train$Outlet_Size +train$Outlet_Location_Type + train$Outlet_Type, data = train, importance = TRUE, ntree=1000)
> which.min(rf$mse)
[1] 100
> imp <- as.data.frame(sort(importance(rf)[,1],decreasing = TRUE),optional = T)
> names(imp) <- "% Inc MSE"
> imp
                                 % Inc MSE
train$Item_MRP                  29.4563329
train$Outlet_Type                3.9207210
train$Outlet_Identifier          3.6955176
train$Outlet_Establishment_Year  2.3734953
train$Item_Identifier_new        2.1815733
train$Outlet_Size                1.4750101
train$Outlet_Location_Type       1.2459310
train$Item_Visibility            0.8424331
train$Item_Type                  0.8346295
train$Item_Fat_Content           0.2369092
train$Item_Weight               -0.3772412

#We see that the most important variable is Item_MRP.The Item_MRP is considered the most important predictor; it is estimated that, in the absence of that variable, the error would increase by 29.45%.

#Calculating RMSE
> train.pred.forest <- predict(rf,train)
> RMSE.forest <- sqrt(mean((train.pred.forest-train$Item_Outlet_Sales)^2))
> RMSE.forest
[1] 771.3495

> print(rf)

Call:
 randomForest(formula = train$Item_Outlet_Sales ~ train$Item_Identifier_new +      train$Item_Weight + train$Item_Fat_Content + train$Item_Visibility +      train$Item_Type + train$Item_MRP + train$Outlet_Identifier +      train$Outlet_Establishment_Year + train$Outlet_Size + train$Outlet_Location_Type +      train$Outlet_Type, data = train, importance = TRUE, ntree = 10)
               Type of random forest: regression
                     Number of trees: 1000
No. of variables tried at each split: 3

          Mean of squared residuals: 1508779
                    % Var explained: 48.18

> rf <- randomForest(train$Item_Outlet_Sales ~ train$Item_Identifier_new + train$Item_Weight + train$Item_Fat_Content + train$Item_Visibility + train$Item_Type + train$Item_MRP + train$Outlet_Identifier + train$Outlet_Establishment_Year + train$Outlet_Size +train$Outlet_Location_Type + train$Outlet_Type, data = train, importance = TRUE, ntree=1000)

> RMSE.forest <- sqrt(mean((train.pred.forest-train$Item_Outlet_Sales)^2))
> RMSE.forest
[1] 736.5056
> print(rf)

Call:
 randomForest(formula = train$Item_Outlet_Sales ~ train$Item_Identifier_new +      train$Item_Weight + train$Item_Fat_Content + train$Item_Visibility +      train$Item_Type + train$Item_MRP + train$Outlet_Identifier +      train$Outlet_Establishment_Year + train$Outlet_Size + train$Outlet_Location_Type +      train$Outlet_Type, data = train, importance = TRUE, ntree = 1000)
               Type of random forest: regression
                     Number of trees: 50
No. of variables tried at each split: 3

          Mean of squared residuals: 1270726
                    % Var explained: 56.36
> varImpPlot(rf)
# Check the image to see the importance of variables in the order

In many cases, we build models not only to predict, but also to gain insight from the data. When,  we performed the RandomForest Algorithm, we saw that the Item_MRP seemed to be an important predictor. Well, the model confirmed it: this variable was the most statistically significant in the linear model, was one of the most important variable  used to predict in the regression tree, and the one that reduced the estimated error the most in the random forest.


## Dale W.R. Rosenthal, 2018
## You are free to distribute and use this code so long as you attribute
## it to me or cite the text.
## The legal disclaimer in _A Quantitative Primer on Investments with R_
## applies to this code.  Use or distribution without these comment lines
## is forbidden.
library(xts)
library(quantmod)
library(Quandl)
library(rugarch)

# get CMT USTs: 3M, 2Y, 10Y, 30Y

## Download Fama-French data
french.base <- "http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp"
factor.file <- "F-F_Research_Data_Factors_daily_CSV.zip"
french.url <- paste(french.base, factor.file, sep="/")
temp.file <- tempfile()
download.file(french.url, destfile=temp.file)
ff.tmp <- read.csv(unz(temp.file, "F-F_Research_Data_Factors_daily.CSV"),
                   header = TRUE, skip = 3)
unlink(temp.file)
# remove obnoxious last line, scale percentages, create xts object
ff.tmp <- ff.tmp[-length(ff.tmp[,1]),]
ff.data <- as.xts(ff.tmp[,c("SMB","HML")],
                  order.by=as.POSIXct(ff.tmp[[1]], format="%Y%m%d"))

## Download Carhart (momentum) data; skip absurd number of comment lines
factor.file <- "F-F_Momentum_Factor_daily_CSV.zip"
french.url <- paste(french.base, factor.file, sep="/")
temp.file <- tempfile()
download.file(french.url, destfile=temp.file)
umd.tmp <- read.csv(unz(temp.file, "F-F_Momentum_Factor_daily.CSV"),
                    header = TRUE, skip = 13)
unlink(temp.file)
# remove obnoxious last line, scale percentages, create xts object
umd.tmp <- umd.tmp[-length(umd.tmp[,1]),]
umd.data <- as.xts(umd.tmp[,c("Mom")], order.by=as.POSIXct(umd.tmp[[1]], format="%Y%m%d"))
colnames(umd.data) <- c("UMD")

### Handle monthly data
# For expected CPI and realized CPI, only get the first column of
# data... like so:
exinfl <- Quandl("FRBC/EXIN", type="xts")[,1]
colnames(exinfl) <- c("EXINFL")

cpi <- Quandl("FRBC/USINFL", type="xts")[,1]
colnames(cpi) <- c("CPI")

excpi <- cpi*(1+exinfl)  # expected CPI in twelve months
cpi.surprise <- log(cpi) - log(lag(excpi, 12))  # % CPI surprise
colnames(cpi.surprise) <- c("INFLSURP")

# For industrial production, compute log-returns (% changes)
indprod <- Quandl("FRED/INDPRO", type="xts")
colnames(indprod) <- c("INDPROD")
indprod.logret <- diff(log(indprod))

# Get index and stock prices; create returns

# Now we join all of the datasets together
alldata.full <- cbind(ust, exinfl, cpi.surprise, ltcorpbond, indprod, ff.data, umd.data, returns)

# For monthly data: Last Observation Carried Forward (until new number)
alldata <- na.locf(alldata.full)["2010/"]

# create excess returns for indices, stocks

# Handy way to compute a function for each column
apply(alldata, 2, mean, na.rm=TRUE)  # "2" = by columns; "1" = by rows

# If your ticker were DAL and you wanted to model returns (not excess
# returns) using HML and SMB, you would do like so:
hml.wrong.model <- lm(DAL ~ HML + SMB, data=alldata)
summary(hml.wrong.model)
# NOTE that this is not a model you are supposed to do for the homework

# Now do GARCH-in-mean models
gim.spec <- ugarchspec(variance.model=list(model="sGARCH", archm=TRUE, archpow=2),
                       mean.model=list(armaOrder=c(0,0), include.mean=TRUE))
garch.in.mean.spx <- ugarchfit(data=alldata$SPX.xs, spec=gim.spec)
show(garch.in.mean.spx)


suppressPackageStartupMessages(library(float))
set.seed(1234)

tester = function(s, x, fun)
{
  test = dbl(fun(s, na.rm=TRUE))
  truth = fun(x, na.rm=TRUE)
  stopifnot(all.equal(test, truth, tol=tol))

  test = dbl(fun(s, na.rm=FALSE))
  truth = fun(x, na.rm=FALSE)
  stopifnot(all.equal(test, truth, tol=tol))
}

tol = 1e-6
m = 5
n = 3

x1 = matrix(stats::rnorm(m*n), m, n)
x2 = x1
x2[1, 1] = NA_real_
x2[3, 2] = NaN

s1 = fl(x1)
s2 = fl(x2)

tester(s1, x1, colSums)
tester(s2, x2, colSums)

tester(s1, x1, rowSums)
tester(s2, x2, rowSums)

tester(s1, x1, colMeans)
tester(s2, x2, colMeans)

tester(s1, x1, rowMeans)
tester(s2, x2, rowMeans)


## Dale W.R. Rosenthal, 2018
## You are free to distribute and use this code so long as you attribute
## it to me or cite the text.
## The legal disclaimer in _A Quantitative Primer on Investments with R_
## applies to this code.  Use or distribution without these comment lines
## is forbidden.

pca.usd <- prcomp(~ T3M + T6M + T1Y + T2Y, data=UST,
                  scale=FALSE, center=FALSE)
pca.usd <- prcomp(yc.usd, scale=FALSE, center=FALSE)
pca.usd$rotation  # eigenvectors
pca.usd$sdev^2    # eigenvalues


# load packages needed to run this code
library(ggplot2)
library(dplyr)
library(tidyr)
library(cowplot)
library(gganimate)
library(magick)

# this function calculates true positive rate and false positive rate to make an ROC curve
calc_ROC <- function(probabilities, known_truth, model.name = NULL) {
  outcome <- as.numeric(factor(known_truth)) - 1
  pos <- sum(outcome) # total known positives
  neg <- sum(1 - outcome) # total known negatives
  pos_probs <- outcome * probabilities # probabilities for known positives
  neg_probs <- (1 - outcome) * probabilities # probabilities for known negatives
  true_pos <- sapply(
    probabilities,
    function(x) sum(pos_probs >= x) / pos
  ) # true pos. rate
  false_pos <- sapply(
    probabilities,
    function(x) sum(neg_probs >= x) / neg
  )
  if (is.null(model.name)) {
    result <- data.frame(true_pos, false_pos)
  } else {
    result <- data.frame(true_pos, false_pos, model.name)
  }

  result %>% add_row(true_pos = 0, false_pos = 0) %>% arrange(false_pos, true_pos)
}

# make a reduced iris data set that only contains virginica and versicolor species
iris.small <- filter(iris, Species %in% c("virginica", "versicolor"))

# fit a logistic regression model to the data
glm.out <- glm(Species ~ Petal.Width + Petal.Length + Sepal.Width,
  data = iris.small,
  family = binomial
)

# make a data frame with linear predictors from the model and true species assignment
lr_data <- data.frame(
  predictor = glm.out$linear.predictors,
  Species = iris.small$Species
)

# get a density plot for each species
d_virg <- density(filter(lr_data, Species == "virginica")$predictor)
d_vers <- density(filter(lr_data, Species == "versicolor")$predictor)

# move each species' density plot to overlap at the center.
# this is the starting point
virg_t1 <- data.frame(predictor = d_virg$x - 11.7, density = d_virg$y, time = 1, Species = "virginica")
vers_t1 <- data.frame(predictor = d_vers$x + 11.8, density = d_vers$y, time = 1, Species = "versicolor")

# change predictor values to shift the density plot along the x-axis
# add `time` variable to respresent a state in an animation
virg_t1 %>% mutate(predictor = predictor + 2.5, time = 2) -> virg_t2
vers_t1 %>% mutate(predictor = predictor - 2.5, time = 2) -> vers_t2

virg_t1 %>% mutate(predictor = predictor + 5, time = 3) -> virg_t3
vers_t1 %>% mutate(predictor = predictor - 5, time = 3) -> vers_t3

virg_t1 %>% mutate(predictor = predictor + 7.5, time = 4) -> virg_t4
vers_t1 %>% mutate(predictor = predictor - 7.5, time = 4) -> vers_t4

virg_t1 %>% mutate(predictor = predictor + 10, time = 5) -> virg_t5
vers_t1 %>% mutate(predictor = predictor - 10, time = 5) -> vers_t5

virg_t1 %>% mutate(predictor = predictor + 20, time = 6) -> virg_t6
vers_t1 %>% mutate(predictor = predictor - 20, time = 6) -> vers_t6

# combine virginica and versicolor data sets separately.
# this is essential to move density plots independently of each other
rbind(virg_t1, virg_t2, virg_t3, virg_t4, virg_t5, virg_t6) -> virg_data
rbind(vers_t1, vers_t2, vers_t3, vers_t4, vers_t5, vers_t6) -> vers_data

# make an animation with distributions of linear predictors
p_dist <- ggplot(mapping = aes(predictor, density, group = 1)) +
  geom_hline(yintercept = 0, color = "black", size = 0.5, linetype = 2) +
  geom_area(data = vers_data, alpha = 0.7, fill = "#C04A56") +
  geom_area(data = virg_data, alpha = 0.7, fill = "#3D8CF1") +
  scale_x_continuous(limits = c(-45, 50)) +
  transition_states(time, transition_length = 1, state_length = 1) +
  ggtitle(" ") + # adding an emtpy title to align y axis with ROC plot
  theme_cowplot()

# create a new data frame with linear predictors, species, and time
virg_t1 <- lr_data %>% filter(Species == "virginica") %>% mutate(predictor = predictor - 11.7, time = 1)
vers_t1 <- lr_data %>% filter(Species == "versicolor") %>% mutate(predictor = predictor + 11.8, time = 1)

# change predictor values and add `time` variable to respresent a state in an animation
# for each time point, changes in predictor values should match to the predictor values of the density plot
virg_t1 %>% mutate(predictor = predictor + 2.5, time = 2) -> virg_t2
vers_t1 %>% mutate(predictor = predictor - 2.5, time = 2) -> vers_t2

virg_t1 %>% mutate(predictor = predictor + 5, time = 3) -> virg_t3
vers_t1 %>% mutate(predictor = predictor - 5, time = 3) -> vers_t3

virg_t1 %>% mutate(predictor = predictor + 7.5, time = 4) -> virg_t4
vers_t1 %>% mutate(predictor = predictor - 7.5, time = 4) -> vers_t4

virg_t1 %>% mutate(predictor = predictor + 10, time = 5) -> virg_t5
vers_t1 %>% mutate(predictor = predictor - 10, time = 5) -> vers_t5

virg_t1 %>% mutate(predictor = predictor + 20, time = 6) -> virg_t6
vers_t1 %>% mutate(predictor = predictor - 20, time = 6) -> vers_t6

# combine all data frames and calculate ROC curves and AUC values
rbind(
  virg_t1, virg_t2, virg_t3, virg_t4, virg_t5, virg_t6, # combine all data frames together
  vers_t1, vers_t2, vers_t3, vers_t4, vers_t5, vers_t6
) %>%
  mutate(probabilities = exp(predictor) / (1 + exp(predictor))) %>% # calculate probabilities for linear predictors
  group_by(time) %>%
  do(results = calc_ROC(
    probabilities = .$probabilities, # calculate TP rate and FP rate for every possible cutoff
    known_truth = .$Species
  )) %>%
  group_by(time) %>%
  do(as.data.frame(.$results)) %>% # store output from calc_ROC() in the data frame
  mutate(delta = false_pos - lag(false_pos)) %>% # calculate AUC values
  mutate(AUC = sprintf("%.3f", sum(delta * true_pos, na.rm = T))) -> ROC

# make an animation with ROC curves
p_ROC <- ggplot(data = ROC, aes(x = false_pos, y = true_pos)) +
  geom_line(size = 1) +
  geom_abline(intercept = 0, slope = 1) +
  transition_states(AUC, transition_length = 1, state_length = 1) +
  ggtitle("AUC = {closest_state}") +
  scale_x_continuous(name = "false positive rate") +
  scale_y_continuous(name = "true positive rate") +
  theme_cowplot()

# save each animation as individual frames
# each frame will be saved as a PNG image
p_dist_gif <- animate(p_dist,
  device = "png",
  width = 400,
  height = 400,
  renderer = file_renderer("../animations/gganim_ROC", prefix = "p_dist", overwrite = TRUE)
)
p_ROC_gif <- animate(p_ROC,
  device = "png",
  width = 400,
  height = 400,
  renderer = file_renderer("../animations/gganim_ROC", prefix = "p_ROC", overwrite = TRUE)
)

# stitch two animations together
# read the first image (frame) of each animation
a <- image_read(p_dist_gif[[1]])
b <- image_read(p_ROC_gif[[1]])
# combine the two images into a single image
combined <- image_append(c(a, b))
new_gif <- c(combined)
for (i in 2:100) { # combine images frame by frame
  a <- image_read(p_dist_gif[[i]])
  b <- image_read(p_ROC_gif[[i]])
  combined <- image_append(c(a, b))
  new_gif <- c(new_gif, combined)
}

# make an animation of the combined images
combined_gif <- image_animate(new_gif)
# save as gif
image_write(combined_gif, "../animations/ROC.gif")


###########################################################
### Class to generate draws for play
###########################################################

###########################################################
### returns list of five lists with same mega and different
### regular numbers
###########################################################
makedraws_mm <- function() {
    out <- list()
    mega<- sample(1:15, 1, replace = FALSE)
    for (i in 1:5){
        reg <- sort(sample(1:75, 5, replace = FALSE))
        out[[length(out) + 1]] <- list(mega, I(reg))
    }
    return(out)
}

makedraws_pb <- function() {
    out <- list()
    mega<- sample(1:35, 1, replace = FALSE)
    for (i in 1:5){
        reg <- sort(sample(1:59, 5, replace = FALSE))
        out[[length(out) + 1]] <- list(mega, I(reg))
    }
    return(out)
}
###########################################################
### returns list of n complete draws
###########################################################
getdraws <- function(n, game){
    out <- list()
    for (i in 1:n){
        if (game=="mm"){out[[length(out) + 1]] <- makedraws_mm()}
        if (game=="pb"){out[[length(out) + 1]] <- makedraws_pb()}
    }
    return(out)
}

### get the numbers to play
numbers <- getdraws(2, "mm")

do.call(rbind, test <- lapply(seq_along(numbers), function(i){
    data.frame(ticket=i, unlist(numbers[[i]], recursive=FALSE))
    }))

lapply(seq_along(numbers), function(i){unlist(numbers[[i]])})
lapply(seq_along(numbers[[1]]), function(j){unlist(numbers[[1]][[j]])})


mtx.descr <- function(X)
{
  X.pa <- vegan::decostand(X, method = 'pa')
  sing <- sum(colSums(X.pa) == 1)
  pr.zeros <- 1 - (sum(X > 0) / prod(dim(X)))
  dbi <- mean(scale(X, center = FALSE, scale =  apply(X, 2, max)))

  list(n.elem = prod(dim(X)),
       n = dim(X)[1],
       p = dim(X)[2],
       singletons = round(sing, 3),
       prop.zero = round(pr.zeros, 3),
       dbi = round(dbi, 3))
}

plot.mds.gg <- function(mds, txt.col = 'blue', txt.x = 0, txt.y = 0, clusters = NULL, labels = FALSE)
{
  require(tidyverse)

  xy <- data.frame(scores(mds)) %>%
    rownames_to_column(var = 'site')
  n <- dim(scores(mds))[1]

  stress.lbl <- paste('Stress = ', round(mds$stress,3))

  if(is.null(clusters))
  {
    plot.obj <- ggplot(data = xy) +
      geom_point(aes(x = NMDS1, y = NMDS2), alpha  = 1, size = 1) +
      annotate(geom = 'text', x = txt.x, y = txt.y, label = stress.lbl, col = txt.col) +
      labs(x = 'NDMS 1', y = 'NMDS 2') +
      theme_minimal() +
      coord_equal() +
      theme(legend.position = "none")
  } else
  {
    xy$cl <- as.factor(clusters)

    plot.obj <- ggplot(data = xy) +
      geom_point(aes(x = NMDS1, y = NMDS2, col = cl, shape = cl), size = 3, alpha  = 1) +
      annotate(geom = 'text', x = txt.x, y = txt.y, label = stress.lbl, col = txt.col) +
      labs(x = 'NDMS 1', y = 'NMDS 2') +
      theme_minimal() +
      coord_equal()
  }

  if (labels == TRUE)
  {
    plot.obj <- plot.obj + geom_text(aes(x = NMDS1, y = NMDS2, label = site))
  }

  plot.obj
}

# Function to extract the elevation range that each spp occurs over
extract.limits <- function(sppsite, spp.list, elev)
{
  e.min <- function(x) { min(elev[which (x != 0)]) }  # assume pres-absence
  e.max <- function(x) { max(elev[which (x != 0)]) }

  sppsite <- sppsite[, spp.list]
  sppsite <- sppsite[rowSums(sppsite) > 0, ]

  elev.rg <- cbind(apply(sppsite, 2, e.min), apply(sppsite, 2, e.max))
  elev.rg <- as.data.frame(elev.rg) %>%
    rownames_to_column(var = "spp") %>%
    rename("min" = V1, "max" = V2) %>%
    mutate(spp = str_to_title(spp))

  elev.rg$single <- ifelse(elev.rg$max == elev.rg$min, 1, 0)

  elev.rg.s <- dplyr::filter(elev.rg, single != 0)
  elev.rg$spp <- fct_reorder(elev.rg$spp, elev.rg$min, min)
  elev.rg.s$spp <- fct_reorder(elev.rg.s$spp, elev.rg.s$min, min)

  list(elev.lims = elev.rg, elev.lims.sgl = elev.rg.s)
}

# get.elevation.limits <- function(sppsite, spp.list, elev)
# {
#   sppsite <- sppsite[, spp.list]
#   sppsite <- sppsite[rowSums(sppsite) > 0, ]
#
#   sppsite <- as.data.frame(sppsite) %>%
#     rownames_to_column(var = "site.elev") %>%
#     separate(col = "site.elev", into = c("site", "elev")) %>%
#     mutate(elev = as.numeric(elev)) %>%
#     arrange(elev)
#
#   e.range <- extract.limits(sppsite, elev)
#   e.range
# }


# returns vector of cluster ids for clusters with internal
# complete-linkage correlation of min.cor
"cluster.by.correlation" <- function(x, min.cor=.75){
#     library('fastcluster')
    cc <- cor(x,use='pairwise.complete.obs',method='pear')
    if(ncol(x) == 379) browser()
    cc <- as.dist(1-cc)
    hc <- hclust(cc)
    res <- cutree(hc,h=1-min.cor)
    names(res) <- colnames(x)
    return(res)
}

# returns vector of cluster ids for clusters with internal
# complete-linkage correlation of min.cor
#
# by default, chooses cluster reps as highest-variance member
# if select.rep.fcn=mean
"collapse.by.correlation" <- function(x, min.cor=.5, select.rep.fcn=c('var','mean','lowest.mean',
			'longest.name', 'shortest.name')[2],
        verbose=FALSE){
    if(verbose) cat('Clustering',ncol(x),'features...')
    gr <- cluster.by.correlation(x, min.cor=min.cor)
    if(verbose) cat('getting means...')
    if(select.rep.fcn == 'mean'){
        v <- apply(x,2,function(xx) mean(xx,na.rm=TRUE))
    } else if(select.rep.fcn == 'lowest.mean'){
        v <- apply(x,2,function(xx) -mean(xx,na.rm=TRUE))
    } else if(select.rep.fcn == 'longest.name'){
        v <- nchar(colnames(x))
    } else if(select.rep.fcn == 'shortest.name'){
        v <- -nchar(colnames(x))
    } else {
        v <- apply(x,2,function(xx) var(xx,use='complete.obs'))
    }
    if(verbose) cat('choosing reps...')
    reps <- sapply(split(1:ncol(x),gr),function(xx) xx[which.max(v[xx])])
    if(verbose)
        cat(sprintf('collapsed from %d to %d.\n',ncol(x), length(reps)))
    return(list(reps=reps, groups=gr))
}


BBOD <- function(B,By,y,max_price, OD){
  if(OD == "o"){
    func = 0.2*B+By*y
  } else{
    func = max_price-0.3072734*B+0.5*By*y
    func[func>max_price] = max_price
  }
  return(func)
}

BBPlot <- function(input,output,values){
  bonds = seq(0,200,length.out = 100)
  to_plot = data.frame(bonds = bonds)

  eq_Pb = BBOD(values$eq$B,input$by,values$eq$y,input$max_price, "o")
  fig = plot_ly(to_plot, x = ~bonds)
  fig = fig %>% add_trace(y = BBOD(bonds,input$by,values$eq$y,input$max_price,"o"), type = "scatter", mode = "lines", name = "$$B^s(N)$$", line = list(color = "blue"),
                          hovertemplate = paste("Bs=%{x:.0f}","
Pb=%{y:.0f}","<extra></extra>"))
  fig = fig %>% add_trace(y = BBOD(bonds,input$by,values$eq$y,input$max_price,"d"), type = "scatter", mode = "lines", name = "$$B^d(N)$$", line = list(color = "red"),
                          hovertemplate = paste("Bd=%{x:.0f}","
Pb=%{y:.0f}","<extra></extra>"))
  fig = fig %>% add_segments(0,eq_Pb,max(bonds),eq_Pb, line = list(color = 'rgb(200, 0, 0)', width = 1, dash = 'dash'), showlegend = FALSE,
                             hovertemplate = paste("B*=%{x:.0f}","
Pb*=%{y:.0f}","<extra></extra>"))
  fig = fig %>% add_segments(values$eq$B,0,values$eq$B,eq_Pb, line = list(color = 'rgb(200, 0, 0)', width = 1, dash = 'dash'), showlegend = FALSE,
                             hovertemplate = paste("B*=%{x:.0f}","
Pb*=%{y:.0f}","<extra></extra>"))

  eq_label = list(
    x = values$eq$B,
    y = eq_Pb,
    text = paste0("B*=",round(values$eq$B),", Pb*=",round(eq_Pb))
  )
  fig = fig %>% layout(annotations = eq_label)

  f <- list(
    family = "Courier New, monospace",
    size = 18,
    color = "#7f7f7f"
  )
  empLevel <- list(
    title = "Nombre de titres en circulation, B",
    titlefont = f
  )
  salReels <- list(
    title = "Prix des titres, Pb (inverse de r)",
    titlefont = f,
    range = c(70,110)
  )
  fig = fig %>% layout(xaxis = empLevel, yaxis = salReels)
}

RPBPlot <- function(input,output,values){
  r = seq(0,20,length.out = 1000)
  to_plot = data.frame(interest = r)

  eq_Pb = BBOD(values$eq$B,input$by,values$eq$y,input$max_price, "o")
  eq_R = values$eq$r
  fig = plot_ly(to_plot, x = ~interest)
  fig = fig %>% add_trace(y = 100/(r/100+1), type = "scatter", mode = "lines", name = "$$\\frac{100}{r+1}$$", line = list(color = "gold"),
                          hovertemplate = paste("r=%{x:.1f}","
Pb=%{y:.0f}","<extra></extra>"))
  fig = fig %>% add_segments(0,eq_Pb,eq_R,eq_Pb, line = list(color = 'rgb(200, 0, 0)', width = 1, dash = 'dash'), showlegend = FALSE,
                             hovertemplate = paste("B*=%{x:.0f}","
Pb*=%{y:.0f}","<extra></extra>"))
  fig = fig %>% add_segments(eq_R,0,eq_R,eq_Pb, line = list(color = 'rgb(200, 0, 0)', width = 1, dash = 'dash'), showlegend = FALSE,
                             hovertemplate = paste("B*=%{x:.0f}","
Pb*=%{y:.0f}","<extra></extra>"))

  eq_label = list(
    x = eq_R,
    y = eq_Pb,
    text = paste0("r*=",round(eq_R,1),", Pb*=",round(eq_Pb))
  )
  fig = fig %>% layout(annotations = eq_label)

  f <- list(
    family = "Courier New, monospace",
    size = 18,
    color = "#7f7f7f"
  )
  empLevel <- list(
    title = "Taux d'intérêt, r",
    titlefont = f
  )
  salReels <- list(
    title = "Prix des titres, Pb",
    titlefont = f
  )
  fig = fig %>% layout(xaxis = empLevel, yaxis = salReels)
}


png("pdqr.png",width = 1250,height = 500)
par(xpd = TRUE,mfrow = c(1,2), mar = c(4,2,0,3))
## Normal dist.
set.seed(1234)
x <- sort(rnorm(1000))
plot(x,dnorm(x),type = "l",axes = FALSE,xlab = "",ylab = "",lwd = 3)
n <- 200
p <- x[n]
## rnorm
col <- grey.colors(1,alpha = 0.1)
h <- hist(x,add = TRUE,freq = FALSE,border = col,cex = 2,col = col)
text(h$breaks[14],h$density[10],cex = 2,"rnorm")
arrows(h$breaks[13],h$density[10],h$breaks[11],h$density[10],col = 'lightgrey',lwd = 3)
## pnorm
polygon(c(x[x <= p],rev(x[x <= p])),c(rep(0,length(x[x<=p])),dnorm(rev(x[x<=p]))),col = "grey")
text(x[n/2],dnorm(x[n/2])/2,"pnorm",cex = 2)
## qnorm
arrows(p,0,p,dnorm(p),lwd = 3,lty = 2,code = 0)
text(p,0 - 0.025,"qnorm",cex = 2)
## dnorm
arrows(min(x),dnorm(p),p,dnorm(p),code = 0, lwd = 3,lty = 3)
text(min(x) - 0.5,dnorm(p),"dnorm",cex = 2,srt = 90)
## redo dnorm
lines(x,dnorm(x),lwd = 3)

## Poisson
set.seed(4321)
lam <- 2.5
x <- sort(rpois(1000,lam))
b <- barplot(dpois(unique(x),lam),axes = FALSE,col = col,border = col,
             ylim = c(-0.03,max(dpois(unique(x),lam))) + 0.02)
points(b[,1],dpois(unique(x),lam),cex = 3,pch = 20)
## rpois
text(b[8,1],dpois(unique(x)[5],lam),"rpois",cex = 2)
arrows(b[8,1],dpois(unique(x)[5],lam) - 0.01,
       b[6,1] + 0.5,dpois(unique(x)[7],lam) + 0.01,col = "lightgrey",lwd = 3)
## dpois
text(b[8,1],dpois(unique(x)[4],lam),"dpois",cex = 2)
arrows(b[7,1],dpois(unique(x)[4],lam),b[4,1],dpois(unique(x)[4],lam),code = 0,lwd = 3, lty = 3)
## qpois
text(b[4,1],-0.01,"qpois",cex = 2)
arrows(b[4,1],dpois(unique(x)[4],lam),b[4,1],0,code = 0,lwd = 3, lty = 2)
## ppois
barplot(dpois(unique(x)[1:2],lam),col = "grey",add = TRUE,border = "grey",axes = FALSE)
text(mean(b[1:2,1]),dpois(unique(x)[1],lam)/2,"ppois",cex = 2)
## redo dpois
points(b[,1],dpois(unique(x),lam),cex = 3,pch = 20)
dev.off()