[R] Problems with sample variance

Peter Alspach PAlspach at hortresearch.co.nz
Thu May 21 07:01:40 CEST 2009


Thanks Milton - I misread Chloe's original question.

nSims <- 1000
sampSiz <- 15
myVars <- tapply(rnorm(nSims*sampSiz), rep(1:nSims, sampSiz), var)

This is a little more succinct, although I do not know enough about
random number generation to be certain about the effect of breaking a
single call to rnorm into 1000 bits, rather than having 1000 calls to
rnorm (or even if there is such an effect.  Comment anyone?).

It used to be (and I imagine still is) not efficient incrementally grow
an object in a loop [as is done in your code with variance.list <-
c(variance.list, var(mysample))].  Better to create variance.list at its
full size first (filled with NA) and then populate it.  Not that it is
likely to make too much difference with these numbers.

HTH .....

Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of milton ruser
> Sent: Thursday, 21 May 2009 4:30 p.m.
> To: Chloe Smith
> Cc: r-help at r-project.org
> Subject: Re: [R] Problems with sample variance
> 
> Hi Chloe,
> 
> Try this:
> 
> NUMBER.OF.RUNS<-10 #change here
> for (NR in 1:NUMBER.OF.RUNS)
> {
> number.simulation<-1000
> sample.size=15;
> variance.list<-NULL
> for(i in 1:number.simulation)
>    {
>    mysample<-rnorm(sample.size)
>    variance.list<- c(variance.list, var(mysample))
>    } #i
> hist(variance.list)
> } #NR
> 
> 
> Bests
> 
> milton
> brazil=toronto
> 
> On Wed, May 20, 2009 at 8:16 PM, Chloe Smith 
> <hapiasfion at hotmail.com> wrote:
> 
> >
> > Dear R users,
> >
> > I am a beginner to R. I generated 1000 samples with 15 data in each 
> > sample
> >
> > I tried finding the variance for each sample
> >
> > I used the code:
> >
> > m=1000;n=15
> > > r<-rnorm(15000)
> > > for(i in 1:m){
> > x=data[,i]
> > v=var(x)}
> >
> > what I got was just the variance for the last sample i.e. 
> the 1000th 
> > sample
> >
> > but what I want is 1000 variance.
> >
> > Does anyone know what I did wrong?
> >
> > Thanks
> >
> > Chloe Smith
> >
> > --
> > View this message in context:
> > 
> http://www.nabble.com/Problems-with-sample-variance-tp23645919p2364591
> > 9.html Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > 
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/p
> > osting-guide.html> and provide commented, minimal, self-contained, 
> > reproducible code.
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.




More information about the R-help mailing list