Create a population of 10,000,000 from a chi square distribution with 2 degrees of freedom and plot the result in a histogram to show skewness. Also record mean and standard deviation of the population:

Create the chi-square distribution using “rchisq()”

pop_1 = rchisq(10000000,2)

Plot a histogram of the distribution of the population

hist(pop_1, breaks = 50, xlim = c(0,20), col = "lightblue", main = "Histogram of a Chi-Square Ditribution")

Record mean and standard deviation for the population

mean(pop_1)
## [1] 1.999785
sd(pop_1)
## [1] 1.999768

According to the central limit theorem, the approximate distribution of sample means of size 50 from this right skewed population will trend to a normal distribution as the number of samples increases. The mean will roughly equal the degrees of freedom and the standard deviation will equal or 2/sqrt(50) or ~0.28.

After drawing 10,000 samples the mean and stdev should adhere to the rules of CLT and return ~2 for mean and ~0.28 for stdev. To calculate this, I used a modified version of the xBarGenerator made in Unit 1.

xBarVec = c() #Global vector to hold the sample means
xbarGenerator = function(sampleSize = 50,number_of_samples = 1)
{
  for(i in 1:number_of_samples)
  {
    theSample = sample(pop_1,sampleSize)
    xbar = mean(theSample)
    xBarVec = c(xBarVec, xbar)
  }
  return(xBarVec)
}

xbars = xbarGenerator(50,10000)
hist(xbars)

mean(xbars)
## [1] 2.001294
sd(xbars)
## [1] 0.283742