getwd()
## [1] "C:/data"
setwd("C:/data")
getwd()
## [1] "C:/data"
ls()
## character(0)
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
library(lubridate)
## 
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## 다음의 패키지를 부착합니다: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
df <- read.csv("Data1.csv")
glimpse(df)
## Rows: 1,925
## Columns: 27
## $ X         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Q1        <int> 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ Q2        <int> 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 2, 2, …
## $ Q3        <int> 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, 3, 2, 3, …
## $ Q4        <int> 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 2, 4, …
## $ Q5        <int> 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 3, 1, 2, …
## $ Q6        <int> 2, 3, 4, 4, 4, 4, 4, 4, 1, 2, 2, 2, 4, 4, 3, 5, 2, 2, 1, 4, …
## $ Q7        <int> 2, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 5, 4, 4, 5, 4, 3, 4, 4, …
## $ Q8        <int> 4, 4, 4, 4, 4, 4, 5, 5, 2, 2, 4, 4, 4, 4, 3, 5, 4, 2, 4, 4, …
## $ Q9        <int> 4, 4, 4, 4, 2, 4, 5, 5, 3, 4, 4, 4, 2, 2, 4, 5, 2, 4, 2, 4, …
## $ Q10       <int> 4, 4, 2, 4, 4, 4, 5, 5, 2, 4, 2, 4, 4, 4, 3, 4, 4, 3, 2, 3, …
## $ Q11       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 3, 4, 4, 4, 4, 5, 4, 3, 3, …
## $ Q12       <int> 4, 4, 4, 4, 4, 4, 5, 5, 3, 4, 4, 3, 4, 3, 3, 4, 5, 4, 4, 2, …
## $ Q13       <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 2, 4, 4, 4, 5, 4, 4, 4, …
## $ Q14       <int> 4, 4, 4, 4, 4, 4, 5, 5, 5, 4, 4, 4, 3, 4, 5, 4, 5, 4, 4, 4, …
## $ Q15       <int> 4, 4, 3, 4, 4, 4, 4, 2, 3, 4, 4, 3, 1, 4, 4, 4, 5, 4, 4, 4, …
## $ Q16       <int> 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 4, 4, …
## $ Q17       <int> 4, 3, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 3, 2, 4, 5, 4, 4, 3, 4, …
## $ Q18       <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, …
## $ Q19       <int> 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 1, 4, 4, 4, 5, 4, 2, 3, …
## $ Q20       <int> 4, 1, 3, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 2, 4, 5, 5, 4, 2, 4, …
## $ Gender    <int> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, …
## $ EDU       <int> 1, 1, 2, 1, 2, 1, 1, 1, 4, 3, 2, 1, 1, 3, 3, 2, 1, 1, 1, 4, …
## $ BF        <dbl> 3.4, 4.0, 3.6, 4.2, 4.0, 4.0, 3.6, 3.6, 3.6, 3.2, 4.0, 3.2, …
## $ BM        <dbl> 3.2, 3.4, 3.6, 4.0, 3.6, 4.0, 4.6, 4.6, 2.2, 3.2, 3.2, 3.6, …
## $ Happiness <dbl> 4.0, 4.0, 3.8, 4.0, 4.0, 4.0, 4.8, 4.4, 3.8, 4.0, 4.0, 3.4, …
## $ Peace     <dbl> 4.0, 2.8, 3.8, 4.0, 4.0, 4.0, 3.8, 2.4, 4.0, 3.2, 4.0, 3.9, …
df$Gender <- as.factor(df$Gender) # int를 fct로 변경
df$Gender <- revalue(df$Gender, replace = c("0"="female", "1" = "male"))

# 데이터명$변수이름 --> 변수에 값 출력됨
# iris$Sepal.Length --> 벡터 값만 출력 (변수이름 포함되지 않음)

