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

Normal Distribution in R

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Normal Distribution

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. The graph produced after plotting the value of the variable on x-axis and count of the value on y-axis is bell-shaped curve graph. The graph signifies that the peak point is the mean of the data set and half of the values of data set lie on the left side of the mean and other half lies on the right part of the mean telling about the distribution of the values. The graph is symmetric distribution. In R, there are 4 built-in functions to generate normal distribution:

  • dnorm()
    dnorm(x, mean, sd)
  • pnorm()
    pnorm(x, mean, sd)
  • qnorm()
    qnorm(p, mean, sd)
  • rnorm()
    rnorm(n, mean, sd)

where,

– x represents the data set of values – mean(x) represents the mean of data set x. It’s default value is 0.
  {\displaystyle ={\frac {1}{n}}\sum _{i=1}^{n}x_{i}={\frac {x_{1}+x_{2}+\cdots +x_{n}}{n}}}
– sd(x) represents the standard deviation of data set x. It’s default value is 1.
 {\displaystyle ={\sqrt{\frac {\sum _{i=1}^{n}(x_{i}-mean)^2}{n}\                                       
– n is the number of observations. – p is vector of probabilities

Functions To Generate Normal Distribution in R

dnorm()

dnorm()

function in R programming measures density function of distribution. In statistics, it is measured by below formula-

{\displaystyle f(x) =    \frac{1}{\sqrt{2\pi}\sigma} e^{-(x-\mu)^2/2\sigma^2}

where,

\mu

is mean and

\sigma

is standard deviation.

Syntax :

dnorm(x, mean, sd)

Example:

Python
# 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:

pnorm()

pnorm()

function is the cumulative distribution function which measures the probability that a random number X takes a value less than or equal to x i.e., in statistics it is given by-

F_X(x) = Pr[X \le x] = \alpha

Syntax:

pnorm(x, mean, sd)

Example:

Python
# 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 :

qnorm()

qnorm()

function is the inverse of

pnorm()

function. It takes the probability value and gives output which corresponds to the probability value. It is useful in finding the percentiles of a normal distribution.

Syntax:

qnorm(p, mean, sd)

Example:

Python
# 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:

rnorm()

rnorm()

function in R programming is used to generate a vector of random numbers which are normally distributed.

Syntax:

rnorm(x, mean, sd)

Example:

Python
# 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 :



Previous Article
Next Article

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 are going to see how to plot log-normal distribution in R Programming Language. A log-normal distribution is a continuous probability distribution of a random variable whose logarithm is normally distributed. In probability, if the random variable X is log-normally distributed, then Y = ln(X) has a normal distribution. To plot t
2 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: # R program to generate # random log normal density points # Setting seed for # random number generation set.seed(1000) # Set sample size N &lt;- 20 # Calling rlnorm() Function y &lt;- rlnorm(
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: # R program to compute value of # log normal quantile function # Creating x-values for density x &lt;- s
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: # R program to compute cumulative # log normal probability density # Creating
1 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: # R program to compute # log normal probability density # Creating x-values for density x &lt;- seq(1, 10, by = 1)
1 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
Multiple Linear Regression Model with Normal Equation
Prerequisite: NumPy Consider a data set, area (x1)rooms (x2)age (x3)price (y)2338656215274569244968972954756231768234253107485 let us consider, Here area, rooms, age are features / independent variables and price is the target / dependent variable. As we know the hypothesis for multiple linear regression is given by: [Tex]$h_{\theta}(x)=\theta_{0}
3 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
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 dis
3 min read
Compute Density of the Distribution Function in R Programming - dunif() Function
dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: # Create vector of random deviation u &lt;- runif(20)
1 min read
NLP | Storing Conditional Frequency Distribution in Redis
The nltk.probability.ConditionalFreqDist class is a container for FreqDist instances, with one FreqDist per condition. It is used to count frequencies that are dependent on another condition, such as another word or a class label. It is being used here to create an API-compatible class on top of Redis using the RedisHashFreqDist . In the code given
2 min read
NLP | Storing Frequency Distribution in Redis
The nltk.probability.FreqDist class is used in many classes throughout NLTK for storing and managing frequency distributions. It's quite useful, but it's all in-memory, and doesn't provide a way to persist the data. A single FreqDist is also not accessible to multiple processes. All that can be changed by building a FreqDist on top of Redis. What i
3 min read
Inverse Gamma Distribution in Python
Inverse Gamma distribution is a continuous probability distribution with two parameters on the positive real line. It is the reciprocate distribution of a variable distributed according to the gamma distribution. It is very useful in Bayesian statistics as the marginal distribution for the unknown variance of a normal distribution. It is used for c
2 min read
Seaborn | Distribution Plots
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. This article deals with the distribution plots in seaborn which is used for examining univariate and bivariate distributions. In this article we will be discussing 4 types of distributio
3 min read
Python - Student’s t Distribution in Statistics
We know the mathematics behind t-distribution. However, we can also use Python to implement t-distribution on a dataset. Python provides a unique package scipy for various statical techniques and methods. We will use this package for t-distribution implementation. prerequisite: t-distribution What is t-Distribution The t-distribution, also known as
5 min read
Binomial Distribution in R Programming
Binomial distribution in R is a probability distribution used in statistics. The binomial distribution is a discrete distribution and has only two outcomes i.e. success or failure. All its trials are independent, the probability of success remains the same and the previous outcome does not affect the next outcome. The outcomes from different trials
3 min read
Converting Power Law Distribution to a Linear graph
Whenever we are working on ML projects, we have to deal with the high dimensionality of the dataset. There is a very particular term to refer to here i.e. Curse of dimensionality. Although there are many ways to handle this problem one of the solutions can be to change the coordinate systems. This solution is somewhat interesting and new from a dat
2 min read
Create a Random Sequence of Numbers within t-Distribution in R Programming - rt() Function
rt() function in R Language is used to create a random sequence of values from Student t-distribution. Syntax: rt(n, df, ncp) Parameters: n: Number of observations df: Degree of Freedom ncp: Numeric vector of non-centrality parameters. Example 1: # R Program to create random sequence # from t distribution # Calling rt() Function rt(15, 2) rt(15, 1)
1 min read
Perform Probability Density Analysis on t-Distribution in R Programming - dt() Function
dt() function in R Language is used to return the probability density analysis on the Student t-distribution with random variable x and degree of freedom df. Syntax: dt(x, df) Parameters: x: Random Variable df: Degree of Freedom Example 1: # R Program to perform # Probability Density Analysis # Calling dt() Function dt(0, 10) dt(1, 15) dt(3, 40) Ou
1 min read
Perform the Probability Cumulative Density Analysis on t-Distribution in R Programming - pt() Function
pt() function in R Language is used to return the probability cumulative density of the Student t-distribution. Syntax: pt(x, df) Parameters: x: Random variable df: Degree of Freedom Example 1: # R Program to perform # Cumulative Density Analysis # Calling pt() Function pt(2, 10) pt(.542, 15) Output: [1] 0.963306 [1] 0.7021105 Example 2: # R Progra
1 min read
Perform the Inverse Probability Cumulative Density Analysis on t-Distribution in R Programming - qt() Function
qt() function in R Language is used to return the inverse probability cumulative density of the Student t-distribution. Syntax: qt(x, df)Parameters: x: Random variable df: Degree of Freedom Example 1: C/C++ Code # R Program to perform Inverse # Cumulative Density Analysis # Calling qt() Function qt(.95, 10) qt(.542, 15) Output: [1] 1.812461 [1] 0.1
1 min read
Compute Beta Distribution in R Programming - dbeta(), pbeta(), qbeta(), and rbeta() Functions
Beta Distribution in R Language is defined as property which represents the possible values of probability. This article is an illustration of dbeta, pbeta, qbeta, and rbeta functions of Beta Distribution. dbeta() Function It is defined as Beta Density function and is used to create beta density value corresponding to the vector of quantiles. Synta
2 min read
Compute the Value of Empirical Cumulative Distribution Function in R Programming - ecdf() Function
ecdf() function in R Language is used to compute and plot the value of Empirical Cumulative Distribution Function of a numeric vector. Syntax: ecdf(x) Parameters: x: Numeric Vector Example 1: # R Program to compute the value of # Empirical Cumulative Distribution Function # Creating a Numeric Vector x &lt;- seq(1, 50, by = 2) # Calling ecdf() Funct
1 min read
Compute the value of F Cumulative Distribution Function in R Programming - pf() Function
pf() function in R Language is used to compute the density of F Cumulative Distribution Function over a sequence of numeric values. It also plots a density graph for F Cumulative Distribution. Syntax: pf(x, df1, df2) Parameters: x: Numeric Vector df: Degree of Freedom Example 1: # R Program to compute # Cumulative F Density # Creating a sequence of
1 min read
Compute the value of Quantile Function over F Distribution in R Programming - qf() Function
qf() function in R Language is used to compute the value of quantile function over F distribution for a sequence of numeric values. It also creates a density plot of quantile function over F Distribution. Syntax: qf(x, df1, df2) Parameters: x: Numeric Vector df: Degree of Freedom Example 1: # R Program to compute value of # Quantile Function over F
1 min read
Compute the Value of Quantile Function over Weibull Distribution in R Programming - qweibull() Function
qweibull() function in R Language is used to compute the value of Quantile Function for Weibull Distribution. Syntax: qweibull(x, shape) Parameters: x: Numeric Vector shape: Shape Parameter Example 1: # R Program to compute the value of # Quantile Weibull Function # Creating a sequence of x-values x &lt;- seq(0, 1, by = 0.2) # Calling qweibull() Fu
1 min read
Compute the value of CDF over Studentized Range Distribution in R Programming - ptukey() Function
ptukey() function in R Language is used to compute the value of Cumulative Distribution Function(CDF) over Studentized Range for a sequence of numeric values. Syntax: ptukey(x, nmeans, df) Parameters: x: Numeric Vector nmeans: Number of means df: Degree of Freedom Example 1: # R Program to compute the value of # CDF for Studentized Range # Creating
1 min read
Compute the value of Quantile Function over Studentized Distribution in R Programming - qtukey() Function
qtukey() function in R Language is used to compute the value of Studentized Range Quantile Function over a sequence of Numeric Values. Syntax: qtukey(x, nmeans, df) Parameters: x: Numeric Vector nmeans: Number of means df: Degree of Freedom Example 1: # R Program to compute the value of # Quantile Function Studentized Range # Creating a sequence of
1 min read
Practice Tags :