#(6) 실전 모형 구축
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
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
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) #NA's -> 결측값
##      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
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,
                                ##EStimate class probabilities
                                classProbs = TRUE,
                                ##Evlauate performance using
                                ##the following function
                                summaryFunction = twoClassSummary)

rffit<-train(diagnosis~., data=training,
                 method="rf",
                 trControl=my_trainControl,
                 verbose=FALSE,
                 ##Specify wich metric to optimize
                 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             
##