Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mean(rnorm(100,mean=0,sd=1)) is not 0; and sd(rnorm(100,mean=0,sd=1)) is not 1. Why?

(Reproducible example added.) I am little bit confused about rnorm function. I expected mean(rnorm(100,mean=0,sd=1)) to be 0; and sd(rnorm(100,mean=0,sd=1)) to be 1. But gave different results. Where am I wrong?

Reproducible Example:

mean(rnorm(100,mean=0,sd=1))
# [1] 0.07872548
sd(rnorm(100,mean=0,sd=1))
# [1] 1.079348

Any help is greatly appreciated.

like image 486
Erdogan CEVHER Avatar asked Jan 05 '15 21:01

Erdogan CEVHER


1 Answers

rnorm(100) gives you a random sample of 100 values from distribution mean = 0 and sd = 1. Because it is random, the actual value of mean(rnorm(100)) depends on which particular values you get back. There is no guarantee that the mean will be 0, but statistically it should converge to 0 as you use larger sample sizes. For example, try mean(rnorm(10000)); it will probably be closer to 0 than before.

Edit: If you want to force the sample to have a particular mean and standard deviation, check out this question: "Generate random numbers with fixed mean and sd".

like image 127
rsoren Avatar answered Oct 06 '22 00:10

rsoren