library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
library(mlbench)
data("Soybean")
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(Soybean)
## Rows: 683
## Columns: 36
## $ Class           <fct> diaporthe-stem-canker, diaporthe-stem-canker, diaporth…
## $ date            <fct> 6, 4, 3, 3, 6, 5, 5, 4, 6, 4, 6, 4, 3, 6, 6, 5, 6, 4, …
## $ plant.stand     <ord> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ precip          <ord> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ temp            <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, …
## $ hail            <fct> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, …
## $ crop.hist       <fct> 1, 2, 1, 1, 2, 3, 2, 1, 3, 2, 1, 1, 1, 3, 1, 3, 0, 2, …
## $ area.dam        <fct> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 2, 3, 3, 3, 2, 2, …
## $ sever           <fct> 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ seed.tmt        <fct> 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, …
## $ germ            <ord> 0, 1, 2, 1, 2, 1, 0, 2, 1, 2, 0, 1, 0, 0, 1, 2, 0, 1, …
## $ plant.growth    <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ leaves          <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ leaf.halo       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ leaf.marg       <fct> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ leaf.size       <ord> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ leaf.shread     <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ leaf.malf       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ leaf.mild       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ stem            <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ lodging         <fct> 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, …
## $ stem.cankers    <fct> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ canker.lesion   <fct> 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, …
## $ fruiting.bodies <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ ext.decay       <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ mycelium        <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ int.discolor    <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ sclerotia       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ fruit.pods      <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ fruit.spots     <fct> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ seed            <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ mold.growth     <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ seed.discolor   <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ seed.size       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ shriveling      <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ roots           <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
head(nearZeroVar(Soybean, saveMetrics = TRUE))
##             freqRatio percentUnique zeroVar   nzv
## Class        1.010989     2.7818448   FALSE FALSE
## date         1.137405     1.0248902   FALSE FALSE
## plant.stand  1.208191     0.2928258   FALSE FALSE
## precip       4.098214     0.4392387   FALSE FALSE
## temp         1.879397     0.4392387   FALSE FALSE
## hail         3.425197     0.2928258   FALSE FALSE
nearZeroVar(Soybean)
## [1] 19 26 28
mySoybean<-Soybean[,-nearZeroVar(Soybean)]
glimpse(mySoybean)
## Rows: 683
## Columns: 33
## $ Class           <fct> diaporthe-stem-canker, diaporthe-stem-canker, diaporth…
## $ date            <fct> 6, 4, 3, 3, 6, 5, 5, 4, 6, 4, 6, 4, 3, 6, 6, 5, 6, 4, …
## $ plant.stand     <ord> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ precip          <ord> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ temp            <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, …
## $ hail            <fct> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, …
## $ crop.hist       <fct> 1, 2, 1, 1, 2, 3, 2, 1, 3, 2, 1, 1, 1, 3, 1, 3, 0, 2, …
## $ area.dam        <fct> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 2, 3, 3, 3, 2, 2, …
## $ sever           <fct> 1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ seed.tmt        <fct> 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, …
## $ germ            <ord> 0, 1, 2, 1, 2, 1, 0, 2, 1, 2, 0, 1, 0, 0, 1, 2, 0, 1, …
## $ plant.growth    <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ leaves          <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ leaf.halo       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ leaf.marg       <fct> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ leaf.size       <ord> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ leaf.shread     <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ leaf.malf       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ stem            <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ lodging         <fct> 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, …
## $ stem.cankers    <fct> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ canker.lesion   <fct> 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, …
## $ fruiting.bodies <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ ext.decay       <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ int.discolor    <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ fruit.pods      <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ fruit.spots     <fct> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ seed            <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ mold.growth     <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ seed.discolor   <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ seed.size       <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ shriveling      <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ roots           <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#(8) 상관된 예측변수의 식별
data(mdrr)
dim(mdrrDescr)
## [1] 528 342
nzv<-nearZeroVar(mdrrDescr)
filteredDescr<-mdrrDescr[,-nzv]
dim(filteredDescr)
## [1] 528 297
descrCor<-cor(filteredDescr)
head(descrCor)
##            MW         AMW          Sv         Se          Sp        Ss
## MW  1.0000000  0.20967709  0.95188957  0.9286910  0.94737618 0.9298075
## AMW 0.2096771  1.00000000 -0.06801714 -0.1545355 -0.07976117 0.2447579
## Sv  0.9518896 -0.06801714  1.00000000  0.9853667  0.99899196 0.8563386
## Se  0.9286910 -0.15453552  0.98536665  1.0000000  0.98465186 0.8537654
## Sp  0.9473762 -0.07976117  0.99899196  0.9846519  1.00000000 0.8409921
## Ss  0.9298075  0.24475788  0.85633860  0.8537654  0.84099210 1.0000000
##              Mv         Me         Mp         Ms        nAT        nSK
## MW  -0.09082461 0.34587009 -0.1052367 0.27981880  0.9162981 0.98205704
## AMW  0.73701464 0.65696280  0.6836689 0.48300240 -0.1867089 0.09189505
## Sv  -0.22831661 0.10947144 -0.2254321 0.09517120  0.9855293 0.97662619
## Se  -0.37580835 0.13263700 -0.3759322 0.12881552  0.9987786 0.95709230
## Sp  -0.23682236 0.08380525 -0.2238575 0.07234055  0.9861753 0.96849177
## Ss  -0.11473060 0.54649645 -0.1904525 0.57136972  0.8303303 0.93316350
##            nBT       nBO       nBM      SCBO      nCIC      nCIR        RBN
## MW   0.9267580 0.9710373 0.5271286 0.9444384 0.4850675 0.2484989  0.5511741
## AMW -0.1576766 0.1303615 0.3515257 0.1972355 0.3053576 0.2568213 -0.3228133
## Sv   0.9910966 0.9604445 0.5032641 0.9277624 0.4480088 0.2112268  0.6254926
## Se   0.9965821 0.9280641 0.3800928 0.8730395 0.3523635 0.1386068  0.6988522
## Sp   0.9906143 0.9508258 0.4862462 0.9157510 0.4336208 0.2057548  0.6362179
## Ss   0.8352902 0.9105428 0.5083830 0.8943251 0.3809503 0.1492953  0.5368455
##            RBF       nDB       nAB         nH          nC        nN         nO
## MW   0.1957697 0.3746549 0.4450441  0.7867276  0.87860051 0.3648968 0.54068460
## AMW -0.3319109 0.2718456 0.3014940 -0.4057519 -0.08428385 0.1474591 0.05945086
## Sv   0.2523732 0.2893485 0.4367403  0.9131029  0.95747325 0.2974324 0.47134498
## Se   0.3397085 0.2877774 0.3128527  0.9528339  0.91016335 0.3212916 0.54654852
## Sp   0.2660536 0.2820837 0.4209141  0.9210769  0.95604008 0.2829388 0.45900836
## Ss   0.2239035 0.4710925 0.4064562  0.6765673  0.78158052 0.3327821 0.65429652
##              nS        nF          nCL         nX        nR05      nR06
## MW   0.08528065 0.2017455  0.147562397 0.27057397  0.05046498 0.5047890
## AMW  0.26497596 0.2350454  0.602236574 0.60469070  0.30600313 0.1606228
## Sv   0.01349455 0.1224224 -0.008037661 0.09533773 -0.01656418 0.4960084
## Se  -0.02596092 0.1074257 -0.075871647 0.03702947 -0.06896035 0.4284537
## Sp   0.03766054 0.1085915 -0.005169256 0.08738106 -0.02417232 0.4845226
## Ss  -0.01819184 0.3763874  0.061259302 0.33812148  0.00919290 0.4231342
##             nR07       nR09       nR10       nBnz       ISIZ        IAC
## MW  -0.101455048 0.05930043 0.10238498 0.25269983  0.9132374 0.96550840
## AMW  0.003413646 0.09905058 0.15431039 0.09516279 -0.1906953 0.08454529
## Sv  -0.075726068 0.04122089 0.05180272 0.30819119  0.9833418 0.93324109
## Se  -0.106529248 0.01723995 0.04135804 0.19312695  0.9980267 0.94832439
## Sp  -0.074356896 0.03655439 0.05453055 0.30654416  0.9841389 0.92736358
## Ss  -0.146987920 0.02998271 0.05449157 0.21919556  0.8277222 0.93084643
##           AAC       ZM1      ZM1V       ZM2      ZM2V    Qindex      SNar
## MW  0.3908679 0.9607061 0.8790962 0.9389345 0.8838700 0.7180823 0.9421343
## AMW 0.7420192 0.1762787 0.2713818 0.2007750 0.2864311 0.3500394 0.1307130
## Sv  0.1328689 0.9367045 0.7892481 0.9081833 0.8074374 0.6529170 0.9387684
## Se  0.1370251 0.8995326 0.7926374 0.8678335 0.7883408 0.5794895 0.8951426
## Sp  0.1162384 0.9259953 0.7676795 0.8965109 0.7847106 0.6379591 0.9286226
## Ss  0.5038406 0.9066836 0.9617485 0.8858304 0.9195280 0.6620563 0.8622643
##            HNar         GNar         Xt        Dz       Ram       Pol      LPRS
## MW  -0.07895621 -0.017588049 -0.4151130 0.9786386 0.8376157 0.9237540 0.9774704
## AMW  0.05607903  0.161815221 -0.1889936 0.1220584 0.3345756 0.1595316 0.0683386
## Sv  -0.03618675 -0.004081915 -0.4053237 0.9534989 0.7490594 0.8968466 0.9775460
## Se  -0.12500379 -0.100815241 -0.3709991 0.9452664 0.7200660 0.8727737 0.9605241
## Sp  -0.04211406 -0.012472006 -0.4011398 0.9428162 0.7358984 0.8860734 0.9702983
## Ss  -0.22081704 -0.147909448 -0.3803876 0.9600181 0.8452294 0.8881577 0.9286448
##           VDA        MSD       SMTI      SMTIV       GMTI      GMTIV         Xu
## MW  0.9250940 -0.2147447 0.90843480 0.90655988 0.90621536 0.88982723 0.97505245
## AMW 0.0120026 -0.2322202 0.01733212 0.05172392 0.03011499 0.09306559 0.07865133
## Sv  0.9404413 -0.1631812 0.92190984 0.90128381 0.91853875 0.86694790 0.97554266
## Se  0.9290220 -0.1366022 0.90805483 0.89376714 0.89883832 0.85966802 0.95511926
## Sp  0.9361956 -0.1536305 0.91715700 0.89282221 0.91314662 0.85475305 0.96847894
## Ss  0.8768191 -0.2028190 0.85633045 0.89295659 0.84977928 0.90692528 0.92412707
##             SPI           W         WA     RDSUM       Har          QW
## MW   0.26055999 0.906004683  0.8545971 0.9682996 0.9664243  0.88003754
## AMW -0.02574053 0.002837938 -0.0135374 0.1169316 0.1431497 -0.03142914
## Sv   0.25039069 0.921035211  0.8784443 0.9532611 0.9485753  0.90389824
## Se   0.28020040 0.913226940  0.8679355 0.9319021 0.9194232  0.90311694
## Sp   0.24974944 0.916981091  0.8761722 0.9433342 0.9381872  0.90180770
## Ss   0.28396439 0.858454955  0.8087871 0.9212793 0.9143984  0.83717139
##           TI1        TI2        HyDp     RHyDp          w         ww        Rww
## MW  0.8214514  0.6215270  0.80857864 0.9646740 0.90872550 0.82233313  0.7507301
## AMW 0.1550434 -0.0948682 -0.03214644 0.1419524 0.09397116 0.08875367 -0.1268642
## Sv  0.8110758  0.6658022  0.83387884 0.9459416 0.90007381 0.81659472  0.7820619
## Se  0.7490754  0.6623035  0.83033405 0.9183889 0.87550987 0.78839754  0.8203557
## Sp  0.8008289  0.6696830  0.83297472 0.9354575 0.89377709 0.81298736  0.7831237
## Ss  0.7397351  0.5889368  0.76093450 0.9146919 0.83620044 0.73865185  0.7672896
##             D.D       Wap       WhetZ       Whetm       Whetv       Whete
## MW   0.88510503 0.6622164  0.88385601  0.88379019  0.87850318  0.88650045
## AMW -0.04408623 0.1689241 -0.03157923 -0.03181383 -0.01341869 -0.02197529
## Sv   0.90999109 0.6331525  0.91152202  0.91152601  0.89208636  0.91140850
## Se   0.91492441 0.5855012  0.90494693  0.90497852  0.89314580  0.90368231
## Sp   0.90633051 0.6250376  0.90878643  0.90879580  0.88813962  0.90917083
## Ss   0.86244503 0.5738079  0.83650132  0.83636307  0.83497547  0.83657103
##           Whetp          J       JhetZ       Jhetm      Jhetv       Jhete
## MW   0.87604995 -0.4929882 -0.47544394 -0.47538005 -0.5778301 -0.47985573
## AMW -0.01411903 -0.1850444 -0.02693976 -0.02632285 -0.0983202 -0.07030296
## Sv   0.88849683 -0.4931155 -0.51976327 -0.51987309 -0.5770464 -0.51332792
## Se   0.89106329 -0.4053414 -0.44502247 -0.44517248 -0.5308853 -0.43329000
## Sp   0.88399289 -0.4850463 -0.51534815 -0.51546579 -0.5705589 -0.51101144
## Ss   0.83486719 -0.3854426 -0.37261340 -0.37241154 -0.4818172 -0.36632066
##           Jhetp     MAXDN     MAXDP      DELS       TIE         X0        X1
## MW  -0.57731901 0.3927627 0.5275936 0.7212662 0.6209822 0.98049752 0.9780086
## AMW -0.08800125 0.3145848 0.1754947 0.3140020 0.0467408 0.07884183 0.0813955
## Sv  -0.57441099 0.2797721 0.4691527 0.5878814 0.5861610 0.97238934 0.9776776
## Se  -0.53593435 0.2842629 0.4747707 0.6246938 0.6219303 0.96351153 0.9562001
## Sp  -0.56644787 0.2702499 0.4594741 0.5680235 0.5814014 0.96515558 0.9694915
## Ss  -0.49092318 0.5774095 0.6526520 0.8993395 0.6623925 0.94724286 0.9210180
##            X2        X3        X4        X5         X0A         X1A        X2A
## MW  0.9708452 0.9473416 0.9022741 0.8212001  0.04598241 -0.13696315 -0.2772059
## AMW 0.1804917 0.1728818 0.2044000 0.2179588 -0.10333808 -0.45402899 -0.2640088
## Sv  0.9432595 0.9258675 0.8727362 0.7880366  0.01515342 -0.05285817 -0.2332195
## Se  0.9088315 0.8874330 0.8285383 0.7379266  0.10892325  0.04309395 -0.1811173
## Sp  0.9333696 0.9150633 0.8597035 0.7741316  0.02201521 -0.03988711 -0.2207029
## Ss  0.9290414 0.8892559 0.8389333 0.7543899  0.18449130 -0.09645243 -0.2213289
##             X3A         X4A        X5A       X0v        X1v        X2v
## MW  -0.21876278 -0.20817672 -0.2819922 0.9792727 0.95946958 0.90990795
## AMW -0.33844435 -0.37364241 -0.3872483 0.0464821 0.03714706 0.09518381
## Sv  -0.14307999 -0.12184197 -0.1868594 0.9855138 0.97576376 0.91424206
## Se  -0.09512692 -0.07138191 -0.1462944 0.9710621 0.94986307 0.87351937
## Sp  -0.13002227 -0.11012620 -0.1757217 0.9853749 0.97803257 0.92013329
## Ss  -0.20629333 -0.19703127 -0.2719451 0.8815476 0.83976337 0.77429532
##           X3v       X4v       X5v        X0Av        X1Av        X2Av
## MW  0.8643729 0.7983370 0.6900370 -0.03593946 -0.01443922 -0.10874705
## AMW 0.0874445 0.1135576 0.1247294 -0.23724135 -0.34599116 -0.30049870
## Sv  0.8732545 0.8000591 0.6902905  0.01712759  0.07643510 -0.02012696
## Se  0.8293005 0.7484376 0.6323131  0.04289127  0.09906758 -0.01701906
## Sp  0.8795394 0.8064078 0.6968579  0.05506621  0.11616461  0.01924686
## Ss  0.7264202 0.6542118 0.5493218 -0.26762486 -0.22149595 -0.31054297
##            X3Av        X4Av        X5Av      X0sol     X1sol     X2sol
## MW  -0.05987416 -0.06518992 -0.08021315 0.97816487 0.9785787 0.9650637
## AMW -0.31383341 -0.32479224 -0.30554157 0.09836941 0.1091732 0.2213484
## Sv   0.03353638  0.02774770  0.01231006 0.96688657 0.9718113 0.9284240
## Se   0.03818486  0.03441237  0.01168322 0.95386558 0.9457745 0.8868714
## Sp   0.07101506  0.06297795  0.04438281 0.96298493 0.9673754 0.9247879
## Ss  -0.23539135 -0.21847983 -0.21657704 0.90409399 0.8854385 0.8644975
##         X3sol     X4sol     X5sol      XMOD     RDCHI      RDSQ       S0K
## MW  0.9438064 0.8969984 0.8060515 0.9949188 0.9377361 0.9551753 0.9464520
## AMW 0.2260177 0.2750821 0.2772367 0.1834925 0.0689678 0.1434686 0.3152925
## Sv  0.9084256 0.8480908 0.7571569 0.9576622 0.9505158 0.9346104 0.8767452
## Se  0.8622731 0.7938203 0.6974890 0.9337297 0.9166418 0.9068176 0.8333945
## Sp  0.9036518 0.8414479 0.7503708 0.9517883 0.9440769 0.9236993 0.8649864
## Ss  0.8380710 0.7857402 0.6899643 0.9260270 0.8634617 0.9037308 0.9136943
##             S1K        S2K        S3K        PHI         BLI          PW2
## MW  0.956387445  0.8615195  0.7384356  0.7965862 -0.01354877  0.183699044
## AMW 0.008159653 -0.1286015 -0.1648575 -0.1643853 -0.34595281  0.515620561
## Sv  0.958757453  0.9018597  0.7852994  0.8373584  0.07720926  0.059971682
## Se  0.970012435  0.9286380  0.8193642  0.8812891  0.09998984 -0.007628512
## Sp  0.955653148  0.9031453  0.7901235  0.8414447  0.11694157  0.049701844
## Ss  0.919815940  0.8074615  0.7002060  0.7549130 -0.22024709  0.234582820
##           PW3       PW4        PW5         PJI2        CSI        ECC
## MW  0.3028725 0.1737005 0.11055903  0.155695654 0.90261252 0.90090114
## AMW 0.2212613 0.1622236 0.21699112 -0.006582789 0.02949439 0.00995003
## Sv  0.2509618 0.1443141 0.06488726  0.168565947 0.91684037 0.91734832
## Se  0.2335272 0.1194211 0.02974979  0.151802596 0.89682437 0.90496350
## Sp  0.2410177 0.1306922 0.05091091  0.169530628 0.91265857 0.91395413
## Ss  0.3180224 0.1550025 0.10052788  0.133723552 0.84735076 0.85186340
##             AECC        DECC        MDDD        UNIP       CENT        VAR
## MW   0.810083019  0.73591710 0.927747443 0.898920290 0.92817756 0.90267900
## AMW -0.007218997 -0.03012813 0.006815232 0.006493899 0.01462347 0.04614105
## Sv   0.831489203  0.75674479 0.942929200 0.914624665 0.94038458 0.91178881
## Se   0.819904643  0.75536066 0.934097821 0.904539808 0.93156671 0.89218723
## Sp   0.829723519  0.75453232 0.939028957 0.910161151 0.93651014 0.91081355
## Ss   0.766038762  0.70279588 0.874833636 0.854931940 0.87549184 0.83826679
##             BAC         Lop       ICR        IDE       IDM      IDDE      IDDM
## MW   0.47031312  0.02192966 0.7876015 0.81646376 0.9617329 0.6384014 0.9649979
## AMW -0.05015641 -0.19599646 0.0132650 0.01219311 0.1334711 0.2671079 0.1275157
## Sv   0.43704795  0.03104165 0.7973554 0.83307875 0.9509064 0.5822384 0.9558603
## Se   0.51989420  0.11146500 0.7913350 0.81882339 0.9249129 0.5451317 0.9298186
## Sp   0.43671043  0.04375501 0.7936255 0.83064064 0.9424712 0.5796454 0.9476139
## Ss   0.55444832  0.05375647 0.7523088 0.77170893 0.9113449 0.5760278 0.9146361
##           IDET          IDMT      IVDE      IVDM       HVcpx     HDcpx
## MW  0.94744644  0.8958035809 0.3167991 0.8755214 0.798720921 0.9415097
## AMW 0.03231042 -0.0004203521 0.2154089 0.2280878 0.005713375 0.1484050
## Sv  0.95438601  0.9108355626 0.2140658 0.8669441 0.815565503 0.9284838
## Se  0.94253822  0.9043192640 0.2547097 0.7955664 0.803671488 0.9007679
## Sp  0.94855011  0.9069051058 0.2158867 0.8550186 0.812834059 0.9200671
## Ss  0.89957682  0.8486213356 0.3757966 0.8364338 0.755102509 0.8907325
##          Uindex     Vindex     Xindex     Yindex       IC0       TIC0
## MW   0.82316140 -0.7229446 -0.7140244 -0.7133289 0.3908679 0.96550840
## AMW -0.09370165 -0.1818615 -0.2077938 -0.1412057 0.7420192 0.08454529
## Sv   0.84593154 -0.7224018 -0.7086989 -0.7192036 0.1328689 0.93324109
## Se   0.88451816 -0.6514077 -0.6312041 -0.6578958 0.1370251 0.94832439
## Sp   0.84601330 -0.7143809 -0.6994866 -0.7130728 0.1162384 0.92736358
## Ss   0.82359158 -0.6267803 -0.6147344 -0.6214166 0.5038406 0.93084643
##            SIC0       CIC0        BIC0       IC1      TIC1       SIC1
## MW  -0.15857185  0.7288128 -0.13387949 0.3823869 0.9481144 -0.0494883
## AMW  0.75165289 -0.4441165  0.71577281 0.6709697 0.1172958  0.7165117
## Sv  -0.42104372  0.8907158 -0.39477203 0.1586852 0.9075099 -0.2895753
## Se  -0.41925462  0.8944228 -0.38061941 0.1535759 0.9183363 -0.2961292
## Sp  -0.43548074  0.8972643 -0.40805970 0.1411329 0.8998483 -0.3060496
## Ss  -0.01059349  0.6004715  0.01450916 0.5001340 0.9273217  0.1025642
##           CIC1        BIC1       IC2        TIC2        SIC2       CIC2
## MW   0.4597330 -0.02559074 0.5323461  0.93664180 -0.03217007  0.3134341
## AMW -0.6145764  0.68732341 0.4171664 -0.03250569  0.55416541 -0.5391314
## Sv   0.6727312 -0.26400523 0.4011042  0.95280569 -0.21364292  0.4947236
## Se   0.6808842 -0.26075384 0.3865776  0.95971188 -0.23075009  0.5134829
## Sp   0.6854829 -0.27954608 0.3952505  0.95137471 -0.21977447  0.5003228
## Ss   0.3040982  0.12667619 0.5523674  0.87196744  0.04705603  0.2162649
##             BIC2       IC3        TIC3        SIC3       CIC3        BIC3
## MW   0.002191439 0.5429803  0.92686681 -0.11423016  0.3022473 -0.07547447
## AMW  0.493340445 0.3211993 -0.07654803  0.48365006 -0.4719258  0.41384188
## Sv  -0.169963729 0.4643880  0.96454305 -0.25081349  0.4401954 -0.20130959
## Se  -0.173186620 0.4235470  0.96449985 -0.29814316  0.4865015 -0.23284907
## Sp  -0.174160964 0.4646966  0.96542058 -0.25056674  0.4399212 -0.19851775
## Ss   0.076929659 0.4960755  0.84012783 -0.09648683  0.2684545 -0.06458362
##           IC4        TIC4       SIC4       CIC4        BIC4       IC5
## MW  0.5685835  0.92533887 -0.1102349  0.2724218 -0.06870447 0.5925035
## AMW 0.2913614 -0.08598676  0.4697413 -0.4600082  0.40051711 0.2731704
## Sv  0.5101493  0.96899700 -0.2299065  0.3944791 -0.17795752 0.5394462
## Se  0.4559088  0.96538772 -0.2943911  0.4570701 -0.22680390 0.4896330
## Sp  0.5094830  0.96960231 -0.2307369  0.3952206 -0.17608878 0.5383935
## Ss  0.4966851  0.83147253 -0.1177486  0.2651057 -0.08485858 0.5204704
##            TIC5        SIC5       CIC5        BIC5       LP1       STN
## MW   0.92345614 -0.08062978  0.2410094 -0.03606779 0.3679611 0.4912387
## AMW -0.09129018  0.46297353 -0.4550421  0.39077261 0.2944266 0.2950020
## Sv   0.96857406 -0.19759493  0.3607933 -0.14204579 0.3021531 0.4576007
## Se   0.96652545 -0.25915097  0.4208403 -0.18716579 0.2632225 0.3623399
## Sp   0.96917598 -0.19897894  0.3619876 -0.14070122 0.2925116 0.4432477
## Ss   0.82985085 -0.08866575  0.2350443 -0.05271510 0.3517254 0.3872485
##           Eig1Z       Eig1m       Eig1v       Eig1e       Eig1p     SEigZ
## MW   0.89782091  0.89774449  0.89593053  0.90263359  0.89209003 0.7069556
## AMW -0.04195232 -0.04233142 -0.01215522 -0.02643372 -0.01283513 0.6473325
## Sv   0.93091630  0.93095089  0.90781169  0.93144464  0.90222953 0.4792664
## Se   0.92267910  0.92275560  0.91088147  0.92137606  0.90748619 0.4784826
## Sp   0.92848954  0.92853289  0.90364222  0.92973214  0.89727988 0.4727616
## Ss   0.84891550  0.84872260  0.85334637  0.84981644  0.85317446 0.7292781
##         SEigm      SEigv     SEige      SEigp      VEA1       VEA2        VRA1
## MW  0.7052265 -0.6718138 0.7275274 -0.6527406 0.6629527 -0.8841094  0.15912651
## AMW 0.6544210 -0.2006190 0.3563896 -0.1837007 0.1201435 -0.1121726 -0.03659697
## Sv  0.4758942 -0.5564118 0.5698509 -0.5432014 0.6537090 -0.8763845  0.18019586
## Se  0.4735491 -0.6218901 0.6195372 -0.6080358 0.6364946 -0.8516654  0.18296374
## Sp  0.4690808 -0.5336744 0.5495943 -0.5189626 0.6446985 -0.8704959  0.18944447
## Ss  0.7318486 -0.8439906 0.8525357 -0.8370624 0.6286907 -0.8378128  0.11086923
##            VRA2      VED1       VED2      VRD1      VRD2      VEZ1       VEZ2
## MW   0.15920562 0.9782735 -0.9380842 0.9740949 0.9376893 0.9773274 -0.9381861
## AMW -0.03663649 0.1091432 -0.1452339 0.1036628 0.1850977 0.1091649 -0.1451271
## Sv   0.18028892 0.9712351 -0.9270212 0.9647529 0.9159770 0.9710664 -0.9264833
## Se   0.18305978 0.9481359 -0.8983849 0.9416647 0.8752327 0.9470446 -0.8985965
## Sp   0.18954015 0.9629822 -0.9190937 0.9556252 0.9050732 0.9627500 -0.9186232
## Ss   0.11093682 0.9286170 -0.8865567 0.9202844 0.8726975 0.9284409 -0.8865364
##          VRZ1      VRZ2      VEm1       VEm2      VRm1      VRm2      VEv1
## MW  0.9726054 0.9304685 0.9773160 -0.9381771 0.9725872 0.9304158 0.9773157
## AMW 0.1009148 0.1770093 0.1091898 -0.1451538 0.1007755 0.1766277 0.1072509
## Sv  0.9629523 0.9081617 0.9710537 -0.9264537 0.9629757 0.9082235 0.9725869
## Se  0.9414850 0.8714892 0.9470240 -0.8985846 0.9415220 0.8715795 0.9473225
## Sp  0.9538125 0.8973261 0.9627338 -0.9185916 0.9538393 0.8973953 0.9645865
## Ss  0.9178315 0.8633026 0.9284625 -0.8865502 0.9177665 0.8631337 0.9249916
##           VEv2      VRv1      VRv2      VEe1       VEe2      VRe1      VRe2
## MW  -0.9367502 0.9720802 0.9212996 0.9773629 -0.9384230 0.9730361 0.9319606
## AMW -0.1465382 0.1043794 0.1865846 0.1089838 -0.1461752 0.1026676 0.1822150
## Sv  -0.9236056 0.9598051 0.8917060 0.9711373 -0.9263970 0.9628832 0.9081854
## Se  -0.8970155 0.9403118 0.8606081 0.9471083 -0.8985070 0.9412564 0.8711150
## Sp  -0.9154148 0.9502730 0.8798033 0.9628716 -0.9185161 0.9537500 0.8973583
## Ss  -0.8881802 0.9226739 0.8694193 0.9283726 -0.8867138 0.9179786 0.8640223
##          VEp1       VEp2      VRp1      VRp2       NGS     MPC03     MPC04
## MW  0.9771354 -0.9367237 0.9716506 0.9174283 0.9471207 0.8920134 0.8052536
## AMW 0.1060723 -0.1484433 0.1050852 0.1878209 0.2072186 0.2213752 0.2590187
## Sv  0.9728510 -0.9228128 0.9589488 0.8868443 0.9139021 0.8556947 0.7574445
## Se  0.9475622 -0.8962844 0.9396692 0.8564189 0.8736258 0.8122556 0.7089309
## Sp  0.9648884 -0.9145914 0.9493265 0.8747353 0.9025012 0.8428941 0.7429826
## Ss  0.9240313 -0.8890013 0.9235915 0.8692068 0.8979814 0.8380596 0.7497826
##         MPC05     MPC06     MPC07     MPC08     MPC09     MPC10    piPC03
## MW  0.7173574 0.6570767 0.6037373 0.5445841 0.5151546 0.5323731 0.7577689
## AMW 0.2641186 0.2711295 0.2373796 0.2250862 0.2145600 0.2037042 0.3456033
## Sv  0.6645452 0.5945680 0.5486836 0.4916518 0.4619019 0.4819810 0.6970106
## Se  0.6170782 0.5562933 0.5145483 0.4587114 0.4323297 0.4522881 0.6269973
## Sp  0.6500343 0.5808155 0.5354061 0.4790220 0.4496367 0.4692021 0.6795068
## Ss  0.6625429 0.6060147 0.5448364 0.4778613 0.4471539 0.4626730 0.7264303
##        piPC04    piPC05    piPC06    piPC07    piPC08    piPC09    piPC10
## MW  0.6329129 0.5295746 0.4310791 0.3656538 0.3035967 0.2656370 0.2528382
## AMW 0.3441709 0.3118301 0.2855542 0.2416010 0.2246867 0.2163616 0.1996793
## Sv  0.5685372 0.4676225 0.3610155 0.3083143 0.2529262 0.2154218 0.2099701
## Se  0.5017487 0.4100400 0.3253045 0.2748108 0.2173930 0.1817059 0.1738459
## Sp  0.5505672 0.4504560 0.3454747 0.2939916 0.2397818 0.2028882 0.1980077
## Ss  0.6016196 0.5004796 0.4090987 0.3369591 0.2667269 0.2275643 0.2100703
##           TPC      piID        PCR       PCD       CID        BID     D.Dr05
## MW  0.6821375 0.2897200 0.19143881 0.2814302 0.9772370 0.98198070 0.15739869
## AMW 0.1964801 0.1197637 0.21129616 0.1177656 0.1066467 0.09246015 0.32050720
## Sv  0.6441606 0.2766750 0.17003228 0.2688798 0.9711357 0.97636426 0.08262305
## Se  0.5969260 0.2301535 0.09829675 0.2226484 0.9446973 0.95677353 0.02540524
## Sp  0.6335528 0.2668059 0.15883288 0.2590821 0.9623074 0.96818570 0.07410402
## Ss  0.5963648 0.2566207 0.15732536 0.2494091 0.9197046 0.93308910 0.10943543
##         D.Dr06    D.Dr10    T.N..N.    T.N..O.      T.O..O.        W3D
## MW  0.82735022 0.2757054 0.34522619 0.55291274  0.460020944  0.8360760
## AMW 0.05511057 0.1381548 0.09345402 0.05167806 -0.004194227 -0.1736700
## Sv  0.84428626 0.2268145 0.28605988 0.48112654  0.417135945  0.9011730
## Se  0.79867683 0.2201655 0.31606239 0.55180342  0.479940731  0.9199331
## Sp  0.83292684 0.2277227 0.27377465 0.46645668  0.409288408  0.9042056
## Ss  0.77941475 0.2213764 0.30695740 0.60092914  0.523912936  0.7557473
##             J3D        H3D       AGDD        DDI       ADDD        G1        G2
## MW  -0.24522730  0.1037439  0.8818824  0.8930943  0.9202306 0.3768724 0.9829996
## AMW -0.44562560 -0.0310218 -0.1501206 -0.1868740 -0.1559420 0.0696526 0.2568025
## Sv  -0.16515193  0.1284895  0.9399523  0.9619152  0.9812691 0.3648644 0.9232752
## Se  -0.06262924  0.1052542  0.9527656  0.9794901  0.9906679 0.3459389 0.8902672
## Sp  -0.14960316  0.1249064  0.9413172  0.9632884  0.9811149 0.3613536 0.9191970
## Ss  -0.18586961  0.1164222  0.8021454  0.8077152  0.8329723 0.3575227 0.8985095
##            RGyr        SPAN       SPAM       SPH         ASP        FDI
## MW   0.86609274  0.78305013 -0.2888798 0.5594058  0.33531596 -0.3210791
## AMW -0.06022415 -0.04019145  0.2536302 0.3376086 -0.04345994  0.2810970
## Sv   0.90022711  0.81121004 -0.3660219 0.5229232  0.35151672 -0.4050076
## Se   0.89908018  0.80809431 -0.3832156 0.4504548  0.35118649 -0.4291695
## Sp   0.89987234  0.81115576 -0.3688108 0.5112859  0.34853543 -0.4124813
## Ss   0.80410390  0.73304933 -0.2114380 0.5723609  0.35195212 -0.2827155
##           PJI3        L.Bw       SEig        DISPm      QXXm      QYYm
## MW  0.45573103  0.31585091  0.9113623  0.068894287 0.6722747 0.6837851
## AMW 0.04920818 -0.07795353 -0.1923731  0.357284891 0.1733190 0.1702246
## Sv  0.44023337  0.34245084  0.9825184 -0.033505353 0.6236656 0.6363601
## Se  0.44293151  0.34993731  0.9964845 -0.075381251 0.6180307 0.6311553
## Sp  0.43471770  0.34207236  0.9830118 -0.019621383 0.6206760 0.6333310
## Ss  0.45151929  0.31278098  0.8262837  0.007996002 0.6013522 0.6124801
##           QZZm      DISPv        QXXv        QYYv        QZZv      DISPe
## MW  0.81227443 -0.1017030  0.65413063  0.68581056  0.78239116 0.15069577
## AMW 0.02616931  0.1353604 -0.06168259 -0.06626239 -0.08682999 0.32368492
## Sv  0.81768974 -0.1347532  0.67920738  0.71295662  0.82765358 0.04654226
## Se  0.81016576 -0.1710991  0.69113684  0.72580095  0.82670370 0.04423588
## Sp  0.81827405 -0.1161762  0.67786074  0.71146208  0.83030540 0.03546723
## Ss  0.75306785 -0.1952218  0.56243500  0.59405386  0.70762012 0.30427145
##           QXXe       QYYe       QZZe       DISPp        QXXp        QYYp
## MW   0.6759131  0.7184111  0.7859986 -0.07920624  0.65625211  0.69193631
## AMW -0.1136526 -0.1174315 -0.1153071  0.21547044 -0.07012325 -0.07504988
## Sv   0.7058970  0.7511621  0.8333300 -0.13148715  0.68351602  0.72148548
## Se   0.7361886  0.7817701  0.8447520 -0.18013171  0.69651349  0.73548868
## Sp   0.7030538  0.7481800  0.8360951 -0.11189530  0.68316480  0.72093113
## Ss   0.6061163  0.6482613  0.7176407 -0.18944213  0.55729459  0.59328016
##            QZZp   G.N..N.    G.N..O.    G.N..S.   G.N..F.     G.N..Cl.
## MW   0.77910978 0.3378901 0.54925352 0.12040609 0.2133348 0.2032267431
## AMW -0.09040239 0.0820371 0.04665133 0.22803052 0.2021923 0.5153135748
## Sv   0.82541415 0.2820395 0.47881599 0.05342510 0.1503352 0.0592438126
## Se   0.82497684 0.3141256 0.55050020 0.01998360 0.1268683 0.0007179174
## Sp   0.82875111 0.2699670 0.46428646 0.07163484 0.1364674 0.0589758231
## Ss   0.69971617 0.3009196 0.59638803 0.01699589 0.3548937 0.1153216202
##          G.O..O.   G.O..Cl.
## MW   0.462790118 0.23218369
## AMW -0.005732334 0.36383665
## Sv   0.420550477 0.11628036
## Se   0.483289557 0.07854104
## Sp   0.412811131 0.11646273
## Ss   0.524748224 0.18548243
highlyCorDescr<-findCorrelation(descrCor, cutoff = 0.75)
filteredDescr<-filteredDescr[,-highlyCorDescr]
glimpse(filteredDescr)
## Rows: 528
## Columns: 50
## $ AMW      <dbl> 7.15, 7.26, 6.94, 7.11, 7.26, 7.23, 7.27, 7.44, 7.60, 6.99, 7…
## $ Mp       <dbl> 0.67, 0.68, 0.68, 0.69, 0.68, 0.69, 0.69, 0.69, 0.69, 0.66, 0…
## $ Ms       <dbl> 1.92, 2.06, 1.85, 1.87, 2.07, 1.79, 1.77, 1.95, 2.13, 1.98, 2…
## $ nDB      <dbl> 0, 1, 0, 0, 1, 0, 0, 1, 2, 0, 0, 3, 1, 2, 1, 3, 0, 0, 0, 1, 0…
## $ nAB      <dbl> 12, 13, 12, 12, 13, 12, 12, 12, 12, 12, 12, 12, 19, 20, 13, 1…
## $ nS       <dbl> 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2…
## $ nF       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0, 0, 0, 0, 3, 0, 0…
## $ nCL      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0…
## $ nR05     <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ nR06     <dbl> 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4…
## $ nR07     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ nR09     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ nR10     <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2…
## $ nBnz     <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2…
## $ HNar     <dbl> 1.941, 1.890, 1.938, 1.935, 1.840, 2.211, 2.055, 2.000, 1.940…
## $ Xt       <dbl> 0.000, 0.000, 0.000, 0.001, 0.000, 0.000, 0.000, 0.000, 0.000…
## $ SPI      <dbl> 68.906, 173.971, 66.940, 58.129, 360.744, 4.583, 28.107, 74.1…
## $ Jhetm    <dbl> 2.266, 2.309, 2.233, 2.374, 2.449, 1.899, 1.876, 1.928, 1.986…
## $ MAXDN    <dbl> 0.753, 1.551, 0.718, 0.834, 1.558, 0.422, 0.609, 2.180, 4.142…
## $ MAXDP    <dbl> 1.915, 4.755, 0.481, 0.450, 4.780, 0.590, 0.563, 5.011, 5.087…
## $ TIE      <dbl> 86.993, 130.429, 82.640, 76.022, 140.016, 68.478, 98.214, 102…
## $ X5v      <dbl> 2.929, 3.026, 2.849, 2.788, 3.042, 3.056, 4.388, 4.498, 4.608…
## $ BLI      <dbl> 1.047, 1.046, 1.076, 1.061, 1.038, 1.074, 1.203, 1.213, 1.223…
## $ PW2      <dbl> 0.570, 0.580, 0.575, 0.583, 0.592, 0.571, 0.576, 0.584, 0.596…
## $ PW3      <dbl> 0.326, 0.324, 0.321, 0.344, 0.343, 0.342, 0.349, 0.347, 0.342…
## $ PW4      <dbl> 0.182, 0.175, 0.193, 0.183, 0.171, 0.200, 0.192, 0.185, 0.177…
## $ PJI2     <dbl> 1.000, 1.000, 0.800, 1.000, 0.800, 0.800, 0.833, 0.833, 0.833…
## $ BAC      <dbl> 17, 24, 14, 13, 32, 0, 6, 11, 18, 11, 6, 30, 27, 18, 15, 61, …
## $ Lop      <dbl> 1.517, 1.532, 1.447, 1.275, 1.375, 0.000, 0.663, 0.721, 0.760…
## $ IVDE     <dbl> 1.891, 1.897, 1.746, 1.681, 1.850, 1.494, 1.811, 1.826, 1.964…
## $ BIC2     <dbl> 0.640, 0.664, 0.604, 0.603, 0.661, 0.612, 0.686, 0.690, 0.688…
## $ BIC5     <dbl> 0.845, 0.843, 0.800, 0.791, 0.840, 0.794, 0.889, 0.888, 0.881…
## $ VEA1     <dbl> 3.933, 4.013, 3.895, 3.902, 4.108, 3.916, 4.114, 4.187, 4.302…
## $ VRA1     <dbl> 252.579, 263.951, 196.831, 140.292, 176.537, 210.078, 298.269…
## $ piPC10   <dbl> 1367.438, 1900.828, 1107.281, 1092.656, 1931.766, 1172.719, 1…
## $ PCR      <dbl> 11.641, 14.717, 11.566, 11.625, 14.717, 11.474, 11.383, 12.30…
## $ T.O..O.  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 28, 0, 14, 15, 64, 0, 0, 0,…
## $ H3D      <dbl> 141.324, 147.498, 1282.095, 1249.000, 159.723, 132.489, 185.7…
## $ G1       <dbl> 66.275, 69.115, 160.356, 155.898, 71.477, 63.272, 84.918, 89.…
## $ SPAM     <dbl> 0.349, 0.324, 0.339, 0.338, 0.328, 0.340, 0.316, 0.320, 0.317…
## $ SPH      <dbl> 0.935, 0.932, 0.919, 0.921, 0.930, 0.917, 0.916, 0.924, 0.925…
## $ FDI      <dbl> 0.720, 0.708, 0.699, 0.705, 0.710, 0.726, 0.696, 0.706, 0.699…
## $ PJI3     <dbl> 0.761, 0.935, 0.946, 0.798, 0.821, 0.850, 0.693, 0.722, 0.732…
## $ L.Bw     <dbl> 2.0, 1.9, 2.6, 1.8, 1.5, 2.4, 1.4, 1.7, 1.8, 4.5, 3.6, 4.3, 5…
## $ DISPm    <dbl> 8.913, 10.523, 10.112, 9.598, 8.027, 10.126, 10.187, 10.832, …
## $ QXXm     <dbl> 66.579, 76.067, 45.509, 49.090, 79.023, 50.395, 101.945, 104.…
## $ DISPe    <dbl> 0.102, 0.142, 0.052, 0.049, 0.150, 0.067, 0.067, 0.090, 0.208…
## $ G.N..N.  <dbl> 3.53, 3.53, 3.52, 2.70, 2.43, 2.69, 3.03, 3.51, 3.53, 3.05, 1…
## $ G.N..O.  <dbl> 9.60, 11.53, 0.00, 0.00, 10.40, 0.00, 0.00, 9.28, 19.31, 18.9…
## $ G.O..Cl. <dbl> 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0…
#3) 시각화
data(iris)
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
featurePlot(x = iris[, 1:4],
            y = iris$Species,
            plot = "density",
            scale = list(x = list(relation="free"),
                         y = list(relation="free")),
            adjust=1.5, #Adjust the scale
            pch = "|", #Pch for plotting
            layout = c(4,1),
            auto.key = list(columns = 3))

