{
  "id": 23363,
  "title": "compute mcc in R",
  "url": "/competitions/bosch-production-line-performance/discussion/23363",
  "author_name": "",
  "post_date": "2016-09-05T00:06:53.550Z",
  "votes": 2,
  "comment_count": 3,
  "views": 1461,
  "content": "<p>Just found out how to calculate mcc in r and how to find the best split to optimize mcc value.</p>\n\n<pre><code>library(ROCR)\ndata(ROCR.simple)\npred &lt;- prediction( ROCR.simple$predictions, ROCR.simple$labels)\nperf2 &lt;- performance(pred, &quot;mat&quot;)\nplot(perf2)\nbest_split = perf2@x.values[[1]][which.max(perf2@y.values[[1]])]\n</code></pre>\n\n<p>Hope it helps. Please correct me if I am wrong.</p>",
  "messages": [
    {
      "id": "134074",
      "postDate": "09/05/2016 00:06:53",
      "content": "<p>Just found out how to calculate mcc in r and how to find the best split to optimize mcc value.</p>\n\n<pre><code>library(ROCR)\ndata(ROCR.simple)\npred &lt;- prediction( ROCR.simple$predictions, ROCR.simple$labels)\nperf2 &lt;- performance(pred, &quot;mat&quot;)\nplot(perf2)\nbest_split = perf2@x.values[[1]][which.max(perf2@y.values[[1]])]\n</code></pre>\n\n<p>Hope it helps. Please correct me if I am wrong.</p>",
      "rawMarkdown": "Just found out how to calculate mcc in r and how to find the best split to optimize mcc value.\r\n\r\n    library(ROCR)\r\n    data(ROCR.simple)\r\n    pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels)\r\n    perf2 <- performance(pred, \"mat\")\r\n    plot(perf2)\r\n    best_split = perf2@x.values[[1]][which.max(perf2@y.values[[1]])]\r\n\r\nHope it helps. Please correct me if I am wrong.",
      "votes": null
    },
    {
      "id": "134085",
      "postDate": "09/05/2016 01:36:26",
      "content": "<p>Hi xyz,</p>\n\n<p>Very nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\n  return(mcc)\n}\n</code></pre>",
      "rawMarkdown": "Hi xyz,\r\n\r\nVery nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.\r\n\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      \r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\r\n      return(mcc)\r\n    }",
      "votes": null
    },
    {
      "id": "134208",
      "postDate": "09/05/2016 19:20:04",
      "content": "<p>[quote=JeffH;134085]</p>\n\n<p>Hi xyz,</p>\n\n<p>Very nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\n  return(mcc)\n}\n</code></pre>\n\n<p>[/quote]</p>\n\n<p>Hey Jeff, thanks. Did you try to customize the loss function when using xgboost? </p>",
      "rawMarkdown": "[quote=JeffH;134085]\r\n\r\nHi xyz,\r\n\r\nVery nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.\r\n\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      \r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\r\n      return(mcc)\r\n    }\r\n\r\n\r\n\r\n[/quote]\r\n\r\n\r\nHey Jeff, thanks. Did you try to customize the loss function when using xgboost?",
      "votes": null
    },
    {
      "id": "139524",
      "postDate": "10/14/2016 17:08:35",
      "content": "<p>Added fixes for defects that surfaced.  </p>\n\n<p>Regards,</p>\n\n<p>ga</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  # Geoffrey Anderson 10/14/2016 \n  # Added zero denominator handling.\n  # Avoided overflow error on large-ish products in denominator.\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n  #TP;TN;FP;FN # for debugging\n  sum1 &lt;- TP+FP; sum2 &lt;-TP+FN ; sum3 &lt;-TN+FP ; sum4 &lt;- TN+FN;\n  denom &lt;- as.double(sum1)*sum2*sum3*sum4 # as.double to avoid overflow error on large products\n  if (any(sum1==0, sum2==0, sum3==0, sum4==0)) {\n    denom &lt;- 1\n  }\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt(denom)\n  return(mcc)\n}\n</code></pre>",
      "rawMarkdown": "Added fixes for defects that surfaced.  \r\n\r\nRegards,\r\n\r\nga\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      # Geoffrey Anderson 10/14/2016 \r\n      # Added zero denominator handling.\r\n      # Avoided overflow error on large-ish products in denominator.\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      #TP;TN;FP;FN # for debugging\r\n      sum1 <- TP+FP; sum2 <-TP+FN ; sum3 <-TN+FP ; sum4 <- TN+FN;\r\n      denom <- as.double(sum1)*sum2*sum3*sum4 # as.double to avoid overflow error on large products\r\n      if (any(sum1==0, sum2==0, sum3==0, sum4==0)) {\r\n        denom <- 1\r\n      }\r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt(denom)\r\n      return(mcc)\r\n    }",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 134085,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "09/05/2016 01:36:26",
      "content": "<p>Hi xyz,</p>\n\n<p>Very nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\n  return(mcc)\n}\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 134208,
      "author_name": "zhugds",
      "author_url": "",
      "post_date": "09/05/2016 19:20:04",
      "content": "<p>[quote=JeffH;134085]</p>\n\n<p>Hi xyz,</p>\n\n<p>Very nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\n  return(mcc)\n}\n</code></pre>\n\n<p>[/quote]</p>\n\n<p>Hey Jeff, thanks. Did you try to customize the loss function when using xgboost? </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 139524,
      "author_name": "ganderson",
      "author_url": "",
      "post_date": "10/14/2016 17:08:35",
      "content": "<p>Added fixes for defects that surfaced.  </p>\n\n<p>Regards,</p>\n\n<p>ga</p>\n\n<pre><code>mcc &lt;- function (actual, predicted)\n{\n  # Compute the Matthews correlation coefficient (MCC) score\n  # Jeff Hebert 9/1/2016\n  # Geoffrey Anderson 10/14/2016 \n  # Added zero denominator handling.\n  # Avoided overflow error on large-ish products in denominator.\n  #\n  # actual = vector of true outcomes, 1 = Positive, 0 = Negative\n  # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\n  # function returns MCC\n\n  TP &lt;- sum(actual == 1 &amp; predicted == 1)\n  TN &lt;- sum(actual == 0 &amp; predicted == 0)\n  FP &lt;- sum(actual == 0 &amp; predicted == 1)\n  FN &lt;- sum(actual == 1 &amp; predicted == 0)\n  #TP;TN;FP;FN # for debugging\n  sum1 &lt;- TP+FP; sum2 &lt;-TP+FN ; sum3 &lt;-TN+FP ; sum4 &lt;- TN+FN;\n  denom &lt;- as.double(sum1)*sum2*sum3*sum4 # as.double to avoid overflow error on large products\n  if (any(sum1==0, sum2==0, sum3==0, sum4==0)) {\n    denom &lt;- 1\n  }\n  mcc &lt;- ((TP*TN)-(FP*FN)) / sqrt(denom)\n  return(mcc)\n}\n</code></pre>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "134074": "Just found out how to calculate mcc in r and how to find the best split to optimize mcc value.\r\n\r\n    library(ROCR)\r\n    data(ROCR.simple)\r\n    pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels)\r\n    perf2 <- performance(pred, \"mat\")\r\n    plot(perf2)\r\n    best_split = perf2@x.values[[1]][which.max(perf2@y.values[[1]])]\r\n\r\nHope it helps. Please correct me if I am wrong.",
    "134085": "Hi xyz,\r\n\r\nVery nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.\r\n\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      \r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\r\n      return(mcc)\r\n    }",
    "134208": "[quote=JeffH;134085]\r\n\r\nHi xyz,\r\n\r\nVery nice. I've been using my own custom metric and letting the model determine cutoff. I get the same results as the ROCR calculation. The graph is a nice touch to help determine if the model is stable. I'll have to experiment with this.\r\n\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      \r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))\r\n      return(mcc)\r\n    }\r\n\r\n\r\n\r\n[/quote]\r\n\r\n\r\nHey Jeff, thanks. Did you try to customize the loss function when using xgboost?",
    "139524": "Added fixes for defects that surfaced.  \r\n\r\nRegards,\r\n\r\nga\r\n\r\n    mcc <- function (actual, predicted)\r\n    {\r\n      # Compute the Matthews correlation coefficient (MCC) score\r\n      # Jeff Hebert 9/1/2016\r\n      # Geoffrey Anderson 10/14/2016 \r\n      # Added zero denominator handling.\r\n      # Avoided overflow error on large-ish products in denominator.\r\n      #\r\n      # actual = vector of true outcomes, 1 = Positive, 0 = Negative\r\n      # predicted = vector of predicted outcomes, 1 = Positive, 0 = Negative\r\n      # function returns MCC\r\n    \r\n      TP <- sum(actual == 1 & predicted == 1)\r\n      TN <- sum(actual == 0 & predicted == 0)\r\n      FP <- sum(actual == 0 & predicted == 1)\r\n      FN <- sum(actual == 1 & predicted == 0)\r\n      #TP;TN;FP;FN # for debugging\r\n      sum1 <- TP+FP; sum2 <-TP+FN ; sum3 <-TN+FP ; sum4 <- TN+FN;\r\n      denom <- as.double(sum1)*sum2*sum3*sum4 # as.double to avoid overflow error on large products\r\n      if (any(sum1==0, sum2==0, sum3==0, sum4==0)) {\r\n        denom <- 1\r\n      }\r\n      mcc <- ((TP*TN)-(FP*FN)) / sqrt(denom)\r\n      return(mcc)\r\n    }"
  },
  "source": "meta"
}