{"action":"created","issue":{"url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/14","repository_url":"https://api.github.com/repos/JackStat/ModelMetrics","labels_url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/14/comments","events_url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/14/events","html_url":"https://github.com/JackStat/ModelMetrics/issues/14","id":192006521,"number":14,"title":"Suggestion for improvement of the auc function","user":{"login":"studerus","id":373643,"avatar_url":"https://avatars.githubusercontent.com/u/373643?v=3","gravatar_id":"","url":"https://api.github.com/users/studerus","html_url":"https://github.com/studerus","followers_url":"https://api.github.com/users/studerus/followers","following_url":"https://api.github.com/users/studerus/following{/other_user}","gists_url":"https://api.github.com/users/studerus/gists{/gist_id}","starred_url":"https://api.github.com/users/studerus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/studerus/subscriptions","organizations_url":"https://api.github.com/users/studerus/orgs","repos_url":"https://api.github.com/users/studerus/repos","events_url":"https://api.github.com/users/studerus/events{/privacy}","received_events_url":"https://api.github.com/users/studerus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-28T13:31:09Z","updated_at":"2016-11-28T16:05:06Z","closed_at":null,"body":"I noticed that your version of `auc` is still about one third slower than the one I recently implemented in `mlr`. One bottleneck seems to be this line:\r\n```\r\nif(class(actual) %in% c('factor', 'character')){\r\n   actual = as.numeric(as.factor(as.character(actual))) - 1\r\n }\r\n```\r\n\r\nI suggest to replace it with\r\n```\r\nif (inherits(actual, 'factor')) {\r\n    actual <- as.integer(actual) - 1L\r\n  } else if (inherits(actual, 'character')) {\r\n    actual <- as.integer(as.factor(actual)) - 1L\r\n  }\r\n```\r\n\r\nSee the following benchmark:\r\n```\r\nlibrary(ModelMetrics)\r\nlibrary(mlr)\r\nlibrary(microbenchmark)\r\nlibrary(data.table)\r\nx <- c('Pos', 'Neg')\r\nactual <- sample(factor(x, x), 50000, replace = T)\r\npredicted <- runif(length(actual))\r\n\r\nauc3_ <- ModelMetrics:::auc3_\r\nbinaryChecks <- ModelMetrics:::binaryChecks\r\nauc2 <- function(actual, predicted, ...) {\r\n  binaryChecks(actual, 'auc')\r\n  if (inherits(actual, 'factor')) {\r\n    actual <- as.integer(actual) - 1L\r\n  } else if (inherits(actual, 'character')) {\r\n    actual <- as.integer(as.factor(actual)) - 1L\r\n  }\r\n  \r\n  if(length(actual > 10000)){\r\n    ranks = frankv(predicted)\r\n    AUC <- ModelMetrics:::auc3_(actual, predicted, ranks)\r\n  } else {\r\n    AUC <- auc_(actual, predicted, ranks)\r\n  }\r\n  return(AUC)\r\n}\r\n\r\nmicrobenchmark(mlr = measureAUC(predicted, actual, positive = 'Pos'),\r\n               modelmetrics = auc(actual, predicted),\r\n               modelmetrics.improved = auc2(actual, predicted))\r\n```\r\n```\r\nUnit: milliseconds\r\n                  expr      min       lq     mean   median       uq      max neval cld\r\n                   mlr 4.148332 4.236479 4.454990 4.305307 4.445979 6.128918   100 a  \r\n          modelmetrics 6.395471 6.496297 6.840453 6.631536 6.790019 9.250582   100   c\r\n modelmetrics.improved 4.480996 4.534126 4.836400 4.623480 4.722645 6.802999   100  b \r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/comments/263311649","html_url":"https://github.com/JackStat/ModelMetrics/issues/14#issuecomment-263311649","issue_url":"https://api.github.com/repos/JackStat/ModelMetrics/issues/14","id":263311649,"user":{"login":"JackStat","id":2999465,"avatar_url":"https://avatars.githubusercontent.com/u/2999465?v=3","gravatar_id":"","url":"https://api.github.com/users/JackStat","html_url":"https://github.com/JackStat","followers_url":"https://api.github.com/users/JackStat/followers","following_url":"https://api.github.com/users/JackStat/following{/other_user}","gists_url":"https://api.github.com/users/JackStat/gists{/gist_id}","starred_url":"https://api.github.com/users/JackStat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JackStat/subscriptions","organizations_url":"https://api.github.com/users/JackStat/orgs","repos_url":"https://api.github.com/users/JackStat/repos","events_url":"https://api.github.com/users/JackStat/events{/privacy}","received_events_url":"https://api.github.com/users/JackStat/received_events","type":"User","site_admin":false},"created_at":"2016-11-28T16:05:06Z","updated_at":"2016-11-28T16:05:06Z","body":"Thank you!"}}
{"actor":{"display_login":"JackStat"}}