Continuous Uniform Distribution in R - GeeksforGeeks

06 Jun, 2021
The continuous uniform distribution is also referred to as the probability distribution of any random number selection from the continuous interval defined between intervals a and b.  A uniform distribution holds the same probability for the entire interval. Thus, its plot is a rectangle, and therefore it is often referred to as Rectangular distribution. Here we will discuss various functions and cases in which these functions should be used to get a required probability.
For uniform distribution, we first need a randomly created sequence ranging between two numbers. The runif() function in R programming language is used to generate a sequence of random following the uniform distribution.  
Syntax:
runif(n, min = 0, max = 1) 
Parameter:
n= number of random samples
min=minimum value(by default 0)
max=maximum value(by default 1)
Example:
R
print("Random 15 numbers between 1 and 3")runif(15, min=1, max=3) 
Output
[1] “Random 15 numbers between 1 and 3” 
[1] 1.534 1.772 1.027 1.765 2.739 1.681 1.964 2.199 1.987 1.372 2.655 2.337 2.588 1.216 2.447
By a quantile, we mean the fraction (or percent) of points below the given value. qunif() method is used to calculate the corresponding quantile for any probability (p) for a given uniform distribution. To use this simply the function had to be called with the required parameters.
Syntax:
qunif(p,  min = 0, max = 1)
Parameter : 
p – The vector of probabilities
min , max – The limits for calculation of quantile function
Example 1:
R
min <- 0max <- 40  print ("Quantile Function Value")  # calculating the quantile function valuequnif(0.2, min = min, max = max)
Output
[1] “Quantile Function Value” 
[1] 8
The x values can be specified in the form of a sequence of vectors using the seq() method in R. The corresponding y positions can be calculated. 
Example 2:
R
min <- 0max <- 1  # Specify x-values for qunif functionxpos <- seq(min, max , by = 0.02)                        # supplying corresponding y coordinationsypos <- qunif(xpos, min = 10, max = 100)         # plotting the graph plot(ypos) 
Output
dunif() method in R programming language is used to generate density function. It calculates the uniform density function in R language in the specified interval (a, b). 
Syntax:
dunif(x,  min = 0,  max = 1,  log = FALSE)
Parameter:
x: input sequence
min, max= range of values
log: indicator, of whether to display the output values as probabilities.
The result produced will be for each value of the interval. Hence, a sequence will be generated.
Example 1:
R
# generating a sequence of valuesx <- 5:10print ("dunif value")  # calculating density functiondunif(x, min = 1, max = 20)
Output
[1] “dunif value” 
[1] 0.05263158 0.05263158 0.05263158 0.05263158 0.05263158 0.05263158
All values are equal and this is the reason why it is called uniform distribution. Let us plot it for a better picture.
Example 2: 
R
min <- 0max <- 100  # Specify x-values for qunif functionxpos <- seq(min, max , by = 0.5)                        # supplying corresponding y coordinationsypos <- dunif(xpos, min = 10, max = 80)         # plotting the graph plot(ypos , type="o")  
Output
The punif() method in R is used to calculate the uniform cumulative distribution function, this is, the probability of a variable X taking a value lower than x (that is, x <= X). If we need to compute a value x > X, we can calculate 1 – punif(x).
Syntax:
punif(q,   min = 0,   max = 1, lower.tail = TRUE)
All the independent probabilities that satisfy the comparison condition will be added.
Example:
R
min <- 0 max <- 60  # calculating punif valuepunif (15 , min =min , max = max)
Output
[1] 0.25
Example:
R
min <- 0 max <- 60  # calculating punif valuepunif (15 , min =min , max = max, lower.tail=FALSE)
Output
[1] 0.75
Picked
R-Statistics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement