본문 바로가기

R

[R] RMSE, R2, RSS, SStot, AUC, accuracy, precision, recall, F1 score 구하는 방법

반응형

1. RMSE

calcRMSE <- function(label, estimation) {

     return(sqrt(mean((label - estimation) ** 2)))

}

 

 

 

2. R^2, RSS, SStot

calcR2 <- function(label, estimation) {

     RSS = sum((label - estimation) ** 2)

     SStot = sum((label - mean(label)) ** 2)

     return (1-RSS/SStot)

}

 

 

 

3. accuracy, precision, recall, F1 score (pred, actual은 predict로 예측한 값과 실제값)

(1)첫번째방법 (table활용)

conf.table <- table(pred, actual)

accuracy <- sum(diag(conf.table)) / sum(conf.table)

precision <- conf.table[2, 2] / sum(conf.table[2, ])

recall <- conf.table[2, 2] / sum(conf.table[, 2])

F1_score <- 2 * precision * recall / (precision + recall)

 

(2)두번째방법 (performance활용)

accuracy <- performance(prediction(pred, actual), 'acc')

precision <- performance(prediction(pred, actual), 'prec')

recall <- performance(prediction(pred, actual), 'rec')

F1_score <- performance(prediction(pred, actual), 'f')

 

 

 

 

4. AUC

calAUC <- function(predCol, targetCol){

     perf <- performance(prediction(predCol, targetCol), 'auc')

     as.numeric(perf@y.values) }

}

 

728x90
반응형