#install packages 

install.packages("BiocManager", repos= ("https://bioconductor.org/biocLite.R"))
## Warning: unable to access index for repository https://bioconductor.org/biocLite.R/src/contrib:
##   cannot open URL 'https://bioconductor.org/biocLite.R/src/contrib/PACKAGES'
## Warning: package 'BiocManager' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository https://bioconductor.org/biocLite.R/bin/macosx/big-sur-arm64/contrib/4.2:
##   cannot open URL 'https://bioconductor.org/biocLite.R/bin/macosx/big-sur-arm64/contrib/4.2/PACKAGES'
library(BiocManager)
BiocManager::install("Rgraphviz")
## Bioconductor version 3.16 (BiocManager 1.30.19), R 4.2.2 (2022-10-31)
## Warning: package(s) not installed when version(s) same as or greater than current; use
##   `force = TRUE` to re-install: 'Rgraphviz'
## Old packages: 'codetools', 'fs', 'markdown', 'nlme', 'readxl', 'tinytex',
##   'utf8', 'xfun'
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
# define the variables 

# Probability of academic 
a          <-   .35

# Probability of sporting event 
a2    <-   .20

# Probability of no event  
a3  <-   .45

#probability of academic event and full
b <- 0.25

#probability of sporting event and full
b2 <- 0.7

#probability of no event and full 
b3 <- 0.05
# Calculate the rest of the values based upon the 3 variables above
notbgivena    <-    1 - b
notbgivena2   <-    1 - b2
notbgivena3   <-    1 - b3
# Joint Probabilities of a and B, a and notb, nota and b, nota and notb
ab  <-   a    *  b
abp <-   a *  notbgivena
a2b <-   a2 * b2
a2bp <- a2 * notbgivena2
a3b <- a3 * b3 
a3bp <- a3 * notbgivena3

ab
## [1] 0.0875
print(paste0( ab , abp , a2b , a2bp , a3b , a3bp ))
## [1] "0.08750.26250.140.060.02250.4275"

is have to be between 0 and 1

# Bayes theorem - probability of A | B
# (a | b) = Prob (a AND b) / prob (b)
bt <- (a2*b2)/(b*a+b2*a2+a3*b3)
bt
## [1] 0.56
#############################################
## 3. START CODING ELEMENTS FOR TREE
#############################################

### NODES (labels)
# These are the labels of the nodes on the graph
# To signify "Not A" - we use A' or A prime 

node1 <- "P"
node2 <- "a"
node3 <- "a2"
node4 <- "a3"
node5 <- "b"
node6 <- "notbgivena"
node7 <- "b2"
node8 <- "notgivena2"
node9 <- "b3"
node10 <- "notgivena3"
nodeNames <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)
rEG   <- new("graphNEL", 
             nodes = nodeNames, 
             edgemode="directed"
)
### 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)
### COLOR
# Set the color, etc, of the tree
attributes <- list(node  = list(label    = "foo", 
                                fillcolor = "purple", 
                                fontsize  = "15"
),
edge  = list(color   = "blue"),
graph = list(rankdir = "LR")
)
### PLOT
# Plot the probability tree using Rgraphvis
plot (rEG, edgeAttrs = eAttrs, attrs=attributes)
nodes(rEG)
##  [1] "P"          "a"          "a2"         "a3"         "b"         
##  [6] "notbgivena" "b2"         "notgivena2" "b3"         "notgivena3"
edges(rEG)
## $P
## [1] "a"  "a2" "a3"
## 
## $a
## [1] "b"          "notbgivena"
## 
## $a2
## [1] "b2"         "notgivena2"
## 
## $a3
## [1] "b3"         "notgivena3"
## 
## $b
## character(0)
## 
## $notbgivena
## character(0)
## 
## $b2
## character(0)
## 
## $notgivena2
## character(0)
## 
## $b3
## character(0)
## 
## $notgivena3
## character(0)
### PROBABILITY (labels)
#Add the probability values to the leaves of A&B, A&B', A'&B, A'&B'

text(500,420,       "ab", cex = .8)
text(500,280,    "abp", cex = .8)
text(500,160,    "a2b", cex = .8)
text(500,30 , "a2bp", cex = .8)
text(340,440,   "a3b", cex = .8)
text(340,230,  "a3bp", cex = .8)

#Write a table in the lower left of the probablites of A and B
text(80,50,  paste("P(A):"   ,a    ),                      cex = .9, col="darkgreen")
text(80,20,  paste("P(A'):"  ,notbgivena2 ),                      cex = .9, col="darkgreen")
text(160,50, paste("P(B):"   ,round(b,       digits = 2)), cex = .9)