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. ** NOTE: The way I am reading this is I have 2 six-sided dice and am rolling it 3 times. What are the chances that I get a 12? Show my answer in R.**

Thoughts: With limited experience in R, the hint to use “expand.grid” was extremely helpful!

Step 1: Create a vector of all possible outcomes of a single die (1-6) Step 2: Create a data frame of all possible outcomes of rolling TWO dice (expand.grid)
Step 3: Count the number of rows that add up to 12 (rowSums)
Step 4: Count the total number of possible outcomes
Step 5: Calculate probability (number of 12s/total number of outcomes)
Step 6: Calculate probability of NOT rolling a 12 (compliment)
Step 7: Calculate probability of NOT rolling a 12 on 3 rolls
Step 8: Calculate probability of rolling a 12 on 3 rolls

# All possible results rolling a single die
die <- 1:6

#Data frame expanded to include all possible combinations for 2 dice
results <- expand.grid(die1 = die, die2 = die)

#Calculate the total number of possibilities
total <- nrow(results)

#Count number of occurrences where combinations = 12
twelve <- sum(rowSums(results) == 12)   #36

#Calculate the probability of rolling a 12
p_twelve_one <- twelve / total   #0.03

#Probability of NOT rolling a twelve (compliment)
p_not_twelve <- 1 - p_twelve_one    #0.98

#Probability of NOT rolling a 12 in 3 rolls
p_not_twelve_three <- (p_not_twelve) ^ 3   #0.92

#Probability of rolling a 12 AT LEAST once in 3 rolls
p_twelve_three <- 1 - p_not_twelve_three   #0.08

print(paste("The probability of rolling a 12 AT LEAST once in 3 rolls is:", round(unname(p_twelve_three), 3)))
## [1] "The probability of rolling a 12 AT LEAST once in 3 rolls is: 0.081"
  1. 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’? Express your answer as a decimal number only. Show your R code. Hint: create the matrix above in R, use rowSums and col.Sums to generate marginal probabilities
# Create the matrix
residence <- matrix(c(200, 300,
                             200, 100,
                             100, 200,
                             200, 100,
                             200, 100),
                           nrow = 5, byrow = TRUE)

# Add column and row labels
rownames(residence) <- c("Apartment", 
                                "Dorm", 
                                "Parent's", 
                                "Sorority/Fraternity", 
                                "Other")
colnames(residence) <-  c("Males", "Females")

# Generate marginal counts
row_totals <- rowSums(residence)
col_totals <- colSums(residence)
total <- sum(residence)


# The probability of being (Male and Other) or (Female and Other) 
prob_other <- row_totals["Other"] / total

# Display the result
print(paste("The probability of being (Male AND Other) OR (Female AND Other) is:", round(unname(prob_other),3)))
## [1] "The probability of being (Male AND Other) OR (Female AND Other) is: 0.176"
  1. 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? Express your answer as a decimal number only. Show your R code.

Thoughts:
1. Cards/Initial State: Standard deck has 52 cards (13 diamonds)
2. Once the first draw is complete, there are 51 cards remaining, obviously
3. There are now 12 diamonds remaining
4. Probability

\[P(Diamond_2\vert Diamond_1) = \frac{12}{15} \]

#Card initial state
diamonds <- 13
full_deck <- 52

#State after drawing first diamond
remaining_diamonds <- diamonds - 1
remaining_deck <- full_deck - 1

#Probability
p_second_diamond <-  remaining_diamonds / remaining_deck

#Display
print(paste("The probability of selecting a second diamond is:", 
            round(unname(p_second_diamond), 3)))
## [1] "The probability of selecting a second diamond is: 0.235"
  1. 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? Show your R code.

Thoughts: This seems to be a simple permutation using the formula:

\[P(n,r)=\frac{n!}{(n-r)!} \]

In this case, it is a permutation of selecting 10 items from a pool of 20

#Given info
r <-  10
n <-  20

#Formula 
sequence <-  factorial(n) / factorial(n - r)

#Display
print(paste("The permutations of selection 10 items from a pool of 20 is:", 
            formatC(unname(sequence), format = "f", big.mark = ",", digits = 0)))
## [1] "The permutations of selection 10 items from a pool of 20 is: 670,442,572,800"

(Honestly, I just learned to format large numbers in a more readable format using commas.)

  1. 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? Show your R code.

Thoughts:
I believe I am supposed to use the fundamental counting principle as we have 3 sets of items and order does not matter. This allows me to simply the 3 elements together.

