# Coding Confidence Intervals

The first step in finding a confidence interval is to set 1 - $\alpha$ equal to the confidence coefficient. The confidence coefficient is the decimal form of the confidence level. So if the confidence leve is 95%, the confidence coefficient is .95. So we can calculate the confidence coefficient in R by dividing the confidence level by 100. The simple function shown below takes in the confidence "level", denoted by level, and solves for the conefidence coefficient, denoted by "coeff". The function is executed for a confidence level of 95% and the resulting output is .95.

1 coeff <- function(level) {
2     level / 100
3 }
4 coeff(95) # .95

Setting the 1 - $\alpha$ equal to the confidence coefficient and then solving for $\alpha/2$, we get $\alpha/2$ = (1 - Confidence Coefficient) / 2. Once we've found the value of $\alpha/2$, we can find the value of $z_{\alpha/2}$. $z_{\alpha/2}$ is the z-value providing an area of $\alpha/2$ in the upper tail of the standard normal, or z, distribution. The function qnorm() can be used to calculate this z-value. The first argument for the qnorm() function is the area. The second argument is lower.tail, which can take a value of TRUE or FALSE.

5 ahalf <- function(coeff) {
6     ahalf <- (1 - coeff) / 2
8 }
9 ahalf(.95) # 1.96

Setting the 1 - $\alpha$ equal to the confidence coefficient and then solving for $\alpha/2$, we get $\alpha/2$ = (1 - Confidence Coefficient) / 2. Once we've found the value of $\alpha/2$, we can find the value of $z_{\alpha/2}$. $z_{\alpha/2}$ is the z-value providing an area of $\alpha/2$ in the upper tail of the standard normal, or z, distribution. The function qnorm() can be used to calculate this z-value. The first argument for the qnorm() function is the area. The second argument is lower.tail, which can take a value of TRUE or FALSE.

5 z <- function(ahalf) {
7     qnorm(ahalf, lower.tail = FALSE)
8 }
9 z(.025) # 1.96

Once we have $z_{\alpha/2}$, we can calculate the margin of errors. The margin of error is made up of three components: $z_{\alpha/2}$, the standard deviation and the sample size. In our R code, $z_{\alpha/2}$ is denoted by "z", the standard deviation is denoted by "sd" and the sample size is denoted by "size". Given the value of these three, we can use the formula $z_{\alpha/2} \sigma / \sqrt(n)$ to calculate the margin of error.

10 margin <- function(z, sd, size) {
11      z * sd / sqrt(size)
12 }
13 margin(1.96, 4, 50) # 5

To get the confidence interval, we need the mean and the margin of error. The confidence interval is equal to the mean plus or minus the margin of error. So in our function for the confidence interval, we input the mean and the margin of error, denoted by "margin". We subtract the margin of error from the mean and add the margin of error to the mean. Since there will be two values outputed, we wanted to combine them into a single entity. We do this using the c() function, which stands for concatenate.

14 conf <- function(mean, margin) {
15      c(mean - margin, mean + margin)
16 }
17 margin(10, 5) # 5, 15

In the case that the population standard deviation is unknown, the procedure is similar to what's shown above, except the t-distribution is used instead of the standard normal or z-distribution. So instead of looking for $z_{\alpha/2}$ as in the sigma known case, we are looking for $t_{\alpha/2}$. Since we are using the t-distribution, we need the degrees of freedom. The degrees of freedom for a confidence interval about the population mean is equal to the sample size minus one (n - 1). The function qt() will gives you the t-value for a given probability. The argument lower.tail being equal to FALSE will make it the upper tail area.

5 t <- function(x ,coeff) { # Input Confidence Coefficient
6     ahalf <- (1 - coeff) / 2 # Calculate Alpha Half
7     size <- length(x) # Calculate Alpha Half
8     qnt(ahalf, df = size - 1, lower.tail = FALSE) # Output z_Alpha Half
9 }
10 t(x, coeff = .95) # 1.96

Once we have $z_{\alpha/2}$, we can calculate the margin of errors. The margin of error is made up of three components: $z_{\alpha/2}$, the standard deviation and the sample size. In our R code, $z_{\alpha/2}$ is denoted by "z", the standard deviation is denoted by "sd" and the sample size is denoted by "size". Given the value of these three, we can use the formula $z_{\alpha/2} \sigma / \sqrt(n)$ to calculate the margin of error.

10 margin <- function(z, sd, size) { # Three Inputs
11      z * sd / sqrt(size) # Output Margin of Error
12 }
13 margin(z = 1.96, sd = 4, size = 50) # 5

To get the confidence interval, we need the mean and the margin of error. The confidence interval is equal to the mean plus or minus the margin of error. So in our function for the confidence interval, we input the mean and the margin of error, denoted by "margin". We subtract the margin of error from the mean and add the margin of error to the mean. Since there will be two values outputed, we wanted to combine them into a single entity. We do this using the c() function, which stands for concatenate.

14 conf <- function(mean, margin) { # Input Mean and Margin of Error
15      c(mean - margin, mean + margin) # Output Confidence Interval
16 }
17 margin(mean = 10, margin = 5) # 5, 15