-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added precision, recall, f1_score #20
Changes from 3 commits
a02e4b9
ef2ef97
f116842
f57c613
ea7f740
1c8ebee
16dd4e7
ac43891
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,3 +76,44 @@ ll <- function(actual, predicted) { | |
logLoss <- function(actual, predicted) { | ||
return(mean(ll(actual, predicted))) | ||
} | ||
|
||
|
||
|
||
#' Precision | ||
#' | ||
#' \code{precision} computes proportion of predicted 1's that are actual 1's | ||
#' @export | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add |
||
#' @examples | ||
#' actual <- c(1, 1, 1, 0, 0, 0) | ||
#' predicted <- c(1, 1, 1, 1, 1, 1) | ||
#' precision(actual, predicted) | ||
precision <- function(actual, predicted) { | ||
return(mean(actual[predicted == 1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use four spaces as the indentation? |
||
} | ||
|
||
#' Recall | ||
#' | ||
#' \code{recall} computes proportion of actual 1's that are predicted 1's | ||
#' @export | ||
#' @examples | ||
#' actual <- c(1, 1, 1, 0, 0, 0) | ||
#' predicted <- c(1, 0, 1, 1, 1, 1) | ||
#' recall(actual, predicted) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add all of the same documentation improvements that were listed for |
||
recall <- function(actual, predicted) { | ||
return(mean(predicted[actual == 1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. four spaces |
||
} | ||
|
||
#' F1 score | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be F-beta Score |
||
#' | ||
#' \code{f1_score} computes the f1 score | ||
#' @export | ||
#' @seealso \code{\link{precision}}, \code{\link{recall}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the comma here |
||
#' @examples | ||
#' actual <- c(1, 1, 1, 0, 0, 0) | ||
#' predicted <- c(1, 0, 1, 1, 1, 1) | ||
#' recall(actual, predicted) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same documentation improvements |
||
f1_score <- function(actual, predicted, beta = 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
prec = precision(actual, predicted) | ||
rec = recall(actual, predicted) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the assignment operator |
||
return((1 + beta^2) * prec * rec / ((beta^2 * prec) + rec)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,21 @@ test_that('mean los loss is calculated correctly', { | |
expect_equal(logLoss(c(1,1,1,0,0,0),c(.5,.1,.01,.9,.75,.001)), 1.881797068998267) | ||
}) | ||
|
||
test_that('precision is calculated correctly', { | ||
expect_equal(precision(c(1,1,0,0),c(1,1,0,0)), 1) | ||
expect_equal(precision(c(0,0,1,1),c(1,1,0,0)), 0) | ||
expect_equal(precision(c(1,1,0,0),c(1,1,1,1)), 1/2) | ||
}) | ||
|
||
test_that('recall is calculated correctly', { | ||
expect_equal(recall(c(1,1,0,0),c(1,1,0,0)), 1) | ||
expect_equal(recall(c(0,0,1,1),c(1,1,0,0)), 0) | ||
expect_equal(recall(c(1,1,1,1),c(1,0,0,1)), 1/2) | ||
}) | ||
|
||
test_that('f1 score is calculated correctly',{ | ||
expect_equal(f1_score(c(1,1,0,0),c(1,1,0,0)), 1) | ||
expect_equal(f1_score(c(0,0,1,1),c(1,1,1,0)), 2/5) | ||
expect_equal(f1_score(c(1,1,1,1),c(1,0,0,1)), 2/3) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the unit tests! |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change this to, "computes proportion of observations predicted to be in the positive class (i.e. the element in \code{predicted} equals 1) that actually belong to the positive class (i.e. the element in \code{actual} equals 1)?
Thanks