library(mlbench)
data(BostonHousing)
glimpse(BostonHousing)
## Rows: 506
## Columns: 14
## $ crim    <dbl> 0.00632, 0.02731, 0.02729, 0.03237, 0.06905, 0.02985, 0.08829,…
## $ zn      <dbl> 18.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.5, 12.5, 12.5, 12.5, 12.5, 1…
## $ indus   <dbl> 2.31, 7.07, 7.07, 2.18, 2.18, 2.18, 7.87, 7.87, 7.87, 7.87, 7.…
## $ chas    <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nox     <dbl> 0.538, 0.469, 0.469, 0.458, 0.458, 0.458, 0.524, 0.524, 0.524,…
## $ rm      <dbl> 6.575, 6.421, 7.185, 6.998, 7.147, 6.430, 6.012, 6.172, 5.631,…
## $ age     <dbl> 65.2, 78.9, 61.1, 45.8, 54.2, 58.7, 66.6, 96.1, 100.0, 85.9, 9…
## $ dis     <dbl> 4.0900, 4.9671, 4.9671, 6.0622, 6.0622, 6.0622, 5.5605, 5.9505…
## $ rad     <dbl> 1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,…
## $ tax     <dbl> 296, 242, 242, 222, 222, 222, 311, 311, 311, 311, 311, 311, 31…
## $ ptratio <dbl> 15.3, 17.8, 17.8, 18.7, 18.7, 18.7, 15.2, 15.2, 15.2, 15.2, 15…
## $ b       <dbl> 396.90, 396.90, 392.83, 394.63, 396.90, 394.12, 395.60, 396.90…
## $ lstat   <dbl> 4.98, 9.14, 4.03, 2.94, 5.33, 5.21, 12.43, 19.15, 29.93, 17.10…
## $ medv    <dbl> 24.0, 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15…
x = BostonHousing[,c("age", "lstat", "tax")]
y = BostonHousing$medv
featurePlot(x, 
            y = BostonHousing$medv,
            plot = "scatter",
            layout = c(3, 1))