table(df$Gender)
## 
## female   male 
##   1136    789
# 결측치 확인
colSums(is.na(df))
##         X        Q1        Q2        Q3        Q4        Q5        Q6        Q7 
##         0         0         0         0         0         0         0         0 
##        Q8        Q9       Q10       Q11       Q12       Q13       Q14       Q15 
##         0         0         0         0         0         0         0         0 
##       Q16       Q17       Q18       Q19       Q20    Gender       EDU        BF 
##         0         0         0         0         0         0         0         0 
##        BM Happiness     Peace 
##         0         0         0
# dara split --> 모형 평가를 받아야 함
# list = FALSE 유무의 차이 알아봄
idx <- createDataPartition(df$Gender, p = .75)
class(idx) # 데이터 유형 함수
## [1] "list"
idx <- createDataPartition(df$Gender, p = .75, list = FALSE)
class(idx) 
## [1] "matrix" "array"
# 대괄호[행, 열]
iris[,c(1,3,5)]
##     Sepal.Length Petal.Length    Species
## 1            5.1          1.4     setosa
## 2            4.9          1.4     setosa
## 3            4.7          1.3     setosa
## 4            4.6          1.5     setosa
## 5            5.0          1.4     setosa
## 6            5.4          1.7     setosa
## 7            4.6          1.4     setosa
## 8            5.0          1.5     setosa
## 9            4.4          1.4     setosa
## 10           4.9          1.5     setosa
## 11           5.4          1.5     setosa
## 12           4.8          1.6     setosa
## 13           4.8          1.4     setosa
## 14           4.3          1.1     setosa
## 15           5.8          1.2     setosa
## 16           5.7          1.5     setosa
## 17           5.4          1.3     setosa
## 18           5.1          1.4     setosa
## 19           5.7          1.7     setosa
## 20           5.1          1.5     setosa
## 21           5.4          1.7     setosa
## 22           5.1          1.5     setosa
## 23           4.6          1.0     setosa
## 24           5.1          1.7     setosa
## 25           4.8          1.9     setosa
## 26           5.0          1.6     setosa
## 27           5.0          1.6     setosa
## 28           5.2          1.5     setosa
## 29           5.2          1.4     setosa
## 30           4.7          1.6     setosa
## 31           4.8          1.6     setosa
## 32           5.4          1.5     setosa
## 33           5.2          1.5     setosa
## 34           5.5          1.4     setosa
## 35           4.9          1.5     setosa
## 36           5.0          1.2     setosa
## 37           5.5          1.3     setosa
## 38           4.9          1.4     setosa
## 39           4.4          1.3     setosa
## 40           5.1          1.5     setosa
## 41           5.0          1.3     setosa
## 42           4.5          1.3     setosa
## 43           4.4          1.3     setosa
## 44           5.0          1.6     setosa
## 45           5.1          1.9     setosa
## 46           4.8          1.4     setosa
## 47           5.1          1.6     setosa
## 48           4.6          1.4     setosa
## 49           5.3          1.5     setosa
## 50           5.0          1.4     setosa
## 51           7.0          4.7 versicolor
## 52           6.4          4.5 versicolor
## 53           6.9          4.9 versicolor
## 54           5.5          4.0 versicolor
## 55           6.5          4.6 versicolor
## 56           5.7          4.5 versicolor
## 57           6.3          4.7 versicolor
## 58           4.9          3.3 versicolor
## 59           6.6          4.6 versicolor
## 60           5.2          3.9 versicolor
## 61           5.0          3.5 versicolor
## 62           5.9          4.2 versicolor
## 63           6.0          4.0 versicolor
## 64           6.1          4.7 versicolor
## 65           5.6          3.6 versicolor
## 66           6.7          4.4 versicolor
## 67           5.6          4.5 versicolor
## 68           5.8          4.1 versicolor
## 69           6.2          4.5 versicolor
## 70           5.6          3.9 versicolor
## 71           5.9          4.8 versicolor
## 72           6.1          4.0 versicolor
## 73           6.3          4.9 versicolor
## 74           6.1          4.7 versicolor
## 75           6.4          4.3 versicolor
## 76           6.6          4.4 versicolor
## 77           6.8          4.8 versicolor
## 78           6.7          5.0 versicolor
## 79           6.0          4.5 versicolor
## 80           5.7          3.5 versicolor
## 81           5.5          3.8 versicolor
## 82           5.5          3.7 versicolor
## 83           5.8          3.9 versicolor
## 84           6.0          5.1 versicolor
## 85           5.4          4.5 versicolor
## 86           6.0          4.5 versicolor
## 87           6.7          4.7 versicolor
## 88           6.3          4.4 versicolor
## 89           5.6          4.1 versicolor
## 90           5.5          4.0 versicolor
## 91           5.5          4.4 versicolor
## 92           6.1          4.6 versicolor
## 93           5.8          4.0 versicolor
## 94           5.0          3.3 versicolor
## 95           5.6          4.2 versicolor
## 96           5.7          4.2 versicolor
## 97           5.7          4.2 versicolor
## 98           6.2          4.3 versicolor
## 99           5.1          3.0 versicolor
## 100          5.7          4.1 versicolor
## 101          6.3          6.0  virginica
## 102          5.8          5.1  virginica
## 103          7.1          5.9  virginica
## 104          6.3          5.6  virginica
## 105          6.5          5.8  virginica
## 106          7.6          6.6  virginica
## 107          4.9          4.5  virginica
## 108          7.3          6.3  virginica
## 109          6.7          5.8  virginica
## 110          7.2          6.1  virginica
## 111          6.5          5.1  virginica
## 112          6.4          5.3  virginica
## 113          6.8          5.5  virginica
## 114          5.7          5.0  virginica
## 115          5.8          5.1  virginica
## 116          6.4          5.3  virginica
## 117          6.5          5.5  virginica
## 118          7.7          6.7  virginica
## 119          7.7          6.9  virginica
## 120          6.0          5.0  virginica
## 121          6.9          5.7  virginica
## 122          5.6          4.9  virginica
## 123          7.7          6.7  virginica
## 124          6.3          4.9  virginica
## 125          6.7          5.7  virginica
## 126          7.2          6.0  virginica
## 127          6.2          4.8  virginica
## 128          6.1          4.9  virginica
## 129          6.4          5.6  virginica
## 130          7.2          5.8  virginica
## 131          7.4          6.1  virginica
## 132          7.9          6.4  virginica
## 133          6.4          5.6  virginica
## 134          6.3          5.1  virginica
## 135          6.1          5.6  virginica
## 136          7.7          6.1  virginica
## 137          6.3          5.6  virginica
## 138          6.4          5.5  virginica
## 139          6.0          4.8  virginica
## 140          6.9          5.4  virginica
## 141          6.7          5.6  virginica
## 142          6.9          5.1  virginica
## 143          5.8          5.1  virginica
## 144          6.8          5.9  virginica
## 145          6.7          5.7  virginica
## 146          6.7          5.2  virginica
## 147          6.3          5.0  virginica
## 148          6.5          5.2  virginica
## 149          6.2          5.4  virginica
## 150          5.9          5.1  virginica
train <- df[idx,]  # 인덱싱(데이터 위치) 추출되지 않음
test <- df[-idx,]

# 순서가 중요함
control <- trainControl(method = "repeatedcv", number = 5)
model <- train(Gender~., data=train, method = "rf",
               preProcess=c("range"), trControl=control) # range 자리에 다른것 넣을 수 있음
