Sampling from a normal distribution
From observing real-world data, we may determine that most data follows a normal distribution. That is to say, when we use a density curve (or histogram) to plot the data, we should see it as bell-shaped. As this kind of distribution is so commonly seen in nature or social science, scientists often use a normal distribution to represent real-value random variables from an unknown distribution. In this recipe, we will demonstrate how R generates samples from a normal distribution.
Getting ready
In this recipe, you need to prepare your environment with R installed.
How to do it…
Please perform the following steps to generate samples from a normal distribution:
First, you can use the
rnorm
function to generate30
and1000
samples from a normal distribution:> set.seed(123) > data1 <- rnorm(30) > data2 <- rnorm(1000)
We can then use the
hist
function to plot the histogram ofdata1
anddata2
:> par(mfrow = c(1,2)) > hist(data1, main="30 samples...