#(4) 변수 선택(Feature Selection)
library(mlbench)
library(caret)
library(dplyr)
data(PimaIndiansDiabetes)
glimpse(PimaIndiansDiabetes)
## Rows: 768
## Columns: 9
## $ pregnant <dbl> 6, 1, 8, 1, 0, 5, 3, 10, 2, 8, 4, 10, 10, 1, 5, 7, 0, 7, 1, 1…
## $ glucose  <dbl> 148, 85, 183, 89, 137, 116, 78, 115, 197, 125, 110, 168, 139,…
## $ pressure <dbl> 72, 66, 64, 66, 40, 74, 50, 0, 70, 96, 92, 74, 80, 60, 72, 0,…
## $ triceps  <dbl> 35, 29, 0, 23, 35, 0, 32, 0, 45, 0, 0, 0, 0, 23, 19, 0, 47, 0…
## $ insulin  <dbl> 0, 0, 0, 94, 168, 0, 88, 0, 543, 0, 0, 0, 0, 846, 175, 0, 230…
## $ mass     <dbl> 33.6, 26.6, 23.3, 28.1, 43.1, 25.6, 31.0, 35.3, 30.5, 0.0, 37…
## $ pedigree <dbl> 0.627, 0.351, 0.672, 0.167, 2.288, 0.201, 0.248, 0.134, 0.158…
## $ age      <dbl> 50, 31, 32, 21, 33, 30, 26, 29, 53, 54, 30, 34, 57, 59, 51, 3…
## $ diabetes <fct> pos, neg, pos, neg, pos, neg, pos, neg, pos, pos, neg, pos, n…
control <- trainControl(method="repeatedcv", number=10, repeats = 3)
model <- train(diabetes~., data=PimaIndiansDiabetes, method="rf", 
               preProcess=c("center", "scale"), trControl=control)
