MLmetrics/0000755000176200001440000000000014606615055012161 5ustar liggesusersMLmetrics/NAMESPACE0000644000176200001440000000132614602420006013364 0ustar liggesusers# Generated by roxygen2: do not edit by hand export(AUC) export(Accuracy) export(Area_Under_Curve) export(ConfusionDF) export(ConfusionMatrix) export(F1_Score) export(FBeta_Score) export(GainAUC) export(Gini) export(KS_Stat) export(LiftAUC) export(LogLoss) export(MAE) export(MAPE) export(MSE) export(MedianAE) export(MedianAPE) export(MultiLogLoss) export(NormalizedGini) export(PRAUC) export(Poisson_LogLoss) export(Precision) export(R2_Score) export(RAE) export(RMSE) export(RMSLE) export(RMSPE) export(RRSE) export(Recall) export(Sensitivity) export(Specificity) export(ZeroOneLoss) importFrom(stats,integrate) importFrom(stats,median) importFrom(stats,model.matrix) importFrom(stats,na.omit) importFrom(stats,splinefun) MLmetrics/man/0000755000176200001440000000000014606615055012734 5ustar liggesusersMLmetrics/man/MedianAE.Rd0000644000176200001440000000105014602420006014604 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{MedianAE} \alias{MedianAE} \title{Median Absolute Error Loss} \usage{ MedianAE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Median Absolute Error Loss } \description{ Compute the median absolute error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) MedianAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/MSE.Rd0000644000176200001440000000101114602420006013622 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{MSE} \alias{MSE} \title{Mean Square Error Loss} \usage{ MSE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Mean Square Error Loss } \description{ Compute the mean squared error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) MSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/PRAUC.Rd0000644000176200001440000000125114602420006014056 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{PRAUC} \alias{PRAUC} \title{Area Under the Precision-Recall Curve (PR AUC)} \usage{ PRAUC(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Area Under the PR Curve (PR AUC) } \description{ Compute the Area Under the Precision-Recall Curve (PR AUC) from prediction scores. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) PRAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/Recall.Rd0000644000176200001440000000142314602420006014407 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Recall} \alias{Recall} \title{Recall} \usage{ Recall(y_true, y_pred, positive = NULL) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} } \value{ Recall } \description{ Compute the recall score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) Recall(y_pred = pred, y_true = mtcars$vs, positive = "0") Recall(y_pred = pred, y_true = mtcars$vs, positive = "1") } MLmetrics/man/Specificity.Rd0000644000176200001440000000147314602420006015465 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Specificity} \alias{Specificity} \title{Specificity} \usage{ Specificity(y_true, y_pred, positive = NULL) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} } \value{ Specificity } \description{ Compute the specificity score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) Specificity(y_pred = pred, y_true = mtcars$vs, positive = "0") Specificity(y_pred = pred, y_true = mtcars$vs, positive = "1") } MLmetrics/man/RMSPE.Rd0000644000176200001440000000110214602420006014065 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{RMSPE} \alias{RMSPE} \title{Root Mean Square Percentage Error Loss} \usage{ RMSPE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Root Mean Squared Percentage Error Loss } \description{ Compute the root mean squared percentage error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) RMSPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/LogLoss.Rd0000644000176200001440000000116114602420006014566 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{LogLoss} \alias{LogLoss} \title{Log loss / Cross-Entropy Loss} \usage{ LogLoss(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Log loss/Cross-Entropy Loss } \description{ Compute the log loss/cross-entropy loss. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) LogLoss(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/Precision.Rd0000644000176200001440000000145314602420006015143 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Precision} \alias{Precision} \title{Precision} \usage{ Precision(y_true, y_pred, positive = NULL) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} } \value{ Precision } \description{ Compute the precision score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) Precision(y_pred = pred, y_true = mtcars$vs, positive = "0") Precision(y_pred = pred, y_true = mtcars$vs, positive = "1") } MLmetrics/man/ConfusionDF.Rd0000644000176200001440000000132014602420006015356 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{ConfusionDF} \alias{ConfusionDF} \title{Confusion Matrix (Data Frame Format)} \usage{ ConfusionDF(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ a data.frame of Confusion Matrix } \description{ Compute data frame format confusion matrix for internal usage. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) ConfusionDF(y_pred = pred, y_true = mtcars$vs) } \keyword{internal} MLmetrics/man/MLmetrics.Rd0000644000176200001440000000057314602420006015111 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/MLmetrics.R \docType{package} \name{MLmetrics} \alias{MLmetrics} \alias{MLmetrics-package} \title{MLmetrics: Machine Learning Evaluation Metrics} \description{ A collection of evaluation metrics, including loss, score and utility functions, that measure regression and classification performance. } MLmetrics/man/Sensitivity.Rd0000644000176200001440000000147314602420006015544 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Sensitivity} \alias{Sensitivity} \title{Sensitivity} \usage{ Sensitivity(y_true, y_pred, positive = NULL) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} } \value{ Sensitivity } \description{ Compute the sensitivity score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) Sensitivity(y_pred = pred, y_true = mtcars$vs, positive = "0") Sensitivity(y_pred = pred, y_true = mtcars$vs, positive = "1") } MLmetrics/man/MedianAPE.Rd0000644000176200001440000000111514602420006014726 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{MedianAPE} \alias{MedianAPE} \title{Median Absolute Percentage Error Loss} \usage{ MedianAPE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Median Absolute Percentage Error Loss } \description{ Compute the Median absolute percentage error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) MedianAPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/RMSLE.Rd0000644000176200001440000000110614602420006014065 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{RMSLE} \alias{RMSLE} \title{Root Mean Squared Logarithmic Error Loss} \usage{ RMSLE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Root Mean Squared Logarithmic Error Loss } \description{ Compute the root mean squared logarithmic error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) RMSLE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/LiftAUC.Rd0000644000176200001440000000120014602420006014425 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{LiftAUC} \alias{LiftAUC} \title{Area Under the Lift Chart} \usage{ LiftAUC(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Area Under the Lift Chart } \description{ Compute the Area Under the Lift Chart from prediction scores. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) LiftAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/Accuracy.Rd0000644000176200001440000000115114602420006014735 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Accuracy} \alias{Accuracy} \title{Accuracy} \usage{ Accuracy(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Accuracy } \description{ Compute the accuracy classification score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) Accuracy(y_pred = pred, y_true = mtcars$vs) } MLmetrics/man/RMSE.Rd0000644000176200001440000000103414602420006013751 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{RMSE} \alias{RMSE} \title{Root Mean Square Error Loss} \usage{ RMSE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Root Mean Square Error Loss } \description{ Compute the root mean squared error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) RMSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/ZeroOneLoss.Rd0000644000176200001440000000126014602420006015426 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{ZeroOneLoss} \alias{ZeroOneLoss} \title{Normalized Zero-One Loss (Classification Error Loss)} \usage{ ZeroOneLoss(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Zero-One Loss } \description{ Compute the normalized zero-one classification loss. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) ZeroOneLoss(y_pred = pred, y_true = mtcars$vs) } MLmetrics/man/MAE.Rd0000644000176200001440000000101614602420006013605 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{MAE} \alias{MAE} \title{Mean Absolute Error Loss} \usage{ MAE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Mean Absolute Error Loss } \description{ Compute the mean absolute error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) MAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/Poisson_LogLoss.Rd0000644000176200001440000000135214602420006016302 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Count.R \name{Poisson_LogLoss} \alias{Poisson_LogLoss} \title{Poisson Log loss} \usage{ Poisson_LogLoss(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a model} \item{y_true}{Ground truth (correct) labels vector} } \value{ Log loss/Cross-Entropy Loss } \description{ Compute the log loss/cross-entropy loss. } \examples{ d_AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), counts = c(18,17,15,20,10,20,25,13,12)) glm_poisson <- glm(counts ~ outcome + treatment, family = poisson(link = "log"), data = d_AD) Poisson_LogLoss(y_pred = glm_poisson$fitted.values, y_true = d_AD$counts) } MLmetrics/man/R2_Score.Rd0000644000176200001440000000111214602420060014616 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{R2_Score} \alias{R2_Score} \title{R-Squared (Coefficient of Determination) Regression Score} \usage{ R2_Score(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ R^2 Score } \description{ Compute the R-Squared (Coefficient of Determination) Regression Score. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) R2_Score(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/AUC.Rd0000644000176200001440000000130714602420006013616 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{AUC} \alias{AUC} \title{Area Under the Receiver Operating Characteristic Curve (ROC AUC)} \usage{ AUC(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Area Under the ROC Curve (ROC AUC) } \description{ Compute the Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) AUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/ConfusionMatrix.Rd0000644000176200001440000000127414602420006016341 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{ConfusionMatrix} \alias{ConfusionMatrix} \title{Confusion Matrix} \usage{ ConfusionMatrix(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ a table of Confusion Matrix } \description{ Compute confusion matrix to evaluate the accuracy of a classification. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) ConfusionMatrix(y_pred = pred, y_true = mtcars$vs) } MLmetrics/man/MultiLogLoss.Rd0000644000176200001440000000133414602420006015603 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{MultiLogLoss} \alias{MultiLogLoss} \title{Multi Class Log Loss} \usage{ MultiLogLoss(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities matrix, as returned by a classifier} \item{y_true}{Ground truth (correct) labels vector or a matrix of correct labels indicating by 0-1, same format as probabilities matrix} } \value{ Multi Class Log Loss } \description{ Compute the multi class log loss. } \examples{ data(iris) svm.model <- e1071::svm(Species~., data = iris, probability = TRUE) pred <- predict(svm.model, iris, probability = TRUE) MultiLogLoss(y_true = iris$Species, y_pred = attr(pred, "probabilities")) } MLmetrics/man/FBeta_Score.Rd0000644000176200001440000000161014602420006015317 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{FBeta_Score} \alias{FBeta_Score} \title{F-Beta Score} \usage{ FBeta_Score(y_true, y_pred, positive = NULL, beta = 1) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} \item{beta}{Weight of precision in harmonic mean} } \value{ F-Beta Score } \description{ Compute the F-Beta Score } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) FBeta_Score(y_pred = pred, y_true = mtcars$vs, positive = "0", beta = 2) FBeta_Score(y_pred = pred, y_true = mtcars$vs, positive = "1", beta = 2) } MLmetrics/man/F1_Score.Rd0000644000176200001440000000143514602420006014611 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{F1_Score} \alias{F1_Score} \title{F1 Score} \usage{ F1_Score(y_true, y_pred, positive = NULL) } \arguments{ \item{y_true}{Ground truth (correct) 0-1 labels vector} \item{y_pred}{Predicted labels vector, as returned by a classifier} \item{positive}{An optional character string for the factor level that corresponds to a "positive" result} } \value{ F1 Score } \description{ Compute the F1 Score. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) F1_Score(y_pred = pred, y_true = mtcars$vs, positive = "0") F1_Score(y_pred = pred, y_true = mtcars$vs, positive = "1") } MLmetrics/man/RAE.Rd0000644000176200001440000000103214602420006013610 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{RAE} \alias{RAE} \title{Relative Absolute Error Loss} \usage{ RAE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Relative Absolute Error Loss } \description{ Compute the relative absolute error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) RAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/NormalizedGini.Rd0000644000176200001440000000136114602420006016121 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Count.R \name{NormalizedGini} \alias{NormalizedGini} \title{Normalized Gini Coefficient} \usage{ NormalizedGini(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted labels vector, as returned by a model} \item{y_true}{Ground truth (correct) labels vector} } \value{ Normalized Gini Coefficient } \description{ Compute the Normalized Gini Coefficient. } \examples{ d_AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), counts = c(18,17,15,20,10,20,25,13,12)) glm_poisson <- glm(counts ~ outcome + treatment, family = poisson(link = "log"), data = d_AD) NormalizedGini(y_pred = glm_poisson$fitted.values, y_true = d_AD$counts) } MLmetrics/man/KS_Stat.Rd0000644000176200001440000000116214602420006014515 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{KS_Stat} \alias{KS_Stat} \title{Kolmogorov-Smirnov Statistic} \usage{ KS_Stat(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Kolmogorov-Smirnov statistic } \description{ Compute the Kolmogorov-Smirnov statistic. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) KS_Stat(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/RRSE.Rd0000644000176200001440000000105214602420006013756 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{RRSE} \alias{RRSE} \title{Root Relative Squared Error Loss} \usage{ RRSE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Root Relative Squared Error Loss } \description{ Compute the root relative squared error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) RRSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/Gini.Rd0000644000176200001440000000110214602420006014065 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{Gini} \alias{Gini} \title{Gini Coefficient} \usage{ Gini(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Gini Coefficient } \description{ Compute the Gini Coefficient. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/man/MAPE.Rd0000644000176200001440000000106314602420006013727 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Regression.R \name{MAPE} \alias{MAPE} \title{Mean Absolute Percentage Error Loss} \usage{ MAPE(y_pred, y_true) } \arguments{ \item{y_pred}{Estimated target values vector} \item{y_true}{Ground truth (correct) target values vector} } \value{ Mean Absolute Percentage Error Loss } \description{ Compute the mean absolute percentage error regression loss. } \examples{ data(cars) reg <- lm(log(dist) ~ log(speed), data = cars) MAPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) } MLmetrics/man/Area_Under_Curve.Rd0000644000176200001440000000141314602420006016355 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Utils.R \name{Area_Under_Curve} \alias{Area_Under_Curve} \title{Calculate the Area Under the Curve} \usage{ Area_Under_Curve(x, y, method = c("trapezoid", "step", "spline"), na.rm = FALSE) } \arguments{ \item{x}{the x-points of the curve} \item{y}{the y-points of the curve} \item{method}{can be "trapezoid" (default), "step" or "spline"} \item{na.rm}{a logical value indicating whether NA values should be stripped before the computation proceeds} } \value{ Area Under the Curve (AUC) } \description{ Calculate the area under the curve. } \examples{ x <- seq(0, pi, length.out = 200) plot(x = x, y = sin(x), type = "l") Area_Under_Curve(x = x, y = sin(x), method = "trapezoid", na.rm = TRUE) } MLmetrics/man/GainAUC.Rd0000644000176200001440000000120014602420006014405 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/Classification.R \name{GainAUC} \alias{GainAUC} \title{Area Under the Gain Chart} \usage{ GainAUC(y_pred, y_true) } \arguments{ \item{y_pred}{Predicted probabilities vector, as returned by a classifier} \item{y_true}{Ground truth (correct) 0-1 labels vector} } \value{ Area Under the Gain Chart } \description{ Compute the Area Under the Gain Chart from prediction scores. } \examples{ data(cars) logreg <- glm(formula = vs ~ hp + wt, family = binomial(link = "logit"), data = mtcars) GainAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) } MLmetrics/DESCRIPTION0000644000176200001440000000136114606615055013670 0ustar liggesusersPackage: MLmetrics Type: Package Title: Machine Learning Evaluation Metrics Version: 1.1.3 Authors@R: person("Yachen", "Yan", email = "yanyachen21@gmail.com", role = c("aut", "cre")) Description: A collection of evaluation metrics, including loss, score and utility functions, that measure regression, classification and ranking performance. URL: https://github.com/yanyachen/MLmetrics BugReports: https://github.com/yanyachen/MLmetrics/issues Depends: R (>= 2.10) Imports: stats, utils, ROCR Suggests: e1071 License: GPL-2 RoxygenNote: 5.0.1 NeedsCompilation: no Packaged: 2024-04-01 02:06:00 UTC; Administrator Author: Yachen Yan [aut, cre] Maintainer: Yachen Yan Repository: CRAN Date/Publication: 2024-04-13 23:50:05 UTC MLmetrics/R/0000755000176200001440000000000014606615055012362 5ustar liggesusersMLmetrics/R/Utils.R0000644000176200001440000000261714602420006013575 0ustar liggesusers#' @title Calculate the Area Under the Curve #' #' @description #' Calculate the area under the curve. #' #' @param x the x-points of the curve #' @param y the y-points of the curve #' @param method can be "trapezoid" (default), "step" or "spline" #' @param na.rm a logical value indicating whether NA values should be stripped before the computation proceeds #' @return Area Under the Curve (AUC) #' @examples #' x <- seq(0, pi, length.out = 200) #' plot(x = x, y = sin(x), type = "l") #' Area_Under_Curve(x = x, y = sin(x), method = "trapezoid", na.rm = TRUE) #' @importFrom stats integrate #' @importFrom stats na.omit #' @importFrom stats splinefun #' @export Area_Under_Curve <- function(x, y, method = c("trapezoid", "step", "spline"), na.rm = FALSE) { if (na.rm == TRUE) { xy_cbind <- na.omit(cbind(x, y)) x <- xy_cbind[, 1] y <- xy_cbind[, 2] } if (length(x) != length(y)) { stop("length x must equal length y") } idx <- order(x) x <- x[idx] y <- y[idx] switch(match.arg(arg = method, choices = c("trapezoid", "step","spline")), trapezoid = { AUC <- sum((apply(cbind(y[-length(y)], y[-1]), 1, mean)) *(x[-1] - x[-length(x)]))}, step = { AUC <- sum(y[-length(y)] * (x[-1] - x[-length(x)]))}, spline = { AUC <- integrate(splinefun(x, y, method = "natural"), lower = min(x),upper = max(x))$value}) return(AUC) } MLmetrics/R/MLmetrics.R0000644000176200001440000000036314602420006014370 0ustar liggesusers#' MLmetrics: Machine Learning Evaluation Metrics #' #' A collection of evaluation metrics, including loss, score and utility functions, #' that measure regression and classification performance. #' #' @docType package #' @name MLmetrics NULLMLmetrics/R/Rank.R0000644000176200001440000000000014602420006013350 0ustar liggesusersMLmetrics/R/Classification.R0000644000176200001440000003607014602420006015430 0ustar liggesusers#' @title Normalized Zero-One Loss (Classification Error Loss) #' #' @description #' Compute the normalized zero-one classification loss. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Zero-One Loss #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' ZeroOneLoss(y_pred = pred, y_true = mtcars$vs) #' @export ZeroOneLoss <- function(y_pred, y_true) { ZeroOneLoss <- mean(y_true != y_pred) return(ZeroOneLoss) } #' @title Accuracy #' #' @description #' Compute the accuracy classification score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Accuracy #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' Accuracy(y_pred = pred, y_true = mtcars$vs) #' @export Accuracy <- function(y_pred, y_true) { Accuracy <- mean(y_true == y_pred) return(Accuracy) } #' @title Confusion Matrix #' #' @description #' Compute confusion matrix to evaluate the accuracy of a classification. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return a table of Confusion Matrix #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' ConfusionMatrix(y_pred = pred, y_true = mtcars$vs) #' @export ConfusionMatrix <- function(y_pred, y_true) { Confusion_Mat <- table(y_true, y_pred) return(Confusion_Mat) } #' @title Confusion Matrix (Data Frame Format) #' #' @description #' Compute data frame format confusion matrix for internal usage. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return a data.frame of Confusion Matrix #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' ConfusionDF(y_pred = pred, y_true = mtcars$vs) #' @keywords internal #' @export ConfusionDF <- function(y_pred, y_true) { Confusion_DF <- transform(as.data.frame(ConfusionMatrix(y_pred, y_true)), y_true = as.character(y_true), y_pred = as.character(y_pred), Freq = as.integer(Freq)) return(Confusion_DF) } utils::globalVariables("Freq") #' @title Precision #' #' @description #' Compute the precision score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @return Precision #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' Precision(y_pred = pred, y_true = mtcars$vs, positive = "0") #' Precision(y_pred = pred, y_true = mtcars$vs, positive = "1") #' @export Precision <- function(y_true, y_pred, positive = NULL) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) TP <- as.integer(subset(Confusion_DF, y_true==positive & y_pred==positive)["Freq"]) FP <- as.integer(sum(subset(Confusion_DF, y_true!=positive & y_pred==positive)["Freq"])) Precision <- TP/(TP+FP) return(Precision) } #' @title Recall #' #' @description #' Compute the recall score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @return Recall #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' Recall(y_pred = pred, y_true = mtcars$vs, positive = "0") #' Recall(y_pred = pred, y_true = mtcars$vs, positive = "1") #' @export Recall <- function(y_true, y_pred, positive = NULL) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) TP <- as.integer(subset(Confusion_DF, y_true==positive & y_pred==positive)["Freq"]) FN <- as.integer(sum(subset(Confusion_DF, y_true==positive & y_pred!=positive)["Freq"])) Recall <- TP/(TP+FN) return(Recall) } #' @title Sensitivity #' #' @description #' Compute the sensitivity score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @return Sensitivity #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' Sensitivity(y_pred = pred, y_true = mtcars$vs, positive = "0") #' Sensitivity(y_pred = pred, y_true = mtcars$vs, positive = "1") #' @export Sensitivity <- function(y_true, y_pred, positive = NULL) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) TP <- as.integer(subset(Confusion_DF, y_true==positive & y_pred==positive)["Freq"]) FN <- as.integer(sum(subset(Confusion_DF, y_true==positive & y_pred!=positive)["Freq"])) Sensitivity <- TP/(TP+FN) return(Sensitivity) } #' @title Specificity #' #' @description #' Compute the specificity score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @return Specificity #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' Specificity(y_pred = pred, y_true = mtcars$vs, positive = "0") #' Specificity(y_pred = pred, y_true = mtcars$vs, positive = "1") #' @export Specificity <- function(y_true, y_pred, positive = NULL) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) TN <- as.integer(subset(Confusion_DF, y_true!=positive & y_pred!=positive)["Freq"]) FP <- as.integer(sum(subset(Confusion_DF, y_true!=positive & y_pred==positive)["Freq"])) Specificity <- TN/(TN+FP) return(Specificity) } #' @title F1 Score #' #' @description #' Compute the F1 Score. #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @return F1 Score #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' F1_Score(y_pred = pred, y_true = mtcars$vs, positive = "0") #' F1_Score(y_pred = pred, y_true = mtcars$vs, positive = "1") #' @export F1_Score <- function(y_true, y_pred, positive = NULL) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) Precision <- Precision(y_true, y_pred, positive) Recall <- Recall(y_true, y_pred, positive) F1_Score <- 2 * (Precision * Recall) / (Precision + Recall) return(F1_Score) } #' @title F-Beta Score #' #' @description #' Compute the F-Beta Score #' #' @param y_pred Predicted labels vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @param positive An optional character string for the factor level that #' corresponds to a "positive" result #' @param beta Weight of precision in harmonic mean #' @return F-Beta Score #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' pred <- ifelse(logreg$fitted.values < 0.5, 0, 1) #' FBeta_Score(y_pred = pred, y_true = mtcars$vs, positive = "0", beta = 2) #' FBeta_Score(y_pred = pred, y_true = mtcars$vs, positive = "1", beta = 2) #' @export FBeta_Score <- function(y_true, y_pred, positive = NULL, beta = 1) { Confusion_DF <- ConfusionDF(y_pred, y_true) if (is.null(positive) == TRUE) positive <- as.character(Confusion_DF[1,1]) Precision <- Precision(y_true, y_pred, positive) Recall <- Recall(y_true, y_pred, positive) Fbeta_Score <- (1 + beta^2) * (Precision * Recall) / (beta^2 * Precision + Recall) return(Fbeta_Score) } #' @title Log loss / Cross-Entropy Loss #' #' @description #' Compute the log loss/cross-entropy loss. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Log loss/Cross-Entropy Loss #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' LogLoss(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export LogLoss <- function(y_pred, y_true) { eps <- 1e-15 y_pred <- pmax(pmin(y_pred, 1 - eps), eps) LogLoss <- -mean(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) return(LogLoss) } #' @title Multi Class Log Loss #' #' @description #' Compute the multi class log loss. #' #' @param y_true Ground truth (correct) labels vector or a matrix of #' correct labels indicating by 0-1, same format as probabilities matrix #' @param y_pred Predicted probabilities matrix, as returned by a classifier #' @return Multi Class Log Loss #' @examples #' data(iris) #' svm.model <- e1071::svm(Species~., data = iris, probability = TRUE) #' pred <- predict(svm.model, iris, probability = TRUE) #' MultiLogLoss(y_true = iris$Species, y_pred = attr(pred, "probabilities")) #' @importFrom stats model.matrix #' @export MultiLogLoss <- function(y_pred, y_true) { if (is.matrix(y_true) == FALSE) { y_true <- model.matrix(~ 0 + ., data.frame(as.character(y_true))) } eps <- 1e-15 N <- nrow(y_pred) y_pred <- pmax(pmin(y_pred, 1 - eps), eps) MultiLogLoss <- (-1 / N) * sum(y_true * log(y_pred)) return(MultiLogLoss) } #' @title Area Under the Receiver Operating Characteristic Curve (ROC AUC) #' #' @description #' Compute the Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Area Under the ROC Curve (ROC AUC) #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' AUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export AUC <- function(y_pred, y_true) { rank <- rank(y_pred) n_pos <- as.double(sum(y_true == 1)) n_neg <- as.double(sum(y_true == 0)) AUC <- (sum(rank[y_true == 1]) - n_pos * (n_pos + 1) / 2) / (n_pos * n_neg) return(AUC) } #' @title Gini Coefficient #' #' @description #' Compute the Gini Coefficient. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Gini Coefficient #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export Gini <- function(y_pred, y_true) { NormalizedGini(y_pred, y_true) } #' @title Area Under the Precision-Recall Curve (PR AUC) #' #' @description #' Compute the Area Under the Precision-Recall Curve (PR AUC) from prediction scores. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Area Under the PR Curve (PR AUC) #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' PRAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export PRAUC <- function(y_pred, y_true) { pred_obj <- ROCR::prediction(y_pred, y_true) perf_obj <- ROCR::performance(pred_obj, measure = "prec", x.measure = "rec") PRAUC <- Area_Under_Curve(perf_obj@x.values[[1]], perf_obj@y.values[[1]], method = "trapezoid", na.rm = TRUE) return(PRAUC) } #' @title Area Under the Lift Chart #' #' @description #' Compute the Area Under the Lift Chart from prediction scores. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Area Under the Lift Chart #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' LiftAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export LiftAUC <- function(y_pred, y_true) { pred_obj <- ROCR::prediction(y_pred, y_true) perf_obj <- ROCR::performance(pred_obj, measure = "lift", x.measure = "rpp") LiftAUC <- Area_Under_Curve(perf_obj@x.values[[1]], perf_obj@y.values[[1]], method = "trapezoid", na.rm = TRUE) return(LiftAUC) } #' @title Area Under the Gain Chart #' #' @description #' Compute the Area Under the Gain Chart from prediction scores. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Area Under the Gain Chart #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' GainAUC(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export GainAUC <- function(y_pred, y_true) { pred_obj <- ROCR::prediction(y_pred, y_true) perf_obj <- ROCR::performance(pred_obj, measure = "tpr", x.measure = "rpp") GainAUC <- Area_Under_Curve(perf_obj@x.values[[1]], perf_obj@y.values[[1]], method = "trapezoid", na.rm = TRUE) return(GainAUC) } #' @title Kolmogorov-Smirnov Statistic #' #' @description #' Compute the Kolmogorov-Smirnov statistic. #' #' @param y_pred Predicted probabilities vector, as returned by a classifier #' @param y_true Ground truth (correct) 0-1 labels vector #' @return Kolmogorov-Smirnov statistic #' @examples #' data(cars) #' logreg <- glm(formula = vs ~ hp + wt, #' family = binomial(link = "logit"), data = mtcars) #' KS_Stat(y_pred = logreg$fitted.values, y_true = mtcars$vs) #' @export KS_Stat <- function(y_pred, y_true) { pred_obj <- ROCR::prediction(y_pred, y_true) perf_obj <- ROCR::performance(pred_obj, measure = "tpr", x.measure = "fpr") KS_Stat <- max(perf_obj@y.values[[1]] - perf_obj@x.values[[1]]) return(KS_Stat) } MLmetrics/R/Regression.R0000644000176200001440000001352414602420006014614 0ustar liggesusers#' @title Mean Square Error Loss #' #' @description #' Compute the mean squared error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Mean Square Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' MSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export MSE <- function(y_pred, y_true) { MSE <- mean((y_true - y_pred)^2) return(MSE) } #' @title Root Mean Square Error Loss #' #' @description #' Compute the root mean squared error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Root Mean Square Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' RMSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export RMSE <- function(y_pred, y_true) { RMSE <- sqrt(mean((y_true - y_pred)^2)) return(RMSE) } #' @title Root Mean Squared Logarithmic Error Loss #' #' @description #' Compute the root mean squared logarithmic error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Root Mean Squared Logarithmic Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' RMSLE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export RMSLE <- function(y_pred, y_true) { RMSLE <- sqrt(mean((log(1 + y_true) - log(1 + y_pred))^2)) return(RMSLE) } #' @title Root Mean Square Percentage Error Loss #' #' @description #' Compute the root mean squared percentage error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Root Mean Squared Percentage Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' RMSPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export RMSPE <- function(y_pred, y_true) { RMSPE <- sqrt(mean(((y_true - y_pred) / y_true)^2)) return(RMSPE) } #' @title Root Relative Squared Error Loss #' #' @description #' Compute the root relative squared error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Root Relative Squared Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' RRSE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export RRSE <- function(y_pred, y_true) { RRSE <- sqrt(sum((y_true - y_pred)^2) / sum((y_true - mean(y_true))^2)) return(RRSE) } #' @title Mean Absolute Error Loss #' #' @description #' Compute the mean absolute error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Mean Absolute Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' MAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export MAE <- function(y_pred, y_true) { MAE <- mean(abs(y_true - y_pred)) return(MAE) } #' @title Mean Absolute Percentage Error Loss #' #' @description #' Compute the mean absolute percentage error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Mean Absolute Percentage Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' MAPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export MAPE <- function(y_pred, y_true) { MAPE <- mean(abs((y_true - y_pred) / y_true)) return(MAPE) } #' @title Median Absolute Error Loss #' #' @description #' Compute the median absolute error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Median Absolute Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' MedianAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @importFrom stats median #' @export MedianAE <- function(y_pred, y_true) { MedianAE <- median(abs(y_true - y_pred)) return(MedianAE) } #' @title Median Absolute Percentage Error Loss #' #' @description #' Compute the Median absolute percentage error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Median Absolute Percentage Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' MedianAPE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @importFrom stats median #' @export MedianAPE <- function(y_pred, y_true) { MedianAPE <- median(abs((y_true - y_pred) / y_true)) return(MedianAPE) } #' @title Relative Absolute Error Loss #' #' @description #' Compute the relative absolute error regression loss. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return Relative Absolute Error Loss #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' RAE(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export RAE <- function(y_pred, y_true) { RAE <- sum(abs(y_true - y_pred)) / sum(abs(y_true - mean(y_true))) return(RAE) } #' @title R-Squared (Coefficient of Determination) Regression Score #' #' @description #' Compute the R-Squared (Coefficient of Determination) Regression Score. #' #' @param y_pred Estimated target values vector #' @param y_true Ground truth (correct) target values vector #' @return R^2 Score #' @examples #' data(cars) #' reg <- lm(log(dist) ~ log(speed), data = cars) #' R2_Score(y_pred = exp(reg$fitted.values), y_true = cars$dist) #' @export R2_Score <- function(y_pred, y_true) { R2_Score <- 1 - sum((y_true - y_pred)^2) / sum((y_true - mean(y_true))^2) return(R2_Score) } MLmetrics/R/Count.R0000644000176200001440000000354514602420006013566 0ustar liggesusers#' @title Poisson Log loss #' #' @description #' Compute the log loss/cross-entropy loss. #' #' @param y_pred Predicted labels vector, as returned by a model #' @param y_true Ground truth (correct) labels vector #' @return Log loss/Cross-Entropy Loss #' @examples #' d_AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), #' counts = c(18,17,15,20,10,20,25,13,12)) #' glm_poisson <- glm(counts ~ outcome + treatment, #' family = poisson(link = "log"), data = d_AD) #' Poisson_LogLoss(y_pred = glm_poisson$fitted.values, y_true = d_AD$counts) #' @export Poisson_LogLoss <- function(y_pred, y_true) { eps <- 1e-15 y_pred <- pmax(y_pred, eps) Poisson_LogLoss <- mean(log(gamma(y_true + 1)) + y_pred - log(y_pred) * y_true) # Poisson_LogLoss <- mean(-dpois(y_true, y_pred, log = TRUE)) return(Poisson_LogLoss) } #' @title Normalized Gini Coefficient #' #' @description #' Compute the Normalized Gini Coefficient. #' #' @param y_pred Predicted labels vector, as returned by a model #' @param y_true Ground truth (correct) labels vector #' @return Normalized Gini Coefficient #' @examples #' d_AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), #' counts = c(18,17,15,20,10,20,25,13,12)) #' glm_poisson <- glm(counts ~ outcome + treatment, #' family = poisson(link = "log"), data = d_AD) #' NormalizedGini(y_pred = glm_poisson$fitted.values, y_true = d_AD$counts) #' @export NormalizedGini <- function(y_pred, y_true) { SumGini <- function(y_pred, y_true) { y_true_sort <- y_true[order(y_pred, decreasing = TRUE)] y_random <- 1:length(y_pred) / length(y_pred) y_Lorentz <- cumsum(y_true_sort) / sum(y_true_sort) SumGini <- sum(y_Lorentz - y_random) return(SumGini) } NormalizedGini <- SumGini(y_pred, y_true) / SumGini(y_true, y_true) return(NormalizedGini) } MLmetrics/MD50000644000176200001440000000374414606615055012501 0ustar liggesusers1c08d4dd4a382eb19d4aa0f9e1cd4ac9 *DESCRIPTION e0b9b74682f61decb99a4452d101ebb0 *NAMESPACE e0b3646d1b80fb40ba08501dbfaa2b1f *R/Classification.R 718bea1b0432c1b9cbde5aea215e00e0 *R/Count.R bd3a7fa24cbb5adc76ccb234dd554421 *R/MLmetrics.R d41d8cd98f00b204e9800998ecf8427e *R/Rank.R d159f10f52c084c2bc9c582cebb11bbe *R/Regression.R 6e6a1f5040002beb219a1757430a1fe0 *R/Utils.R 43ae2d7624de4f6fffe8ab8ee00fbf97 *man/AUC.Rd 17c74b22bc251573b7b18dfe491c8445 *man/Accuracy.Rd 27f4807d29eebe1c92db3bd45a188e1b *man/Area_Under_Curve.Rd 8b215629bce5ff521f104228cdceffd3 *man/ConfusionDF.Rd 9639dfc010f8d7f79828a059449ac2e7 *man/ConfusionMatrix.Rd 5a87f9fbda7b9a442b6572c8bec5e0af *man/F1_Score.Rd 7dde86ea0ee64d28380f95ad9bf40b38 *man/FBeta_Score.Rd 0f21045c23029e5f5e9e2ce30e3be9e1 *man/GainAUC.Rd c62d306de4441cd98772dbe1aa89b52b *man/Gini.Rd 6a469a0a3c6621deed1f9f1569d27846 *man/KS_Stat.Rd 4e1c5857b7958334143827f9cdf8bd17 *man/LiftAUC.Rd d2fd2e2061317e475bc1786d2e5e886a *man/LogLoss.Rd 99761ad2791182f03d5e3f480cccfd74 *man/MAE.Rd e0e88620a3afd0de235a051bfe9d6c17 *man/MAPE.Rd a33f449248f5dc1d896225609fb454f3 *man/MLmetrics.Rd 2bc0da63b31d445aa8f1e9adbb3f3312 *man/MSE.Rd 1ad0f1db9b8eda80442ef8cde0cc24bb *man/MedianAE.Rd 7aded3101d9229dc6a753a98e2c396a0 *man/MedianAPE.Rd c686a5b8e0dca93a836e1277f0b8af66 *man/MultiLogLoss.Rd 8b53f81229a536426a6202faa580fb6d *man/NormalizedGini.Rd b57adf6fdb830f1c3d225386241b31f7 *man/PRAUC.Rd 08d3db4c802e09072877b49148b9ccc2 *man/Poisson_LogLoss.Rd 07145bcb05489a34f88671668120dc35 *man/Precision.Rd 79bea7f0d124bbc2b4f18fa254890024 *man/R2_Score.Rd 44f0bfc9f591201325f5cf3783715813 *man/RAE.Rd ad0b176c24e54d38f0d171e1333f77df *man/RMSE.Rd 74d81199bd3ab5ad9f0cb140787b6deb *man/RMSLE.Rd f76c3852d6c7103e38607b8b1068fba2 *man/RMSPE.Rd 2fbec8f4a83dcdcb1aaef9127b5d89c8 *man/RRSE.Rd a7d6b8d7a4a6b0792b1574dd67505b18 *man/Recall.Rd 87982d7aa2ebc092fd5b95645e1c0f1f *man/Sensitivity.Rd bd31d298a00baa433d8d5d9731a1cdd4 *man/Specificity.Rd a90c6a9edce238e603f1aa74b40ced03 *man/ZeroOneLoss.Rd