# Packages list
library(stats)
library(base)
library(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
library(grid)
library(graph)
library(BiocManager)
library(Rgraphviz)
To understand Bayes’ Theorem, we first need to understand the Law of conditional probability. The law of conditional probability deals with the probability of an event occurring given that another event has already happened, which conditions the probability of one event to another. Bayes’ Theorem is just the application of the law of conditional probability where we are updating the conditional probabilities of an event given new information or data.
\[P(A \mid B) = \frac{P(B \mid A) * P(A)}{P(B)}\]
I am just replacing the formula with the values I was given from the problem. Since I have all the joint probabilities I need to calculate B, I donnot have to look for the complementary probabilities.
BT <- .70 * .20 / (.20 * .70 + .35 * .25 + .45 * .05)
print(BT)
## [1] 0.56
Tree Diagram
First, I am assigning the given values to a character.
# Probability of Academic events A1
A1 <- .35
# Probability of Sports events A2
A2 <- .20
# Probability of no events A3
A3 <- .45
# Probability of (B | A1)
BgivA1 <- .25
# Probability of (B | A2)
BgivA2 <- .70
# Probability of (B | A3)
BgivA3 <- .05
Now I am multiplying the given probabilities so I obtain the joint probabilities for the events. These joint probabilities describe the probability that there is a certain event and the garage is full.
#Joint probability of Academic events and full garage
A1nB <- A1 * BgivA1
#Joint probability of Sport event and full garage
A2nB <- A2 * BgivA2
#Joint probability of No event and full garage
A3nB <- A3 * BgivA3
print(A1nB)
## [1] 0.0875
print(A2nB)
## [1] 0.14
print(A3nB)
## [1] 0.0225
In the next chunk, I just calculated the probability of a full garage and then used the baye’s theorem formula to solve for the probability that there is a sports even given a full garage
# Probability of full garage
B <- A1nB + A2nB + A3nB
#Probability of not full garage
notB <- 1 - B
# Bayes theorem - probability of A2 | B
# Prob (A2 | b) = Prob (A2 AND B) / Prob (B)
A2GivenB <- A2nB / B
print(paste0("The probability that there is a sports even given that the garage is full is ", round(x = A2GivenB,
digits = 4)))
## [1] "The probability that there is a sports even given that the garage is full is 0.56"
In this chunk, I am calculating the complementary probability of the conditional probabilities
# Complementary Probabilities
notBgivA1 <- 1 - BgivA1
notBgivA2 <- 1 - BgivA2
notBgivA3 <- 1 - BgivA3
print(notBgivA1)
## [1] 0.75
print(notBgivA2)
## [1] 0.3
print(notBgivA3)
## [1] 0.95
In this chunk, I am calculating the complementary probabilities of the joint probabilities but for the cases when the garages are not full
# Joint probabilities
A1nBnot <- notBgivA1 * A1
A2nBnot <- notBgivA2 * A2
A3nBnot <- notBgivA3 * A3
print(A1nBnot)
## [1] 0.2625
print(A2nBnot)
## [1] 0.06
print(A3nBnot)
## [1] 0.4275
Using the code provided to do the graph, I adjusted some of the code to fit my case
### NODES (labels)
# These are the labels of the nodes on the graph
node1 <- "P"
node2 <- "A1"
node3 <- "A2"
node4 <- "A3"
node5 <- "A1nB"
node6 <- "A1nB'"
node7 <- "A2nB"
node8 <- "A2nB'"
node9 <- "A3nB"
node10 <- "A3nB'"
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, 1)
eAttrs <- list()
q <- edgeNames(rEG)
### PROBABILITY VALUES
# Add the probability values to the the branch lines
eAttrs$label <- c(toString(A1), toString(A2),
toString(A3), toString(BgivA1),
toString(notBgivA1), toString(BgivA2),
toString(notBgivA2), toString(BgivA3),
toString(notBgivA3))
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 = "grey",
fontsize = "15"
),
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" "A1nB" "A1nB'" "A2nB" "A2nB'" "A3nB"
## [10] "A3nB'"
edges(rEG)
## $P
## [1] "A1" "A2" "A3"
##
## $A1
## [1] "A1nB" "A1nB'"
##
## $A2
## [1] "A2nB" "A2nB'"
##
## $A3
## [1] "A3nB" "A3nB'"
##
## $A1nB
## character(0)
##
## $`A1nB'`
## character(0)
##
## $A2nB
## character(0)
##
## $`A2nB'`
## character(0)
##
## $A3nB
## character(0)
##
## $`A3nB'`
## character(0)
### PROBABILITY (labels)
text(500,470, A1nB, cex = .8)
text(500,385, A1nBnot, cex = .8)
text(500,298, A2, cex = .8)
text(500,210, A2nBnot, cex = .8)
text(500,120 , A3, cex = .8)
text(500,35 , A3nBnot, cex = .8)
#Write a table in the lower left of the probablites of A and B
text(80,50, paste("P(A1):",A1 ), cex = .9,col="darkgreen")
text(80,30, paste("P(A2):",A2 ), cex = .9, col="darkgreen")
text(80,10, paste("P(A3):",A3 ), cex = .9, col="darkgreen")
text(160,50, paste("P(B):" ,round(B, digits = 2)), cex = .9)
text(160,20, paste("P(B'):" ,round(notB, digits = 2)), cex = .9)
text(80,420, paste("P(A2|B):",round(A2GivenB, digits = 2)), cex = .9, col = "blue")
I encountered a problem. In my code, the graph looks perfect but when I knit it to html, it shifts the values of the joint probabilities to the left. I am attaching a picture of the graph.
# Probability of Users
U <- .03
# Probability of (B | U)
BgivU <- .95
# Probability of (B' | NonU)
BnotgivNonU <- .99
# Complementary Probabilities
nonU <- 1 - U
BnotgivU <- 1 - BgivU
BgivNonU <- 1 - BnotgivNonU
print(nonU)
## [1] 0.97
print(BnotgivU)
## [1] 0.05
print(BgivNonU)
## [1] 0.01
UnB <- U * BgivU
NonUnB <- nonU * BgivNonU
print(UnB)
## [1] 0.0285
print(NonUnB)
## [1] 0.0097
# Probability of full garage
B <- UnB + NonUnB
#Probability of not full garage
notB <- 1 - B
# Bayes theorem - probability of U | B
# Prob (U | b) = Prob (U AND B) / Prob (B)
UGivenB <- UnB / B
print(paste0("The probability that a person is a user even given that test result is positive is ", round(x = UGivenB,
digits = 4)))
## [1] "The probability that a person is a user even given that test result is positive is 0.7461"