Carlos Junior
Carlos Junior

Reputation: 67

How to repeat rnorm in R using each value in a vector as the mean?

I have created a code using rnorm with the mean set to 1000 and now I want to create a vector of 10000 elements, each one being the mean of this normal distribution I created. How do I do it?

I have tried using

a = replicate(10000, (mean(rnorm(4, mean=1000, sd=4))))

but then I get values around 1004, what makes me believe it is not the mean of the distribution that I am getting. Is there something I am missing.

Upvotes: 1

Views: 1489

Answers (1)

Cettt
Cettt

Reputation: 11981

as pointed out in the comments: the fact that you are only using four random numbers in your sample can have the consequence that the empirial mean is far away from the true mean (which is 1000).

As you increase the number of random numbers per iteration, the empirical means will get closer to the true mean (when talking about unbiased estimators this is exactly what is meant).

See e.g. the following

set.seed(123)
replicate(5, (mean(rnorm(4, mean=1000, sd=4))))
[1] 1000.8386 1001.0402 1000.4514 1001.7425  998.7598
replicate(5, (mean(rnorm(1000, mean=1000, sd=4))))
[1] 1000.0734 1000.1480  999.9374  999.9468  999.8768
replicate(5, (mean(rnorm(100000, mean=1000, sd=4))))
[1]  999.9981  999.9939 1000.0004 1000.0001  999.9966

Upvotes: 1

Related Questions