model
## Random Forest 
## 
## 1444 samples
##   26 predictor
##    2 classes: 'female', 'male' 
## 
## Pre-processing: re-scaling to [0, 1] (26) 
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 1156, 1156, 1154, 1154, 1156 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.6274473  0.1774960
##   14    0.6197941  0.1785228
##   26    0.6170307  0.1774701
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
control <- trainControl(method = "cv", number = 5,
                        summaryFunction = twoClassSummary, 
                        classProbs = TRUE)
model1 <- train(Gender~., data=train, method = "rf", # 민감도와 ~만 보겠다
               preProcess=c("center", "scale"), trControl=control) # 성능지표만 다르게 보는 것
## Warning in train.default(x, y, weights = w, ...): The metric "Accuracy" was not
## in the result set. ROC will be used instead.
model1
## Random Forest 
## 
## 1444 samples
##   26 predictor
##    2 classes: 'female', 'male' 
## 
## Pre-processing: centered (26), scaled (26) 
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 1155, 1156, 1154, 1156, 1155 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.6572632  0.8250430  0.3565447
##   14    0.6499403  0.7675886  0.4190571
##   26    0.6454424  0.7617406  0.4275174
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(model, newdata = test) # factor로 결과 출력
##   [1] female female female female female female female female female male  
##  [11] female female female female female female female female female female
##  [21] female female female female female female female female female female
##  [31] female female male   male   female female male   female female female
##  [41] female male   female male   female female female female female female
##  [51] male   male   female female male   female female female female female
##  [61] female male   female female male   female female female female male  
##  [71] female female female female female female female female female female
##  [81] female female male   female female female female female female female
##  [91] male   female female female female male   female female female female
## [101] female female female male   female female female female female female
## [111] female male   female female female female female female male   female
## [121] male   female female female male   female female female male   female
## [131] female female female female female male   female female female female
## [141] female female male   female female female female female female female
## [151] female female female female female female female female female female
## [161] female female female female female female female female female male  
## [171] female male   male   female male   female female female female female
## [181] female male   male   female male   male   female female female female
## [191] female female female female male   female female female female male  
## [201] male   female female male   female female female female female male  
## [211] female male   female male   male   female male   male   female female
## [221] female female female female female female female female female male  
## [231] female female female male   female female female female female female
## [241] female female female male   female male   female female male   female
## [251] female female male   female male   female female female female female
## [261] female female female male   female female female male   female female
## [271] male   male   female male   female male   male   female female female
## [281] female female female female female female male   male   female female
## [291] female female male   male   female female male   male   male   female
## [301] male   female female male   female male   male   male   female female
## [311] female female female female female male   male   male   male   female
## [321] female female female male   male   female female female female male  
## [331] female female female female female male   female female female male  
## [341] female male   female female female male   male   female male   female
## [351] female male   female female female female male   male   male   female
## [361] male   female female female female male   male   male   female female
## [371] male   female female female female female female female female female
## [381] female female female female female female male   female female male  
## [391] female female female female female male   female female female female
## [401] female male   female female female female female male   female female
## [411] male   female female male   female female male   female male   female
## [421] female female female female male   female female male   male   male  
## [431] female female female female female male   female female female female
## [441] male   female male   female female male   male   male   male   female
## [451] female female female female female male   male   female male   female
## [461] female male   female female female male   male   female female male  
## [471] male   male   female female male   male   female male   male   female
## [481] female
## Levels: female male
predict(model, newdata = test, type = 'prob') # 확률로 출력
##      female  male
## 3     0.734 0.266
## 8     0.732 0.268
## 13    0.552 0.448
## 16    0.520 0.480
## 18    0.792 0.208
## 27    0.630 0.370
## 33    0.696 0.304
## 34    0.622 0.378
## 38    0.868 0.132
## 39    0.462 0.538
## 42    0.656 0.344
## 44    0.540 0.460
## 50    0.650 0.350
## 52    0.858 0.142
## 55    0.786 0.214
## 58    0.890 0.110
## 60    0.884 0.116
## 61    0.884 0.116
## 62    0.856 0.144
## 70    0.730 0.270
## 74    0.560 0.440
## 76    0.704 0.296
## 77    0.598 0.402
## 80    0.764 0.236
## 94    0.700 0.300
## 100   0.846 0.154
## 101   0.750 0.250
## 108   0.684 0.316
## 113   0.626 0.374
## 119   0.640 0.360
## 131   0.638 0.362
## 135   0.524 0.476
## 144   0.366 0.634
## 150   0.350 0.650
## 151   0.504 0.496
## 154   0.698 0.302
## 157   0.272 0.728
## 169   0.722 0.278
## 173   0.624 0.376
## 179   0.630 0.370
## 181   0.904 0.096
## 184   0.492 0.508
## 188   0.760 0.240
## 194   0.224 0.776
## 204   0.820 0.180
## 206   0.672 0.328
## 213   0.796 0.204
## 216   0.808 0.192
## 217   0.802 0.198
## 219   0.562 0.438
## 232   0.370 0.630
## 236   0.466 0.534
## 241   0.534 0.466
## 245   0.536 0.464
## 250   0.330 0.670
## 251   0.644 0.356
## 255   0.554 0.446
## 257   0.762 0.238
## 258   0.622 0.378
## 259   0.622 0.378
## 260   0.806 0.194
## 263   0.362 0.638
## 273   0.702 0.298
## 279   0.638 0.362
## 290   0.400 0.600
## 291   0.556 0.444
## 293   0.564 0.436
## 298   0.720 0.280
## 300   0.676 0.324
## 302   0.402 0.598
## 303   0.574 0.426
## 304   0.518 0.482
## 306   0.578 0.422
## 309   0.738 0.262
## 318   0.562 0.438
## 319   0.926 0.074
## 327   0.688 0.312
## 335   0.786 0.214
## 340   0.532 0.468
## 341   0.574 0.426
## 342   0.568 0.432
## 343   0.514 0.486
## 345   0.422 0.578
## 351   0.574 0.426
## 356   0.582 0.418
## 364   0.622 0.378
## 369   0.734 0.266
## 383   0.570 0.430
## 389   0.644 0.356
## 394   0.644 0.356
## 396   0.494 0.506
## 404   0.544 0.456
## 407   0.726 0.274
## 409   0.628 0.372
## 413   0.532 0.468
## 415   0.326 0.674
## 416   0.816 0.184
## 418   0.660 0.340
## 420   0.548 0.452
## 422   0.528 0.472
## 426   0.744 0.256
## 427   0.642 0.358
## 431   0.534 0.466
## 441   0.470 0.530
## 442   0.610 0.390
## 446   0.598 0.402
## 447   0.722 0.278
## 456   0.612 0.388
## 462   0.702 0.298
## 466   0.650 0.350
## 468   0.570 0.430
## 469   0.342 0.658
## 473   0.534 0.466
## 477   0.600 0.400
## 481   0.792 0.208
## 482   0.644 0.356
## 488   0.654 0.346
## 490   0.602 0.398
## 491   0.458 0.542
## 500   0.506 0.494
## 503   0.414 0.586
## 506   0.546 0.454
## 510   0.670 0.330
## 520   0.506 0.494
## 531   0.420 0.580
## 537   0.710 0.290
## 541   0.544 0.456
## 546   0.620 0.380
## 553   0.470 0.530
## 558   0.508 0.492
## 562   0.552 0.448
## 567   0.540 0.460
## 569   0.532 0.468
## 575   0.612 0.388
## 581   0.600 0.400
## 586   0.446 0.554
## 587   0.560 0.440
## 593   0.716 0.284
## 594   0.606 0.394
## 595   0.606 0.394
## 599   0.582 0.418
## 602   0.594 0.406
## 604   0.364 0.636
## 607   0.598 0.402
## 608   0.560 0.440
## 610   0.752 0.248
## 611   0.768 0.232
## 617   0.760 0.240
## 620   0.780 0.220
## 622   0.670 0.330
## 629   0.570 0.430
## 634   0.610 0.390
## 636   0.664 0.336
## 641   0.628 0.372
## 649   0.632 0.368
## 655   0.644 0.356
## 668   0.660 0.340
## 673   0.594 0.406
## 674   0.610 0.390
## 683   0.672 0.328
## 686   0.668 0.332
## 691   0.652 0.348
## 692   0.766 0.234
## 693   0.786 0.214
## 707   0.608 0.392
## 713   0.584 0.416
## 715   0.824 0.176
## 716   0.712 0.288
## 719   0.758 0.242
## 720   0.418 0.582
## 721   0.626 0.374
## 724   0.464 0.536
## 725   0.428 0.572
## 731   0.628 0.372
## 743   0.392 0.608
## 747   0.568 0.432
## 748   0.550 0.450
## 751   0.668 0.332
## 754   0.510 0.490
## 762   0.864 0.136
## 763   0.608 0.392
## 767   0.182 0.818
## 768   0.404 0.596
## 773   0.522 0.478
## 774   0.212 0.788
## 776   0.404 0.596
## 783   0.664 0.336
## 784   0.630 0.370
## 792   0.578 0.422
## 798   0.768 0.232
## 800   0.554 0.446
## 801   0.826 0.174
## 806   0.540 0.460
## 807   0.718 0.282
## 814   0.302 0.698
## 815   0.550 0.450
## 816   0.530 0.470
## 817   0.738 0.262
## 824   0.564 0.436
## 825   0.360 0.640
## 828   0.412 0.588
## 830   0.508 0.492
## 834   0.636 0.364
## 839   0.436 0.564
## 840   0.506 0.494
## 846   0.578 0.422
## 854   0.618 0.382
## 862   0.678 0.322
## 866   0.732 0.268
## 868   0.496 0.504
## 870   0.508 0.492
## 878   0.486 0.514
## 880   0.736 0.264
## 886   0.314 0.686
## 888   0.450 0.550
## 889   0.782 0.218
## 895   0.358 0.642
## 900   0.454 0.546
## 903   0.772 0.228
## 907   0.772 0.228
## 909   0.770 0.230
## 913   0.782 0.218
## 916   0.786 0.214
## 927   0.686 0.314
## 932   0.530 0.470
## 933   0.664 0.336
## 937   0.560 0.440
## 940   0.730 0.270
## 953   0.686 0.314
## 957   0.322 0.678
## 958   0.528 0.472
## 963   0.558 0.442
## 964   0.516 0.484
## 966   0.442 0.558
## 971   0.582 0.418
## 973   0.686 0.314
## 976   0.676 0.324
## 977   0.868 0.132
## 981   0.510 0.490
## 982   0.600 0.400
## 988   0.694 0.306
## 994   0.588 0.412
## 997   0.786 0.214
## 1011  0.404 0.596
## 1016  0.604 0.396
## 1019  0.402 0.598
## 1025  0.652 0.348
## 1028  0.734 0.266
## 1039  0.362 0.638
## 1041  0.532 0.468
## 1047  0.610 0.390
## 1049  0.508 0.492
## 1052  0.480 0.520
## 1053  0.588 0.412
## 1054  0.488 0.512
## 1055  0.530 0.470
## 1058  0.678 0.322
## 1063  0.736 0.264
## 1070  0.720 0.280
## 1072  0.852 0.148
## 1077  0.584 0.416
## 1080  0.636 0.364
## 1081  0.680 0.320
## 1086  0.494 0.506
## 1087  0.724 0.276
## 1089  0.654 0.346
## 1093  0.550 0.450
## 1099  0.500 0.500
## 1103  0.502 0.498
## 1104  0.572 0.428
## 1106  0.498 0.502
## 1111  0.490 0.510
## 1112  0.700 0.300
## 1118  0.362 0.638
## 1138  0.656 0.344
## 1142  0.384 0.616
## 1146  0.386 0.614
## 1150  0.572 0.428
## 1159  0.548 0.452
## 1167  0.936 0.064
## 1169  0.546 0.454
## 1171  0.538 0.462
## 1174  0.606 0.394
## 1175  0.560 0.440
## 1178  0.556 0.444
## 1181  0.616 0.384
## 1188  0.490 0.510
## 1190  0.026 0.974
## 1193  0.760 0.240
## 1198  0.802 0.198
## 1214  0.726 0.274
## 1216  0.632 0.368
## 1217  0.274 0.726
## 1218  0.424 0.576
## 1223  0.590 0.410
## 1225  0.614 0.386
## 1232  0.446 0.554
## 1233  0.472 0.528
## 1236  0.116 0.884
## 1240  0.962 0.038
## 1248  0.418 0.582
## 1251  0.580 0.420
## 1252  0.610 0.390
## 1260  0.442 0.558
## 1262  0.618 0.382
## 1272  0.496 0.504
## 1274  0.496 0.504
## 1276  0.498 0.502
## 1283  0.610 0.390
## 1284  0.562 0.438
## 1286  0.666 0.334
## 1288  0.640 0.360
## 1304  0.512 0.488
## 1307  0.590 0.410
## 1313  0.656 0.344
## 1315  0.460 0.540
## 1319  0.456 0.544
## 1323  0.418 0.582
## 1324  0.284 0.716
## 1339  0.900 0.100
## 1341  0.640 0.360
## 1348  0.618 0.382
## 1351  0.866 0.134
## 1356  0.486 0.514
## 1358  0.478 0.522
## 1360  0.526 0.474
## 1363  0.630 0.370
## 1364  0.682 0.318
## 1366  0.648 0.352
## 1367  0.496 0.504
## 1368  0.660 0.340
## 1370  0.710 0.290
## 1372  0.678 0.322
## 1374  0.596 0.404
## 1379  0.810 0.190
## 1384  0.498 0.502
## 1386  0.542 0.458
## 1389  0.544 0.456
## 1393  0.724 0.276
## 1397  0.422 0.578
## 1402  0.512 0.488
## 1405  0.438 0.562
## 1407  0.530 0.470
## 1411  0.670 0.330
## 1413  0.602 0.398
## 1415  0.292 0.708
## 1420  0.204 0.796
## 1422  0.540 0.460
## 1423  0.462 0.538
## 1425  0.508 0.492
## 1426  0.678 0.322
## 1429  0.444 0.556
## 1431  0.880 0.120
## 1432  0.654 0.346
## 1433  0.624 0.376
## 1435  0.512 0.488
## 1437  0.450 0.550
## 1443  0.022 0.978
## 1448  0.048 0.952
## 1455  0.908 0.092
## 1457  0.114 0.886
## 1466  0.522 0.478
## 1469  0.554 0.446
## 1470  0.526 0.474
## 1471  0.618 0.382
## 1475  0.466 0.534
## 1477  0.430 0.570
## 1482  0.448 0.552
## 1485  0.798 0.202
## 1487  0.600 0.400
## 1494  0.344 0.656
## 1496  0.824 0.176
## 1499  0.628 0.372
## 1507  0.578 0.422
## 1511  0.602 0.398
## 1514  0.610 0.390
## 1518  0.728 0.272
## 1520  0.616 0.384
## 1533  0.720 0.280
## 1536  0.536 0.464
## 1540  0.624 0.376
## 1542  0.618 0.382
## 1543  0.628 0.372
## 1545  0.544 0.456
## 1546  0.652 0.348
## 1549  0.600 0.400
## 1551  0.244 0.756
## 1552  0.564 0.436
## 1553  0.578 0.422
## 1557  0.480 0.520
## 1560  0.668 0.332
## 1564  0.564 0.436
## 1566  0.770 0.230
## 1576  0.586 0.414
## 1584  0.646 0.354
## 1589  0.386 0.614
## 1595  0.622 0.378
## 1597  0.510 0.490
## 1599  0.640 0.360
## 1600  0.510 0.490
## 1602  0.636 0.364
## 1607  0.448 0.552
## 1611  0.588 0.412
## 1616  0.650 0.350
## 1617  0.544 0.456
## 1620  0.680 0.320
## 1631  0.522 0.478
## 1637  0.346 0.654
## 1642  0.638 0.362
## 1646  0.542 0.458
## 1660  0.248 0.752
## 1665  0.660 0.340
## 1666  0.574 0.426
## 1670  0.436 0.564
## 1671  0.654 0.346
## 1673  0.640 0.360
## 1675  0.462 0.538
## 1676  0.520 0.480
## 1677  0.478 0.522
## 1679  0.764 0.236
## 1690  0.632 0.368
## 1691  0.608 0.392
## 1692  0.586 0.414
## 1696  0.576 0.424
## 1697  0.486 0.514
## 1698  0.692 0.308
## 1700  0.674 0.326
## 1704  0.204 0.796
## 1706  0.414 0.586
## 1716  0.396 0.604
## 1718  0.568 0.432
## 1723  0.566 0.434
## 1724  0.516 0.484
## 1748  0.678 0.322
## 1749  0.564 0.436
## 1751  0.462 0.538
## 1761  0.638 0.362
## 1766  0.816 0.184
## 1768  0.598 0.402
## 1769  0.592 0.408
## 1778  0.488 0.512
## 1780  0.558 0.442
## 1781  0.336 0.664
## 1784  0.648 0.352
## 1796  0.716 0.284
## 1798  0.474 0.526
## 1816  0.484 0.516
## 1818  0.438 0.562
## 1826  0.490 0.510
## 1832  0.756 0.244
## 1834  0.668 0.332
## 1837  0.862 0.138
## 1839  0.592 0.408
## 1842  0.534 0.466
## 1845  0.596 0.404
## 1849  0.404 0.596
## 1854  0.468 0.532
## 1857  0.566 0.434
## 1858  0.470 0.530
## 1859  0.670 0.330
## 1860  0.594 0.406
## 1865  0.438 0.562
## 1866  0.586 0.414
## 1867  0.568 0.432
## 1874  0.660 0.340
## 1876  0.486 0.514
## 1879  0.486 0.514
## 1885  0.550 0.450
## 1890  0.562 0.438
## 1898  0.356 0.644
## 1900  0.468 0.532
## 1904  0.498 0.502
## 1905  0.576 0.424
## 1906  0.558 0.442
## 1907  0.496 0.504
## 1909  0.344 0.656
## 1910  0.534 0.466
## 1914  0.480 0.520
## 1920  0.472 0.528
## 1924  0.564 0.436
## 1925  0.600 0.400
df1 <- predict(model, newdata = test)
head(df1)
## [1] female female female female female female
## Levels: female male
test$Gender_pred <- df1
View(test)
df2 <- test %>% select(Gender, Gender_pred)
View(df2)
predict(model, newdata = test) %>% confusionMatrix(test$Gender)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction female male
##     female    240  119
##     male       44   78
##                                           
##                Accuracy : 0.6611          
##                  95% CI : (0.6169, 0.7034)
##     No Information Rate : 0.5904          
##     P-Value [Acc > NIR] : 0.0008567       
##                                           
##                   Kappa : 0.2559          
##                                           
##  Mcnemar's Test P-Value : 6.786e-09       
##                                           
##             Sensitivity : 0.8451          
##             Specificity : 0.3959          
##          Pos Pred Value : 0.6685          
##          Neg Pred Value : 0.6393          
##              Prevalence : 0.5904          
##          Detection Rate : 0.4990          
##    Detection Prevalence : 0.7464          
##       Balanced Accuracy : 0.6205          
##                                           
##        'Positive' Class : female          
## 
             # 예측값                  # 혼돈행렬 # 실제값

