Open In App

How to Use the Triangular Distribution in R

Statistical distributions are crucial for analyzing data, and the triangular distribution is especially handy for handling uncertain values with clear minimum, maximum, and peak points. In R Programming Language we can easily generate random numbers following this distribution pattern. It helps in simulating scenarios, assessing risks, and making data-driven decisions.

What is Triangular Distribution?

The triangular distribution is a type of probability distribution that represents uncertainty about an outcome where the minimum, maximum, and most likely values are known. It forms a triangle shape when plotted on a graph, with the peak of the triangle at the most likely value and the sides extending to the minimum and maximum values. It is defined by three parameters:

  1. Minimum value (a): The lowest possible value that the random variable can take.
  2. Maximum value (b): The highest possible value that the random variable can take.
  3. Mode or peak value (c): The most likely value or the point where the distribution is at its highest.

To represent the Triangular Distribution there are multiple packages that provide functions for working with them. Two commonly used packages are:

Triangular Distribution in R using EnvStats package

We will use EnvStats package in R Programming to find Triangular Distribution.

Syntax :- ptri(q, min = 0, max = 1, mode = 1/2)

library(EnvStats)
library(ggplot2)

# Define parameters for the triangular distribution
min_value <- 0
max_value <- 100
mode_value <- 50

# Calculate cumulative probability for a quantile
quantile_of_interest <- 70
cumulative_prob <- ptri(q = quantile_of_interest, min = min_value, max = max_value,
                         mode = mode_value)
cumulative_prob

Output:

[1] 0.82

min_value, max_value, mode_value , Parameters defining the Triangular Distribution. Here, min_value = 0, max_value = 100, and mode_value = 50 are used.

Create a plot to visualize the Cumulative Distribution Function

# Generate x values for plotting the CDF
x_values <- seq(min_value, max_value, length.out = 100)
cdf_values <- ptri(q = x_values, min = min_value, max = max_value, mode = mode_value)

# Create a plot to visualize the CDF
ggplot(data.frame(x = x_values, cdf = cdf_values), aes(x = x, y = cdf)) +
  geom_line() +
  geom_vline(xintercept = quantile_of_interest, linetype = "dashed", color = "red") +
  labs(title = "Cumulative Distribution Function (CDF) of Triangular Distribution",
       x = "Random Variable (x)",
       y = "Cumulative Probability") +
  theme_minimal()

Output:

gh

Triangular Distribution in R

Generates a sequence of x values for plotting the Cumulative Distribution Function (CDF). The length.out parameter determines the number of points in the sequence.

Triangular Distribution in R using triangle package

We will use triangle package in R Programming to find Triangular Distribution.

Syntax :- rtriangle(n, a, b, c)

library(triangle)
library(ggplot2)

# Define parameters for the triangular distribution
num_samples <- 1000
min_value <- 0
max_value <- 100
mode_value <- 50

# Generate random numbers from the triangular distribution
random_numbers <- rtriangle(n = num_samples, a = min_value, b = max_value, 
                            c = mode_value)

# Create a histogram to visualize the distribution of random numbers
ggplot(data.frame(x = random_numbers), aes(x = x)) +
  geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
  labs(title = "Histogram of Random Numbers from Triangular Distribution",
       x = "Random Variable (x)",
       y = "Frequency") +
  theme_minimal()

Output:

gh

Triangular Distribution in R

Specifies the number of random numbers (samples) to generate from the triangular distribution.

Conclusion

The Triangular Distribution is a useful probability distribution for modeling uncertain events with known minimum, maximum, and mode values. It forms a triangular shape on a graph, emphasizing the most likely outcomes.

Article Tags :