#What we have 
tvs <-  20
sound_systems <-  20
dvd_player <- 18

#Total combinations (using the fundamental counting principle)
total_systems <- tvs * sound_systems * dvd_player

#Display
print(paste("Total different home theater systems is: ", total_systems))
## [1] "Total different home theater systems is:  7200"
  1. A doctor visits her patients during morning rounds. In how many ways can the doctor visit 10 patients during the morning rounds? Show your R code.

Thoughts:
We have a permutation here since we have an ordered arrangement ogf a set of objects

\[n! = n \times (n-1) \times (n-2) \times \dots \times 1\]

#Patients
n <- 10

#Possible visits
visits <- factorial(n)

#Display
print(paste("Total number of visits possible is:", visits))
## [1] "Total number of visits possible is: 3628800"
  1. 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? Show your R code.

Thoughts: We have a compound event here. 3 independent events
Step 1: Get number of options for each of the 3 events.
* Coin Toss: Since only 2 outcomes (H or T), then \(2^7\)
* Die rolled 3 times. 6 outcomes. \(6^3\) * Cards: Pick 4 cards out of 52. No replacement. \[\binom{n}{r} = \frac{n!}{r!(n-r)!}\]

Step 2: Total possible outcomes = \[Outcomes_{possible} = Coin_{possible} * Die_{possible} * Cards_{possible}\]
Step 3: That’s it?

#Coin
coin_outcomes <- 2^7

#Die
die_outcomes <-  6^3

#Cards
#worked but resulted in a bizarre number when multiplied with above
#cards_outcomes <-  factorial(52)/(factorial(4) * factorial(52 - 4))

#Trying the choose function from our reading
cards_outcomes <- choose(52, 4)

#Total Outcomes
total_outcomes <-  coin_outcomes * die_outcomes * cards_outcomes

coin_outcomes
## [1] 128
die_outcomes
## [1] 216
cards_outcomes
## [1] 270725
print(paste("The total of all different outcomes is:" , format(total_outcomes, format = "f", big.mark = ",", digits = 1 )))
## [1] "The total of all different outcomes is: 7e+09"
  1. 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. Show your R code.

Thoughts: Round table means that there is no obvious starting position? So perhaps we have to arbitrarily assign a male or female to a seat to get us out of the starting block. Let’s say M.

This took a lot of thinking as I kept trying to simply move in order around the table (ie MFMFMFMF). I could not understand how to do this.

Then I realized that if we seat the remaining 3 Males first, then we can determine that there are 6 ways to do that (4-1)!.

My brain also could not wrap itself around that I was doing math on 3 males and 4 females. I kept wanting this to be the same.

Once I “got there,” it was easy math.

#Given
men <- 4
women <- 4

#Number of ways to seat the men
men_seating <- factorial(men - 1) 
women_seating <- factorial(women)

#Permutations
total <- men_seating * women_seating

#display
print(paste("The total number of ways for men and women to be seated is:", total))
## [1] "The total number of ways for men and women to be seated is: 144"
  1. 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 | +)? Show your R code. You may use a tree, table, or Bayes to answer this problem.

Thoughts:
Yikes!

#Given
p_positive_given_user <- 0.95        #P(+|user)
p_negative_given_not_user <- 0.99    #P(-|not user)
p_user <- 0.03                       #P(user)

#Calculate some stuff
p_not_user <-  1 - p_user            #P(not user)
p_positive_given_not_user <- 1 - p_negative_given_not_user  #P(+|not user) "False Positive"

#Calculate P(+)
p_positive <- (p_positive_given_user * p_user) + (p_positive_given_not_user * p_not_user)

#P(user|+) = (P(+|user) * P(user))/P(+)
p_user_given_positive <- ((p_positive_given_user) * (p_user))/p_positive

#display
print(p_user_given_positive)
## [1] 0.7460733
  1. 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.
#What we have
#pancake_golden_golden (0 ways to show brown)
#pancake_golden_brown (1 way to show brown)
#pancake_brown_brown (2 ways to show brown)

#Total ways to see a brown side
total <- 0 + 1 + 2

#Ways to see brown on one side AND brown on the other side
brown_both_sides <- 2

#Math
probability <- brown_both_sides/total

#display


#print(paste("The probability of selecting a second diamond is:", 
 #           round(unname(p_second_diamond), 3)))
print(paste("P(other side is brown|observed brown) is:", round(unname(probability), 3) ))
## [1] "P(other side is brown|observed brown) is: 0.667"