{"id":5149,"date":"2025-07-14T07:42:55","date_gmt":"2025-07-14T06:42:55","guid":{"rendered":"https:\/\/pcool.dyndns.org\/?page_id=5149"},"modified":"2025-07-14T12:22:35","modified_gmt":"2025-07-14T11:22:35","slug":"prevalence-ppv-and-npv","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/prevalence-ppv-and-npv\/","title":{"rendered":"Prevalence, PPV and NPV"},"content":{"rendered":"\n<p>Prevalence, positive and negative predictive value change with different values for alpha (significance level) and beta (probability of making a type 2 error, 1 &#8211; power). This is illustrated below.<\/p>\n\n\n\n<p>First, define functions to calculate the positive and negative predictive value:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f60707\" class=\"has-inline-color\"># define functions\n\nppv = function(alpha = NA, beta = NA, prevalence = NA){\n\tresult = (1-beta)*prevalence \/ ((1-beta)*prevalence + alpha*(1-prevalence))\n\treturn(result)\n}\n\nnpv = function(alpha = NA, beta = NA, prevalence = NA){\n\tresult = (1-alpha)*(1-prevalence) \/ ( (1-alpha)*(1-prevalence) + beta*(prevalence))\n\treturn(result)\n}\n<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Create a data frame with values:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f40909\" class=\"has-inline-color\"># create dataframe with variable prevalence, alpha and beta\nprevalence = rep(c(0.1, 0.3, 0.5, 0.7, 0.9), 9)\nalpha = rep(c(rep(0.001,5), rep(0.01, 5), rep(0.05,5)), 3)\nbeta = c(rep(0.2, 15), rep(0.5, 15), rep(0.8, 15))\n\ndf = data.frame(prevalence, alpha, beta)\ndf$power = 1-df$beta\ndf$PPV = ppv(alpha=df$alpha, beta=df$beta, prevalence=df$prevalence)\ndf$NPV = npv(alpha=df$alpha, beta=df$beta, prevalence=df$prevalence)\nhead(df)\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#570af5\" class=\"has-inline-color\">  prevalence alpha beta power       PPV       NPV\n1        0.1 0.001  0.2   0.8 0.9888752 0.9782396\n2        0.3 0.001  0.2   0.8 0.9970918 0.9209798\n3        0.5 0.001  0.2   0.8 0.9987516 0.8331943\n4        0.7 0.001  0.2   0.8 0.9994646 0.6816011\n5        0.9 0.001  0.2   0.8 0.9998611 0.3569132\n6        0.1 0.010  0.2   0.8 0.8988764 0.9780461<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Convert to long format<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f50505\" class=\"has-inline-color\"># convert to long format:\ndf_long = pivot_longer(df, cols = c(PPV, NPV), names_to = 'rate', values_to = 'value')\ndf_long $rate = as.factor(df_long $rate)\ndf_long\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#6405f5\" class=\"has-inline-color\"># A tibble: 90 \u00d7 6\n   prevalence alpha  beta power rate  value\n        &lt;dbl> &lt;dbl> &lt;dbl> &lt;dbl> &lt;fct> &lt;dbl>\n 1        0.1 0.001   0.2   0.8 PPV   0.989\n 2        0.1 0.001   0.2   0.8 NPV   0.978\n 3        0.3 0.001   0.2   0.8 PPV   0.997\n 4        0.3 0.001   0.2   0.8 NPV   0.921\n 5        0.5 0.001   0.2   0.8 PPV   0.999\n 6        0.5 0.001   0.2   0.8 NPV   0.833\n 7        0.7 0.001   0.2   0.8 PPV   0.999\n 8        0.7 0.001   0.2   0.8 NPV   0.682\n 9        0.9 0.001   0.2   0.8 PPV   1.00 \n10        0.9 0.001   0.2   0.8 NPV   0.357\n# \u2139 80 more rows\n# \u2139 Use `print(n = ...)` to see more rows<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Show the plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ef0303\" class=\"has-inline-color\">df_long %>%\n\tggplot(aes(x=prevalence, y=value, colour=rate, shape=as.factor(beta))) +\n\tgeom_line() +\n\tgeom_hline(yintercept = 0.8, colour = 'steelblue', linetype = 'dashed', alpha = 0.5) +\n\tfacet_grid(alpha ~ beta, labeller = label_both) +\n\ttheme_bw() +\n\tscale_x_continuous('Prevalence') +\n\tscale_y_continuous('Value') +\n\tscale_colour_manual(values = c(\"tomato\", \"olivedrab\"), name = \"\") +\n\tggtitle(\"PPV and NPV for different values of alpha and beta\")\nggsave('\/path_to_file\/ppv_npv_plot.jpg')<\/mark><\/em>\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-1024x1024.jpg\" alt=\"\" class=\"wp-image-5207\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-1024x1024.jpg 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-300x300.jpg 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-150x150.jpg 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-768x768.jpg 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-1536x1536.jpg 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/07\/ppv_npv_plot-2048x2048.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">The dotted blue line indicates 80% power<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prevalence, positive and negative predictive value change with different values for alpha (significance level) and beta (probability of making a type 2 error, 1 &#8211; power). This is illustrated below. First, define functions to calculate the positive and negative predictive value: Create a data frame with values: Convert to long format Show the plot: The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-5149","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/5149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=5149"}],"version-history":[{"count":2,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/5149\/revisions"}],"predecessor-version":[{"id":5208,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/5149\/revisions\/5208"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=5149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}