#(6) 실전 모형 구축
rm(list=ls()) # 객체이름 초기화화
ls() # 확인
## character(0)
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
getwd()
## [1] "C:/Users/이재영/Desktop/대학원/머시닝"
setwd("c:/data") # 작업환경 모든 데이터 작업은 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)

glimpse(df)
## Rows: 569
## Columns: 31
## $ 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$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…
# 결측값(무응답) 확인하기
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
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
set.seed(3) # 재현성
inTraining<- createDataPartition(y=df$diagnosis,p=.70,list=FALSE)
training<- df[inTraining,]
testing<- df[-inTraining,]

dim(df) # 행의수, 열의수 출력
## [1] 569  31
dim(training)
## [1] 399  31
dim(testing)
## [1] 170  31
my_trainControl<- trainControl(method="repeatedcv",
                               number=5,
                               classProbs=TRUE,
                               summaryFunction=twoClassSummary)

rffit<- train(diagnosis~ ., data=training,
              method="rf",
              trControl=my_trainControl,
              verbose=FALSE,
              ## Specify which metric to optimize
              metric="ROC")
rffit
## Random Forest 
## 
## 399 samples
##  30 predictor
##   2 classes: 'B', 'M' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 319, 320, 319, 319, 319 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens   Spec     
##    2    0.9918000  0.968  0.9131034
##   16    0.9919287  0.956  0.9195402
##   30    0.9906437  0.956  0.9128736
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 16.
predict(rffit,newdata=testing) # 모델의 예측값
##   [1] M M B M M M M M M M B B M M M M B M B B M B M B M B B B M B B M M M M M B
##  [38] B B B B B B B B B B M B M M B M B B B M M B M M B M B B B B M M B B M M B
##  [75] B B B B B B M B B B B B M B M B B B B B M M B B M B B B M M M B M B B B B
## [112] B B M B M M B B B B B B M B M B B B B B B M M B M B M B B B B B B B B M M
## [149] M B B M B B B B M B B B M B B B B B B B M M
## Levels: B M
predict(rffit,newdata=testing, type="prob") # 모델의 확률값
##         B     M
## 8   0.016 0.984
## 9   0.026 0.974
## 20  0.994 0.006
## 27  0.036 0.964
## 28  0.000 1.000
## 32  0.034 0.966
## 34  0.000 1.000
## 35  0.000 1.000
## 36  0.000 1.000
## 37  0.124 0.876
## 39  0.960 0.040
## 42  0.602 0.398
## 43  0.002 0.998
## 46  0.000 1.000
## 48  0.068 0.932
## 55  0.152 0.848
## 56  1.000 0.000
## 57  0.000 1.000
## 67  1.000 0.000
## 68  1.000 0.000
## 71  0.000 1.000
## 72  0.994 0.006
## 73  0.000 1.000
## 81  1.000 0.000
## 86  0.000 1.000
## 91  0.842 0.158
## 94  1.000 0.000
## 98  1.000 0.000
## 100 0.204 0.796
## 111 1.000 0.000
## 114 1.000 0.000
## 122 0.000 1.000
## 123 0.058 0.942
## 130 0.000 1.000
## 133 0.006 0.994
## 134 0.434 0.566
## 136 1.000 0.000
## 137 0.996 0.004
## 138 1.000 0.000
## 140 0.998 0.002
## 141 1.000 0.000
## 143 1.000 0.000
## 155 0.964 0.036
## 160 1.000 0.000
## 161 0.994 0.006
## 164 1.000 0.000
## 167 1.000 0.000
## 169 0.002 0.998
## 171 1.000 0.000
## 182 0.000 1.000
## 183 0.072 0.928
## 189 1.000 0.000
## 191 0.090 0.910
## 192 0.986 0.014
## 193 0.930 0.070
## 196 1.000 0.000
## 198 0.330 0.670
## 200 0.090 0.910
## 201 1.000 0.000
## 208 0.298 0.702
## 214 0.232 0.768
## 217 1.000 0.000
## 219 0.000 1.000
## 223 1.000 0.000
## 235 1.000 0.000
## 242 0.912 0.088
## 250 1.000 0.000
## 255 0.000 1.000
## 266 0.000 1.000
## 271 0.994 0.006
## 272 1.000 0.000
## 275 0.170 0.830
## 284 0.006 0.994
## 286 1.000 0.000
## 288 1.000 0.000
## 289 0.978 0.022
## 293 1.000 0.000
## 295 1.000 0.000
## 298 0.986 0.014
## 302 1.000 0.000
## 303 0.000 1.000
## 304 1.000 0.000
## 305 1.000 0.000
## 306 1.000 0.000
## 307 1.000 0.000
## 317 0.990 0.010
## 318 0.000 1.000
## 321 1.000 0.000
## 324 0.000 1.000
## 325 1.000 0.000
## 328 1.000 0.000
## 332 0.992 0.008
## 334 1.000 0.000
## 335 1.000 0.000
## 336 0.000 1.000
## 338 0.000 1.000
## 341 0.548 0.452
## 345 1.000 0.000
## 354 0.002 0.998
## 355 1.000 0.000
## 360 1.000 0.000
## 361 0.994 0.006
## 366 0.000 1.000
## 367 0.000 1.000
## 370 0.000 1.000
## 375 1.000 0.000
## 376 0.498 0.502
## 377 0.812 0.188
## 379 1.000 0.000
## 384 0.996 0.004
## 385 1.000 0.000
## 387 1.000 0.000
## 389 1.000 0.000
## 390 0.000 1.000
## 391 1.000 0.000
## 393 0.000 1.000
## 394 0.000 1.000
## 399 1.000 0.000
## 402 1.000 0.000
## 403 1.000 0.000
## 404 0.904 0.096
## 405 1.000 0.000
## 408 0.988 0.012
## 414 0.484 0.516
## 416 1.000 0.000
## 418 0.000 1.000
## 419 1.000 0.000
## 421 1.000 0.000
## 423 1.000 0.000
## 426 1.000 0.000
## 428 1.000 0.000
## 432 1.000 0.000
## 433 0.000 1.000
## 434 0.000 1.000
## 435 0.986 0.014
## 436 0.070 0.930
## 438 0.996 0.004
## 442 0.020 0.980
## 454 0.916 0.084
## 455 1.000 0.000
## 456 0.974 0.026
## 465 0.998 0.002
## 473 0.706 0.294
## 476 0.998 0.002
## 479 1.000 0.000
## 482 0.930 0.070
## 485 0.362 0.638
## 488 0.000 1.000
## 490 0.406 0.594
## 496 0.820 0.180
## 497 0.876 0.124
## 500 0.000 1.000
## 501 0.642 0.358
## 503 1.000 0.000
## 506 0.842 0.158
## 511 1.000 0.000
## 515 0.376 0.624
## 521 0.888 0.112
## 526 1.000 0.000
## 527 0.838 0.162
## 534 0.002 0.998
## 535 1.000 0.000
## 540 0.996 0.004
## 542 0.502 0.498
## 547 1.000 0.000
## 548 1.000 0.000
## 554 1.000 0.000
## 555 1.000 0.000
## 564 0.000 1.000
## 567 0.066 0.934
df<-predict(rffit,newdata=testing)

confusionMatrix(testing$diagnosis,df)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   B   M
##          B 103   4
##          M   4  59
##                                           
##                Accuracy : 0.9529          
##                  95% CI : (0.9094, 0.9795)
##     No Information Rate : 0.6294          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.8991          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9626          
##             Specificity : 0.9365          
##          Pos Pred Value : 0.9626          
##          Neg Pred Value : 0.9365          
##              Prevalence : 0.6294          
##          Detection Rate : 0.6059          
##    Detection Prevalence : 0.6294          
##       Balanced Accuracy : 0.9496          
##                                           
##        'Positive' Class : B               
##