[R] factor levels 변경 및 혼동행렬 positive 변경

: ) YOUNG·2022년 6월 1일
2

R

목록 보기
6/8

levels 변경 예제



library(dplyr)
lvs <- c("normal", "abnormal")

#lvs의 normal 86번 반복, abnormal 258번 반복
truth <- factor(
    rep(
        lvs, 
        times = c(86, 258)
    ),
    levels = rev(lvs)
)
head(truth)



pred <- factor(
    c(
        rep(lvs, times = c(54, 32)),
        rep(lvs, times = c(27, 231))
    ),
        levels = rev(lvs)
)

length(truth)
length(pred)
str(truth)

xtab <- table(pred, truth)
# 이원테이블
xtab

confusionMatrix(xtab)


truthpred의 levels의 첫번째가 abnormal 이기 때문에 confusionMatrix를 실행하게 될 경우, abnormal
abnormal이 positive로 나오게 됨

> levels(truth)
[1] "abnormal" "normal"
> levels(pred)
[1] "abnormal" "normal"

혼동행렬

> confusionMatrix(xtab)
Confusion Matrix and Statistics

          truth
pred       abnormal normal
  abnormal      231     32
  normal         27     54

               Accuracy : 0.8285
                 95% CI : (0.7844, 0.8668)
    No Information Rate : 0.75
    P-Value [Acc > NIR] : 0.0003097

                  Kappa : 0.5336

 Mcnemar's Test P-Value : 0.6025370

            Sensitivity : 0.8953
            Specificity : 0.6279
         Pos Pred Value : 0.8783
         Neg Pred Value : 0.6667
             Prevalence : 0.7500
         Detection Rate : 0.6715
   Detection Prevalence : 0.7645
      Balanced Accuracy : 0.7616

       'Positive' Class : abnormal

여기서 positive를 바꾸려면 간다하게 confusionMatirx() 옵션에서 positive를 줘서 바꿀 수 있다.


> confusionMatrix(xtab, positive = 'normal')
Confusion Matrix and Statistics

          truth
pred       abnormal normal
  abnormal      231     32
  normal         27     54

               Accuracy : 0.8285
                 95% CI : (0.7844, 0.8668)
    No Information Rate : 0.75
    P-Value [Acc > NIR] : 0.0003097

                  Kappa : 0.5336

 Mcnemar's Test P-Value : 0.6025370

            Sensitivity : 0.6279
            Specificity : 0.8953
         Pos Pred Value : 0.6667
         Neg Pred Value : 0.8783
             Prevalence : 0.2500
         Detection Rate : 0.1570
   Detection Prevalence : 0.2355          
      Balanced Accuracy : 0.7616

       'Positive' Class : normal

하지만 levels 순서를 바꿔소 positive를 설정해주고 싶거나 순서를 바꿔야할 경우가 필요할 때가 있다.

이럴 때는 levels() 함수를 사용해서 바꿔줄 수 있다.


SVM를 사용해서 만든 예측모델이 있고,
검증 데이터를 사용해서 confusionMatrix를 사용한다고 했을 때의 예시이다.


> head(svm.pred, 40)
 109 1798  177 1502  735 1246   39 1694  676  219 1416  529 1430  709 1286  481
   0    0    1    0    1    1    0    0    0    1    0    0    0    0    0    1
 678 1075 1274  171 1224 1270  757 1571 1115  541   49  238    3   29 1966  615
   0    1    0    1    0    0    0    0    0    1    0    0    0    0    0    0
1046  577 1417  334 1279 1928 1511 1066
   0    1    0    1    0    0    0    0
Levels: 0 1
> levels(svm.pred)
[1] "0" "1"



> head(t.valid$TravelInsurance, 40)
 [1] 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1
[39] 0 0
Levels: 0 1
> levels(t.valid$TravelInsurance)
[1] "0" "1"


둘 다 levels의 첫번째가 0으로 되어있기 때문에 이 상태로 confusionMatrix를 사용하게 될 경우, positive는 0으로 나오게 된다.

이번에는 relevel을 사용해서 바꾸어주자.


svm.pred <- relevel(svm.pred, ref = '1')
head(svm.pred, 40)
levels(svm.pred)

t.valid$TravelInsurance <- relevel(t.valid$TravelInsurance , ref = '1')
head(t.valid$TravelInsurance, 40)
levels(t.valid$TravelInsurance)


> head(svm.pred, 40)
 109 1798  177 1502  735 1246   39 1694  676  219 1416  529 1430  709 1286  481
   0    0    1    0    1    1    0    0    0    1    0    0    0    0    0    1
 678 1075 1274  171 1224 1270  757 1571 1115  541   49  238    3   29 1966  615 
   0    1    0    1    0    0    0    0    0    1    0    0    0    0    0    0
1046  577 1417  334 1279 1928 1511 1066
   0    1    0    1    0    0    0    0
Levels: 1 0
> levels(svm.pred)
[1] "1" "0"



> head(t.valid$TravelInsurance, 40)
 [1] 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1
[39] 0 0
Levels: 1 0
> levels(t.valid$TravelInsurance)
[1] "1" "0"



혼동행렬 확인


caret::confusionMatrix(
    data = svm.pred,
    refer = t.valid$TravelInsurance,
)


Confusion Matrix and Statistics

          Reference
Prediction   1   0
         1  71  20
         0  70 257

               Accuracy : 0.7847
                 95% CI : (0.7421, 0.8232)
    No Information Rate : 0.6627
    P-Value [Acc > NIR] : 3.005e-08

                  Kappa : 0.4725

 Mcnemar's Test P-Value : 2.404e-07

            Sensitivity : 0.5035
            Specificity : 0.9278
         Pos Pred Value : 0.7802
         Neg Pred Value : 0.7859
             Prevalence : 0.3373
         Detection Rate : 0.1699
   Detection Prevalence : 0.2177
      Balanced Accuracy : 0.7157

       'Positive' Class : 1

> 

positive가 1로 출력된 모습 확인

0개의 댓글