The following code snippet allows you to substitute values and obtain a confidence interval based on the central limit theorem.

```
# Code snippet to construct a confidence interval
xbar = 100 # Sample Mean
sd = 10 # Population Standard Deviation
n = 25 # 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 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 96.080072 100.000000 103.919928
## Margin of Error
## 3.919928
```

For example, suppose we had a sample mean of 123 based on a sample of size 50 and we knew that the sample was drawn from a population with a standard deviation of 15. What is a 95% confidence interval for the population mean.

```
# Code snippet to construct a confidence interval
xbar = 100 # Sample Mean
sd = 10 # Population Standard Deviation
n = 25 # 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 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 96.080072 100.000000 103.919928
## Margin of Error
## 3.919928
```

We can do the same kind of thing for confidence intervals for proportions.

The Sampling Distribution of the Proportion

We have the basic theoretical results.

The estimates of a proportion \(\hat{p}\) based on a sample of size n is approsimately normal and has the following mean and standard deviation provided that \(n\hat{p} > 10\) and \(n(1-\hat{p}) > 10\).

\[\mu_{\hat{p}} = p\] and \[\sigma_{\hat{p}} = \sqrt{\frac{p(1-p)}{n}}\]

Here is a code snippet. You can replace the values in the first few lines and run the entire snippet.

```
# Code snippet to compute a confidence interval for a proportion
phat <- .7 # Estimated proportion
CL <- .95 # Required confidence level
n <- 100 # Sample size
zstar <- qnorm(CL+.5*(1-CL))
se.phat <-sqrt(phat*(1-phat)/n)
lb <- phat - zstar * se.phat
ub <- phat + zstar * se.phat
CI <- c(CL,lb,phat,ub)
names(CI) <- c("Confidence Level", "lower Bound","phat","Upper Bound")
CI
```

```
## Confidence Level lower Bound phat Upper Bound
## 0.9500000 0.6101832 0.7000000 0.7898168
```

For example, suppose we had a sample proportion of .2, a sample size of 1,000 and we wanted an 80% confidence interval for the population proportion.

```
# Code snippet to compute a confidence interval for a proportion
phat <- .2 # Estimated proportion
CL <- .8 # Required confidence level
n <- 1000 # Sample size
zstar <- qnorm(CL+.5*(1-CL))
se.phat <-sqrt(phat*(1-phat)/n)
lb <- phat - zstar * se.phat
ub <- phat + zstar * se.phat
CI <- c(CL,lb,phat,ub)
names(CI) <- c("Confidence Level", "lower Bound","phat","Upper Bound")
CI
```

```
## Confidence Level lower Bound phat Upper Bound
## 0.8000000 0.1837895 0.2000000 0.2162105
```