Continuous Uniform Distribution in R

What is Continuous Uniform Distribution?

The continuous uniform distribution is a probability distribution where all values within a given range are equally likely to occur. In R, you can generate random numbers from a continuous uniform distribution using the runif() function.

The runif() function takes the following arguments:

  • n: the number of random numbers to generate.
  • min: the minimum value of the range.
  • max: the maximum value of the range.

Here’s an example of how to generate 10 random numbers from a continuous uniform distribution with a range of 0 to 1:

 

random_numbers <- runif(n = 10, min = 0, max = 1)
print(random_numbers)

# [1] 0.3948746 0.3693547 0.6501776 0.9014492 0.3934446 0.2226859
# 0.0258276 0.7570578 0.1945626 0.2776624


x <- seq(0, 1, by = 0.01)
y <- dunif(x, min = 0, max = 1)
plot(
x,
y,
type = "l",
main = "Probability Density Function",
xlab = "x",
ylab = "Density"
)

This will generate a plot of the probability density function of the continuous uniform distribution with a range of 0 to 1.

Example – 2

In R, you can work with continuous uniform distributions using the runif() function to generate random numbers, and dunif(), punif(), and qunif() functions for the probability density function (PDF), cumulative distribution function (CDF), and quantile function, respectively.

Here’s how you can use these functions:

1. Generating random numbers from a continuous uniform distribution:

# Generate random numbers from a continuous uniform distribution
n <- 10 # Number of random numbers
min_val <- 0 # Minimum value
max_val <- 1 # Maximum value

random_numbers <- runif(n, min = min_val, max = max_val)
print(random_numbers)

2. Probability density function (PDF):

# Calculate the PDF of a continuous uniform distribution
x <- seq(-1, 2, by = 0.1) # Define a sequence of values
min_val <- 0
max_val <- 1

pdf <- dunif(x, min = min_val, max = max_val)
print(pdf)

3. Cumulative distribution function (CDF):

# Calculate the CDF of a continuous uniform distribution
x <- seq(-1, 2, by = 0.1) # Define a sequence of values
min_val <- 0
max_val <- 1

cdf <- punif(x, min = min_val, max = max_val)
print(cdf)

4. Quantile function:

# Calculate the quantiles of a continuous uniform distribution
p <- seq(0, 1, by = 0.1) # Define a sequence of probabilities
min_val <- 0
max_val <- 1

quantiles <- qunif(p, min = min_val, max = max_val)
print(quantiles)

These examples demonstrate how to work with continuous uniform distributions in R. Remember to adjust the min_val and max_val variables according to your specific use case.

Example – 3

Here, I’ll demonstrate how to plot the PDF and CDF of a continuous uniform distribution using R’s ggplot2 library. First, you need to install and load the ggplot2 library if you haven’t already:

# Define a sequence of values
x <- seq(-1, 2, by = 0.01)

# Calculate the PDF and CDF
min_val <- 0
max_val <- 1
pdf <- dunif(x, min = min_val, max = max_val)
cdf <- punif(x, min = min_val, max = max_val)

# Create a data frame
data <- data.frame(x = x, pdf = pdf, cdf = cdf)

# Plot the PDF
pdf_plot <- ggplot(data, aes(x = x, y = pdf)) +
geom_line(color = "blue") +
labs(title = "Probability Density Function (PDF)",
x = "x",
y = "Density") +
theme_minimal()

# Plot the CDF
cdf_plot <- ggplot(data, aes(x = x, y = cdf)) +
geom_line(color = "red") +
labs(title = "Cumulative Distribution Function (CDF)",
x = "x",
y = "Probability") +
theme_minimal()

# Display plots
pdf_plot
cdf_plot

Log Normal Distribution in R

Understanding the t-distribution in R