Calculating confidence intervals in R is a handy trick to have in your toolbox of statistical operations. A confidence interval essentially allows you to estimate about where a true probability is based on sample probabilities. The confidence interval function in R makes inferential statistics a breeze. We’re going to walk through how to calculate confidence interval in R. There are a couple of ways this problem can be presented to us….

#### Calculate Confidence Interval in R – Normal Distribution

Given the parameters of the distribution, generate the confidence interval. In this situation, we’re basically using r like an error interval calculator… Using the 95 percent confidence interval function, we will now create the R code for a confidence interval. What does a 95 percent confidence interval mean? Essentially, a calculating a 95 percent confidence interval in R means that we are 95 percent sure that the true probability falls within the confidence interval range that we create.

```
# Calculate Confidence Interval in R for Normal Distribution
# Confidence Interval Statistics
# Assume mean of 12
# Standard deviation of 3
# Sample size of 30
# 95 percent confidence interval so tails are .925
> center <- 12
> stddev <- 3
> n <- 30
> error <- qnorm(0.975)*stddev/sqrt(n)
> error
[1] 1.073516
> lower_bound <- center - error
> lower_bound
[1] 10.92648
> upper_bound <- center + error
> upper_bound
[1] 13.07352
```

Thus the range in this case is between 10.9 and 13.1 (rounding outwards).

#### Calculate Confidence Interval in R – t Distribution

For experiments run with smaller sample sizes it is generally inappropriate to use the normal distribution. Student’s t distribution is the correct choice for this environment. A t confidence interval is slightly different from a normal or percentile confidence interval in R. When creating a confidence interval using a t table or t distribution, you help to eliminate some of the variability in your data by using a slightly different base distribution.

R can support this by substituting the qt function for the qnorm function, as demonstrated below…. assume we are working with a sample size of 15. You will need to tell the qt function the degrees of freedom as a parameter (should be n-1).

```
# Calculate Confidence Interval in R for t Distribution
# t test confidence interval
# Assume mean of 12
# Standard deviation of 3
# Sample size of 15
# 95% confidence interval so tails are .925
> center <- 12
> stddev <- 3
> n <- 30
> error <- qt(0.975, df=n-1)*stddev/sqrt(n)
> error
[1] 1.661345
> lower_bound <- center - error
> lower_bound
[1] 10.33866
> upper_bound <- center + error
> upper_bound
[1] 13.66134
```

As expected, the confidence interval widens… But why calculate a larger confidence interval? Larger confidence intervals increase the chances of capturing the true proportion, so you can feel more confident that you know what that true proportion is. These confidence interval techniques can be applied to find the confidence interval of a mean in R, calculate confidence interval from a p value, or even compute a confidence interval for variance in R.

Related Materials

- Find the mean in R
- Calculate Standard Error in R
- Calculate Standard Deviation in R
- Calculate Variance in R
- Calculate Skewness in R
- Calculate Kurtosis in R
- Calculate Confidence Interval in R
- Using a Chi Square Test in R
- Power analysis in R
- Percentile in R
- Quartile in R