rm(list=ls())   # remove all objects from environment
cat("\f")       # Clear the console
graphics.off()  # Clear all graphs
gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 527625 28.2    1172795 62.7         NA   669408 35.8
## Vcells 969261  7.4    8388608 64.0      16384  1851555 14.2

Bayes theorem

Based on an event that has already happened, the Bayes theorem provides us with the likelihood that another event will also occur. It enables us to revise our evaluation in light of a fresh update to the occurrence.

This is Bayes’ formula:

\(P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}\)

Where:

  • \(P(A|B)\) is the conditional probability of event A given event B.

  • \(P(B|A)\) is the conditional probability of event B given event A.

  • \(P(A)\)is the probability of event A.

  • \(P(B)\) is the probability of event B.

#Loading the libraries
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

Solution of 3.43

Jose visits campus every Thursday evening. However, some days the parking garage is full, often due to college events. There are academic events on 35% of evenings, sporting events on 20% of evenings, and no events on 45% of evenings. When there is an academic event, the garage fills up about 25% of the time, and it fills up 70% of evenings with sporting events. On evenings when there are no events, it only fills up about 5% of the time. If Jose comes to campus and finds the garage full, what is the probability that there is a sporting event? Use a tree diagram to solve this problem.

#Inputting the given values in the question
#Here: a: Academic event, s: Sporting event, n: No event
a = 0.35
s = 0.2
n = 0.45

#Calculating the conditional probabilities
bGivena = 0.25
bGivens = 0.7
bGivenn = 0.05

notbGivena = 1-0.25
notbGivens = 1-0.7
notbGivenn = 1-0.05
#Calculating the denominator of the Bayes' formula
aANDb = a * bGivena
sANDb = s * bGivens
nANDb = n * bGivenn

b = aANDb + sANDb + nANDb
#Bayes formula gives us the output
(sGivenb = sANDb / b)
## [1] 0.56
#Assigning names to the nodes
node1     <-  "B&S"
node2     <-  "A"
node3     <-  "S"
node4     <-  "N"

node5     <-  "A&B"
node6     <-  "A&B'"
node7     <-  "S&B"
node8     <-  "S&B'"
node9     <-  "N&B"
node10     <-  "N&B'"

nodeNames <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)
rEG   <- new("graphNEL", 
             nodes = nodeNames, 
             edgemode="directed"
             )
#Drawing edges to the nods and cononecting them
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, 1)

#Plotting
plot(rEG)

eAttrs  <- list()
q    <-  edgeNames(rEG)

# Add the probability values to the branch lines
eAttrs$label <- c(toString(a), toString(s), toString(n),
                  toString(bGivena),  toString(notbGivena),   
                  toString(bGivens),  toString(notbGivens), 
                  toString(bGivenn),  toString(notbGivenn))

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

edgeAttrs <- eAttrs

Final diagram of the tree

attributes <- list(node  = list(label    = "foo", 
                              fillcolor = "lightblue", 
                              fontsize  = "10"
                              ),
                   edge  = list(color   = "red",
                                fontsize  = "10"),
                   graph = list(rankdir = "LR")
                   )

#Adding attributes to the tree
plot (rEG, edgeAttrs = eAttrs, attrs=attributes)
#nodes(rEG)
#edges(rEG)


# Adding probability labels to specific edges
text(570, 400, aANDb, cex = 0.8)
text(570, 250, sANDb, cex = 0.8)
text(570, 30, nANDb, cex = 0.8)