# [R] sample variance from simulation

Mike Lawrence Mike.Lawrence at dal.ca
Mon May 18 16:10:42 CEST 2009

```Ah, I thought this smelled like homework...

(http://www.r-project.org/posting-guide.html), specifically:

"Basic statistics and classroom homework:  R-help is not intended for these."

On Mon, May 18, 2009 at 10:35 AM, Kon Knafelman <konk2001 at hotmail.com> wrote:
> Hey,
>
> when i type in either of those formulas into R, i dont really get the answer
> im looking for. For such large samples, isnt the sample variance meant to
> approach the actual variance, which is 1 for a standard normal?
>
> also, when i use sapply, i 1000 results for variance, where i think i just
> need one number.
>
> I've worked on this problem for so long. The initial problem is as follows
>
> "Use the simulation capacity of R to generate m = 1 000
> samples of size n = 15 from a N(0,1) distribution. Compute the statistic
> (n-1)S^2/σ^2 for the normally generated values, labelling as NC14. Produce
> probability histogram for NC14 and superimpose the theoretical distribution
> for a χ2 (14 degrees of freedom)"
>
>> g=list()
>> for(i in 1:1000){z[[i]]=rnorm(15,0,1)}
>
>> for (i in 1:1000)vars[[i]] = sum(z[[i]])
>
>> vars[[i]]
>
>> sum(var(z[[i]]))
>
>  0.9983413
>
> Does this make sense?  my logic is that i use the loop again to add up all
> the individual variances. im not really sure if i did it correctly, but if
> someone could make the necessary corrections, i'd be very very greatful.
>
> Thanks heaps guys for taking the time to look at this
>
>> Date: Mon, 18 May 2009 15:06:47 +0200
>> From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
>> To: konk2001 at hotmail.com
>> CC: Mike.Lawrence at dal.ca; r-help at r-project.org
>> Subject: Re: [R] sample variance from simulation
>>
>> Mike Lawrence wrote:
>> > why not simply
>> >
>> > vars=list()
>> > for (i in 1:1000) vars[[i]] = var(z[[i]])
>> >
>> >
>>
>> ... or, much simpler,
>>
>> vars = sapply(z, var)
>>
>> vQ
>
> ________________________________
> Let ninemsn property help Looking to move somewhere new this winter?

--
Mike Lawrence