rm(list = ls()) # Clear environment
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 533885 28.6 1190671 63.6 NA 669428 35.8
## Vcells 987564 7.6 8388608 64.0 16384 1851741 14.2
cat("\f") # Clear the console
if(!is.null(dev.list())) dev.off() # Clear all plots
## null device
## 1
setwd("/Users/josephmancuso/Documents/BC/Spring'24/Week 2")
getwd()
## [1] "/Users/josephmancuso/Documents/BC/Spring'24/Week 2"
outcome <- 12
#Creating a dataframe w/ three columns containing a sequence of 1-6 (numbers on dice). The sequence is repeated three times (per each roll).
combos <- expand.grid(rep(list(1:6), 3))
possible.outcomes <- rowSums(combos)
successful.outcome <- sum(possible.outcomes == outcome)
total.outcomes <- length(possible.outcomes)
probability <- successful.outcome/total.outcomes
print(probability)
## [1] 0.1157407
Result: There is a .1157 probability of rolling a sum of 12 on three rolls of six-sided dice.
#Determining total number of customers
total.males <- sum(200,200,100,200,200)
total.females <- sum(300,100,200,100,100)
total.customers <- total.males + total.females
#Determining number of customers who live in "other" by total/gender
males.other <- 200
females.other <- 100
total.other <- males.other + females.other
#Determining probability customer is male or female and lives in "other"
probability.other <- ((males.other/total.customers) + (females.other/total.customers))
print(probability.other)
## [1] 0.1764706
Result: There is a .1765 probability that a male or female customer lives in “Other.”
#Determining total number of events
full.deck <- 52
#Determining number of successful outcomes
suit.diamond <- 13
#Probability of drawing diamond on first draw
suit.diamond/full.deck
## [1] 0.25
#probability of drawing diamond on second draw
(suit.diamond -1)/(full.deck-1)
## [1] 0.2352941
Result: There is a .2353 probability of choosing a diamond on the second draw.
list.songs <- 20 #total number of options
selected.songs <- 10 #number of selected options
different.lineups <- factorial(list.songs)/factorial(list.songs - selected.songs)
print(different.lineups)
## [1] 670442572800
Result: There are 670,442,572,800 unique song lineup combinations.
tv <- 20
surround.sound <- 20
dvd <- 18
types.home.theater.systems <- (tv * surround.sound * dvd)
print(types.home.theater.systems)
## [1] 7200
Result: There are 7,200 different types of home theater systems which can be built.
factorial(10)
## [1] 3628800
Result: There are 3,628,800 the doctor can visit 10 patients during their morning rounds.
#possible coin toss outcomes
coin.toss <- (2^7) #two sides, seven tosses
#possible die roll outcomes
die.roll <- (6^3) #six sides, three rolls
#possible card draw outcomes
card.draw <- 52*51*50*49 #52 card deck, four draws w/out replacement
outcomes <- (coin.toss * die.roll * card.draw)
print(outcomes)
## [1] 179640115200
Result: There are 179,640,115,200 unique possible outcomes.
men <- 4 #number of men in party
women <- 4 #number of women in party
seating.arrangements.men <- factorial(men - 1)
seating.arrangements.women <- factorial(women)
total.seating.arrangements <- (seating.arrangements.men * seating.arrangements.women)
print(total.seating.arrangements)
## [1] 144
Result: There are 144 unquie seating arrangements possible if males and females occupy alternate seats.
positive.user <- .95 #P( + | User), probability of positive test given person is a user
negative.not.user <- .99 #P( - | Not User), probability of negative given person is not a user
users <- .03 #P(+), 3% of population are users of opiods
#complements
positive.not.user <- (1-positive.user)
negative.user <- (1-negative.not.user)
not.users <- (1-users)
Applying Bayes Theorem:
#P( + | User) * P(+)
numerator <- positive.user * users
#( + | User) * P(+) + ( - | User) * P( +')
denominator <- (positive.user * users) + (negative.user * not.users)
bayes.theorem <- numerator / denominator
print(bayes.theorem)
## [1] 0.7460733
Result: There is a .7461 probability that a user will test positive.
#probability of drawing each different type of pancake
golden <- 1/3
brown <- 1/3
golden.brown <- 1/3
#probability of drawing a brown side
P.golden <- 0
P.brown <- 1
P.golden.brown <- 1
probability.brown <- (golden * P.golden) + (brown * P.brown) + (golden.brown * P.golden.brown)
print(probability.brown)
## [1] 0.6666667
Result: There is a .66 probability of drawing a pancake with a “brown side.” Given there are three pancakes, there is an equal probability (1/3) of withdrawing one of the three different pancakes. Only two of the pancakes have brown sides - adding together the probability of withdrawing one of the two pancakes with brown sides is thus .66 (1/3 +1/3).