library(mlbench)
data("Sonar")
glimpse(Sonar)
## Rows: 208
## Columns: 61
## $ V1    <dbl> 0.0200, 0.0453, 0.0262, 0.0100, 0.0762, 0.0286, 0.0317, 0.0519, …
## $ V2    <dbl> 0.0371, 0.0523, 0.0582, 0.0171, 0.0666, 0.0453, 0.0956, 0.0548, …
## $ V3    <dbl> 0.0428, 0.0843, 0.1099, 0.0623, 0.0481, 0.0277, 0.1321, 0.0842, …
## $ V4    <dbl> 0.0207, 0.0689, 0.1083, 0.0205, 0.0394, 0.0174, 0.1408, 0.0319, …
## $ V5    <dbl> 0.0954, 0.1183, 0.0974, 0.0205, 0.0590, 0.0384, 0.1674, 0.1158, …
## $ V6    <dbl> 0.0986, 0.2583, 0.2280, 0.0368, 0.0649, 0.0990, 0.1710, 0.0922, …
## $ V7    <dbl> 0.1539, 0.2156, 0.2431, 0.1098, 0.1209, 0.1201, 0.0731, 0.1027, …
## $ V8    <dbl> 0.1601, 0.3481, 0.3771, 0.1276, 0.2467, 0.1833, 0.1401, 0.0613, …
## $ V9    <dbl> 0.3109, 0.3337, 0.5598, 0.0598, 0.3564, 0.2105, 0.2083, 0.1465, …
## $ V10   <dbl> 0.2111, 0.2872, 0.6194, 0.1264, 0.4459, 0.3039, 0.3513, 0.2838, …
## $ V11   <dbl> 0.1609, 0.4918, 0.6333, 0.0881, 0.4152, 0.2988, 0.1786, 0.2802, …
## $ V12   <dbl> 0.1582, 0.6552, 0.7060, 0.1992, 0.3952, 0.4250, 0.0658, 0.3086, …
## $ V13   <dbl> 0.2238, 0.6919, 0.5544, 0.0184, 0.4256, 0.6343, 0.0513, 0.2657, …
## $ V14   <dbl> 0.0645, 0.7797, 0.5320, 0.2261, 0.4135, 0.8198, 0.3752, 0.3801, …
## $ V15   <dbl> 0.0660, 0.7464, 0.6479, 0.1729, 0.4528, 1.0000, 0.5419, 0.5626, …
## $ V16   <dbl> 0.2273, 0.9444, 0.6931, 0.2131, 0.5326, 0.9988, 0.5440, 0.4376, …
## $ V17   <dbl> 0.3100, 1.0000, 0.6759, 0.0693, 0.7306, 0.9508, 0.5150, 0.2617, …
## $ V18   <dbl> 0.2999, 0.8874, 0.7551, 0.2281, 0.6193, 0.9025, 0.4262, 0.1199, …
## $ V19   <dbl> 0.5078, 0.8024, 0.8929, 0.4060, 0.2032, 0.7234, 0.2024, 0.6676, …
## $ V20   <dbl> 0.4797, 0.7818, 0.8619, 0.3973, 0.4636, 0.5122, 0.4233, 0.9402, …
## $ V21   <dbl> 0.5783, 0.5212, 0.7974, 0.2741, 0.4148, 0.2074, 0.7723, 0.7832, …
## $ V22   <dbl> 0.5071, 0.4052, 0.6737, 0.3690, 0.4292, 0.3985, 0.9735, 0.5352, …
## $ V23   <dbl> 0.4328, 0.3957, 0.4293, 0.5556, 0.5730, 0.5890, 0.9390, 0.6809, …
## $ V24   <dbl> 0.5550, 0.3914, 0.3648, 0.4846, 0.5399, 0.2872, 0.5559, 0.9174, …
## $ V25   <dbl> 0.6711, 0.3250, 0.5331, 0.3140, 0.3161, 0.2043, 0.5268, 0.7613, …
## $ V26   <dbl> 0.6415, 0.3200, 0.2413, 0.5334, 0.2285, 0.5782, 0.6826, 0.8220, …
## $ V27   <dbl> 0.7104, 0.3271, 0.5070, 0.5256, 0.6995, 0.5389, 0.5713, 0.8872, …
## $ V28   <dbl> 0.8080, 0.2767, 0.8533, 0.2520, 1.0000, 0.3750, 0.5429, 0.6091, …
## $ V29   <dbl> 0.6791, 0.4423, 0.6036, 0.2090, 0.7262, 0.3411, 0.2177, 0.2967, …
## $ V30   <dbl> 0.3857, 0.2028, 0.8514, 0.3559, 0.4724, 0.5067, 0.2149, 0.1103, …
## $ V31   <dbl> 0.1307, 0.3788, 0.8512, 0.6260, 0.5103, 0.5580, 0.5811, 0.1318, …
## $ V32   <dbl> 0.2604, 0.2947, 0.5045, 0.7340, 0.5459, 0.4778, 0.6323, 0.0624, …
## $ V33   <dbl> 0.5121, 0.1984, 0.1862, 0.6120, 0.2881, 0.3299, 0.2965, 0.0990, …
## $ V34   <dbl> 0.7547, 0.2341, 0.2709, 0.3497, 0.0981, 0.2198, 0.1873, 0.4006, …
## $ V35   <dbl> 0.8537, 0.1306, 0.4232, 0.3953, 0.1951, 0.1407, 0.2969, 0.3666, …
## $ V36   <dbl> 0.8507, 0.4182, 0.3043, 0.3012, 0.4181, 0.2856, 0.5163, 0.1050, …
## $ V37   <dbl> 0.6692, 0.3835, 0.6116, 0.5408, 0.4604, 0.3807, 0.6153, 0.1915, …
## $ V38   <dbl> 0.6097, 0.1057, 0.6756, 0.8814, 0.3217, 0.4158, 0.4283, 0.3930, …
## $ V39   <dbl> 0.4943, 0.1840, 0.5375, 0.9857, 0.2828, 0.4054, 0.5479, 0.4288, …
## $ V40   <dbl> 0.2744, 0.1970, 0.4719, 0.9167, 0.2430, 0.3296, 0.6133, 0.2546, …
## $ V41   <dbl> 0.0510, 0.1674, 0.4647, 0.6121, 0.1979, 0.2707, 0.5017, 0.1151, …
## $ V42   <dbl> 0.2834, 0.0583, 0.2587, 0.5006, 0.2444, 0.2650, 0.2377, 0.2196, …
## $ V43   <dbl> 0.2825, 0.1401, 0.2129, 0.3210, 0.1847, 0.0723, 0.1957, 0.1879, …
## $ V44   <dbl> 0.4256, 0.1628, 0.2222, 0.3202, 0.0841, 0.1238, 0.1749, 0.1437, …
## $ V45   <dbl> 0.2641, 0.0621, 0.2111, 0.4295, 0.0692, 0.1192, 0.1304, 0.2146, …
## $ V46   <dbl> 0.1386, 0.0203, 0.0176, 0.3654, 0.0528, 0.1089, 0.0597, 0.2360, …
## $ V47   <dbl> 0.1051, 0.0530, 0.1348, 0.2655, 0.0357, 0.0623, 0.1124, 0.1125, …
## $ V48   <dbl> 0.1343, 0.0742, 0.0744, 0.1576, 0.0085, 0.0494, 0.1047, 0.0254, …
## $ V49   <dbl> 0.0383, 0.0409, 0.0130, 0.0681, 0.0230, 0.0264, 0.0507, 0.0285, …
## $ V50   <dbl> 0.0324, 0.0061, 0.0106, 0.0294, 0.0046, 0.0081, 0.0159, 0.0178, …
## $ V51   <dbl> 0.0232, 0.0125, 0.0033, 0.0241, 0.0156, 0.0104, 0.0195, 0.0052, …
## $ V52   <dbl> 0.0027, 0.0084, 0.0232, 0.0121, 0.0031, 0.0045, 0.0201, 0.0081, …
## $ V53   <dbl> 0.0065, 0.0089, 0.0166, 0.0036, 0.0054, 0.0014, 0.0248, 0.0120, …
## $ V54   <dbl> 0.0159, 0.0048, 0.0095, 0.0150, 0.0105, 0.0038, 0.0131, 0.0045, …
## $ V55   <dbl> 0.0072, 0.0094, 0.0180, 0.0085, 0.0110, 0.0013, 0.0070, 0.0121, …
## $ V56   <dbl> 0.0167, 0.0191, 0.0244, 0.0073, 0.0015, 0.0089, 0.0138, 0.0097, …
## $ V57   <dbl> 0.0180, 0.0140, 0.0316, 0.0050, 0.0072, 0.0057, 0.0092, 0.0085, …
## $ V58   <dbl> 0.0084, 0.0049, 0.0164, 0.0044, 0.0048, 0.0027, 0.0143, 0.0047, …
## $ V59   <dbl> 0.0090, 0.0052, 0.0095, 0.0040, 0.0107, 0.0051, 0.0036, 0.0048, …
## $ V60   <dbl> 0.0032, 0.0044, 0.0078, 0.0117, 0.0094, 0.0062, 0.0103, 0.0053, …
## $ Class <fct> R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R…
colSums(is.na(Sonar))
##    V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12   V13 
##     0     0     0     0     0     0     0     0     0     0     0     0     0 
##   V14   V15   V16   V17   V18   V19   V20   V21   V22   V23   V24   V25   V26 
##     0     0     0     0     0     0     0     0     0     0     0     0     0 
##   V27   V28   V29   V30   V31   V32   V33   V34   V35   V36   V37   V38   V39 
##     0     0     0     0     0     0     0     0     0     0     0     0     0 
##   V40   V41   V42   V43   V44   V45   V46   V47   V48   V49   V50   V51   V52 
##     0     0     0     0     0     0     0     0     0     0     0     0     0 
##   V53   V54   V55   V56   V57   V58   V59   V60 Class 
##     0     0     0     0     0     0     0     0     0
idx2 <- createDataPartition(Sonar$Class, p = .75, list = FALSE)
train2 <- Sonar[idx2,]  
test2 <- Sonar[-idx2,]
control <- trainControl(method = "cv", number = 5,
                        summaryFunction = twoClassSummary, 
                        classProbs = TRUE)
