##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 533762 28.6    1190324 63.6         NA   669420 35.8
## Vcells 986513  7.6    8388608 64.0      16384  1851779 14.2
## null device 
##           1
packages <- c("psych",
              "tidyverse",
              "ggplot2",
              "lemon",
              "gridExtra",
              "ggrepel",
              "scales",
              "knitr",
              "BiocManager"
              )
   
  for (i in 1:length(packages)) {
    if (!packages[i] %in% rownames(installed.packages())) {
      install.packages(packages[i], dependencies = TRUE)
    }
    library(packages[i], character.only = TRUE)
  }
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%()   masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'lemon'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     %||%
## 
## 
## 
## Attaching package: 'gridExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## 
## 
## The following objects are masked from 'package:psych':
## 
##     alpha, rescale
  rm(packages)

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.

#Defining outcomes of interest

PA1 <- 20/100 #probability of sporting event

PA2 <- 35/100 #probability of academic event

PA3 <- 45/100 #probability of no event on campus

sum(c(PA1,PA2,PA3)) #check
## [1] 1
#Defining conditions

PB1 <- 70/100 #probability of garage being full given there is a sporting event

PB2 <- 25/100 #probability of garage being full given there is an academic event

PB3 <- 5/100 #probablity of garage being full given there is no event

sum(c(PB1,PB2,PB3)) #check
## [1] 1
#complements

P.notB1 <- 1-PB1 #probability of garage not being full given there is a sporting event 

P.notB2 <- 1- PB2 #probability of garage not being full given there is an academic event

P.notB3 <- 1-PB3 #probablity of garage not being full given there is no event
#Using Bayes’ Theorem to compute the probability of a sporting event (A1) under the condition that the parking lot is full.

#P(B|A)*P(A1)
numerator <- PA1 * PB1

#P(B|A)*P(A1) + P(B|A2)*P(A2) + P(B|A3)*P(A3)
denominator <- ((PA1 * PB1) + (PA2 * PB2) + (PA3 * PB3)) 

bayes.theorem <- numerator / denominator
print(bayes.theorem)
## [1] 0.56

Interpretation: If the garage is full, there’s a 56% probability a sporting event is occurring.

#loading packages
library(Rgraphviz)
## Loading required package: graph
## Loading required package: BiocGenerics
## 
## Attaching package: 'BiocGenerics'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following objects are masked from 'package:lubridate':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:dplyr':
## 
##     combine, intersect, setdiff, union
## 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
## 
## Attaching package: 'graph'
## The following object is masked from 'package:stringr':
## 
##     boundary
## Loading required package: grid
#Defining nodes of probablity tree
node.1 <- "P"
node.2 <- "Sporting"
node.3 <- "Academic"
node.4 <- "None"
node.5 <- "Sport & Full"
node.6 <- "Sport & Capacity"
node.7 <- "Academic & Full"
node.8 <- "Academic & Capacity"
node.9 <- "No Event & Full"
node.10 <- "No Event & Capacity"

node.labels <- c(node.1, node.2, node.3, node.4, node.5, node.6, node.7, node.8, node.9, node.10)

rEG <- new("graphNEL", nodes=node.labels, edgemode="directed") #creating directed graph
setwd("/Users/josephmancuso/Documents/BC/Spring'24/Week 2/Discussion")
getwd()
## [1] "/Users/josephmancuso/Documents/BC/Spring'24/Week 2/Discussion"
#Creating branches of probability tree
rEG <- addEdge(node.labels[1], node.labels[2], rEG, 1) 
rEG <- addEdge(node.labels[1], node.labels[3], rEG, 1)
rEG <- addEdge(node.labels[1], node.labels[4], rEG, 1)
rEG <- addEdge(node.labels[2], node.labels[5], rEG, 1)
rEG <- addEdge(node.labels[2], node.labels[6], rEG, 1)
rEG <- addEdge(node.labels[3], node.labels[7], rEG, 1)
rEG <- addEdge(node.labels[3], node.labels[8], rEG, 1)
rEG <- addEdge(node.labels[4], node.labels[9], rEG, 1)
rEG <- addEdge(node.labels[4], node.labels[10], rEG, 1)

eAttrs <- list() #creating empty list 
 
edge.names <-edgeNames(rEG) #assigning edge names to empty vector 
#adding probability values to branches 
eAttrs$label <- c(toString(PA1),toString(PA2), toString(PA3), toString(PB1), toString(P.notB1),                   toString(PB2), toString(P.notB2), toString(PB3), toString(P.notB3))

#assigning labels using values from the empty edge.names vector
names(eAttrs$label) <- c(edge.names[1],edge.names[2], edge.names[3], edge.names[4], edge.names[5], edge.names[6], edge.names[7], edge.names[8], edge.names[9])
#formatting
attributes <- list(node=list(fillcolor="lightblue", fontsize="15"), edge=list(color="black"  ),graph=list(rankdir="LR"))
#plotting probability tree

probability.tree <-plot(rEG, edgeAttrs=eAttrs, attrs=attributes)