library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.1.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(pROC)
## Type 'citation("pROC")' for a citation.
##
## 다음의 패키지를 부착합니다: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
library(caret)
## Warning: 패키지 'caret'는 R 버전 4.1.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: ggplot2
## Warning: 패키지 'ggplot2'는 R 버전 4.1.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: lattice
library(recipes)
## Warning: 패키지 'recipes'는 R 버전 4.1.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'recipes'
## The following object is masked from 'package:stats':
##
## step
library(lubridate)
## Warning: 패키지 'lubridate'는 R 버전 4.1.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# 1. ROC_AUC 이해하기
# AUC(Area Under the ROC curve)란 ROC Curve 의 아래 면적을 나타내는
# 수치로 분류 모델(분류기)의 성능을 나타내는 지표
# ROC curve는 1-특이도(False Positive Rate, FPR, 거짓 긍정률)와
# 민감도(True Positive Rate, TPR, 참 긍정률)을 각각 x축, y축에 나타낸 그래프
# x축은 1-특이도 y축은 민감도 roc 커브 특정 임계값일 때 좌표 이으면 커브가
# 만들어진다.
library(mlbench)
## Warning: 패키지 'mlbench'는 R 버전 4.1.3에서 작성되었습니다
data("BreastCancer")
BreastCancer %>% glimpse
## Rows: 699
## Columns: 11
## $ Id <chr> "1000025", "1002945", "1015425", "1016277", "1017023",~
## $ Cl.thickness <ord> 5, 5, 3, 6, 4, 8, 1, 2, 2, 4, 1, 2, 5, 1, 8, 7, 4, 4, ~
## $ Cell.size <ord> 1, 4, 1, 8, 1, 10, 1, 1, 1, 2, 1, 1, 3, 1, 7, 4, 1, 1,~
## $ Cell.shape <ord> 1, 4, 1, 8, 1, 10, 1, 2, 1, 1, 1, 1, 3, 1, 5, 6, 1, 1,~
## $ Marg.adhesion <ord> 1, 5, 1, 1, 3, 8, 1, 1, 1, 1, 1, 1, 3, 1, 10, 4, 1, 1,~
## $ Epith.c.size <ord> 2, 7, 2, 3, 2, 7, 2, 2, 2, 2, 1, 2, 2, 2, 7, 6, 2, 2, ~
## $ Bare.nuclei <fct> 1, 10, 2, 4, 1, 10, 10, 1, 1, 1, 1, 1, 3, 3, 9, 1, 1, ~
## $ Bl.cromatin <fct> 3, 3, 3, 3, 3, 9, 3, 3, 1, 2, 3, 2, 4, 3, 5, 4, 2, 3, ~
## $ Normal.nucleoli <fct> 1, 2, 1, 7, 1, 7, 1, 1, 1, 1, 1, 1, 4, 1, 5, 3, 1, 1, ~
## $ Mitoses <fct> 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 4, 1, 1, 1, ~
## $ Class <fct> benign, benign, benign, benign, benign, malignant, ben~
colSums(is.na(BreastCancer))
## Id Cl.thickness Cell.size Cell.shape Marg.adhesion
## 0 0 0 0 0
## Epith.c.size Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses
## 0 16 0 0 0
## Class
## 0
str(BreastCancer)
## 'data.frame': 699 obs. of 11 variables:
## $ Id : chr "1000025" "1002945" "1015425" "1016277" ...
## $ Cl.thickness : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 5 5 3 6 4 8 1 2 2 4 ...
## $ Cell.size : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 4 1 8 1 10 1 1 1 2 ...
## $ Cell.shape : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 4 1 8 1 10 1 2 1 1 ...
## $ Marg.adhesion : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 1 5 1 1 3 8 1 1 1 1 ...
## $ Epith.c.size : Ord.factor w/ 10 levels "1"<"2"<"3"<"4"<..: 2 7 2 3 2 7 2 2 2 2 ...
## $ Bare.nuclei : Factor w/ 10 levels "1","2","3","4",..: 1 10 2 4 1 10 10 1 1 1 ...
## $ Bl.cromatin : Factor w/ 10 levels "1","2","3","4",..: 3 3 3 3 3 9 3 3 1 2 ...
## $ Normal.nucleoli: Factor w/ 10 levels "1","2","3","4",..: 1 2 1 7 1 7 1 1 1 1 ...
## $ Mitoses : Factor w/ 9 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 5 1 ...
## $ Class : Factor w/ 2 levels "benign","malignant": 1 1 1 1 1 2 1 1 1 1 ...
# BreastCancer$Bare.nuclei 결측값을 중앙값으로 대체
BreastCancer$Bare.nuclei<-as.numeric(BreastCancer$Bare.nuclei)
median(BreastCancer$Bare.nuclei,na.rm=TRUE)
## [1] 1
BreastCancer$Bare.nuclei<-ifelse(is.na(BreastCancer$Bare.nuclei),1,BreastCancer$Bare.nuclei)
set.seed(210615)
library(caret)
idx1<-createDataPartition(BreastCancer$Class,p=0.8,list=F)
# 데이터를 훈련 데이터를 테스트 데이터로 분할하여
# 훈련데이터로 사용할 데이터의 색인을 list 반환
# p 훈련 데이터에서 사용할 데이터의 비율
train<-BreastCancer[idx1,]
test<-BreastCancer[-idx1,]
library(randomForest)
## Warning: 패키지 'randomForest'는 R 버전 4.1.3에서 작성되었습니다
## randomForest 4.7-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
rffit<-randomForest(Class~.-Id,data=train)
library(pROC)
prefit<-predict(rffit,newdata=test,type="response")
# 'resopnse' factor
class(prefit)
## [1] "factor"
prfit_num<-as.numeric(prefit)
prfit_num
## [1] 2 1 1 1 2 1 2 2 2 2 2 1 2 1 1 1 1 1 2 2 1 1 2 1 2 2 1 1 1 1 1 1 2 1 2 1 1
## [38] 2 1 1 1 2 2 1 2 1 2 2 2 1 1 2 2 1 1 1 2 2 1 1 2 1 2 1 1 1 2 2 1 2 2 2 1 2
## [75] 1 1 1 2 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 2 1 2 1 1 1 1
## [112] 2 1 1 1 1 1 1 2 1 1 1 2 1 2 1 1 1 1 2 1 1 2 1 1 1 1 2 1
library(pROC)
# 정답라벨,수치형 예측결과
result<-roc(test$Class,prfit_num)
## Setting levels: control = benign, case = malignant
## Setting direction: controls < cases
plot(result,legacy.axes=TRUE)

result$auc
## Area under the curve: 0.9627