1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/r-services/telco-customer-churn-v1/R/telcoChurn-evaluate.R
2017-02-02 13:46:51 -08:00

40 lines
1.5 KiB
R

################################################################
## Title: Telco Customer Churn
## Description: Defining pre-functions
## Author: Microsoft
################################################################
####################################################################################################
## Define functions for model evaluation
####################################################################################################
## Define evaluation metrics
evaluateModel <- function(data, observed, predicted)
{
confusion <- table(data[[observed]], data[[predicted]])
print(confusion)
tp <- confusion[rownames(confusion) == 1, colnames(confusion) == 1]
fn <- confusion[rownames(confusion) == 1, colnames(confusion) == 0]
fp <- confusion[rownames(confusion) == 0, colnames(confusion) == 1]
tn <- confusion[rownames(confusion) == 0, colnames(confusion) == 0]
accuracy <- (tp + tn) / (tp + fn + fp + tn)
precision <- tp / (tp + fp)
recall <- tp / (tp + fn)
fscore <- 2 * (precision * recall) / (precision + recall)
metrics <- c("Accuracy" = accuracy,
"Precision" = precision,
"Recall" = recall,
"F-Score" = fscore)
return(metrics)
}
## Define ROC curve
rxrocCurve <- function(data, observed, predicted)
{
data <- data[, c(observed, predicted)]
data[[observed]] <- as.numeric(as.character(data[[observed]]))
rxRocCurve(actualVarName = observed,
predVarNames = predicted,
data = data)
}