importance <- varImp(model, scale=FALSE)
print(importance)
## rf variable importance
## 
##          Overall
## glucose   105.69
## mass       60.70
## age        46.70
## pedigree   43.59
## pressure   29.97
## pregnant   23.63
## insulin    19.31
## triceps    18.46
plot(importance)

#변수 선택
library(mlbench)
library(caret)
data(PimaIndiansDiabetes)
control <- rfeControl(functions = rfFuncs, method = "cv", number=10)

results <- rfe(PimaIndiansDiabetes[,1:8], PimaIndiansDiabetes[,9],
              size=c(1:8), rfeControl = control)
print(results)
## 
## Recursive feature selection
## 
## Outer resampling method: Cross-Validated (10 fold) 
## 
## Resampling performance over subset size:
## 
##  Variables Accuracy  Kappa AccuracySD KappaSD Selected
##          1   0.7018 0.2863    0.04043 0.11849         
##          2   0.7293 0.3799    0.02896 0.06880         
##          3   0.7436 0.4180    0.04824 0.11104         
##          4   0.7540 0.4424    0.04652 0.10809         
##          5   0.7514 0.4405    0.04220 0.09352         
##          6   0.7501 0.4317    0.05075 0.11690         
##          7   0.7396 0.4140    0.04586 0.09701         
##          8   0.7631 0.4610    0.04719 0.10383        *
## 
## The top 5 variables (out of 8):
##    glucose, mass, age, pregnant, insulin
predictors(results)
## [1] "glucose"  "mass"     "age"      "pregnant" "insulin"  "pedigree" "triceps" 
## [8] "pressure"
#5) Training and Parameter Tuning the model
library(caret)
library(mlbench)
data(Sonar)
set.seed(998)
inTraining <- createDataPartition(y=Sonar$Class, p = .75, list = FALSE)
training <- Sonar[inTraining,]
testing <- Sonar[-inTraining,]

