{"id":292,"date":"2015-03-02T17:51:10","date_gmt":"2015-03-02T23:51:10","guid":{"rendered":"http:\/\/harrysurden.com\/wordpress\/?p=292"},"modified":"2015-05-04T15:49:11","modified_gmt":"2015-05-04T21:49:11","slug":"probability-tree-diagrams-in-r","status":"publish","type":"post","link":"https:\/\/www.harrysurden.com\/wordpress\/archives\/292","title":{"rendered":"Probability Tree Diagrams in R"},"content":{"rendered":"<p>As part of a\u00a0<a href=\"http:\/\/lawweb.colorado.edu\/courses\/courseSection.jsp?id=LAWS6813&term=20141#section_001\">Problem Solving Course<\/a>\u00a0that I teach, I have\u00a0several sessions\u00a0on probability theory. Given that attorneys must frequently make decisions in environments of\u00a0uncertainty, probability\u00a0can be a useful skill for law students to learn.<\/p><p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Conditional_probability\">Conditional probability<\/a>, and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bayes%27_theorem\">Bayes&#8217; Theorem<\/a>, are important sub-topics that I focus upon. \u00a0In teaching my\u00a0students about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Conditional_probability\">Conditional Probability<\/a>, it is often helpful\u00a0to create a <em>Conditional Probability Tree<\/em> diagram\u00a0like the one pictured below (sometimes called a probability tree). \u00a0I&#8217;ll explain in a future post why such a diagram\/graph\u00a0is a useful visualization for learners.<\/p><p>(See also this <a href=\"http:\/\/www.harrysurden.com\/wordpress\/archives\/581\">Javascript Conditional Probability Tree Diagram webpage that I created in that I describe<\/a> in a different\u00a0post.)<\/p>\n<h2 style=\"text-align: center;\"><strong>Conditional Probability\u00a0Tree\u00a0Diagram<\/strong><\/h2><p><a href=\"http:\/\/harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Conditioal-Probability-Tree-3.jpeg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-324 \" src=\"http:\/\/harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Conditioal-Probability-Tree-3.jpeg\" alt=\"Conditioal Probability Tree 3\" width=\"766\" height=\"433\" srcset=\"https:\/\/www.harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Conditioal-Probability-Tree-3.jpeg 864w, https:\/\/www.harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Conditioal-Probability-Tree-3-300x169.jpeg 300w, https:\/\/www.harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Conditioal-Probability-Tree-3-620x350.jpeg 620w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/a><\/p>\n<h2>No Probability Tree Diagrams in R ?<\/h2><p>Like many others, I use the popular free, and open-source\u00a0<a href=\"http:\/\/www.r-project.org\/\">R statistical programming language<\/a>.\u00a0 R is one of the top computing platforms in which to perform machine learning and other statistical tasks (along with <a href=\"https:\/\/www.python.org\/\">Python<\/a> &#8211; another favorite of mine). \u00a0To program in R, I use the excellent<a href=\"http:\/\/www.rstudio.com\/\"> R-Studio application<\/a> which makes the experience\u00a0much better.<\/p><p>Given the relationship between R and statistics, I was somewhat surprised that I was unable to find any easily accessible R code or functions to create visually appealing Conditional Probability Tree diagrams\u00a0like the one above.<\/p><p>Thus, I put together some basic R code below for visualizing conditional probability trees, using the\u00a0<a href=\"http:\/\/www.bioconductor.org\/packages\/release\/bioc\/html\/Rgraphviz.html\">Rgraphviz<\/a> R package. \u00a0You must install the Rgraphviz package before using the R code below. If you know of other ways to create visually appealing conditional probability tree in R that I may have missed in my search, please let me know.<\/p><p>I thought I&#8217;d release the\u00a0code below to others in case it is useful.<\/p><p>(Caveat: \u00a0This is rough code, and has not been thoroughly tested, and is just meant as a starting example to help make your own probability tree diagrams &#8211; so no guarantees).<\/p><p>(You can also look at this\u00a0<a href=\"http:\/\/harrysurden.com\/wordpress\/archives\/581\">other\u00a0post about\u00a0creating a\u00a0Probability Tree Diagram Using Javascript and D3<\/a>\u00a0if R is not your preferred platform.)<\/p>\n<h3><strong>R Code to Create a Visual Conditional Probability Tree<\/strong><\/h3>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n\r\n# R Conditional Probability Tree Diagram\r\n\r\n# The Rgraphviz graphing package must be installed to do this\r\nrequire(&quot;Rgraphviz&quot;)\r\n\r\n# Change the three variables below to match your actual values\r\n# These are the values that you can change for your own probability tree\r\n# From these three values, other probabilities (e.g. prob(b)) will be calculated \r\n\r\n# Probability of a\r\na&lt;-.01\r\n\r\n# Probability (b | a)\r\nbGivena&lt;-.99\r\n\r\n# Probability (b | \u00aca)\r\nbGivenNota&lt;-.10\r\n\r\n###################### Everything below here will be calculated\r\n\r\n# Calculate the rest of the values based upon the 3 variables above\r\nnotbGivena&lt;-1-bGivena\r\nnotA&lt;-1-a\r\nnotbGivenNota&lt;-1-bGivenNota\r\n\r\n#Joint Probabilities of a and B, a and notb, nota and b, nota and notb\r\naANDb&lt;-a*bGivena\r\naANDnotb&lt;-a*notbGivena\r\nnotaANDb &lt;- notA*bGivenNota\r\nnotaANDnotb &lt;- notA*notbGivenNota\r\n\r\n# Probability of B\r\nb&lt;- aANDb + notaANDb\r\nnotB &lt;- 1-b\r\n\r\n# Bayes theorum - probabiliyt of A | B\r\n# (a | b) = Prob (a AND b) \/ prob (b)\r\naGivenb &lt;- aANDb \/ b\r\n\r\n# These are the labels of the nodes on the graph\r\n# To signify &quot;Not A&quot; - we use A' or A prime \r\n\r\nnode1&lt;-&quot;P&quot;\r\nnode2&lt;-&quot;A&quot;\r\nnode3&lt;-&quot;A'&quot;\r\nnode4&lt;-&quot;A&B&quot;\r\nnode5&lt;-&quot;A&B'&quot;\r\nnode6&lt;-&quot;A'&B&quot;\r\nnode7&lt;-&quot;A'&B'&quot;\r\nnodeNames&lt;-c(node1,node2,node3,node4, node5,node6, node7)\r\n\r\nrEG &lt;- new(&quot;graphNEL&quot;, nodes=nodeNames, edgemode=&quot;directed&quot;)\r\n#Erase any existing plots\r\ndev.off()\r\n\r\n# Draw the &quot;lines&quot; or &quot;branches&quot; of the probability Tree\r\nrEG &lt;- addEdge(nodeNames[1], nodeNames[2], rEG, 1)\r\nrEG &lt;- addEdge(nodeNames[1], nodeNames[3], rEG, 1)\r\nrEG &lt;- addEdge(nodeNames[2], nodeNames[4], rEG, 1)\r\nrEG &lt;- addEdge(nodeNames[2], nodeNames[5], rEG, 1)\r\nrEG &lt;- addEdge(nodeNames[3], nodeNames[6], rEG, 1)\r\nrEG &lt;- addEdge(nodeNames[3], nodeNames[7], rEG, 10)\r\n\r\neAttrs &lt;- list()\r\n\r\nq&lt;-edgeNames(rEG)\r\n\r\n# Add the probability values to the the branch lines\r\n\r\neAttrs$label &lt;- c(toString(a),toString(notA),\r\n toString(bGivena), toString(notbGivena),\r\n toString(bGivenNota), toString(notbGivenNota))\r\nnames(eAttrs$label) &lt;- c(q[1],q[2], q[3], q[4], q[5], q[6])\r\nedgeAttrs&lt;-eAttrs\r\n\r\n# Set the color, etc, of the tree\r\nattributes&lt;-list(node=list(label=&quot;foo&quot;, fillcolor=&quot;lightgreen&quot;, fontsize=&quot;15&quot;),\r\n edge=list(color=&quot;red&quot;),graph=list(rankdir=&quot;LR&quot;))\r\n\r\n#Plot the probability tree using Rgraphvis\r\nplot(rEG, edgeAttrs=eAttrs, attrs=attributes)\r\nnodes(rEG)\r\nedges(rEG)\r\n\r\n#Add the probability values to the leaves of A&B, A&B', A'&B, A'&B'\r\ntext(500,420,aANDb, cex=.8)\r\n\r\ntext(500,280,aANDnotb,cex=.8)\r\n\r\ntext(500,160,notaANDb,cex=.8)\r\n\r\ntext(500,30,notaANDnotb,cex=.8)\r\n\r\ntext(340,440,&quot;(B | A)&quot;,cex=.8)\r\n\r\ntext(340,230,&quot;(B | A')&quot;,cex=.8)\r\n\r\n#Write a table in the lower left of the probablites of A and B\r\ntext(80,50,paste(&quot;P(A):&quot;,a),cex=.9, col=&quot;darkgreen&quot;)\r\ntext(80,20,paste(&quot;P(A'):&quot;,notA),cex=.9, col=&quot;darkgreen&quot;)\r\n\r\ntext(160,50,paste(&quot;P(B):&quot;,round(b,digits=2)),cex=.9)\r\ntext(160,20,paste(&quot;P(B'):&quot;,round(notB, 2)),cex=.9)\r\n\r\ntext(80,420,paste(&quot;P(A|B): &quot;,round(aGivenb,digits=2)),cex=.9,col=&quot;blue&quot;)\r\n\r\n<\/pre>\n<h3 style=\"text-align: center;\"><strong>Another Probability Tree Example in Light Blue with (\u00ac sign)<\/strong><\/h3><p>&nbsp;<\/p><p><a href=\"http:\/\/harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Rplot02.jpeg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-336 size-full\" src=\"http:\/\/harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Rplot02.jpeg\" alt=\"Rplot02\" width=\"543\" height=\"402\" srcset=\"https:\/\/www.harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Rplot02.jpeg 543w, https:\/\/www.harrysurden.com\/wordpress\/wp-content\/uploads\/2015\/03\/Rplot02-300x222.jpeg 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of a\u00a0Problem Solving Course\u00a0that I teach, I have\u00a0several sessions\u00a0on probability theory. Given that attorneys must frequently make decisions in environments of\u00a0uncertainty, probability\u00a0can be a useful skill for law students to learn.Conditional probability, and Bayes&#8217; Theorem, are important sub-topics that I focus upon. \u00a0In teaching my\u00a0students about Conditional Probability, it is often helpful\u00a0to create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/posts\/292"}],"collection":[{"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":10,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/posts\/292\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.harrysurden.com\/wordpress\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}