getwd()
## [1] "D:/박사과정/23 2학기/머신런닝공정안전사례분석/과제"
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
143/500
## [1] 0.286
1-0.953333
## [1] 0.046667
#파라메타 조정
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
# 중요변수로 랜던ㅁ포레스트 생성
model3<-randomForest(Species~.,data = iris,
                     importance=TRUE,na.action = na.omit)
importance(model3)
##                 setosa versicolor virginica MeanDecreaseAccuracy
## Sepal.Length  6.588993   7.249157  9.204133            12.002263
## Sepal.Width   4.709073   1.472450  4.196997             5.343738
## Petal.Length 20.965677  31.294997 28.761698            32.537021
## Petal.Width  22.673163  32.691523 30.679460            33.859716
##              MeanDecreaseGini
## Sepal.Length        10.137871
## Sepal.Width          2.607514
## Petal.Length        41.925407
## Petal.Width         44.574088
varImpPlot(model3)

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: 4.04%
## Confusion matrix:
##     B   M class.error
## B 347  10  0.02801120
## M  13 199  0.06132075
model5<-randomForest(diagnosis~.,data = df,
                     importance=TRUE,na.action = na.omit)
importance(model5)
##                                  B          M MeanDecreaseAccuracy
## id                       4.9839906  3.8014956            6.1362110
## radius_mean              8.8571675  5.2642978            9.7463802
## texture_mean             9.0026562  9.5645095           12.3770246
## perimeter_mean           8.9785948  5.8189116           10.1864571
## area_mean               10.1079685  5.5703129           10.8238328
## smoothness_mean          3.0881723  6.5647198            7.4239818
## compactness_mean         3.1277302  3.4668608            4.6925848
## concavity_mean           7.8610948  7.7493834           11.2927414
## concave.points_mean     10.7104875 11.2273873           15.4870157
## symmetry_mean           -0.2241972  3.3074479            2.5411158
## fractal_dimension_mean   2.9915510  1.7632156            3.5053021
## radius_se                7.8214763  5.9207011            9.9250404
## texture_se               2.8552111  3.2438595            4.2321543
## perimeter_se             7.9257688  5.4855597            9.0987230
## area_se                 10.4814043  8.2190587           12.7280616
## smoothness_se            1.2106341 -0.1420017            0.9399487
## compactness_se           5.8292229  1.0274689            5.3974536
## concavity_se             4.8527554  4.5545128            6.1851029
## concave.points_se        3.5985470  2.7048074            4.6282317
## symmetry_se              2.8832086  1.8151758            3.5303698
## fractal_dimension_se     3.9285759  0.7918499            3.4211018
## radius_worst            13.7580664 11.7254974           17.3836100
## texture_worst           11.1928763 12.4949491           14.5861253
## perimeter_worst         13.2485048 11.7487828           16.7888148
## area_worst              16.1536149 11.7544628           18.3736308
## smoothness_worst         7.2103529  7.8146351            9.8670385
## compactness_worst        6.5204054  5.7628370            8.1066413
## concavity_worst          6.7559557 11.5548391           13.4229039
## concave.points_worst    14.5142280 12.4121088           18.5908390
## symmetry_worst           5.0714398  6.4313442            7.6153574
## fractal_dimension_worst  3.7671420  3.8748260            5.7571598
##                         MeanDecreaseGini
## id                             1.6084637
## radius_mean                   10.4679307
## texture_mean                   3.4601051
## perimeter_mean                14.3822231
## area_mean                     15.6573441
## smoothness_mean                1.5466342
## compactness_mean               2.0745301
## concavity_mean                12.5513479
## concave.points_mean           26.7399087
## symmetry_mean                  1.1203167
## fractal_dimension_mean         0.8761113
## radius_se                      3.0918201
## texture_se                     1.2931929
## perimeter_se                   3.2741247
## area_se                       10.3510251
## smoothness_se                  1.1539609
## compactness_se                 1.1281441
## concavity_se                   1.7952188
## concave.points_se              1.3405850
## symmetry_se                    1.0310767
## fractal_dimension_se           1.3038225
## radius_worst                  27.0810470
## texture_worst                  4.6941597
## perimeter_worst               29.5697190
## area_worst                    28.6915108
## smoothness_worst               3.4968190
## compactness_worst              4.5222918
## concavity_worst               10.7789526
## concave.points_worst          36.0539225
## symmetry_worst                 2.6409361
## fractal_dimension_worst        1.8025933
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.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: 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.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= 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%
## 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: 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