4.2 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Generate a Normal Distribution in R (With Examples)

How to Generate a Normal Distribution in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can quickly generate a normal distribution in R by using the rnorm() function, which uses the following syntax:

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

where:

  • n: Number of observations.
  • mean: Mean of normal distribution. Default is 0.
  • sd: Standard deviation of normal distribution. Default is 1.

This tutorial shows an example of how to use this function to generate a normal distribution in R.

Related: A Guide to dnorm, pnorm, qnorm, and rnorm in R

Example: Generate a Normal Distribution in R

The following code shows how to generate a normal distribution in R:

#make this example reproducible
set.seed(1)

#generate sample of 200 obs. that follows normal dist. with mean=10 and sd=3
data #view first 6 observations in sample
head(data)

[1]  8.120639 10.550930  7.493114 14.785842 10.988523  7.538595

We can quickly find the mean and standard deviation of this distribution:

#find mean of sample
mean(data)

[1] 10.10662

#find standard deviation of sample
sd(data)

[1] 2.787292

We can also create a quick histogram to visualize the distribution of data values:

hist(data, col='steelblue')

Generate normal distribution in R

We can even perform a Shapiro-Wilk test to see if the dataset comes from a normal population:

shapiro.test(data)

	Shapiro-Wilk normality test

data:  data
W = 0.99274, p-value = 0.4272

 The p-value of the test turns out to be 0.4272. Since this value is not less than .05, we can assume the sample data comes from a population that is normally distributed.

This result shouldn’t be surprising since we generated the data using the rnorm() function, which naturally generates a random sample of data that comes from a normal distribution.

Additional Resources

How to Plot a Normal Distribution in R
A Guide to dnorm, pnorm, qnorm, and rnorm in R
How to Perform a Shapiro-Wilk Test for Normality in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories