title: “DISCUSSION_2” author: “ANDI XU” date: “2024-03-24” output: pdf_document: default html_document: default —
prior <- c( 0.35, 0.2, 0.45 ) # marginal probabilities given in the question stored in a vector
likelihood <- c( 0.25, 0.7, 0.05 ) # conditional probabilities vector stored in a vector
posterior <- prior * likelihood
posterior
## [1] 0.0875 0.1400 0.0225
bayesian <- posterior / sum(posterior)
bayesian
## [1] 0.35 0.56 0.09
#Bayes' Theorem is a mathematical formula used to update the probability of an event based on new evidence. It's a cornerstone of Bayesian statistics, allowing us to refine our predictions or beliefs about the likelihood of something happening as we gather more information. Essentially, it tells us how to revise our estimates of the probability of an event (the posterior probability) by combining our initial estimate (the prior probability) with the new evidence (the likelihood of the evidence given the event).
#The formula for Bayes' Theorem is:
## $$P(A \mid B)$ = $\frac{P(B \mid A)}{P(B)}$$ ##
##P(A∣B) is the updated probability of event A given the new evidence B,##
##P(B∣A) is the likelihood of finding evidence B if A is true,##
##P(A) is the initial probability of event A, and ##
##P(B) is the probability of observing the evidence.##
# install.packages("BiocManager")
library(BiocManager)
## Bioconductor version '3.18' is out-of-date; the current release version '3.19'
## is available with R version '4.4'; see https://bioconductor.org/install
require("Rgraphviz")
## 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, 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
a1 <- .20
a2 <- .35
a3 <- .45
b1Givena1 <- .70
b2Givena2 <- .25
b3Givena3 <- .05
b1GivenNota1 <- .30
b2GivenNota2 <- .75
b3GivenNota3 <- .95
probability_b1_no_park_given_a1 <- a1 * b1Givena1
probability_b2_with_park_given_a1 <- a1 * b2Givena2
probability_b1_no_park_given_a2 <- a2 * b2Givena2
probability_b2_with_park_given_a2 <- a2 * b2GivenNota2
probability_b1_no_park_given_a3 <- a3 * b3Givena3
probability_b2_with_park_given_a3 <- a3 * b3GivenNota3
notA1 <- 1 - a1
notA1
## [1] 0.8
notb1Givena1 <- 1 - b1Givena1
notb1Givena1
## [1] 0.3
notb1GivenNota1 <- 1 - b1GivenNota1
notb1GivenNota1
## [1] 0.7
notA2 <- 1 - a2
notA2
## [1] 0.65
notb2Givena2 <- 1 - b2Givena2
notb2Givena2
## [1] 0.75
notb2GivenNota2 <- 1 - b2GivenNota2
notb2GivenNota2
## [1] 0.25
notA3 <- 1 - a3
notA3
## [1] 0.55
notb3Givena3 <- 1 - b3Givena3
notb3Givena3
## [1] 0.95
notb3GivenNota3 <- 1 - b3GivenNota3
notb3GivenNota3
## [1] 0.05
a1ANDb1 <- a1 * b1Givena1
a1ANDb1
## [1] 0.14
a1ANDnotb1 <- a1 * notb1Givena1
a1ANDnotb1
## [1] 0.06
nota1ANDb1 <- notA1 * b1GivenNota1
nota1ANDb1
## [1] 0.24
nota1ANDnotb1 <- notA1 * notb1GivenNota1
nota1ANDnotb1
## [1] 0.56
#academic event
a2ANDb2 <- a2 * b2Givena2
a2ANDb2
## [1] 0.0875
a2ANDnotb2 <- a2 * notb2Givena2
a2ANDnotb2
## [1] 0.2625
nota2ANDb2 <- notA2 * b2GivenNota2
nota2ANDb2
## [1] 0.4875
nota2ANDnotb2 <- notA2 * notb2GivenNota2
nota2ANDnotb2
## [1] 0.1625
#no event
a3ANDb3 <- a3 * b3Givena3
a3ANDb3
## [1] 0.0225
a3ANDnotb3 <- a3 * notb3Givena3
a3ANDnotb3
## [1] 0.4275
nota3ANDb3 <- notA3 * b3GivenNota3
nota3ANDb3
## [1] 0.5225
nota3ANDnotb3 <- notA3 * notb3GivenNota3
nota3ANDnotb3
## [1] 0.0275
# Probability of B
b1 <- a1ANDb1 + nota1ANDb1
b1
## [1] 0.38
notB1 <- 1 - b1
notB1
## [1] 0.62
b2 <- a2ANDb2 + nota2ANDb2
b2
## [1] 0.575
notB2 <- 1 - b2
notB2
## [1] 0.425
b3 <- a3ANDb3 + nota3ANDb3
b3
## [1] 0.545
notB3 <- 1 - b3
notB3
## [1] 0.455
# Bayes theorem - probability of A | B
# Prob (a | b) = Prob (a AND b) / prob (b)
a1Givenb1 <- a1ANDb1 / b1
a2Givenb2 <- a2ANDb2 / b2
a3Givenb3 <- a3ANDb3 / b3
node1 <- "P"
node2 <- "A1"
node3 <- "A2"
node4 <- "A3"
node5 <- "B1&A1"
node6 <- "B1&A1'"
node7 <- "B2&A2"
node8 <- "B2&A2'"
node9 <- "B3&A3"
node10 <- "B3&A3'"
Names <- c(node1, node2, node3, node4, node5, node6, node7, node8, node9, node10)
rEG <- new("graphNEL", nodes=Names, edgemode="directed")
rEG
## A graphNEL graph with directed edges
## Number of Nodes = 10
## Number of Edges = 0
rEG <- addEdge(Names[1], Names[2], rEG, 1)
rEG <- addEdge(Names[1], Names[3], rEG, 1)
rEG <- addEdge(Names[1], Names[4], rEG, 1)
rEG <- addEdge(Names[2], Names[5], rEG, 1)
rEG <- addEdge(Names[2], Names[6], rEG, 1)
rEG <- addEdge(Names[3], Names[7], rEG, 1)
rEG <- addEdge(Names[3], Names[8], rEG, 1)
rEG <- addEdge(Names[4], Names[9], rEG, 1)
rEG <- addEdge(Names[4], Names[10],rEG, 10)
ettrs <- list()
q<-edgeNames(rEG)
# Add the probability values to the the branch lines
ettrs$label <- c(toString(a1),toString(a2), toString(a3), toString(b1Givena1),
toString(b1GivenNota1),toString(b2Givena2), toString(b2GivenNota2), toString(b3Givena3), toString(b3GivenNota3))
names(ettrs$label) <- c(q[1],q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9])
edgeAttrs<-ettrs
#Now we will add some color to our tree diagram.
# Set the color, etc, of the tree
attributes <- list(node=list(label="foo", fillcolor="green", fontsize="15"),
edge=list(color="blue"),graph=list(rankdir="LR"))
# Plot the probability tree using Rgraphvis
plot(rEG, edgeAttrs=ettrs, attrs=attributes)
nodes(rEG)
## [1] "P" "A1" "A2" "A3" "B1&A1" "B1&A1'" "B2&A2" "B2&A2'"
## [9] "B3&A3" "B3&A3'"
edges(rEG)
## $P
## [1] "A1" "A2" "A3"
##
## $A1
## [1] "B1&A1" "B1&A1'"
##
## $A2
## [1] "B2&A2" "B2&A2'"
##
## $A3
## [1] "B3&A3" "B3&A3'"
##
## $`B1&A1`
## character(0)
##
## $`B1&A1'`
## character(0)
##
## $`B2&A2`
## character(0)
##
## $`B2&A2'`
## character(0)
##
## $`B3&A3`
## character(0)
##
## $`B3&A3'`
## character(0)