Normal Distribution in R – Basic Probability distribution


1. Objective

In this tutorial, we will be going to learn about Normal Distribution in R.we will also cover different functions which will help in generating the normal distribution.Along with this, we will also include graphs for easy representation and understanding.

Normal Distribution in R

2. Introduction to Normal Distribution in R

Generally, it is observed that the collection of random data from independent sources is distributed normally.We get a bell shape curve on plotting a graph with the value of the variable on the horizontal axis and the count of the values in the vertical axis.The center of the curve represents the mean of the dataset.

It has four inbuilt functions. They are described below:

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

Description of the parameters used in above functions −

  • x is a vector of numbers.
  • p is a vector of probabilities.
  • n is a number of observations(sample size).
  • here mean is the mean value of the sample data. Also, its default value is zero.
  • sd is the standard deviation. Its default value is 1.

2.1 Functions to generate Normal Distribution

a. dnorm()

The height of the probability distribution at each point for a given mean and standard deviation is provided by this function.

Purpose: Probability Density Function (PDF)

Syntax: dnorm(x, mean, sd)

For Example:

Create a sequence of numbers between -10 and 10 incrementing by 0.1.

x <- seq(-10, 10, by = .1)

# Choose the mean as 2.5 and standard deviation as 0.5.
y <- dnorm(x, mean = 2.5, sd = 0.5)

# Give the chart file a name.
png(file = “dnorm.png”)

plot(x,y)

# Save the file.
dev.off()

When we execute the above code, it produces the following result −

sequence of numbers between -10 and 10 incrementing by 0.1.

b. pnorm()

Purpose: Cumulative Distribution Function (CDF)

Syntax:pnorm(q, mean, sd)

For Example:

Create a sequence of numbers between -10 and 10 incrementing by 0.2.
x <- seq(-10,10,by = .2)

# Choose the mean as 2.5 and standard deviation as 2.
y <- pnorm(x, mean = 2.5, sd = 2)

# Give the chart file a name.
png(file = “pnorm.png”)

# Plot the graph.
plot(x,y)

# Save the file.
dev.off()

When we execute the above code, it produces the following result −

Create a sequence of numbers between -10 and 10 incrementing by 0.2.

c. qnorm()

Purpose:Quantile Function – inverse of pnorm

Syntax: qnorm(p, mean, sd)

For Example:

Create a sequence of probability values incrementing by 0.02.

x <- seq(0, 1, by = 0.02)

# Choose the mean as 2 and standard deviation as 3.
y <- qnorm(x, mean = 2, sd = 1)

# Give the chart file a name.
png(file = “qnorm.png”)

# Plot the graph.
plot(x,y)

# Save the file.
dev.off()

When we execute the above code, it produces the following result −

Create a sequence of probability values incrementing by 0.02.

d. rnorm()

Purpose: Generates random numbers from normal distribution

Syntax: rnorm(n, mean, sd)

For Example:

Create a sample of 50 numbers which are normally distributed.

y <- rnorm(50)

# Give the chart file a name.
png(file = “rnorm.png”)

# Plot the histogram for this sample.
hist(y, main = “Normal DIstribution”)

# Save the file.
dev.off()

When we execute the above code, it produces the following result −

Create a sample of 50 numbers which are normally distributed.

3. Conclusion

We have studied Normal distribution in R in detail. We have also learnt different functions which are used in generating Normal Distribution. In above-mentioned information, we have used graphs as well as syntax and examples also which helps you a lot in an understanding of R Normal distribution and their functions.

 

Leave a comment

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