set.seed(825)
rffit <- train(Class ~ ., data = training, #target, predictor 변수
               method = "rf", #모델 학습 알고리즘
               preProc = c("center", "scale")) #중심화, 척도 수행

rffit
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.7869345  0.5637847
##   31    0.7772599  0.5465693
##   60    0.7626313  0.5153456
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
# (2) trainControl() 함수의 기능
fitControl <- trainControl(method="repeatedcv",
                           number=5,
                           ## Estimated class probabilities
                           classProbs = TRUE,
                           ## Evaluate performance using
                           ## the following function
                           summaryFunction = twoClassSummary)

fitControl
## $method
## [1] "repeatedcv"
## 
## $number
## [1] 5
## 
## $repeats
## [1] 1
## 
## $search
## [1] "grid"
## 
## $p
## [1] 0.75
## 
## $initialWindow
## NULL
## 
## $horizon
## [1] 1
## 
## $fixedWindow
## [1] TRUE
## 
## $skip
## [1] 0
## 
## $verboseIter
## [1] FALSE
## 
## $returnData
## [1] TRUE
## 
## $returnResamp
## [1] "final"
## 
## $savePredictions
## [1] FALSE
## 
## $classProbs
## [1] TRUE
## 
## $summaryFunction
## function (data, lev = NULL, model = NULL) 
## {
##     if (length(lev) > 2) {
##         stop(paste("Your outcome has", length(lev), "levels. The twoClassSummary() function isn't appropriate."))
##     }
##     requireNamespaceQuietStop("pROC")
##     if (!all(levels(data[, "pred"]) == lev)) {
##         stop("levels of observed and predicted data do not match")
##     }
##     rocObject <- try(pROC::roc(data$obs, data[, lev[1]], direction = ">", 
##         quiet = TRUE), silent = TRUE)
##     rocAUC <- if (inherits(rocObject, "try-error")) 
##         NA
##     else rocObject$auc
##     out <- c(rocAUC, sensitivity(data[, "pred"], data[, "obs"], 
##         lev[1]), specificity(data[, "pred"], data[, "obs"], lev[2]))
##     names(out) <- c("ROC", "Sens", "Spec")
##     out
## }
## <bytecode: 0x00000215129b4698>
## <environment: namespace:caret>
## 
## $selectionFunction
## [1] "best"
## 
## $preProcOptions
## $preProcOptions$thresh
## [1] 0.95
## 
## $preProcOptions$ICAcomp
## [1] 3
## 
## $preProcOptions$k
## [1] 5
## 
## $preProcOptions$freqCut
## [1] 19
## 
## $preProcOptions$uniqueCut
## [1] 10
## 
## $preProcOptions$cutoff
## [1] 0.9
## 
## 
## $sampling
## NULL
## 
## $index
## NULL
## 
## $indexOut
## NULL
## 
## $indexFinal
## NULL
## 
## $timingSamps
## [1] 0
## 
## $predictionBounds
## [1] FALSE FALSE
## 
## $seeds
## [1] NA
## 
## $adaptive
## $adaptive$min
## [1] 5
## 
## $adaptive$alpha
## [1] 0.05
## 
## $adaptive$method
## [1] "gls"
## 
## $adaptive$complete
## [1] TRUE
## 
## 
## $trim
## [1] FALSE
## 
## $allowParallel
## [1] TRUE
set.seed(825)
rmfit1 <- train(Class ~ ., data = training,
                method = "rf",
                trControl = fitControl,
                verbose = FALSE,
                ## Specify which metric to optimize
                metric = "ROC")

