# Confidence Interval Exercises

October 27, 2015

Use the code snippets in the Module 6 notes to answer the following questions.

# Problem 1

A sample of size 100 is taken from a population with a known standard deviation of 12.The sample estimate of the mean is 34. Construct a 95% connfidence interval for the population mean.

# Code snippet to construct a confidence interval
xbar = 34  # Sample Mean
sd   = 12   # Population Standard Deviation
n    = 100   # Sample size
CL   = .95  # Required Confidence Level

zstar <- qnorm(CL+.5*(1-CL)) # Obtain Z-score for this confidence level
sd.xbar <- sd/sqrt(n)        # Compute standard error of sample mean
ME <- zstar * sd.xbar        # Compute margin of error

lb <- xbar - ME              # Compute lower bound of CI
ub <- xbar + ME              # Compute upper bound of CI

CI <- c(CL,lb,xbar,ub,ME)    # Put our results in a vector

names(CI) <- c("Confidence Level","Lower Bound","Xbar","Upper Bound",
"Margin of Error") # Name the vector elements
CI                           # Display the vector
## Confidence Level      Lower Bound             Xbar      Upper Bound
##         0.950000        31.648043        34.000000        36.351957
##  Margin of Error
##         2.351957

# Problem 2

Repeat problem 1, but provide an 80% confidence interval. How does the width of the 80% CI compare with that of the 95% CI?

# Code snippet to construct a confidence interval
xbar = 34  # Sample Mean
sd   = 12   # Population Standard Deviation
n    = 100   # Sample size
CL   = .80  # Required Confidence Level

zstar <- qnorm(CL+.5*(1-CL)) # Obtain Z-score for this confidence level
sd.xbar <- sd/sqrt(n)        # Compute standard error of sample mean
ME <- zstar * sd.xbar        # Compute margin of error

lb <- xbar - ME              # Compute lower bound of CI
ub <- xbar + ME              # Compute upper bound of CI

CI <- c(CL,lb,xbar,ub,ME)    # Put our results in a vector

names(CI) <- c("Confidence Level","Lower Bound","Xbar","Upper Bound",
"Margin of Error") # Name the vector elements
CI                           # Display the vector
## Confidence Level      Lower Bound             Xbar      Upper Bound
##         0.800000        32.462138        34.000000        35.537862
##  Margin of Error
##         1.537862

The 80% CI is narrower than the 95% CI.

# Problem 3

Considering the common parameters of problems 1 and 2, how large a sample size would be required to obtain a margin of error of .5, with a confidence level of 95%?

CL = .95
zstar = qnorm(CL+.5*(1-CL))
ME = .5
sigma = 12
n = (zstar^2*sigma^2)/ME^2
ceiling(n)
##  2213

# problem 4

A proportion is estimated based on a sample size of 150. The estimated proportion is .3. Construct a 90% confidence for the true value of the proportion.

# Code snippet to compute a confidence interval for a proportion

phat <- .3   # Estimated proportion
CL <- .90    # Required confidence level
n <- 150     # Sample size

zstar <- qnorm(CL+.5*(1-CL))
se.phat <-sqrt(phat*(1-phat)/n)
ME = zstar * se.phat

lb <- phat - ME
ub <- phat + ME

CI <- c(CL,ME,lb,phat,ub)
names(CI) <- c("Confidence Level", "Margin of Error",  "lower Bound","phat","Upper Bound")

CI
## Confidence Level  Margin of Error      lower Bound             phat
##       0.90000000       0.06154479       0.23845521       0.30000000
##      Upper Bound
##       0.36154479

# Problem 5

If a proportion is known to be approximately .3, how large a sample would be required to obtain a margin of error of .01 with a 95% confidence level?

# Code Snippet to compute a required sample size for a proportion
phat = .3
ME = .01
CL = .95
zstar = qnorm(CL+.5*(1-CL))
n = (phat*(1-phat)*zstar^2)/ME^2
ceiling(n)
##  8068

# Problem 6

A survey of 865 voters in one state reveals that 408 favor approval of an issue before the legislature. Construct the 95% confidence interval for the true proportion of all voters in the state who favor approval.

# Code snippet to compute a confidence interval for a proportion

phat <- 408/865   # Estimated proportion
CL <- .95         # Required confidence level
n <- 865          # Sample size

zstar <- qnorm(CL+.5*(1-CL))
se.phat <-sqrt(phat*(1-phat)/n)
ME = zstar * se.phat

lb <- phat - ME
ub <- phat + ME

CI <- c(CL,ME,lb,phat,ub)
names(CI) <- c("Confidence Level", "Margin of Error",  "lower Bound","phat","Upper Bound")

CI
## Confidence Level  Margin of Error      lower Bound             phat
##       0.95000000       0.03326688       0.43840942       0.47167630
##      Upper Bound
##       0.50494318

# Problem 7

Of 90 adults selected randomly from one town, 69 have health insurance. Find a 90% confidence interval for the true proportion of all adults in the town who have health insurance.

# Code snippet to compute a confidence interval for a proportion

phat <- 69/90   # Estimated proportion
CL <- .90         # Required confidence level
n <- 90          # Sample size

zstar <- qnorm(CL+.5*(1-CL))
se.phat <-sqrt(phat*(1-phat)/n)
ME = zstar * se.phat

lb <- phat - ME
ub <- phat + ME

CI <- c(CL,ME,lb,phat,ub)
names(CI) <- c("Confidence Level", "Margin of Error",  "lower Bound","phat","Upper Bound")

CI
## Confidence Level  Margin of Error      lower Bound             phat
##        0.9000000        0.0733327        0.6933340        0.7666667
##      Upper Bound
##        0.8399994