Skip to content
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

Merged
merged 8 commits into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions R/binary_classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

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

#' @export
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add #' @seealso \code{\link{recall}} \code{\link{fbeta_score}}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add #' @inheritParams params_binary

#' @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]))
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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 precision?

recall <- function(actual, predicted) {
return(mean(predicted[actual == 1]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

four spaces

}

#' F1 score
Copy link
Owner

Choose a reason for hiding this comment

The 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}}
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same documentation improvements

f1_score <- function(actual, predicted, beta = 1) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fbeta_score

prec = precision(actual, predicted)
rec = recall(actual, predicted)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the assignment operator <- and use four spaces.

return((1 + beta^2) * prec * rec / ((beta^2 * prec) + rec))
}
18 changes: 18 additions & 0 deletions tests/testthat/test-binary_classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the unit tests!