A numerical experiment on CLT
Recall Central Limit Theorem
Let $X_1 ,X_2 ,…,X_n$ be an independent random sample of size n from a distribution (which may or may not be normal) with mean $\mu$ and standard deviation $\sigma$.
1. If sample size (n) is large enough, the distribution of sample mean $\bar{X}$ is approximately a normal distribution
2. And the mean and standard deviation of the sample means are $\mu$ and $\sigma / \sqrt{n}$
We will now do a numerical experiments to check if the CLT is true. The first three steps are exactly the same as we did in Lab I, when we took a look at the mean and variance of the distribution of sample mean. A last step is added so we can see (literally) if the distribution is approximately normal.
1. Take a random sample of 100 from a distribution, compute the sample mean
2. Repeat it 10000 times
3. computer the mean and standard deviation of the sample means
4. Draw a histogram of the sample means
Based on central limit theorem, the sampling distribution of the sample means should be close to a Normal distribution with mean at 0 and the standard variation of of 1/10.
First, we will take random samples of 100 from a standard normal distribution
xbar <- rep(0,10000) # create a vector to store the sample means
for(i in 1:10000){
x <- rnorm(100) # take a random sample of 100 from a standard normal distribution
xbar[i] <- mean(x)
}
mean(xbar)
sqrt(var(xbar))
hist(xbar)
The best way to visualize if data fits a certain distribution is Quantile-Quantile plot, on which we will discuss more later. R function qqnorm let you compare your data with a normal distribution. If you see a straight line, the data is approximately normal.
qqnorm(xbar)
Now let take a sample of 100 from a Poisson distribution with $\lambda =1$. The Poisson distribution is very useful in modeling the rare events and the parameter $\lambda$ is both its mean and variance.
xbar <- rep(0,10000) # create a vector to store the sample means
for(i in 1:10000){
x <- rpois(100,1) # take a random sample of 100 from a Poisson distribution with its parameter lambda=1
xbar[i] <- mean(x)
}
mean(xbar)
sqrt(var(xbar))
hist(xbar)
Finally let's take a sample of 100 from a binomial distribution with $p=0.05$.
xbar <- rep(0,10000) # create a vector to store the sample means
for(i in 1:10000){
x <- rbinom(100,size=1,prob=0.05) # take a random sample of 100 from a Bernoulli trial, aka, binomial of size 1.
xbar[i] <- mean(x)
}
mean(xbar)
sqrt(var(xbar))
hist(xbar)
Does the sampling distribution look like a Normal distribution? Is CLT wrong?
In Lab exercise:
Now I will ask you to lead an investigation to find out what sample size is considered "large enough" for the sampling distribution of the sample mean (of Bernoulli trials with p=0.05) being approximately normally distributed. Does such sample size also work for Bernoulli trials with p=0.01.
No comments:
Post a Comment