rmfit1
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 125, 125, 125, 127, 126 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9317665  0.9176471  0.7533333
##   31    0.9252381  0.8816176  0.7247619
##   60    0.9146569  0.8580882  0.7514286
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
# (3) Parameter Tuning
set.seed(825)
customGrid <- expand.grid(mtry = 1:10)
rffit2 <- train(Class ~ ., data = training, method = "rf",
                trContol = fitControl, tuneGrid = customGrid, metic = "ROC")

rffit2
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    1    0.7874250  0.5645447
##    2    0.7774658  0.5432509
##    3    0.7895389  0.5686985
##    4    0.7822599  0.5545972
##    5    0.7876411  0.5652034
##    6    0.7842371  0.5583549
##    7    0.7811081  0.5532847
##    8    0.7855180  0.5623813
##    9    0.7872588  0.5664177
##   10    0.7850315  0.5623536
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 3.
set.seed(825)
fitControl <- trainControl(method = "repeatedcv", number = 5,  
                           classProbs = TRUE,
                           summaryFunction = twoClassSummary,
                           search = "random")
rffit3 <- train(Class ~ ., data = training, method = "rf",
                metric = "ROC", trControl = fitControl)

rffit3
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 125, 125, 125, 127, 126 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##   26    0.9208824  0.8698529  0.7800000
##   42    0.9162290  0.8816176  0.7380952
##   56    0.9154237  0.8698529  0.7523810
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 26.
set.seed(825)
fitControl <- trainControl(method = "repeatedcv", number = 5,
                           classProbs = TRUE,
                           summaryFunction = twoClassSummary)
rffit4 <- train(Class ~ ., data = training, method = "rf",
                trContol = fitControl, tuneLength = 20)