model2 <- train(Class ~ ., data=train2, method = "rf", 
                preProcess=c("center", "scale"), trControl=control) 
## Warning in train.default(x, y, weights = w, ...): The metric "Accuracy" was not
## in the result set. ROC will be used instead.
model2
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 126, 126, 125, 126, 125 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9204727  0.8808824  0.7800000
##   31    0.8930672  0.8095588  0.7647619
##   60    0.8812780  0.7867647  0.7371429
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(model2, newdata = test2) 
##  [1] R R R R M R R M R R R M R R R R M R R R R R R R M M M M M M M M M M M M M M
## [39] M M M M M R M M M M M M M
## Levels: M R
predict(model2, newdata = test2, type = 'prob') 
##         M     R
## 11  0.122 0.878
## 13  0.340 0.660
## 24  0.250 0.750
## 28  0.398 0.602
## 30  0.514 0.486
## 32  0.252 0.748
## 35  0.482 0.518
## 46  0.508 0.492
## 51  0.336 0.664
## 52  0.138 0.862
## 54  0.318 0.682
## 57  0.584 0.416
## 59  0.152 0.848
## 63  0.204 0.796
## 71  0.132 0.868
## 76  0.328 0.672
## 82  0.546 0.454
## 88  0.482 0.518
## 90  0.422 0.578
## 93  0.402 0.598
## 94  0.456 0.544
## 95  0.410 0.590
## 96  0.252 0.748
## 97  0.272 0.728
## 102 0.580 0.420
## 105 0.566 0.434
## 108 0.550 0.450
## 110 0.508 0.492
## 111 0.520 0.480
## 113 0.652 0.348
## 116 0.594 0.406
## 118 0.862 0.138
## 120 0.716 0.284
## 124 0.854 0.146
## 136 0.680 0.320
## 137 0.656 0.344
## 139 0.612 0.388
## 140 0.546 0.454
## 141 0.720 0.280
## 144 0.684 0.316
## 146 0.648 0.352
## 147 0.772 0.228
## 149 0.758 0.242
## 156 0.476 0.524
## 161 0.878 0.122
## 163 0.656 0.344
## 164 0.562 0.438
## 174 0.504 0.496
## 185 0.844 0.156
## 189 0.826 0.174
## 207 0.668 0.332
df2 <- predict(model2, newdata = test2)
head(df2)
## [1] R R R R M R
## Levels: M R
test2$Class_pred <- df2
View(test2)
df2 <- test2 %>% select(Class, Class_pred)
View(df2)
predict(model2, newdata = test2) %>% confusionMatrix(test2$Class)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  M  R
##          M 26  4
##          R  1 20
##                                           
##                Accuracy : 0.902           
##                  95% CI : (0.7859, 0.9674)
##     No Information Rate : 0.5294          
##     P-Value [Acc > NIR] : 1.209e-08       
##                                           
##                   Kappa : 0.8019          
##                                           
##  Mcnemar's Test P-Value : 0.3711          
##                                           
##             Sensitivity : 0.9630          
##             Specificity : 0.8333          
##          Pos Pred Value : 0.8667          
##          Neg Pred Value : 0.9524          
##              Prevalence : 0.5294          
##          Detection Rate : 0.5098          
##    Detection Prevalence : 0.5882          
##       Balanced Accuracy : 0.8981          
##                                           
##        'Positive' Class : M               
##