Simulate the random variable

In this example, we’ll simulate a random variable with a normal distribution.

The parameters of the distribution can be edited here:

meanPopulation <- 1
varPopulation <- 2
sdPopulation <- sqrt(varPopulation)

The distribution looks like this:

Sampling

Now, we can take some samples from the population:

nSamples <- 40
sample <- rnorm(nSamples, mean = meanPopulation, sd = sdPopulation)

And calculate the sample mean and variance of our sample:

meanSample <- mean(sample)
varSample <- var(sample)
sdSample <- sqrt(varSample)

Here we show the population and sample means. We expect them to be closer as the sample gets larger:

##                    Population    Sample
## Mean                 1.000000 0.6322541
## Variance             2.000000 2.4607796
## Standard deviation   1.414214 1.5686872

Our inference

Using these parameters for the predicted distribution, we find that it differs slightly from the real one. This difference is expected to be smaller as the sample gets larger: