rm(list = ls()) # Clear env
gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 508409 27.2    1130474 60.4   644245 34.5
## Vcells 898578  6.9    8388608 64.0  1635533 12.5
cat("\f")       # Clear console

If Jose comes to campus and finds the garage full, what is the probability that there is a sporting event? (Bayes)

Define Variables

A1<-.2 # P(Sporting Event)
A2<-.35 # P(Academic Event)
A3<-.45 # P(No Event)
BGivenA1<-.7 # P(Garage Full Sporting)
BGivenA2<-.25 # P(Garage Full Academic)
BGivenA3<-.05 # P(Garage Full None)

Calculate Bayes P(A1|B)

A1GivenB<-(BGivenA1*A1)/((BGivenA1*A1)+(BGivenA2*A2)+(BGivenA3*A3))
A1GivenB
## [1] 0.56

There is a 56% chance that it is a sporting event

Solve the same problem with a Tree Diagram

Calculate and Define Missing Variable

notBGivenA1<- 1-BGivenA1 # P(Garage Not Full Sporting)
notBGivenA2<- 1-BGivenA2 # P(Garage Not Full Academic)
notBGivenA3<- 1-BGivenA3 # P(Garage Not Full None)

A1ANDB<- A1*BGivenA1 # P(A1&B)
A2ANDB<- A2*BGivenA2 # P(A2&B)
A3ANDB<- A3*BGivenA3 # P(A3&B)
A1ANDnotB<- A1*notBGivenA1 # P(A1&B')
A2ANDnotB<- A2*notBGivenA2 # P(A2&B')
A3ANDnotB<- A3*notBGivenA3 # P(A3&B')

Plot the Tree Diagram Define Nodes, Edges, Add Labels

library(BiocManager)
library("Rgraphviz")
## Loading required package: graph
## Loading required package: BiocGenerics
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, aperm, append, as.data.frame, basename, cbind,
##     colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
##     get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
##     match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
##     table, tapply, union, unique, unsplit, which.max, which.min
## Loading required package: grid
# Create Nodes
node1     <-  "P"
node2     <-  "A1"
node3     <-  "A2"
node4     <-  "A3"
node5     <-  "A1&B"
node6     <-  "A1&B'"
node7     <-  "A2&B"
node8     <-  "A2&B'"
node9     <-  "A3&B"
node10    <-  "A3&B'"
nodeNames <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)

rEG   <- new("graphNEL", 
             nodes = nodeNames, 
             edgemode="directed"
         )

# Erase any existing plots
dev.off()
## null device 
##           1
### LINES
# Draw the "lines" or "branches" of the probability Tree
rEG  <- addEdge (nodeNames[1], nodeNames[2], rEG, 1)
rEG  <- addEdge (nodeNames[1], nodeNames[3], rEG, 1)
rEG  <- addEdge (nodeNames[1], nodeNames[4], rEG, 1)
rEG  <- addEdge (nodeNames[2], nodeNames[5], rEG, 1)
rEG  <- addEdge (nodeNames[2], nodeNames[6], rEG, 1)
rEG  <- addEdge (nodeNames[3], nodeNames[7], rEG, 1)
rEG  <- addEdge (nodeNames[3], nodeNames[8], rEG, 1)
rEG  <- addEdge (nodeNames[4], nodeNames[9], rEG, 1)
rEG  <- addEdge (nodeNames[4], nodeNames[10], rEG, 10)

eAttrs  <- list()
q       <-  edgeNames(rEG)
eAttrs$label <- c(toString(A1), toString(A2), toString(A3),
                  toString(BGivenA1), toString(notBGivenA1),
                  toString(BGivenA2), toString(notBGivenA2),
                  toString(BGivenA3), toString(notBGivenA3)
                  )

names(eAttrs$label) <- c( q[1], q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9])
edgeAttrs <- eAttrs

### COLOR
# Set the color, etc, of the tree
attributes <- list(node  = list(label    = "foo", 
                                fillcolor = "lightgreen", 
                                fontsize  = "12"
),
edge  = list(color   = "red"),
graph = list(rankdir = "LR")
)


### PLOT
# Plot the probability tree using Rgraphvis
plot (rEG, edgeAttrs = eAttrs, attrs=attributes)
nodes(rEG)
##  [1] "P"     "A1"    "A2"    "A3"    "A1&B"  "A1&B'" "A2&B"  "A2&B'" "A3&B" 
## [10] "A3&B'"
edges(rEG)
## $P
## [1] "A1" "A2" "A3"
## 
## $A1
## [1] "A1&B"  "A1&B'"
## 
## $A2
## [1] "A2&B"  "A2&B'"
## 
## $A3
## [1] "A3&B"  "A3&B'"
## 
## $`A1&B`
## character(0)
## 
## $`A1&B'`
## character(0)
## 
## $`A2&B`
## character(0)
## 
## $`A2&B'`
## character(0)
## 
## $`A3&B`
## character(0)
## 
## $`A3&B'`
## character(0)
### PROBABILITY (labels)
#Add the probability values to the leaves of A&B, A&B', A'&B, A'&B'
text(625,390,       A1ANDB, cex = .8)
text(625,320,    A1ANDnotB, cex = .8)
text(630,245,       A2ANDB, cex = .8)
text(630,175,    A2ANDnotB, cex = .8)
text(630,100,       A3ANDB, cex = .8)
text(630,30,     A3ANDnotB, cex = .8)

#Probability that it's a sporting event if the garage is full - P(A1|B)
text(125,25, paste(" P(A1|B)=",A1ANDB,"/(",A1ANDB,"+",A2ANDB,"+",A3ANDB,")=",A1GivenB ),cex = .9, col = "blue")

Image isn’t plotting so publishing that separately. If anyone knows why the markdown file won’t plot the tree diagram, let me know

sessionInfo()   # Session Information
## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8 
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] Rgraphviz_2.42.0    graph_1.76.0        BiocGenerics_0.44.0
## [4] BiocManager_1.30.19
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.31   R6_2.5.1        jsonlite_1.8.4  stats4_4.2.2   
##  [5] evaluate_0.20   cachem_1.0.6    rlang_1.0.6     cli_3.6.0      
##  [9] rstudioapi_0.14 jquerylib_0.1.4 bslib_0.4.2     rmarkdown_2.20 
## [13] tools_4.2.2     xfun_0.36       yaml_2.3.7      fastmap_1.1.0  
## [17] compiler_4.2.2  htmltools_0.5.4 knitr_1.42      sass_0.4.5