Hi, Is there a way to simulate a population with R and pull out m samples, each with n values for calculating m means? I need that kind of data to plot a graphic, demonstrating the central limit theorem and I don't know how to begin. So, perhaps someone can give me some tips and hints how to start and which functions to use. thanks for any help, joerg
Look at the clt.examp function in the TeachingDemos package, it generates samples from normal, uniform, gamma (default exponential), and beta (default U-shaped) distributions and plots histograms of the means along with a reference line of a normal distribution with the same mean and sd. The default sample size is 1 which will show the shape of the population, then you can run it again with larger values of n to show how all of them become more and more normal (the exponential is the slowest). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of J?rg Gro? > Sent: Wednesday, October 15, 2008 1:49 PM > To: r-help at r-project.org > Subject: [R] plot - central limit theorem > > Hi, > > > Is there a way to simulate a population with R and pull out m samples, > each with n values > for calculating m means? > > I need that kind of data to plot a graphic, demonstrating the central > limit theorem > and I don't know how to begin. > > So, perhaps someone can give me some tips and hints how to start and > which functions to use. > > > > thanks for any help, > joerg > > ______________________________________________ > 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.
On 10/15/2008 3:48 PM, J?rg Gro? wrote:> Hi, > > > Is there a way to simulate a population with R and pull out m samples, > each with n values > for calculating m means? > > I need that kind of data to plot a graphic, demonstrating the central > limit theorem > and I don't know how to begin.The easiest way to do this is to put the simulations into a matrix, and calculate row means (or column means). For example: par(mfrow=c(2,2)) m <- 10000 for (n in c(1,2,4,8)) { sims <- matrix(runif(n*m), ncol=n) means <- rowMeans(sims) hist(means, main=paste("n = ", n), xlim=c(0,1)) } Duncan Murdoch> > So, perhaps someone can give me some tips and hints how to start and > which functions to use. > > > > thanks for any help, > joerg > > ______________________________________________ > 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.
Hi Joerg,> Is there a way to simulate a population with R and pull out m samples, > each with n values > for calculating m means? > > I need that kind of data to plot a graphic, demonstrating the central > limit theorem > and I don't know how to begin. > > So, perhaps someone can give me some tips and hints how to start and > which functions to use.Have a look at library(TeachingDemos) ?clt.examp HTH, Tobias
J?rg Gro? wrote:> Hi, > > > Is there a way to simulate a population with R and pull out m samples, > each with n values > for calculating m means? > > I need that kind of data to plot a graphic, demonstrating the central > limit theorem > and I don't know how to begin. > > So, perhaps someone can give me some tips and hints how to start and > which functions to use. > > > > thanks for any help, > joerg >x <- runif(10000,0,1) y <- runif(10000,0,1) z <- runif(10000,0,1) u <- runif(10000,0,1) v <- runif(10000,0,1) w <- runif(10000,0,1) t <- x+y+z+u+v+w hist(t,breaks=100,xlab="sum of i.i.d r.v with finite mean and variance",ylab="Frequency",main="") Change runif for the corresponding function of the distribution of your choice. Not exactly what you wanted but it does verify the C.L.T. Rub?n
Hi J?rg, our package distrTeach has functions to visualize the central limit theorem and the law of large numbers for "arbitrary" univariate distributions; e.g., library(distrTeach) D <- sin(Norm()) + Pois() plot(D) illustrateCLT(D, len = 10) illustrateLLN(D, m = 10) Best Matthias J?rg Gro? wrote:> Hi, > > > Is there a way to simulate a population with R and pull out m samples, > each with n values > for calculating m means? > > I need that kind of data to plot a graphic, demonstrating the central > limit theorem > and I don't know how to begin. > > So, perhaps someone can give me some tips and hints how to start and > which functions to use. > > > > thanks for any help, > joerg > > ______________________________________________ > 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.-- Dr. Matthias Kohl www.stamats.de