The Wayback Machine - https://web.archive.org/web/20250304101342/https://www.geeksforgeeks.org/normal-distribution-in-r/
Open In App

Normal Distribution in R

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we will discuss in detail the normal distribution and different types of built-in functions to generate normal distribution using R Programming Language.

What is Normal Distribution in R?

Normal Distribution in R is a probability function used in statistics that tells about how the data values are distributed. It is the most important probability distribution function used in statistics because of its advantages in real-case scenarios. For example, the height of the population, shoe size, IQ level, rolling a dice, and many more. It is generally observed that data distribution is normal when there is a random collection of data from independent sources. In R, there are 4 built-in functions to generate normal distribution:

  1. dnorm()
  2. pnorm()
  3. qnorm()
  4. rnorm()

Now we will discuss all 4 built-in functions to generate normal distribution in detail using R Programming Language.

1: Genrate Normal Distribution using dnorm()

The dnorm() function computes the density (height of the probability density function) at a given point for a normal distribution.

norm(x, mean = 0, sd = 1, log = FALSE)

Where,

  • x: A vector of quantiles.
  • mean: The mean of the distribution.
  • sd: The standard deviation of the distribution.
  • log: If TRUE, probabilities p are returned as log(p).

Lets perform one example to Genrate Normal Distribution using dnorm() in R.

R
# creating a sequence of values 
# between -15 to 15 with a difference of 0.1
x = seq(-15, 15, by=0.1)
 
y = dnorm(x, mean(x), sd(x))
 
# output to be present as PNG file
png(file="dnormExample.png")
 
# Plot the graph.
plot(x, y)
 
# saving the file
dev.off()  

Output:

Normal Distribution in R

2: Genrate Normal Distribution using pnorm()

The pnorm() function calculates the cumulative probability up to a given quantile.

pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)

Where,

  • q: A vector of quantiles.
  • mean: The mean of the distribution.
  • sd: The standard deviation of the distribution.
  • lower.tail: If TRUE, probabilities are P[X ≤ x]; otherwise, P[X > x].
  • log.p: If TRUE, probabilities p are returned as log(p).

Lets perform one example to Genrate Normal Distribution using pnorm() in R.

R
# creating a sequence of values
# between -10 to 10 with a difference of 0.1
x <- seq(-10, 10, by=0.1)

y <- pnorm(x, mean = 2.5, sd = 2)

# output to be present as PNG file
png(file="pnormExample.png")

# Plot the graph.
plot(x, y)

# saving the file
dev.off() 

Output:

Normal Distribution in R

3: Genrate Normal Distribution using qnorm()

The qnorm() function computes the quantile (inverse of pnorm()) for a given cumulative probability.

qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)

  • p: A vector of probabilities.
  • mean: The mean of the distribution.
  • sd: The standard deviation of the distribution.
  • lower.tail: If TRUE, probabilities are P[X ≤ x]; otherwise, P[X > x].
  • log.p: If TRUE, probabilities p are considered as log(p).

Lets perform one example to Genrate Normal Distribution using qnorm() in R.

R
# Create a sequence of probability values 
# incrementing by 0.02.
x <- seq(0, 1, by = 0.02)

y <- qnorm(x, mean(x), sd(x))

# output to be present as PNG file
png(file = "qnormExample.png")

# Plot the graph.
plot(x, y)

# Save the file.
dev.off()

Output:

Normal Distribution in R

4: Genrate Normal Distribution using rnorm()

The rnorm() function generates random numbers following a normal distribution.

rnorm(n, mean = 0, sd = 1)

  • n: Number of observations to generate.
  • mean: The mean of the distribution.
  • sd: The standard deviation of the distribution.

Lets perform one example to Genrate Normal Distribution using qnorm() in R.

R
# Create a vector of 1000 random numbers
# with mean=90 and sd=5
x <- rnorm(10000, mean=90, sd=5)

# output to be present as PNG file
png(file = "rnormExample.png")

# Create the histogram with 50 bars
hist(x, breaks=50)

# Save the file.
dev.off()

Output:

Normal Distribution in R

Conclusion

