[R] sample variance from simulation

2009-05-18 Thread Kon Knafelman

Hi,

 g=list()
 for(i in 1:1000){z[[i]]=rnorm(15,0,1)}

I've attempted a similar problem based on the above method. Now, if i want to 
find the sample variance, do i go about it like this?

 for (i in 1:1000)vars[[i]] = sum(z[[i]])
 vars[[i]] 

the overall sigma squared will just be 1, because the distribution is standard 
normal. Is this correct?

if so, then to find (n−1)S^2/σ^2,

i will need s=999*sum(vars[[i]]))/1?

Is this correct, or am i getting lost along the way?

Thank you
 Date: Wed, 13 May 2009 16:45:22 +0100
 From: b.rowling...@lancaster.ac.uk
 To: csa...@rmki.kfki.hu
 CC: r-help@r-project.org
 Subject: Re: [R] Simulation
 
 On Wed, May 13, 2009 at 4:26 PM, Gábor Csárdi csa...@rmki.kfki.hu wrote:
  On Wed, May 13, 2009 at 5:13 PM, Debbie Zhang debbie0...@hotmail.com 
  wrote:
 
 
  Dear R users,
 
  Can anyone please tell me how to generate a large number of samples in R, 
  given certain distribution and size.
 
  For example, if I want to generate 1000 samples of size n=100, with a 
  N(0,1) distribution, how should I proceed?
 
  (Since I dont want to do rnorm(100,0,1) in R for 1000 times)
 
  Why not? It took 0.05 seconds on my 5 years old laptop.
 
  Second-guessing the user, I think she maybe doesn't want to type in
 'rnorm(100,0,1)' 1000 times...
 
  Soln - for loop:
 
   z=list()
   for(i in 1:1000){z[[i]]=rnorm(100,0,1)}
 
 now inspect the individual bits:
 
   hist(z[[1]])
   hist(z[[545]])
 
 If that's the problem, then I suggest she reads an introduction to R...
 
 Barry
 
 __
 R-help@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.

_
Looking to change your car this year? Find car news, reviews and more
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fsecure%2Dau%2Eimrworldwide%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801459%2Fpi%5F1004813%2Fai%5F859641_t=762955845_r=tig_OCT07_m=EXT
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] sample variance from simulation

2009-05-18 Thread Mike Lawrence
why not simply

vars=list()
for (i in 1:1000) vars[[i]] = var(z[[i]])


On Mon, May 18, 2009 at 6:51 AM, Kon Knafelman konk2...@hotmail.com wrote:

 Hi,

 g=list()
 for(i in 1:1000){z[[i]]=rnorm(15,0,1)}

 I've attempted a similar problem based on the above method. Now, if i want to 
 find the sample variance, do i go about it like this?

 for (i in 1:1000)vars[[i]] = sum(z[[i]])
 vars[[i]]

 the overall sigma squared will just be 1, because the distribution is 
 standard normal. Is this correct?

 if so, then to find (n-1)S^2/σ^2,

 i will need s=999*sum(vars[[i]]))/1?

 Is this correct, or am i getting lost along the way?

 Thank you
 Date: Wed, 13 May 2009 16:45:22 +0100
 From: b.rowling...@lancaster.ac.uk
 To: csa...@rmki.kfki.hu
 CC: r-help@r-project.org
 Subject: Re: [R] Simulation

 On Wed, May 13, 2009 at 4:26 PM, Gábor Csárdi csa...@rmki.kfki.hu wrote:
  On Wed, May 13, 2009 at 5:13 PM, Debbie Zhang debbie0...@hotmail.com 
  wrote:
 
 
  Dear R users,
 
  Can anyone please tell me how to generate a large number of samples in R, 
  given certain distribution and size.
 
  For example, if I want to generate 1000 samples of size n=100, with a 
  N(0,1) distribution, how should I proceed?
 
  (Since I dont want to do rnorm(100,0,1) in R for 1000 times)
 
  Why not? It took 0.05 seconds on my 5 years old laptop.

  Second-guessing the user, I think she maybe doesn't want to type in
 'rnorm(100,0,1)' 1000 times...

  Soln - for loop:

   z=list()
   for(i in 1:1000){z[[i]]=rnorm(100,0,1)}

 now inspect the individual bits:

   hist(z[[1]])
   hist(z[[545]])

 If that's the problem, then I suggest she reads an introduction to R...

 Barry

 __
 R-help@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.

 _
 Looking to change your car this year? Find car news, reviews and more
 http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fsecure%2Dau%2Eimrworldwide%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801459%2Fpi%5F1004813%2Fai%5F859641_t=762955845_r=tig_OCT07_m=EXT
[[alternative HTML version deleted]]


 __
 R-help@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.





-- 
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

__
R-help@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.


Re: [R] sample variance from simulation

2009-05-18 Thread Wacek Kusnierczyk
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

__
R-help@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.


Re: [R] sample variance from simulation

2009-05-18 Thread Mike Lawrence
Ah, I thought this smelled like homework...

Please read the R-help mailing list posting guide
(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 konk2...@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]]))

 [1] 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.kusnierc...@idi.ntnu.no
 To: konk2...@hotmail.com
 CC: mike.lawre...@dal.ca; r-help@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
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

__
R-help@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.