Get startedGet started for free

Computing probabilities

The where9am data frame contains 91 days (thirteen weeks) worth of data in which Brett recorded his location at 9am each day as well as whether the daytype was a weekend or weekday.

Using the conditional probability formula below, you can compute the probability that Brett is working in the office, given that it is a weekday.

$$ P(A|B) = \frac{P(A \text{ and } B)}{P(B)} $$

Calculations like these are the basis of the Naive Bayes destination prediction model you'll develop in later exercises.

This exercise is part of the course

Supervised Learning in R: Classification

View Course

Exercise instructions

  • Find P(office) using nrow() and subset() to count rows in the dataset and save the result as p_A.
  • Find P(weekday), using nrow() and subset() again, and save the result as p_B.
  • Use nrow() and subset() a final time to find P(office and weekday). Save the result as p_AB.
  • Compute P(office | weekday) and save the result as p_A_given_B.
  • Print the value of p_A_given_B.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Compute P(A) 
p_A <- ___

# Compute P(B)
p_B <- ___

# Compute the observed P(A and B)
p_AB <- ___

# Compute P(A | B) and print its value
p_A_given_B <- ___
___
Edit and Run Code