Sampling distributions and Central Limit theorem in R

Source: Internet
Author: User

The Central Limit theorem (CLT), and the concept of the sampling distribution, is critical for understanding why Statisti Cal inference works. There is at least a handful of problems so require you to invoke the central Limit theorem on every ASQ certified Six S Igma Black Belt (CSSBB) exam. The CLT says so if you take many repeated samples from a population, and calculate the averages or sum of each one, the collection of those averages would be normally distributed ... and it doesn ' t matter what the shape of the S Ource Distribution is!

I wrote some R code to help illustrate this principle for my students. This code allows the Choose a sample size (n), a source distribution, and parameters for that source distribution, and Generate a plot of the sampling distributions of the mean, sum, and variance. (Note:the sampling distribution for the variance is a chi-square distribution!)

Sdm.sim <-Function (n,src.dist=null,param1=null,param2=null) {R <-10000 # number of replications/samples-do n OT ADJUST # This produces a matrix of observations with # N columns and R rows. Each row is one sample:my.samples <-switch (src.dist, "E" = Matrix (Rexp (N*R,PARAM1), R), "N" = Matrix (Rnorm (N*R,PARAM1, PARAM2), R), "U" = Matrix (runif (N*R,PARAM1,PARAM2), R), "P" = Matrix (Rpois (N*R,PARAM1), R), "C" = Matrix (Rcauchy (N*r, PARAM1,PARAM2), R), "B" = Matrix (Rbinom (N*R,PARAM1,PARAM2), R), "G" = Matrix (Rgamma (N*R,PARAM1,PARAM2), R), "X" = Matrix   (RCHISQ (N*R,PARAM1), R), "T" = Matrix (RT (N*R,PARAM1), R)) All.sample.sums <-apply (my.samples,1,sum)   All.sample.means <-Apply (my.samples,1,mean) All.sample.vars <-apply (My.samples,1,var) par (mfrow=c (2,2)) hist (my.samples[1,],col= "Gray", main= "distribution of One Sample") hist (all.sample.sums,col= "Gray", main= "sampling Distributionnofthe Sum ") hist (all.sample.means,col=" Gray ", main=" sampling distributionnof theMean ") hist (all.sample.vars,col=" Gray ", main=" sampling distributionnofthe Variance ")} 

There is 9 population distributions to choose from:exponential (E), Normal (N), Uniform (U), Poisson (P), Cauchy (C), bi Nomial (B), Gamma (G), Chi-Square (X), and the Student ' s t distribution (t). Note also that there is to provide either one or both parameters, depending upon what distribution is selecting. For example, a normal distribution requires so specify the mean and standard deviation to describe where it ' s center Ed, and how fat or thin it was (that's, the parameters). A chi-square distribution requires that's specify the degrees of freedom (that's only one parameter). Can find out exactly what distributions require what parameters by going Here:http://en.wikibooks.org/wiki/r_programmi Ng/probability_distributions.

Here's an example, draws from an exponential distribution with a mean of 1/1 (you specify the number of your want in the Denominator of the mean):

Sdm.sim (50,src.dist= "E", Param1=1)

The code above produces this sequence of plots:

You aren ' t allowed to change the number of replications in this simulation because of theNatureOf the sampling distribution:it ' s a theoretical model that describes the distribution of statistics from an infinite numb Er of samples. As a result, if you increase the number of replications, you'll see the mean of the sampling distribution bounce around UN Til it converges on the mean of the population. This was just an artifact of the simulation Process:it's not a characteristic of the sampling distribution, because A sampling distribution, you ' ve got to has an infinite number of samples. Watkins et al. A great description of this effect AllStatistics instructors should be aware of. I chose for the number of replications because 1) It's close enough to infinity to ensure that the mean of the Samp Ling Distribution isThe same as the mean of the population, but 2) it's far enough away from infinity to don't crash your computer, even if you Only 4GB or 8GB of memory.

Here is some more examples to try. Can see this as you increase your sample size (n), the shapes of the sampling distributions become more and more Norma L, and the variance decreases, constraining your estimates of the population parameters more and more.

Sdm.sim (10,src.dist= "E", 1) Sdm.sim (50,src.dist= "E", 1) Sdm.sim (100,src.dist= "E", 1) Sdm.sim (10,src.dist= "X", 14) Sdm.sim (50,src.dist= "x", +) Sdm.sim (100,src.dist= "x", +) Sdm.sim (10,src.dist= "N", param1=20,param2=3) Sdm.sim (50, Src.dist= "n", param1=20,param2=3) Sdm.sim (100,src.dist= "n", param1=20,param2=3) Sdm.sim (10,src.dist= "G", param1=5, param2=5) Sdm.sim (50,src.dist= "G", param1=5,param2=5) Sdm.sim (100,src.dist= "G", param1=5,param2=5)

Transfer from:http://www.r-bloggers.com/sampling-distributions-and-central-limit-theorem-in-r/?utm_source=feedburner& Utm_medium=email&utm_campaign=feed%3a+rbloggers+%28r+bloggers%29

Sampling distributions and Central Limit theorem in R (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.