Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php on line 71

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home1/mbithide/public_html/wp-content/plugins/astra-sites/admin/bsf-analytics/class-bsf-analytics-stats.php:71) in /home1/mbithide/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":2852,"date":"2023-10-18T10:58:21","date_gmt":"2023-10-18T10:58:21","guid":{"rendered":"https:\/\/mbithiguide.com\/?p=2852"},"modified":"2023-10-18T10:58:24","modified_gmt":"2023-10-18T10:58:24","slug":"how-to-plot-roc-curve-in-r","status":"publish","type":"post","link":"https:\/\/mbithiguide.com\/how-to-plot-roc-curve-in-r\/","title":{"rendered":"How to Plot ROC Curve in R"},"content":{"rendered":"\n

A Receiver Operating Characteristic (ROC)<\/a> curve is a graphical representation used to assess the performance of a binary classification model, such as logistic regression, support vector machines, or decision trees. It illustrates the trade-off between a model’s ability to correctly classify true positive instances and its tendency to incorrectly classify false positive instances as you vary the model’s classification threshold.<\/p>\n\n\n\n

The ROC curve<\/a> is created by plotting the True Positive Rate (TPR) against the False Positive Rate (FPR) at various threshold settings. Here are some key terms related to ROC curves:<\/p>\n\n\n\n

    \n
  1. True Positive Rate (TPR), Sensitivity, or Recall:<\/strong> This is the proportion of actual positive cases that the model correctly predicts as positive. It is calculated as:TPR = True Positives \/ (True Positives + False Negatives)In the context of a medical test, TPR is the percentage of people with the disease who test positive.<\/li>\n\n\n\n
  2. False Positive Rate (FPR):<\/strong> This is the proportion of actual negative cases that the model incorrectly predicts as positive. It is calculated as:FPR = False Positives \/ (False Positives + True Negatives). FPR is the percentage of healthy individuals who test positive in a medical test.<\/li>\n\n\n\n
  3. Threshold:<\/strong> A threshold is a value that the model uses to determine whether a given instance should be classified as positive or negative. Adjusting this threshold allows you to control the balance between TPR and FPR.<\/li>\n<\/ol>\n\n\n\n

    Step 1: Install and Load Required Packages<\/strong><\/h2>\n\n\n\n

    If you haven’t already installed the ROCR<\/code> package, you can do so using the install.packages<\/code> function:<\/p>\n\n\n\n

    install.packages(\"ROCR\")<\/pre>\n\n\n\n

    After installation, load the package:<\/p>\n\n\n\n

    library(ROCR)<\/pre>\n\n\n\n

    Step 2: Prepare the Data<\/strong><\/h2>\n\n\n\n

    I will use a hypothetical dataset and a simple logistic regression model for this example. You can replace this with your own data and model.<\/p>\n\n\n\n

    # Load sample data\ndata(ROCR.simple)\n\n# Extract true class labels and predicted probabilities\npredictions <- ROCR.simple$predictions\nlabels <- ROCR.simple$labels<\/pre>\n\n\n\n

    Here, predictions<\/code> are the predicted probabilities (scores) for the positive class, and labels<\/code> are the true binary labels (0 or 1).<\/p>\n\n\n\n

    Step 3: Create a Prediction Object<\/strong><\/h2>\n\n\n\n

    To create a prediction object that ROCR can work with, use the prediction<\/code> function:<\/p>\n\n\n\n

    pred <- prediction(predictions, labels)<\/pre>\n\n\n\n

    Step 4: Calculate ROC Curve Data<\/strong><\/h2>\n\n\n\n

    Use the performance<\/code> function to calculate the ROC curve data:<\/p>\n\n\n\n

    roc <- performance(pred, \"tpr\", \"fpr\")<\/pre>\n\n\n\n

    Step 5: Plot the ROC Curve<\/strong><\/h2>\n\n\n\n

    You can now create and customize the ROC curve plot using the plot<\/code> function:<\/p>\n\n\n\n

    plot(roc, main=\"ROC Curve\", colorize=TRUE, print.auc=TRUE)\nabline(a=0, b=1, lty=2, col=\"gray\")<\/pre>\n\n\n\n
      \n
    • main<\/code> sets the title of the plot.<\/li>\n\n\n\n
    • colorize<\/code> adds color to the plot.<\/li>\n\n\n\n
    • print.auc<\/code> displays the AUC (Area Under the Curve) on the plot.<\/li>\n\n\n\n
    • abline<\/code> adds a dashed reference line representing a random classifier.<\/li>\n<\/ul>\n\n\n\n
      \"How<\/figure>\n\n\n\n

      Step 6: Save the Plot (Optional)<\/strong><\/h2>\n\n\n\n

      If you want to save the plot to a file, you can use the pdf<\/code>, png<\/code>, or other plotting functions:<\/p>\n\n\n\n

      pdf(\"ROC_curve.pdf\")\nplot(roc, main=\"ROC Curve\", colorize=TRUE, print.auc=TRUE)\nabline(a=0, b=1, lty=2, col=\"gray\")\ndev.off()<\/pre>\n\n\n\n

      This code saves the ROC curve to a PDF file.<\/p>\n\n\n\n

      Your ROC curve plot should now be displayed on the screen (or saved as a file if you opted to do so). As you change the classification threshold, the curve will show the trade-off between true positive rate (sensitivity) and false positive rate (1 – specificity).<\/p>\n","protected":false},"excerpt":{"rendered":"

      A Receiver Operating Characteristic (ROC) curve is a graphical representation used to assess the performance of a binary classification model, such as logistic regression, support vector machines, or decision trees. It illustrates the trade-off between a model’s ability to correctly classify true positive instances and its tendency to incorrectly classify false positive instances as you […]<\/p>\n","protected":false},"author":1,"featured_media":2854,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2852","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-programming"],"uagb_featured_image_src":{"full":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"thumbnail":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5-150x150.png",150,150,true],"medium":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5-300x300.png",300,300,true],"medium_large":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"large":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"1536x1536":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"2048x2048":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"blogus-slider-full":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"blogus-featured":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",540,540,false],"blogus-medium":["https:\/\/mbithiguide.com\/wp-content\/uploads\/2023\/10\/rsz_simple_blg_post_5.png",380,380,false]},"uagb_author_info":{"display_name":"Benard Mbithi","author_link":"https:\/\/mbithiguide.com\/author\/benard-mbithi\/"},"uagb_comment_info":0,"uagb_excerpt":"A Receiver Operating Characteristic (ROC) curve is a graphical representation used to assess the performance of a binary classification model, such as logistic regression, support vector machines, or decision trees. It illustrates the trade-off between a model’s ability to correctly classify true positive instances and its tendency to incorrectly classify false positive instances as you…","_links":{"self":[{"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/posts\/2852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/comments?post=2852"}],"version-history":[{"count":4,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/posts\/2852\/revisions"}],"predecessor-version":[{"id":2858,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/posts\/2852\/revisions\/2858"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/media\/2854"}],"wp:attachment":[{"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/media?parent=2852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/categories?post=2852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mbithiguide.com\/wp-json\/wp\/v2\/tags?post=2852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}