Uniform distribution in R [Practically Explained]

Filed Under: R Programming
Uniform Distribution In R

Hello, readers! In this article, we will be focusing on Uniform distribution in R – dunif(), punif(), qunif(), runif() functions in detail.

So, let us begin!! 馃檪


First, what is Uniform Distribution?

In the domain of statistics, we do come across the various distribution of data variables such as :

Today, we will be focusing on Uniform Distribution in detail.

In simple terms, Uniform Distribution is a probability based distribution wherein it happens to have equal chances of outcome to occur as a result. That is, it has equals chances for the probability to have the group values.

For example: Consider a scenario wherein we deal with the question that determines the probability of males or females who would opt for blue color for their car.

In such a scenario, with respect to the Uniform distribution of data, both males and females would have equal probability outcomes.

So equal chances of expecting either group of the probability values.

Within the Uniform Distribution, R programming provides us with the below functions to understand the variants in it.

  • dunif() function
  • runif() function
  • qunif() function
  • punif() function

Let us have a look at each variant one by one in the upcoming section!


1. R dunif() function

R dunif() function enables us calculate the uniform probability density function for our passed set of values.

In this example, we have created a sequence of values between 0 to 50 using seq() function. Further, we use dunif() function to get the uniform density probability value.

Before that, have a look at the below syntax!

dunif(data, min, max)
  • data: The values on which density function is built.
  • min: Specifies the minimum value to be set for the uniform density function.
  • max: Depicts the maximum value for the uniform density function.

Example:

#Removed all the existing objects
rm(list = ls())

info = seq(0, 50, by = 1)
dta = dunif(info, min = 10, max = 25)
plot(dta,type="o")

Output:

As seen below, the density stays uniform from 0 upto the minimum value i.e. 10. Further to which, it scales up nearly to 1. And then, as soon as it reaches the maximum point i.e. 25, it again maintains the uniform density of values.

dunif() Function Uniform Distribution in R
dunif() Function

2. R punif() function

R punif() function helps us estimate the uniform cumulative distribution function for the set of values.

In the below example, we use the same set of values as above! Further, punif() function calculates the cumulative frequency distribution for the set of values passed to it.

Example:

#Removed all the existing objects
rm(list = ls())

info = seq(0, 50, by = 1)
dta = punif(info, min = 10, max = 25)
plot(dta,type="o")

Output:

punif() Function Uniform Distribution in R
punif() Function

3. R qunif() function

R qunif() function helps us to get the uniform quantile distribution probability values for the data values.

In this example, we have used qunif() function to get the quantile distribution values from the data values passed.

Example:

#Removed all the existing objects
rm(list = ls())

info = seq(0, 1, by = 0.04)
dta = qunif(info, min = 10, max = 25)
plot(dta,type="o")

Output:

Qunif Function Uniform Distribution in R
qunif Function

4. R runif() function

Now, R runif() function helps us get random numbers as usual but with a little twist. Using runif() function, we get a set of random numbers that are uniformly distributed i.e. follow an uniform distribution.

Example:

#Removed all the existing objects
rm(list = ls())

info = 20000
dta = runif(info, min = 10, max = 25)
hist(dta,breaks = 40,
     main = "runif function",
     xlim = c(0, 100))

Output:

As seen below, we can see the distribution of the random numbers. They follow an uniform distribution between the min value (10) and the maximum value (25), respectively.

Runif Function
runif Function

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages