getwd()
## [1] "C:/data"
setwd('c:/data')
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
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…
iris %>% count(Species)
##      Species  n
## 1     setosa 50
## 2 versicolor 50
## 3  virginica 50
library(randomForest)
## Warning: 패키지 'randomForest'는 R 버전 4.3.2에서 작성되었습니다
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## 다음의 패키지를 부착합니다: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
## The following object is masked from 'package:dplyr':
## 
##     combine
data(iris)

# 모델 생성

model<-randomForest(Species~.,data=iris)
model
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
# 파라미터 조정

model2<-randomForest(Species~.,data = iris,
                     ntree=300,mtry=4,na.action = na.omit)
model2
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = 300,      mtry = 4, na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 300
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 4.67%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          4        46        0.08
# ntree : 의사결정나무수
# ntry : 변수의 수
# na.action=na.omit, : 결측값 제외

# 중요변수로 랜덤포레스트 생성

model3<-randomForest(Species~.,data = iris,
                     importance=TRUE,na.action = na.omit)
importance(model3)
##                 setosa versicolor virginica MeanDecreaseAccuracy
## Sepal.Length  6.285315  7.7580996  8.318833            11.006706
## Sepal.Width   4.697518  0.8599407  4.525422             4.980005
## Petal.Length 23.592581 33.2500076 28.695831            34.988107
## Petal.Width  20.891067 31.2084947 29.866009            31.751415
##              MeanDecreaseGini
## Sepal.Length         9.902379
## Sepal.Width          2.183811
## Petal.Length        44.344088
## Petal.Width         42.853362
varImpPlot(model3)

model3
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, importance = TRUE,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 4.67%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          4        46        0.08
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$diagnosis<-as.factor(df$diagnosis)

model4<-randomForest(diagnosis~.,data=df)
model4
## 
## Call:
##  randomForest(formula = diagnosis ~ ., data = df) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 5
## 
##         OOB estimate of  error rate: 3.69%
## Confusion matrix:
##     B   M class.error
## B 348   9  0.02521008
## M  12 200  0.05660377
model5<-randomForest(diagnosis~.,data=df,
                     importance=TRUE,na.action = na.omit)
importance(model5)
##                                  B          M MeanDecreaseAccuracy
## id                       3.8428519  2.0289764             4.194150
## radius_mean              8.6801335  5.8686423            10.082423
## texture_mean             8.3725037 10.4610989            12.336569
## perimeter_mean           8.2585854  6.6687783             9.544170
## area_mean                9.6981169  6.0720535            10.761861
## smoothness_mean          1.8983962  6.6598919             6.585680
## compactness_mean         5.1754607  3.7217382             6.595889
## concavity_mean           8.0399468  8.6706300            11.157199
## concave.points_mean      9.9573970 11.6111522            14.635617
## symmetry_mean            0.5635987  3.3337075             2.557454
## fractal_dimension_mean   2.9830744  1.7577371             3.860888
## radius_se                8.9899748  6.6856802            11.459186
## texture_se               4.4621795  1.2501906             4.591369
## perimeter_se             7.2700709  6.2833349             9.331803
## area_se                 12.2358499  8.0236732            14.540549
## smoothness_se            2.4199693  1.8146775             3.016382
## compactness_se           5.3596293  0.6271874             5.129329
## concavity_se             4.7367879  4.7530544             6.781877
## concave.points_se        3.7866011  3.3613287             5.079640
## symmetry_se              3.4401768  0.5664939             3.203903
## fractal_dimension_se     4.6254603  0.9893715             4.342012
## radius_worst            13.7522294 10.9734992            16.278331
## texture_worst           11.1332175 10.9625182            13.895092
## perimeter_worst         13.9466204 12.0592028            17.066045
## area_worst              14.4091670 11.8932027            17.381925
## smoothness_worst         8.5057752  9.3358957            12.116801
## compactness_worst        6.7310483  4.8378843             8.071270
## concavity_worst          8.0623088 10.1519882            13.292868
## concave.points_worst    14.4648347 12.5228307            17.958737
## symmetry_worst           4.9036613  4.9459500             6.478844
## fractal_dimension_worst  4.1556640  4.0723171             4.819689
##                         MeanDecreaseGini
## id                             1.7284788
## radius_mean                   11.7943896
## texture_mean                   4.5272405
## perimeter_mean                17.0260764
## area_mean                     13.2827139
## smoothness_mean                1.6305190
## compactness_mean               1.9679797
## concavity_mean                14.1314185
## concave.points_mean           25.7946966
## symmetry_mean                  1.1151585
## fractal_dimension_mean         1.0426816
## radius_se                      4.7715434
## texture_se                     1.3266861
## perimeter_se                   3.9801767
## area_se                        8.1216705
## smoothness_se                  1.2212003
## compactness_se                 0.9663741
## concavity_se                   2.2173390
## concave.points_se              1.4221855
## symmetry_se                    1.0580048
## fractal_dimension_se           1.4491882
## radius_worst                  27.3208677
## texture_worst                  4.7266560
## perimeter_worst               35.3036942
## area_worst                    27.0461749
## smoothness_worst               3.6690311
## compactness_worst              3.5868518
## concavity_worst                8.7826622
## concave.points_worst          30.6766495
## symmetry_worst                 2.6878658
## fractal_dimension_worst        1.6343279
varImpPlot(model5)

# 최적의 파라미터 찾기
ntree<-c(400,500,600)
mtry<-c(2:4)

param<-data.frame(n=ntree,m=mtry)

for(i in param$n){
  cat('ntree=',i,'\n')
  for(j in param$m){
    cat('mtry=',j,'\n')
    model_iris<-randomForest(Species~.,data=iris,
                            ntree=i,mtry=j,na.action=na.omit)
    print(model_iris)
  }
}
## ntree= 400 
## mtry= 2 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 400
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## mtry= 3 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 400
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## mtry= 4 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 400
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## ntree= 500 
## mtry= 2 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 4.67%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          4        46        0.08
## mtry= 3 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## mtry= 4 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## ntree= 600 
## mtry= 2 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 600
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## mtry= 3 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 600
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 4%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          3        47        0.06
## mtry= 4 
## 
## Call:
##  randomForest(formula = Species ~ ., data = iris, ntree = i, mtry = j,      na.action = na.omit) 
##                Type of random forest: classification
##                      Number of trees: 600
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 4.67%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         50          0         0        0.00
## versicolor      0         47         3        0.06
## virginica       0          4        46        0.08