The normal distribution is a crucial concept in statistics, with applications ranging from data analysis to inferential statistics. R provides a variety of functions (dnorm(), pnorm(), qnorm(), rnorm()) to work with normal distributions, making it easy to perform statistical analyses and generate visualizations. Understanding how to use these tools is essential for effective data analysis in R.


Get IBM Certification and a 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

Master Machine Learning, Data Science & AI with this complete program and also get a 90% refund. What more motivation do you need? Start the challenge right away!


Next Article
Practice Tags :

Similar Reads

Plot Normal Distribution over Histogram in R
In this article, we will discuss how to plot normal distribution over Histogram in the R Programming Language. In a random dataset, it is generally observed that the distribution of data is normal i.e. on its visualization using density plot with the value of the variable in the x-axis and y-axis we get a bell shape curve. The center of that curve
3 min read
Simulate Bivariate and Multivariate Normal Distribution in R
In this article, we will learn how to simulate Bivariate and Multivariate Normal distribution in the R Programming Language. To simulate a Multivariate Normal Distribution in the R Language, we use the mvrnorm() function of the MASS package library. The mvrnorm() function is used to generate a multivariate normal distribution of random numbers with
3 min read
How to Plot a Log Normal Distribution in R
In this article, we will explore how to plot a log-normal distribution in R, providing you with an understanding of its properties and the practical steps for visualizing it using R Programming Language.Log-Normal DistributionThe log-normal distribution is a probability distribution of a random variable whose logarithm is normally distributed. This
5 min read
Compute Log Normal Probability Density in R Programming - dlnorm() Function
dlnorm() function in R Language is used to compute the log normal value of the probability density function. It also creates a plot of the log normal density. Syntax: dlnorm(vec) Parameters: vec: x-values for normal density Example 1: Python3 1== # R program to compute # log normal probability density # Creating x-values for density x <- seq(1,
1 min read
Compute Cumulative Log Normal Probability Density in R Programming - plnorm() Function
plnorm() function in R Language is used to compute the log normal value of the cumulative probability density function. It also creates a plot of the cumulative distribution of log normal density. Syntax: plnorm(vec) Parameters: vec: x-values for normal density Example 1: Python3 1== # R program to compute cumulative # log normal probability densit
1 min read
Compute value of Log Normal Quantile Function in R Programming - qlnorm() Function
qlnorm() function in R Language is used to compute the value of log normal quantile function. It also creates a plot of the quantile function of log normal distribution. Syntax: qlnorm(vec) Parameters: vec: x-values for normal density Example 1: Python3 1== # R program to compute value of # log normal quantile function # Creating x-values for densi
1 min read
Compute Randomly Drawn Log Normal Density in R Programming - rlnorm() Function
rlnorm() function in R Language is used to compute random log normal density among a range of inputs. Syntax: rlnorm(N) Parameters: N: Sample Size Example 1: Python3 1== # R program to generate # random log normal density points # Setting seed for # random number generation set.seed(1000) # Set sample size N <- 20 # Calling rlnorm() Function y
1 min read
Normal Probability Plot
The probability plot is a way of visually comparing the data coming from different distributions. These data can be of empirical dataset or theoretical dataset. The probability plot can be of two types:P-P plot: The (Probability-to-Probability) p-p plot is the way to visualize the comparing of cumulative distribution function (CDFs) of the two dist
3 min read
Normal Probability Plot in R using ggplot2
A normal probability plot is a graphical representation of the data. A normal probability plot is used to check if the given data set is normally distributed or not. It is used to compare a data set with the normal distribution. If a given data set is normally distributed then it will reside in a shape like a straight line. In this article, we are
2 min read
What's the difference of stateless LSTM and a normal feed-forward NN?
Answer: Stateless LSTM disregards past sequences, while a feed-forward neural network lacks internal memory and processes inputs sequentially.Here is a comparison between stateless LSTM and a normal feed-forward NN: AspectStateless LSTMFeed-Forward Neural Network (NN)ArchitectureIncorporates Long Short-Term Memory (LSTM) cells that allow capturing
2 min read