rffit4
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.7855692  0.5596400
##    5    0.7809369  0.5520572
##    8    0.7813258  0.5532250
##   11    0.7836148  0.5580372
##   14    0.7845479  0.5606224
##   17    0.7858537  0.5644398
##   20    0.7790600  0.5496321
##   23    0.7843440  0.5613930
##   26    0.7821766  0.5567932
##   29    0.7817184  0.5546300
##   32    0.7791112  0.5501343
##   35    0.7711063  0.5335099
##   38    0.7797011  0.5512932
##   41    0.7691698  0.5304349
##   44    0.7691550  0.5306893
##   47    0.7737565  0.5388417
##   50    0.7697303  0.5304371
##   53    0.7612522  0.5131599
##   56    0.7592694  0.5089673
##   60    0.7702464  0.5309459
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 17.
# (5) 모형평가
library(dplyr)
predict(rmfit1, newdata = testing) %>% confusionMatrix(testing$Class)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  M  R
##          M 24  5
##          R  3 19
##                                           
##                Accuracy : 0.8431          
##                  95% CI : (0.7141, 0.9298)
##     No Information Rate : 0.5294          
##     P-Value [Acc > NIR] : 2.534e-06       
##                                           
##                   Kappa : 0.6837          
##                                           
##  Mcnemar's Test P-Value : 0.7237          
##                                           
##             Sensitivity : 0.8889          
##             Specificity : 0.7917          
##          Pos Pred Value : 0.8276          
##          Neg Pred Value : 0.8636          
##              Prevalence : 0.5294          
##          Detection Rate : 0.4706          
##    Detection Prevalence : 0.5686          
##       Balanced Accuracy : 0.8403          
##                                           
##        'Positive' Class : M               
## 
# (6) 실전 모형 구축
library(dplyr)
library(caret)

setwd("c:/data")
df<-read.csv("diagnosis.csv")
glimpse(df)
## Rows: 569
## Columns: 32
## $ id                      <int> 842302, 842517, 84300903, 84348301, 84358402, …
## $ diagnosis               <chr> "M", "M", "M", "M", "M", "M", "M", "M", "M", "…
## $ radius_mean             <dbl> 17.990, 20.570, 19.690, 11.420, 20.290, 12.450…
## $ texture_mean            <dbl> 10.38, 17.77, 21.25, 20.38, 14.34, 15.70, 19.9…
## $ perimeter_mean          <dbl> 122.80, 132.90, 130.00, 77.58, 135.10, 82.57, …
## $ area_mean               <dbl> 1001.0, 1326.0, 1203.0, 386.1, 1297.0, 477.1, …
## $ smoothness_mean         <dbl> 0.11840, 0.08474, 0.10960, 0.14250, 0.10030, 0…
## $ compactness_mean        <dbl> 0.27760, 0.07864, 0.15990, 0.28390, 0.13280, 0…
## $ concavity_mean          <dbl> 0.30010, 0.08690, 0.19740, 0.24140, 0.19800, 0…
## $ concave.points_mean     <dbl> 0.14710, 0.07017, 0.12790, 0.10520, 0.10430, 0…
## $ symmetry_mean           <dbl> 0.2419, 0.1812, 0.2069, 0.2597, 0.1809, 0.2087…
## $ fractal_dimension_mean  <dbl> 0.07871, 0.05667, 0.05999, 0.09744, 0.05883, 0…
## $ radius_se               <dbl> 1.0950, 0.5435, 0.7456, 0.4956, 0.7572, 0.3345…
## $ texture_se              <dbl> 0.9053, 0.7339, 0.7869, 1.1560, 0.7813, 0.8902…
## $ perimeter_se            <dbl> 8.589, 3.398, 4.585, 3.445, 5.438, 2.217, 3.18…
## $ area_se                 <dbl> 153.40, 74.08, 94.03, 27.23, 94.44, 27.19, 53.…
## $ smoothness_se           <dbl> 0.006399, 0.005225, 0.006150, 0.009110, 0.0114…
## $ compactness_se          <dbl> 0.049040, 0.013080, 0.040060, 0.074580, 0.0246…
## $ concavity_se            <dbl> 0.05373, 0.01860, 0.03832, 0.05661, 0.05688, 0…
## $ concave.points_se       <dbl> 0.015870, 0.013400, 0.020580, 0.018670, 0.0188…
## $ symmetry_se             <dbl> 0.03003, 0.01389, 0.02250, 0.05963, 0.01756, 0…
## $ fractal_dimension_se    <dbl> 0.006193, 0.003532, 0.004571, 0.009208, 0.0051…
## $ radius_worst            <dbl> 25.38, 24.99, 23.57, 14.91, 22.54, 15.47, 22.8…
## $ texture_worst           <dbl> 17.33, 23.41, 25.53, 26.50, 16.67, 23.75, 27.6…
## $ perimeter_worst         <dbl> 184.60, 158.80, 152.50, 98.87, 152.20, 103.40,…
## $ area_worst              <dbl> 2019.0, 1956.0, 1709.0, 567.7, 1575.0, 741.6, …
## $ smoothness_worst        <dbl> 0.1622, 0.1238, 0.1444, 0.2098, 0.1374, 0.1791…
## $ compactness_worst       <dbl> 0.6656, 0.1866, 0.4245, 0.8663, 0.2050, 0.5249…
## $ concavity_worst         <dbl> 0.71190, 0.24160, 0.45040, 0.68690, 0.40000, 0…
## $ concave.points_worst    <dbl> 0.26540, 0.18600, 0.24300, 0.25750, 0.16250, 0…
## $ symmetry_worst          <dbl> 0.4601, 0.2750, 0.3613, 0.6638, 0.2364, 0.3985…
## $ fractal_dimension_worst <dbl> 0.11890, 0.08902, 0.08758, 0.17300, 0.07678, 0…
df <- df %>% 
  select(-id)
df$diagnosis <- as.factor(df$diagnosis) #문자 변수를 카테고리 변수로 변경
glimpse(df)
## Rows: 569
## Columns: 31
## $ diagnosis               <fct> M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M…
## $ radius_mean             <dbl> 17.990, 20.570, 19.690, 11.420, 20.290, 12.450…
## $ texture_mean            <dbl> 10.38, 17.77, 21.25, 20.38, 14.34, 15.70, 19.9…
## $ perimeter_mean          <dbl> 122.80, 132.90, 130.00, 77.58, 135.10, 82.57, …
## $ area_mean               <dbl> 1001.0, 1326.0, 1203.0, 386.1, 1297.0, 477.1, …
## $ smoothness_mean         <dbl> 0.11840, 0.08474, 0.10960, 0.14250, 0.10030, 0…
## $ compactness_mean        <dbl> 0.27760, 0.07864, 0.15990, 0.28390, 0.13280, 0…
## $ concavity_mean          <dbl> 0.30010, 0.08690, 0.19740, 0.24140, 0.19800, 0…
## $ concave.points_mean     <dbl> 0.14710, 0.07017, 0.12790, 0.10520, 0.10430, 0…
## $ symmetry_mean           <dbl> 0.2419, 0.1812, 0.2069, 0.2597, 0.1809, 0.2087…
## $ fractal_dimension_mean  <dbl> 0.07871, 0.05667, 0.05999, 0.09744, 0.05883, 0…
## $ radius_se               <dbl> 1.0950, 0.5435, 0.7456, 0.4956, 0.7572, 0.3345…
## $ texture_se              <dbl> 0.9053, 0.7339, 0.7869, 1.1560, 0.7813, 0.8902…
## $ perimeter_se            <dbl> 8.589, 3.398, 4.585, 3.445, 5.438, 2.217, 3.18…
## $ area_se                 <dbl> 153.40, 74.08, 94.03, 27.23, 94.44, 27.19, 53.…
## $ smoothness_se           <dbl> 0.006399, 0.005225, 0.006150, 0.009110, 0.0114…
## $ compactness_se          <dbl> 0.049040, 0.013080, 0.040060, 0.074580, 0.0246…
## $ concavity_se            <dbl> 0.05373, 0.01860, 0.03832, 0.05661, 0.05688, 0…
## $ concave.points_se       <dbl> 0.015870, 0.013400, 0.020580, 0.018670, 0.0188…
## $ symmetry_se             <dbl> 0.03003, 0.01389, 0.02250, 0.05963, 0.01756, 0…
## $ fractal_dimension_se    <dbl> 0.006193, 0.003532, 0.004571, 0.009208, 0.0051…
## $ radius_worst            <dbl> 25.38, 24.99, 23.57, 14.91, 22.54, 15.47, 22.8…
## $ texture_worst           <dbl> 17.33, 23.41, 25.53, 26.50, 16.67, 23.75, 27.6…
## $ perimeter_worst         <dbl> 184.60, 158.80, 152.50, 98.87, 152.20, 103.40,…
## $ area_worst              <dbl> 2019.0, 1956.0, 1709.0, 567.7, 1575.0, 741.6, …
## $ smoothness_worst        <dbl> 0.1622, 0.1238, 0.1444, 0.2098, 0.1374, 0.1791…
## $ compactness_worst       <dbl> 0.6656, 0.1866, 0.4245, 0.8663, 0.2050, 0.5249…
## $ concavity_worst         <dbl> 0.71190, 0.24160, 0.45040, 0.68690, 0.40000, 0…
## $ concave.points_worst    <dbl> 0.26540, 0.18600, 0.24300, 0.25750, 0.16250, 0…
## $ symmetry_worst          <dbl> 0.4601, 0.2750, 0.3613, 0.6638, 0.2364, 0.3985…
## $ fractal_dimension_worst <dbl> 0.11890, 0.08902, 0.08758, 0.17300, 0.07678, 0…
prop.table(table(df$diagnosis))
## 
##         B         M 
## 0.6274165 0.3725835
any(is.na(df)) #결측값이 존재를 파악한다. 결측존재하면 TRUE출력함
## [1] FALSE
colSums(is.na(df))
##               diagnosis             radius_mean            texture_mean 
##                       0                       0                       0 
##          perimeter_mean               area_mean         smoothness_mean 
##                       0                       0                       0 
##        compactness_mean          concavity_mean     concave.points_mean 
##                       0                       0                       0 
##           symmetry_mean  fractal_dimension_mean               radius_se 
##                       0                       0                       0 
##              texture_se            perimeter_se                 area_se 
##                       0                       0                       0 
##           smoothness_se          compactness_se            concavity_se 
##                       0                       0                       0 
##       concave.points_se             symmetry_se    fractal_dimension_se 
##                       0                       0                       0 
##            radius_worst           texture_worst         perimeter_worst 
##                       0                       0                       0 
##              area_worst        smoothness_worst       compactness_worst 
##                       0                       0                       0 
##         concavity_worst    concave.points_worst          symmetry_worst 
##                       0                       0                       0 
## fractal_dimension_worst 
##                       0
tt<-c(1,2,3,NA) #NA 무응답 값
is.na(tt) #결측값이 존재하면 TRUE로 출력함
## [1] FALSE FALSE FALSE  TRUE
data("airquality")
#결측확인방법
summary(airquality)
##      Ozone           Solar.R           Wind             Temp      
##  Min.   :  1.00   Min.   :  7.0   Min.   : 1.700   Min.   :56.00  
##  1st Qu.: 18.00   1st Qu.:115.8   1st Qu.: 7.400   1st Qu.:72.00  
##  Median : 31.50   Median :205.0   Median : 9.700   Median :79.00  
##  Mean   : 42.13   Mean   :185.9   Mean   : 9.958   Mean   :77.88  
##  3rd Qu.: 63.25   3rd Qu.:258.8   3rd Qu.:11.500   3rd Qu.:85.00  
##  Max.   :168.00   Max.   :334.0   Max.   :20.700   Max.   :97.00  
##  NA's   :37       NA's   :7                                       
##      Month            Day      
##  Min.   :5.000   Min.   : 1.0  
##  1st Qu.:6.000   1st Qu.: 8.0  
##  Median :7.000   Median :16.0  
##  Mean   :6.993   Mean   :15.8  
##  3rd Qu.:8.000   3rd Qu.:23.0  
##  Max.   :9.000   Max.   :31.0  
## 
colSums(is.na(airquality))
##   Ozone Solar.R    Wind    Temp   Month     Day 
##      37       7       0       0       0       0
glimpse(airquality)
## Rows: 153
## Columns: 6
## $ Ozone   <int> 41, 36, 12, 18, NA, 28, 23, 19, 8, NA, 7, 16, 11, 14, 18, 14, …
## $ Solar.R <int> 190, 118, 149, 313, NA, NA, 299, 99, 19, 194, NA, 256, 290, 27…
## $ Wind    <dbl> 7.4, 8.0, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1, 8.6, 6.9, 9…
## $ Temp    <int> 67, 72, 74, 62, 56, 66, 65, 59, 61, 69, 74, 69, 66, 68, 58, 64…
## $ Month   <int> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
## $ Day     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,…
View(df)

