[R] Using R to illustrate the Central Limit Theorem

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Apr 21 22:21:39 CEST 2005


(Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> writes:

> On 21-Apr-05 Paul Smith wrote:
> > Dear All
> > 
> > I am totally new to R and I would like to know whether R is able and
> > appropriate to illustrate to my students the Central Limit Theorem,
> > using for instance 100 independent variables with uniform distribution
> > and showing that their sum is a variable with an approximated normal
> > distribution.
> > 
> > Thanks in advance,
> > 
> > Paul
> 
> Similar to Francisco's suggestion:
> 
>   m<-numeric(10000);
>   for(k in (1:20)){
>     for(i in(1:10000)){m[i]<-(mean(runif(k))-0.5)*sqrt(12*k)}
>     hist(m,breaks=0.3*(-15:15),xlim=c(-4,4),main=sprintf("%d",k))
>   }

Ted, you and several others in this thread may want to notice the
existence of a little function called replicate():

...
    m <- replicate(10000, (mean(runif(k))-0.5)*sqrt(12*k)) 
...


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list