I used the notation in this week’s video ‘BayesTheoremScript’ to lay out my problem:
Probability of an Academic Event = AE
Probability of a Sports Event = SE
Probability of No Event = NE
Probability of a full garage given an academic event = FgAE
Probability of a full garage given a sports event = FgSE
Probability of a full garage given no event = FgNE
AE = 0.35
SE = 0.20
NE = 0.45
FgAE = 0.25
FgSE = 0.70
FgNE = 0.05
#Bayes theorem to find the probability of a sporting event given the garage is full.
SEgF = (FgSE*SE)/(AE*FgAE + SE*FgSE + NE*FgNE)
SEgF
## [1] 0.56
There is a 56% chance there is a sporting event if the garage is full.
R Conditional Probability Tree Diagram
## Loading required package: 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, 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
# Probability of an academic event (AE)
AE<-.35
# Probability of a full garage given an academic event (F | AE)
FgAE<-.25
# Probability of a sports event (SE)
SE<-.20
# Probability of a full garage given an academic event (F | AE)
FgSE<-.70
# Probability of no event (SE)
NE<-.45
# Probability of a full garage given no event (F | AE)
FgNE<-.05
# Calculate the rest of the values based upon the 3 variables above
notFgAE<-1-FgAE
notFgSE<-1-FgSE
notFgNE<-1-FgNE
#Joint Probabilities of a and B, a and notb, nota and b, nota and notb
AEandF<-AE*FgAE
AEandnotF<-AE*notFgAE
SEandF<-SE*FgSE
SEandnotF<-SE*notFgSE
NEandF<-NE*FgNE
NEandnotF<-NE*notFgNE
# Probability of Sporting Event
SE
## [1] 0.2
# Bayes theorem - probability of SE | F
# (SE | F) = Prob (SEandF) / (AEandF + SEandF + NEandF)
SEgF = SEandF/(AEandF + SEandF + NEandF)
# These are the labels of the nodes on the graph
# To signify "Not A" - we use A' or A prime
node1<-"P"
node2<-"Academic Event"
node3<-"Sporting Event"
node4<-"No Event"
node5<-"Academic Event & Full Garage"
node6<-"Academic Event & Not Full Garage"
node7<-"Sport Event & Full Garage"
node8<-"Sport Event & Not Full Garage"
node9<-"No Event & Full Garage"
node10<-"No Event & Not Full Garage"
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
# Draw the "lines" or "branches" of the probability Tree
rEG <- addEdge(nodeNames[1], nodeNames[2], rEG, .05)
rEG <- addEdge(nodeNames[1], nodeNames[3], rEG, .05)
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)
eAttrs <- list()
q<-edgeNames(rEG)
# Add the probability values to the the branch lines
eAttrs$label <- c(toString(AE),toString(SE),
toString(NE), toString(FgAE),
toString(notFgAE), toString(FgSE), toString(notFgSE), toString(FgNE),toString(notFgNE))
names(eAttrs$label) <- c(q[1],q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9])
edgeAttrs<-eAttrs
# Set the color, etc, of the tree
attributes<-list(node=list(label="foo", fillcolor="lightgreen", fontsize="25"),
edge=list(color="red"),graph=list(rankdir="LR"))
#Plot the probability tree using Rgraphvis
plot(rEG, edgeAttrs=eAttrs, attrs=attributes)
nodes(rEG)
## [1] "P" "Academic Event"
## [3] "Sporting Event" "No Event"
## [5] "Academic Event & Full Garage" "Academic Event & Not Full Garage"
## [7] "Sport Event & Full Garage" "Sport Event & Not Full Garage"
## [9] "No Event & Full Garage" "No Event & Not Full Garage"
edges(rEG)
## $P
## [1] "Academic Event" "Sporting Event" "No Event"
##
## $`Academic Event`
## [1] "Academic Event & Full Garage" "Academic Event & Not Full Garage"
##
## $`Sporting Event`
## [1] "Sport Event & Full Garage" "Sport Event & Not Full Garage"
##
## $`No Event`
## [1] "No Event & Full Garage" "No Event & Not Full Garage"
##
## $`Academic Event & Full Garage`
## character(0)
##
## $`Academic Event & Not Full Garage`
## character(0)
##
## $`Sport Event & Full Garage`
## character(0)
##
## $`Sport Event & Not Full Garage`
## character(0)
##
## $`No Event & Full Garage`
## character(0)
##
## $`No Event & Not Full Garage`
## character(0)
#Add the probability values to the leaves of A&B, A&B', A'&B, A'&B'
text(500,480, AEandF, cex=.8)
text(500,390, AEandnotF, cex=.8)
text(500,300,SEandF,cex=.8)
text(500,210,SEandnotF,cex=.8)
text(500,120, NEandF, cex=.8)
text(500,40,NEandnotF,cex=.8)
text(340,450,"(F | AE)",cex=.8)
text(340,300,"(F | SE)",cex=.8)
text(340,160,"(F | NE)",cex=.8)
text(80,420,paste("P(SE|F): ",round(SEgF,digits=2)),cex=.9,col="blue")
## [1] "P" "Academic Event"
## [3] "Sporting Event" "No Event"
## [5] "Academic Event & Full Garage" "Academic Event & Not Full Garage"
## [7] "Sport Event & Full Garage" "Sport Event & Not Full Garage"
## [9] "No Event & Full Garage" "No Event & Not Full Garage"
## $P
## [1] "Academic Event" "Sporting Event" "No Event"
##
## $`Academic Event`
## [1] "Academic Event & Full Garage" "Academic Event & Not Full Garage"
##
## $`Sporting Event`
## [1] "Sport Event & Full Garage" "Sport Event & Not Full Garage"
##
## $`No Event`
## [1] "No Event & Full Garage" "No Event & Not Full Garage"
##
## $`Academic Event & Full Garage`
## character(0)
##
## $`Academic Event & Not Full Garage`
## character(0)
##
## $`Sport Event & Full Garage`
## character(0)
##
## $`Sport Event & Not Full Garage`
## character(0)
##
## $`No Event & Full Garage`
## character(0)
##
## $`No Event & Not Full Garage`
## character(0)