set.seed(3) #재현성 확인
inTraining<-createDataPartition(y=df$diagnosis, p=.75, list=FALSE)
training<-df[inTraining,]
testing<-df[-inTraining,]

my_trainControl<- trainControl(method="repeatedcv",
                               number=5,
                               classProbs = TRUE,
                               summaryFunction = twoClassSummary)

rffit<-train(diagnosis ~., data = training,
             method = "rf",
             trControl = my_trainControl,
             verbose = FALSE,
             metric = "ROC")

rffit
## Random Forest 
## 
## 427 samples
##  30 predictor
##   2 classes: 'B', 'M' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 342, 341, 343, 341, 341 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9941180  0.9665968  0.9370968
##   16    0.9925519  0.9590496  0.9433468
##   30    0.9916058  0.9552760  0.9433468
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(rffit, newdata = testing) #모델의 예측값
##   [1] M B M M M M M B B M M M M B M M B B B M B M B B B M B B M M M M B B B B B
##  [38] B B B B B M M M B M B B B M M M B M B M M B M M B B M B B B B B B B B B B
##  [75] B B B B M B M M B B M M B B B B M B B B B B B B M B B B B B B B B B B B B
## [112] B M B M B B M B B B B B M B M M B B M B B B B M B B B B B B M
## Levels: B M
predict(rffit, newdata = testing, type='prob') #모델의 확률값
##         B     M
## 8   0.152 0.848
## 20  0.940 0.060
## 24  0.028 0.972
## 27  0.052 0.948
## 32  0.154 0.846
## 34  0.002 0.998
## 36  0.008 0.992
## 39  0.670 0.330
## 42  0.786 0.214
## 43  0.012 0.988
## 46  0.010 0.990
## 48  0.200 0.800
## 55  0.292 0.708
## 56  0.998 0.002
## 57  0.006 0.994
## 65  0.070 0.930
## 67  0.998 0.002
## 68  0.994 0.006
## 72  0.976 0.024
## 73  0.010 0.990
## 81  0.996 0.004
## 86  0.012 0.988
## 91  0.928 0.072
## 94  0.998 0.002
## 98  0.992 0.008
## 100 0.412 0.588
## 111 0.998 0.002
## 114 0.992 0.008
## 120 0.202 0.798
## 122 0.010 0.990
## 130 0.004 0.996
## 133 0.056 0.944
## 136 0.994 0.006
## 137 0.926 0.074
## 138 0.996 0.004
## 140 0.974 0.026
## 141 0.998 0.002
## 143 0.998 0.002
## 155 0.906 0.094
## 160 1.000 0.000
## 164 0.992 0.008
## 167 0.998 0.002
## 182 0.016 0.984
## 183 0.102 0.898
## 187 0.206 0.794
## 189 0.994 0.006
## 191 0.188 0.812
## 192 0.796 0.204
## 193 0.970 0.030
## 196 0.994 0.006
## 200 0.106 0.894
## 208 0.312 0.688
## 214 0.174 0.826
## 217 0.972 0.028
## 219 0.002 0.998
## 235 1.000 0.000
## 237 0.000 1.000
## 245 0.004 0.996
## 250 1.000 0.000
## 253 0.014 0.986
## 255 0.006 0.994
## 271 1.000 0.000
## 272 0.996 0.004
## 273 0.002 0.998
## 286 0.996 0.004
## 289 0.918 0.082
## 293 0.990 0.010
## 295 0.998 0.002
## 302 0.994 0.006
## 304 1.000 0.000
## 305 1.000 0.000
## 307 1.000 0.000
## 317 0.998 0.002
## 325 1.000 0.000
## 328 1.000 0.000
## 332 0.952 0.048
## 335 1.000 0.000
## 341 0.554 0.446
## 344 0.000 1.000
## 345 0.998 0.002
## 352 0.034 0.966
## 353 0.002 0.998
## 355 0.984 0.016
## 361 0.990 0.010
## 367 0.006 0.994
## 374 0.014 0.986
## 375 0.984 0.016
## 376 0.576 0.424
## 377 0.844 0.156
## 379 0.970 0.030
## 380 0.468 0.532
## 384 0.952 0.048
## 385 0.976 0.024
## 386 0.696 0.304
## 387 0.998 0.002
## 389 0.982 0.018
## 391 1.000 0.000
## 399 0.996 0.004
## 401 0.010 0.990
## 402 0.998 0.002
## 403 0.996 0.004
## 404 0.960 0.040
## 405 1.000 0.000
## 408 0.954 0.046
## 414 0.610 0.390
## 415 0.660 0.340
## 416 0.992 0.008
## 419 0.994 0.006
## 421 0.998 0.002
## 423 0.988 0.012
## 426 0.998 0.002
## 428 1.000 0.000
## 431 0.110 0.890
## 432 0.992 0.008
## 434 0.002 0.998
## 435 0.976 0.024
## 438 0.980 0.020
## 447 0.002 0.998
## 455 0.998 0.002
## 456 0.976 0.024
## 473 0.744 0.256
## 476 0.978 0.022
## 479 1.000 0.000
## 480 0.036 0.964
## 482 0.936 0.064
## 485 0.378 0.622
## 490 0.396 0.604
## 496 0.888 0.112
## 497 0.834 0.166
## 499 0.010 0.990
## 501 0.580 0.420
## 506 0.876 0.124
## 526 0.990 0.010
## 527 0.794 0.206
## 534 0.006 0.994
## 535 0.992 0.008
## 537 0.592 0.408
## 540 0.956 0.044
## 542 0.516 0.484
## 554 1.000 0.000
## 555 0.990 0.010
## 563 0.040 0.960
df<-predict(rffit, newdata=testing)
confusionMatrix(testing$diagnosis, df)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  B  M
##          B 88  1
##          M  6 47
##                                         
##                Accuracy : 0.9507        
##                  95% CI : (0.9011, 0.98)
##     No Information Rate : 0.662         
##     P-Value [Acc > NIR] : <2e-16        
##                                         
##                   Kappa : 0.8926        
##                                         
##  Mcnemar's Test P-Value : 0.1306        
##                                         
##             Sensitivity : 0.9362        
##             Specificity : 0.9792        
##          Pos Pred Value : 0.9888        
##          Neg Pred Value : 0.8868        
##              Prevalence : 0.6620        
##          Detection Rate : 0.6197        
##    Detection Prevalence : 0.6268        
##       Balanced Accuracy : 0.9577        
##                                         
##        'Positive' Class : B             
## 

```