Professional Documents
Culture Documents
16/04/2022
## -- Conflicts ------------------------------------------
tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library('caret')
library(e1071)
If you look at the example data, you’ll notice that the first variable, ‘ID code,’ isn’t used
much for forecasting the o/p variable, therefore we’ll get rid of it.
bank_statistics <- bank_statistics[-1]
Check to see if the data has any missing values. Missing values in the data as a percentage of
the total
colMeans(is.na(bank_statistics))
Build the Support vector machine Model with the first three Principal Components.
summary(PrinCompAna)
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4
## Standard deviation 1.105719777 1.029822431 1.029429108 1.029245081
## Proportion of Variance 0.006113081 0.005302671 0.005298621 0.005296727
## Cumulative Proportion 0.006113081 0.011415752 0.016714374 0.022011101
## Comp.5 Comp.6 Comp.7 Comp.8
## Standard deviation 1.028391179 1.027880189 1.027567621 1.027060349
## Proportion of Variance 0.005287942 0.005282688 0.005279476 0.005274265
## Cumulative Proportion 0.027299043 0.032581731 0.037861208 0.043135472
## Comp.9 Comp.10 Comp.11 Comp.12
## Standard deviation 1.026221764 1.025946720 1.025236326 1.02468922
## Proportion of Variance 0.005265656 0.005262833 0.005255548 0.00524994
## Cumulative Proportion 0.048401128 0.053663961 0.058919509 0.06416945
## Comp.13 Comp.14 Comp.15 Comp.16
## Standard deviation 1.024306994 1.023413060 1.023309657 1.023233771
## Proportion of Variance 0.005246024 0.005236871 0.005235813 0.005235037
## Cumulative Proportion 0.069415473 0.074652344 0.079888158 0.085123194
## Comp.17 Comp.18 Comp.19 Comp.20
## Standard deviation 1.022474589 1.02213694 1.021691268 1.021593309
## Proportion of Variance 0.005227271 0.00522382 0.005219265 0.005218264
## Cumulative Proportion 0.090350466 0.09557429 0.100793551 0.106011815
## Comp.21 Comp.22 Comp.23 Comp.24
## Standard deviation 1.021270679 1.020801522 1.020518686 1.020040798
## Proportion of Variance 0.005214969 0.005210179 0.005207292 0.005202416
## Cumulative Proportion 0.111226784 0.116436963 0.121644255 0.126846671
## Comp.25 Comp.26 Comp.27 Comp.28
## Standard deviation 1.019779756 1.019264275 1.019116219 1.01907406
## Proportion of Variance 0.005199754 0.005194498 0.005192989 0.00519256
## Cumulative Proportion 0.132046425 0.137240923 0.142433912 0.14762647
## Comp.29 Comp.30 Comp.31 Comp.32
## Standard deviation 1.018573850 1.018183339 1.018125035 1.017674008
## Proportion of Variance 0.005187463 0.005183487 0.005182893 0.005178302
## Cumulative Proportion 0.152813935 0.157997422 0.163180315 0.168358617
## Comp.33 Comp.34 Comp.35 Comp.36
## Standard deviation 1.017245621 1.016780533 1.016496110 1.016461835
## Proportion of Variance 0.005173943 0.005169213 0.005166322 0.005165973
## Cumulative Proportion 0.173532560 0.178701773 0.183868095 0.189034068
## Comp.37 Comp.38 Comp.39 Comp.40
## Standard deviation 1.016112050 1.015851051 1.015350554 1.015219848
## Proportion of Variance 0.005162418 0.005159767 0.005154684 0.005153357
## Cumulative Proportion 0.194196487 0.199356254 0.204510937 0.209664294
## Comp.41 Comp.42 Comp.43 Comp.44
## Standard deviation 1.014838546 1.014393913 1.014158651 1.014012473
## Proportion of Variance 0.005149486 0.005144975 0.005142589 0.005141106
## Cumulative Proportion 0.214813780 0.219958756 0.225101344 0.230242451
## Comp.45 Comp.46 Comp.47 Comp.48
## Standard deviation 1.013610854 1.013356991 1.013096979 1.012820323
## Proportion of Variance 0.005137035 0.005134462 0.005131827 0.005129025
## Cumulative Proportion 0.235379486 0.240513948 0.245645775 0.250774800
## Comp.49 Comp.50 Comp.51 Comp.52
## Standard deviation 1.012704097 1.012436300 1.011836606 1.011706062
## Proportion of Variance 0.005127848 0.005125136 0.005119067 0.005117746
## Cumulative Proportion 0.255902648 0.261027784 0.266146851 0.271264597
## Comp.53 Comp.54 Comp.55 Comp.56
## Standard deviation 1.011570455 1.01143759 1.01088178 1.010575325
## Proportion of Variance 0.005116374 0.00511503 0.00510941 0.005106312
## Cumulative Proportion 0.276380971 0.28149600 0.28660541 0.291711723
## Comp.57 Comp.58 Comp.59 Comp.60
## Standard deviation 1.010532630 1.01026329 1.010079712 1.009774676
## Proportion of Variance 0.005105881 0.00510316 0.005101305 0.005098224
## Cumulative Proportion 0.296817604 0.30192076 0.307022069 0.312120293
## Comp.61 Comp.62 Comp.63 Comp.64
## Standard deviation 1.00948796 1.009225817 1.009070469 1.008761326
## Proportion of Variance 0.00509533 0.005092684 0.005091116 0.005087997
## Cumulative Proportion 0.31721562 0.322308307 0.327399423 0.332487420
## Comp.65 Comp.66 Comp.67 Comp.68
## Standard deviation 1.008359864 1.008017693 1.007888809 1.007549264
## Proportion of Variance 0.005083948 0.005080498 0.005079199 0.005075778
## Cumulative Proportion 0.337571368 0.342651866 0.347731065 0.352806843
## Comp.69 Comp.70 Comp.71 Comp.72
## Standard deviation 1.007177022 1.00686541 1.006768525 1.006323404
## Proportion of Variance 0.005072028 0.00506889 0.005067914 0.005063434
## Cumulative Proportion 0.357878871 0.36294776 0.368015675 0.373079109
## Comp.73 Comp.74 Comp.75 Comp.76
## Standard deviation 1.006203308 1.005898022 1.005621760 1.005434625
## Proportion of Variance 0.005062225 0.005059154 0.005056376 0.005054494
## Cumulative Proportion 0.378141334 0.383200488 0.388256864 0.393311358
## Comp.77 Comp.78 Comp.79 Comp.80
## Standard deviation 1.005193594 1.005056096 1.004629822 1.004335492
## Proportion of Variance 0.005052071 0.005050689 0.005046405 0.005043449
## Cumulative Proportion 0.398363429 0.403414118 0.408460523 0.413503972
## Comp.81 Comp.82 Comp.83 Comp.84
## Standard deviation 1.004246820 1.003960960 1.00388546 1.003389966
## Proportion of Variance 0.005042558 0.005039688 0.00503893 0.005033957
## Cumulative Proportion 0.418546530 0.423586218 0.42862515 0.433659106
## Comp.85 Comp.86 Comp.87 Comp.88
## Standard deviation 1.003027881 1.002843548 1.002692203 1.002394921
## Proportion of Variance 0.005030325 0.005028476 0.005026958 0.005023978
## Cumulative Proportion 0.438689430 0.443717906 0.448744864 0.453768842
## Comp.89 Comp.90 Comp.91 Comp.92
Comp.93
## Standard deviation 1.002129301 1.001959597 1.0017784 1.001399918
1.0013291
## Proportion of Variance 0.005021316 0.005019615 0.0050178 0.005014009
0.0050133
## Cumulative Proportion 0.458790158 0.463809773 0.4688276 0.473841582
0.4788549
## Comp.94 Comp.95 Comp.96 Comp.97
## Standard deviation 1.000906248 1.000564321 1.000319758 1.000246087
## Proportion of Variance 0.005009067 0.005005645 0.005003198 0.005002461
## Cumulative Proportion 0.483863948 0.488869593 0.493872791 0.498875252
## Comp.98 Comp.99 Comp.100 Comp.101
## Standard deviation 0.999843125 0.999293565 0.999202589 0.998936060
## Proportion of Variance 0.004998431 0.004992938 0.004992029 0.004989366
## Cumulative Proportion 0.503873683 0.508866622 0.513858651 0.518848017
## Comp.102 Comp.103 Comp.104 Comp.105
## Standard deviation 0.998748103 0.99861307 0.998331831 0.998149480
## Proportion of Variance 0.004987489 0.00498614 0.004983332 0.004981512
## Cumulative Proportion 0.523835506 0.52882165 0.533804978 0.538786490
## Comp.106 Comp.107 Comp.108 Comp.109
## Standard deviation 0.997771324 0.997668986 0.997339518 0.996849819
## Proportion of Variance 0.004977738 0.004976717 0.004973431 0.004968548
## Cumulative Proportion 0.543764228 0.548740945 0.553714376 0.558682924
## Comp.110 Comp.111 Comp.112 Comp.113
## Standard deviation 0.996723750 0.996441176 0.996258649 0.996082099
## Proportion of Variance 0.004967291 0.004964475 0.004962656 0.004960898
## Cumulative Proportion 0.563650215 0.568614690 0.573577346 0.578538244
## Comp.114 Comp.115 Comp.116 Comp.117
## Standard deviation 0.995765351 0.995622718 0.995382190 0.994997908
## Proportion of Variance 0.004957743 0.004956323 0.004953929 0.004950104
## Cumulative Proportion 0.583495987 0.588452310 0.593406239 0.598356343
## Comp.118 Comp.119 Comp.120 Comp.121
## Standard deviation 0.994868229 0.994660893 0.994424829 0.99402620
## Proportion of Variance 0.004948814 0.004946751 0.004944404 0.00494044
## Cumulative Proportion 0.603305157 0.608251908 0.613196312 0.61813675
## Comp.122 Comp.123 Comp.124 Comp.125
## Standard deviation 0.993908183 0.993571970 0.993493407 0.993088658
## Proportion of Variance 0.004939267 0.004935926 0.004935146 0.004931125
## Cumulative Proportion 0.623076020 0.628011946 0.632947092 0.637878217
## Comp.126 Comp.127 Comp.128 Comp.129
## Standard deviation 0.992966138 0.992702816 0.992625127 0.99220564
## Proportion of Variance 0.004929909 0.004927294 0.004926523 0.00492236
## Cumulative Proportion 0.642808126 0.647735421 0.652661944 0.65758430
## Comp.130 Comp.131 Comp.132 Comp.133
## Standard deviation 0.992042540 0.991908895 0.991439602 0.991234259
## Proportion of Variance 0.004920742 0.004919416 0.004914762 0.004912727
## Cumulative Proportion 0.662505046 0.667424462 0.672339225 0.677251951
## Comp.134 Comp.135 Comp.136 Comp.137
## Standard deviation 0.991050581 0.990732667 0.990468753 0.990376945
## Proportion of Variance 0.004910906 0.004907756 0.004905142 0.004904232
## Cumulative Proportion 0.682162858 0.687070614 0.691975756 0.696879988
## Comp.138 Comp.139 Comp.140 Comp.141
## Standard deviation 0.990043974 0.989915233 0.989638621 0.989350388
## Proportion of Variance 0.004900935 0.004899661 0.004896923 0.004894071
## Cumulative Proportion 0.701780923 0.706680584 0.711577507 0.716471578
## Comp.142 Comp.143 Comp.144 Comp.145
## Standard deviation 0.988808954 0.988676403 0.988471795 0.987895599
## Proportion of Variance 0.004888716 0.004887405 0.004885382 0.004879689
## Cumulative Proportion 0.721360294 0.726247699 0.731133082 0.736012770
## Comp.146 Comp.147 Comp.148 Comp.149
## Standard deviation 0.987489364 0.987237785 0.987163909 0.986930163
## Proportion of Variance 0.004875676 0.004873192 0.004872463 0.004870156
## Cumulative Proportion 0.740888446 0.745761639 0.750634101 0.755504257
## Comp.150 Comp.151 Comp.152 Comp.153
## Standard deviation 0.986730047 0.986360921 0.986046874 0.985848279
## Proportion of Variance 0.004868181 0.004864539 0.004861442 0.004859484
## Cumulative Proportion 0.760372438 0.765236977 0.770098420 0.774957904
## Comp.154 Comp.155 Comp.156 Comp.157
## Standard deviation 0.985601252 0.9854034 0.984950929 0.984777560
## Proportion of Variance 0.004857049 0.0048551 0.004850642 0.004848934
## Cumulative Proportion 0.779814953 0.7846701 0.789520694 0.794369628
## Comp.158 Comp.159 Comp.160 Comp.161
## Standard deviation 0.984524008 0.984393268 0.984114620 0.98396748
## Proportion of Variance 0.004846438 0.004845151 0.004842408 0.00484096
## Cumulative Proportion 0.799216066 0.804061217 0.808903625 0.81374458
## Comp.162 Comp.163 Comp.164 Comp.165
## Standard deviation 0.983431890 0.983259936 0.983081320 0.982790493
## Proportion of Variance 0.004835691 0.004834001 0.004832244 0.004829386
## Cumulative Proportion 0.818580276 0.823414276 0.828246521 0.833075907
## Comp.166 Comp.167 Comp.168 Comp.169
## Standard deviation 0.982689722 0.982193906 0.981566695 0.981350989
## Proportion of Variance 0.004828395 0.004823524 0.004817366 0.004815249
## Cumulative Proportion 0.837904302 0.842727826 0.847545192 0.852360441
## Comp.170 Comp.171 Comp.172 Comp.173
## Standard deviation 0.981156986 0.980937039 0.980634414 0.980446431
## Proportion of Variance 0.004813345 0.004811187 0.004808219 0.004806376
## Cumulative Proportion 0.857173786 0.861984974 0.866793193 0.871599569
## Comp.174 Comp.175 Comp.176 Comp.177
## Standard deviation 0.980058665 0.979885529 0.979669532 0.979178959
## Proportion of Variance 0.004802575 0.004800878 0.004798762 0.004793957
## Cumulative Proportion 0.876402144 0.881203022 0.886001784 0.890795741
## Comp.178 Comp.179 Comp.180 Comp.181
## Standard deviation 0.979123281 0.978591938 0.97813295 0.977674583
## Proportion of Variance 0.004793412 0.004788211 0.00478372 0.004779238
## Cumulative Proportion 0.895589153 0.900377364 0.90516108 0.909940322
## Comp.182 Comp.183 Comp.184 Comp.185
## Standard deviation 0.977534333 0.977241822 0.976819697 0.976228536
## Proportion of Variance 0.004777867 0.004775008 0.004770884 0.004765111
## Cumulative Proportion 0.914718189 0.919493197 0.924264081 0.929029192
## Comp.186 Comp.187 Comp.188 Comp.189
## Standard deviation 0.975882947 0.975736385 0.975336627 0.97489798
## Proportion of Variance 0.004761738 0.004760307 0.004756408 0.00475213
## Cumulative Proportion 0.933790929 0.938551237 0.943307644 0.94805977
## Comp.190 Comp.191 Comp.192 Comp.193
## Standard deviation 0.97444244 0.973860243 0.973657731 0.973090097
## Proportion of Variance 0.00474769 0.004742019 0.004740047 0.004734522
## Cumulative Proportion 0.95280747 0.957549484 0.962289531 0.967024052
## Comp.194 Comp.195 Comp.196 Comp.197
## Standard deviation 0.972756706 0.972401962 0.971319846 0.970912041
## Proportion of Variance 0.004731278 0.004727828 0.004717311 0.004713351
## Cumulative Proportion 0.971755331 0.976483158 0.981200470 0.985913821
## Comp.198 Comp.199 Comp.200
## Standard deviation 0.969434370 0.969170184 0.96857733
## Proportion of Variance 0.004699015 0.004696454 0.00469071
## Cumulative Proportion 0.990612836 0.995309290 1.00000000
split your time between training and testing 75% of the total sample size
X <- PrinCompAna$scores[,1:3]
PrinCompAna_Info<-cbind(Y,PrinCompAna$scores[,1:3])
set.seed(4)
sample_index <- sample(seq_len(nrow(PrinCompAna_Info)), size = size)
confusionMatrix(table(testset_predictions, PrinCompAna_testset$target))