First, we’ll generate some data for you to use. Generate some random numbers and save it:
# generate 100 random numbers from a normal distribution
mydata <- rnorm(100, mean = 10, sd = 4)
We know that we need quantiles of a normal distribution in order to produce confidence intervals. We use the qnorm() function to get the quantiles. We need to tell R the proportion of the distribution we want below a certain point, and it will tell us that point. If I want to find the point such that 2.5% of a standard normal distribution is below it, I type the following:
qnorm(.025)
## [1] -1.959964
Remember how we use 1.96 to produce a 95% confidence interval? Why is it related to a .025 quantile? (Answer: because 100% - 95% = 5%. We split that between the two tails: 5%/2 = 2.5% = .025 in each tail.)
You now have everything you need to produce a confidence interval for a mean when the standard deviation is known. You also have all the tools necessary to produce a confidence interval for a proportion. Yes, really. Let’s see some examples.
mymean <- mean(mydata)
mysd <- 4 #this is because earlier I choose sd = 4
n <- length(mydata)
myq <- qnorm(.025)
mymean + c(1,-1) * myq * mysd / sqrt(n)
## [1] 9.796699 11.364670
This gives the lower and upper bound of my 95% confidence interval for my mean. If this looks overwhelming, split it into two very similar calculations:
mymean + myq * mysd / sqrt(n)
## [1] 9.796699
mymean - myq * mysd / sqrt(n)
## [1] 11.36467
I am 95 percent confident that the mean lies between ___ and ___. (Results will vary, since we’re all using random numbers.)
Let’s do another example. You have binomial data. Let’s say you have 80 trials and 23 of them resulted in success. What is a confidence interval for the probability of success?
First, let’s find a point estimate for the probability of successs.
p.est <- 23/80
p.est
## [1] 0.2875
Now let’s work on estimating the variability of our point estimator.
pvar <- p.est * (1-p.est) / 80
my.se <- sqrt(pvar)
Let’s put it all together to produce a 90% confidence interval for the probability of success
p.est + c(1, -1) * qnorm(.05) * my.se
## [1] 0.2042674 0.3707326
I am 90 percent confident that the true proportion of success lies between .204 and .371.
Use R for the following problems. Use the qnorm() function when you need to find quantiles.
A random sample of size 8 was taken from a normal distribution with variance of 72. This yielded a sample eman of 85. Create confidence intervals for the population mean \(\mu\) using the following levels of confidence: 99%, 95%, 90%, 80%. (Hint: change the argument in qnorm.)
Let X equal the weight in grams of a “52 gram” snack pack of candies. Assume that the distribution of X is normal with variance 4. A random sample of size n=10 observations of X yielded the following data:
x <- c(55.95, 56.54, 55.13, 57.48, 56.06, 59.93, 58.30, 52.57, 58.46)
Let p equal the proportion of letters mailed in the Netherlands that are delivered the next day. Suppose that y=142 out of a random sample of n=200 letters were delivered the day after they were mailed.
Let p equal the proportion of Americans who favor the death penalty. If a random sample of n = 1234 Americans yielded y = 864 who favored the death penalty, find an approximate 95% CI for p.