Preparing Workspace

  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

Setting WD

setwd("/Users/josephmancuso/Documents/BC/Spring'24/Week 2")
getwd()
## [1] "/Users/josephmancuso/Documents/BC/Spring'24/Week 2"

Question 1: What is the probability of rolling a sum of 12 on three rolls of six-sided dice? Express your answer as a decimal number only. Show your R code.

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.

Question 2: A newspaper company classifies its customers by gender and location of residence. The research department has gathered data from a random sample of customers:

What is the probability that a customer is male and lives in ‘Other’ or is female and lives in ‘Other’?

#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.”

Question 3: Two cards are drawn without replacement from a standard deck of 52 playing cards. What is the probability of choosing a diamond for the second card drawn, if the first card, drawn without replacement, was a diamond?

#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.

Question 4: A coordinator will select 10 songs from a list of 20 songs to compose an event’s musical entertainment lineup. How many different lineups are possible?

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.

Question 5: You are ordering a new home theater system that consists of a TV, surround sound system, and DVD player. You can choose from 20 different TVs, 20 types of surround sound systems, and 18 types of DVD players. How many different home theater systems can you build?

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.

Questions 6: A doctor visits her patients during morning rounds. In how many ways can the doctor visit 10 patients during the morning rounds?

factorial(10)
## [1] 3628800

Result: There are 3,628,800 the doctor can visit 10 patients during their morning rounds.

Question 7: If a coin is tossed 7 times, and then a standard six-sided die is rolled 3 times, and finally a group of four cards are drawn from a standard deck of 52 cards without replacement, how many different outcomes are possible?

#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.

Question 8: In how many ways may a party of four women and four men be seated at a round table if the women and men are to occupy alternate seats?

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.

Question 9: An opioid urinalysis test is 95% sensitive for a 30-day period, meaning that if a person has actually used opioids within 30 days, they will test positive 95% of the time P( + | User) =.95. The same test is 99% specific, meaning that if they did not use opioids within 30 days, they will test negative P( - | Not User) = .99. Assume that 3% of the population are users. Then what is the probability that a person who tests positive is actually a user P(User | +)?

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.

Question 10: You have a hat in which there are three pancakes. One is golden on both sides, one is brown on both sides, and one is golden on one side and brown on the other. You withdraw one pancake and see that one side is brown. What is the probability that the other side is brown? Explain.

#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).