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) }
}
'R' 카테고리의 다른 글
[R] k, M 변환 (0) | 2021.07.16 |
---|---|
[R] 숫자 콤마(,) 제거 - gsub() (2) | 2021.07.15 |
[R] Classification - Logistic Regression을 사용한 classification (0) | 2021.07.10 |
[R] Regression - lm()를 사용한 linear regression (0) | 2021.07.10 |
[R] MAC read.csv encoding error 해결방법 (0) | 2021.07.06 |