# How to Calculate Conditional Probability in R

Probability is a vital concept in statistical analysis, machine learning, and data science, as it provides a way to quantify uncertainty. The concept of conditional probability is especially useful in these domains, as it enables us to update our beliefs based on new evidence. R, being a language specifically designed for statistical computing, provides a variety of functions and tools to calculate probabilities. This article aims to provide a detailed guide on how to calculate conditional probabilities in R.

## Understanding Conditional Probability

Before we delve into the practical aspect of calculating conditional probability in R, it is important to understand what conditional probability means. Conditional probability is the probability of an event given that another event has already occurred. If the event of interest is A and event B is known or assumed to have occurred, the conditional probability of A given B is usually written as P(A|B).

### Formal Definition

The formal definition of conditional probability, according to the formula provided by the probability theory, is:

P(A|B) = P(A ∩ B) / P(B)

Here,

• P(A|B) is the conditional probability of A given B.
• P(A ∩ B) is the probability of both A and B occurring.
• P(B) is the probability of B occurring.

## Calculating Conditional Probability in R

Now let’s turn our attention to how to calculate conditional probability using R. To make it practical, let’s consider an example.

### Step 1: Creating a Contingency Table

Let’s consider a survey of 100 people where they are asked whether they like ice cream and whether they like chocolate. The responses can be summarized in a contingency table:

# Creating a data frame
survey <- data.frame(
IceCream = c("Yes", "Yes", "No", "No"),
Chocolate = c("Yes", "No", "Yes", "No"),
Count = c(45, 15, 25, 15)
)

# Creating a contingency table
survey_matrix <- matrix(survey$Count, nrow = 2, byrow = TRUE, dimnames = list(unique(survey$IceCream), unique(survey\$Chocolate)))
print(survey_matrix)

This will create the contingency table.

     Yes No
Yes   45 15
No    25 15

### Step 2: Calculating the Probabilities

Now, we can calculate the total number of people and the probabilities of liking ice cream and chocolate.

# Total number of people
total_people <- sum(survey_matrix)

# Probability of liking ice cream
p_icecream <- sum(survey_matrix["Yes", ]) / total_people

# Probability of liking chocolate
p_chocolate <- sum(survey_matrix[, "Yes"]) / total_people

### Step 3: Calculating the Joint Probability

The joint probability is the probability of both events (liking ice cream and chocolate) happening together.

# Joint probability of liking ice cream and chocolate
p_icecream_chocolate <- survey_matrix["Yes", "Yes"] / total_people

### Step 4: Calculating the Conditional Probability

Finally, we can calculate the conditional probability of liking ice cream given that the person likes chocolate.

# Conditional probability of liking ice cream given chocolate
p_icecream_given_chocolate <- p_icecream_chocolate / p_chocolate

## Further Example: Conditional Probability with Probability Distributions

R provides functions to work with probability distributions, which can be used to calculate conditional probabilities. Let’s consider a problem involving the normal distribution.

Suppose a manufacturing process produces items with lengths that are normally distributed with a mean of 10 cm and a standard deviation of 2 cm. We might want to know the probability that an item is less than 9 cm long, given that it is less than 11 cm long.

This can be calculated using the pnorm() function in R, which gives the cumulative distribution function for a specified mean and standard deviation.

# Probability of item less than 9 cm
p_less_than_9 <- pnorm(9, mean = 10, sd = 2)

# Probability of item less than 11 cm
p_less_than_11 <- pnorm(11, mean = 10, sd = 2)

# Conditional probability of item less than 9 cm given it is less than 11 cm
p_less_than_9_given_less_than_11 <- p_less_than_9 / p_less_than_11

## Conclusion

In this article, we have provided a comprehensive guide to calculating conditional probability in R, including the underlying concepts and principles, and practical applications using built-in functions and custom code. Understanding and being able to compute conditional probabilities are crucial skills in many areas of data analysis, enabling informed decisions to be made based on observed data.

Posted in RTagged