getwd()
## [1] "C:/data"
setwd("c:/data")
getwd()
## [1] "c:/data"
getwd()
## [1] "c:/data"
setwd("c:/data")
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.2.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: '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)
## Warning: 패키지 'caret'는 R 버전 4.2.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: ggplot2
## Warning: 패키지 'ggplot2'는 R 버전 4.2.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: 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.2.3에서 작성되었습니다
## 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
# 모델 생성
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
# 파라미터 조정
# ntree 의사결정나무수
# mtry 변수의 수
# na.action=na.omit 결측값 제외
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 7.036061 8.1171235 7.445483 10.627570
## Sepal.Width 4.511597 0.8520852 4.762895 4.735915
## Petal.Length 21.313574 32.0848453 26.837766 31.674798
## Petal.Width 23.575615 31.6771946 30.911386 33.466058
## MeanDecreaseGini
## Sepal.Length 9.647837
## Sepal.Width 2.194967
## Petal.Length 41.637017
## Petal.Width 45.807138
varImpPlot(model3)

df3<-read.csv("diagnosis.csv")
glimpse(df3)
## 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…
df3$diagnosis<-as.factor(df3$diagnosis)
model4<-randomForest(diagnosis~.,data=df3)
model4
##
## Call:
## randomForest(formula = diagnosis ~ ., data = df3)
## Type of random forest: classification
## Number of trees: 500
## No. of variables tried at each split: 5
##
## OOB estimate of error rate: 4.22%
## Confusion matrix:
## B M class.error
## B 347 10 0.02801120
## M 14 198 0.06603774
model5<-randomForest(diagnosis~.,data=df3,
importance=TRUE,na.action=na.omit)
importance(model5)
## B M MeanDecreaseAccuracy
## id 4.73461269 2.8469443 5.476957
## radius_mean 8.26306211 6.4829827 9.837430
## texture_mean 9.18058440 11.2223759 13.126758
## perimeter_mean 8.48869898 5.9739772 10.230839
## area_mean 9.16904121 5.2297264 9.847538
## smoothness_mean 2.10180279 6.4216153 6.706034
## compactness_mean 5.00317474 3.9272634 6.459428
## concavity_mean 7.44329164 8.8429192 11.496254
## concave.points_mean 10.15858495 12.1065931 15.296708
## symmetry_mean 0.08211263 3.3744089 2.648842
## fractal_dimension_mean 3.38207211 0.1196050 3.054339
## radius_se 8.59723844 5.5649368 10.088750
## texture_se 1.51812665 -0.6024346 0.982277
## perimeter_se 7.52492377 5.6592553 9.285813
## area_se 11.44434951 6.8323920 13.433846
## smoothness_se 3.43484158 2.8379483 4.395638
## compactness_se 5.68554036 0.5749777 5.549240
## concavity_se 4.26351033 4.4879148 6.120516
## concave.points_se 3.85658949 2.7104340 4.939566
## symmetry_se 3.24248685 0.3102650 3.021548
## fractal_dimension_se 3.64674034 1.9359223 4.137858
## radius_worst 14.15415868 11.2638765 16.892315
## texture_worst 12.29022531 11.2540816 14.817490
## perimeter_worst 14.02035569 13.2214512 18.286215
## area_worst 15.89935235 12.9320286 19.297529
## smoothness_worst 6.78273286 9.1454015 10.644067
## compactness_worst 6.89445343 3.8117953 7.941997
## concavity_worst 8.49748186 10.8929041 13.380001
## concave.points_worst 15.00815126 12.1260775 18.789028
## symmetry_worst 3.95153105 6.1959165 6.205365
## fractal_dimension_worst 3.19625053 4.1783408 4.728254
## MeanDecreaseGini
## id 1.4561822
## radius_mean 10.7182720
## texture_mean 3.7652243
## perimeter_mean 14.5984240
## area_mean 11.4229540
## smoothness_mean 1.7556424
## compactness_mean 3.0792427
## concavity_mean 12.9410094
## concave.points_mean 26.5849422
## symmetry_mean 0.9835063
## fractal_dimension_mean 0.9623507
## radius_se 4.3873440
## texture_se 0.9094339
## perimeter_se 3.8697541
## area_se 7.2870030
## smoothness_se 1.1433081
## compactness_se 1.1675098
## concavity_se 2.1760343
## concave.points_se 1.4892290
## symmetry_se 1.0615236
## fractal_dimension_se 1.2602459
## radius_worst 27.8545590
## texture_worst 4.7866364
## perimeter_worst 32.3153515
## area_worst 34.3970741
## smoothness_worst 3.3265905
## compactness_worst 4.0599507
## concavity_worst 9.2932544
## concave.points_worst 32.5697723
## symmetry_worst 2.5616567
## fractal_dimension_worst 1.4709360
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 |nmtry= 2 |n
## 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 |n
## 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 |n
## 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 |nmtry= 2 |n
## 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 |n
## 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 |n
## 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.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= 600 |nmtry= 2 |n
## 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.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 |n
## 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.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= 4 |n
## 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%
## 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