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) Thanks for help Debbie _________________________________________________________________ Looking to change your car this year? Find car news, reviews and more e%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]]
On Wed, May 13, 2009 at 5:13 PM, Debbie Zhang <debbie0621 at 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. Gabor> > > Thanks for help > > > > Debbie > > _________________________________________________________________ > Looking to change your car this year? Find car news, reviews and more > > e%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 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. >-- Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
what about putting in a matrix, e.g., matrix(rnorm(1000*100), 1000, 100) I hope it helps. Best, Dimitris Debbie Zhang 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) > > > > Thanks for help > > > > Debbie > > _________________________________________________________________ > Looking to change your car this year? Find car news, reviews and more > > e%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 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Dear Debbie, Here are two options: # Parameters N <- 1000 n <- 100 # Option 1 mys <- replicate(N, rnorm(n)) mys # Option 2 mys2 <- matrix(rnorm(N*n),ncol=N) mys2 HTH, Jorge On Wed, May 13, 2009 at 11:13 AM, Debbie Zhang <debbie0621@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) > > > > Thanks for help > > > > Debbie > > _________________________________________________________________ > Looking to change your car this year? Find car news, reviews and more > > > e%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. >[[alternative HTML version deleted]]
Does every 100 numbers in rnorm(100 * 1000, 0, 1) have the N(0,1) distribution? On Wed, May 13, 2009 at 11:13 PM, Debbie Zhang <debbie0621 at 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) > > > > Thanks for help > > > > Debbie > > _________________________________________________________________ > Looking to change your car this year? Find car news, reviews and more > > e%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 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. >
If you want k samples of size n, why generate k*n samples and put them in a k-by-n matrix where you can do what you want to each sample: k = 10 n = 100 x=matrix(rnorm(k*n),k,n) rowMeans(x) If you need to do more complex things to each sample and if k is large enough that you don't want the matrix sitting around in memory while you do these things, you could also check out ?replicate . On Wed, May 13, 2009 at 12:13 PM, Debbie Zhang <debbie0621 at 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) > > > > Thanks for help > > > > Debbie > > _________________________________________________________________ > Looking to change your car this year? Find car news, reviews and more > > e%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 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. >-- 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. ~
So far nobody seems to have warned the OP about seeding. Presumably Debbie wants 1000 different sets of samples, but as we all know there are ways to get the same sequence (initial seed) every time. If there's a starting seed for one of the "generate a single giant matrix" methods proposed, the whole matrix will be the same for a given seed. If rnorm is called 1000 times (hopefully w/ different random (oops) seeds), the entire set of samples will be different. and so on. Carl
Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote>> Seriously? You think: >> >> lapply(1:n, rnorm, 0, 1) >> >> is 'clearer' than: >> >> x=list() >> for(i in 1:n){ >> x[[i]]=rnorm(i,0,1) >> } >> >> for beginners? >> >> Firstly, using 'lapply' introduces a function (lapply) that doesn't >> have an intuitive name. Also, it takes a function as an argument. The >> concept of having a function as a parameter to another function is >> something that a lot of programming beginners have trouble with - >> unless they were brought up on LISP of course, and few of us are. >> >> I propose that the for-loop example is clearer to a larger population >> than the lapply version. >As a beginner, I agree .... the for loop is much clearer to me. Peter Peter L. Flom, PhD Statistical Consultant www DOT peterflomconsulting DOT com
Stefan Grosse wrote:> Debbie Zhang schrieb: > >> Now, I am trying to obtain the sample variance (S^2) of the 1000 samples that I have generated before. >> >> I am wondering what command I should use in order to get the sample variance for all the 1000 samples. >> >> >> >> What I am capable of doing now is just typing in >> >> var(z[[1]]) >> >> var(z[[2]])..................... >> >>if you have the data produced the for loop way, i.e., as a list of vectors, you can go the intuitive way: vars = list() for (i in 1:1000) vars[[i]] = z[[i]] or the unintuitive way: vars = lapply(z, var) if you have the data produced the matrix or replicate way (i.e., a matrix with columns representing samples), you can go the intuitive way: vars = c() for (i in 1:1000) vars[i] = var(z[,i]) or the unintuitive way: vars = apply(z, 2, var) consider reading an intro to r unless you like to receive responses as the one below. vQ> Common please use the package brain 2.0.
hey guys, i've been following this discussion about the simulation, and being a beginner myself, im really unsure of the best method. I hve the same problem as the initial one, except i need 1000 samples of size 15, and my distribution is Exp(1). I've adjusted some of the loop formulas for my n=15, but im unsure how to proceed in the quickest way. Can someone please help? _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
Hi, that was really helpful about the simulation. Im now trying to find (n−1)S2/σ2, and fit it to a chi squared dist with 5 degrees of freedom. im having trouble with the coding for this. i think for the second part of that i need to use the fitdist function, but to get it to where i am able to do that, im not sure what to do. THis is what i have been trying to do so far, but it hasn't returned me anything good sum((x-mean(x))^2)/(length(x)-1) i am really confused, can someone please help? Cheers ps. if someone knows of a simpler way of creating the simulation in the first place, your input is highly appreciated using your method, im getting a bit confused.> Date: Fri, 15 May 2009 12:16:45 +0100 > From: S.Ellison@lgc.co.uk > To: konk2001@hotmail.com > Subject: Re: [R] Simulation > > The tidiest way of doing something 'simple' with a set of B random > samples of size n is often to create a matrix with b rows and n columns, > and then use apply() with a suitable function to get the statistic of > interest. > > For example, in the particular case of exp(1): > > samples<-matrix( rexp(1000*15), ncol=15) #generates a 1000 x 15 > matrix; > #each row is an n=15 sample from > exp(1) > > meds<-apply(samples, 1, median) #applies the function 'median' to each > row and returns the result as a vector > > hist(meds) > > For more complex problems, write a function that can be applied to each > row. For example, look at the distribution of the test statisic for a > wilcoxon test of whether the medians above are centred on 1.0: > > medtest<-function(x) { > wilcox.test(x, mu=1)$statistic #'statistic' is the name of the > calculated test statistic here > #don;t need to 'return' anything because the function returns > #the value of the last expression evaluated. > } > > medsim<-apply(samples,1, medtest) > hist(medsim) > > This trick (form a matrix and use apply()) can also be used to get > multi-parameter stats, 'cos apply will return a matrix or list if the > function returns a vector or list. > > The only snag is the memory usage; for large simulations that can be a > problem. If it is, resorting to looping is still an option. > > Steve E > > >>> Kon Knafelman <konk2001@hotmail.com> 15/05/2009 10:17 >>> > > hey guys, i've been following this discussion about the simulation, and > being a beginner myself, im really unsure of the best method. > > > > I hve the same problem as the initial one, except i need 1000 samples > of size 15, and my distribution is Exp(1). I've adjusted some of the > loop formulas for my n=15, but im unsure how to proceed in the quickest > way. > > > > Can someone please help? > > _________________________________________________________________ > [[elided Hotmail spam]] > > [[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. > > ******************************************************************* > This email and any attachments are confidential. Any use, copying or > disclosure other than by the intended recipient is unauthorised. If > you have received this message in error, please notify the sender > immediately via +44(0)20 8943 7000 or notify postmaster@lgc.co.uk > and delete this message and any copies from your computer and network. > LGC Limited. Registered in England 2991879. > Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK_________________________________________________________________ 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]]
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 <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]])) > > [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.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 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. ~