How to Find Confidence Intervals in R?
The confidence interval signifies how much uncertainty is present in statistical data. In other words, it is defined as an interval that depicts a population parameter with a probability of 1 – α. The expression for the confidence interval is given below,
x̄ ± tα / 2,N – 1 Sx̄
Here,
x̄ ± tα / 2 : It signifies the value required to form an area of α / 2 (each tail of a t-distribution where
degree of freedom = n – 1)
Sx̄ = s / √n : It represents the standard error of the mean
Determining confidence interval in R:
Firstly we need to create sample data. R provides inbuilt datasets. In this article, we are going to use the iris dataset for illustration. iris dataset depicts the sepal length, sepal width, petal length, and petal width in centimeters. It provides the data of fifty flowers from each of the three species of iris. The species are:
- Iris setosa
- versicolor
- virginica
R
# Printing the contents of iris inbuilt datasetprint(iris) |
Output:

Method 1: Calculating Intervals using base R
In this method, we will find the confidence interval step-by-step using mathematical formulas and R functions. You can follow the below steps to determine the confidence interval in R.
Step 1: Calculate the mean. The very first step is to determine the mean of the given sample data.
R
# R program to determine the mean# Calculate the mean of the Sepal.Lengthmean_value <- mean(iris$Sepal.Length) |
Step 2: Now let’s compute the standard error of the mean.
In order to compute the standard error of the mean (Sx̄), we need to find the standard deviation (s) and the length of the sample data (n).
R
# Compute the sizen <- length(iris$Sepal.Length)# Find the standard deviationstandard_deviation <- sd(iris$Sepal.Length)# Find the standard errorstandard_error <- standard_deviation / sqrt(n) |
Step 3: Determine the t-score that is linked to the confidence level.
In this step, we will compute the t-score related to the confidence level. We are required to have exactly α / 2 probability in the lower and upper tail. R provides qt() function using which we can calculate the t-score easily. The syntax is given below,
Syntax:
qt(random_variable, degree_of_freedom)
Parameters:
random_variable: It must be a random variable
degree_of_freedom: It must be degree of Freedom
R
alpha = 0.05degrees_of_freedom = sample.n - 1t_score = qt(p=alpha/2, df=degrees_of_freedom,lower.tail=F)print(t_score) |
Step 4: Compute the margin of error and form the confidence interval.
The margin of error is given by,
tα / 2,N – 1 Sx̄
It can be easily calculated as,
R
margin_error <- t_score * standard_error |
The confidence interval is equal to the mean +/- margin of error. It can be calculated as,
R
# Calculate the lower bound lower_bound <- mean_value - margin_error# Calculate the upper boundupper_bound <- mean_value + margin_error |
Combining all the steps
Example:
R
# R program to find the confidence interval# Calculate the mean of the sample datamean_value <- mean(iris$Sepal.Length)# Compute the sizen <- length(iris$Sepal.Length)# Find the standard deviationstandard_deviation <- sd(iris$Sepal.Length)# Find the standard errorstandard_error <- standard_deviation / sqrt(n)alpha = 0.05degrees_of_freedom = n - 1t_score = qt(p=alpha/2, df=degrees_of_freedom,lower.tail=F)margin_error <- t_score * standard_error# Calculating lower bound and upper boundlower_bound <- mean_value - margin_errorupper_bound <- mean_value + margin_error# Print the confidence intervalprint(c(lower_bound,upper_bound)) |
Output:

Method 2: Calculating Confidence Intervals using confint() function
We can compute confidence interval using the inbuilt functions in R. The steps are given below,
Step 1: Calculating mean and standard error.
R provides us lm() function which is used to fit linear models into data frames. We can calculate the mean and standard error (that are required to find confidence interval) using this function. The syntax is given below,
Syntax:
lm(fitting_formula, dataframe)
Parameters:
fitting_formula: It must be the formula for the linear model.
dataframe: It must be the name of the data frame that contains the data.
R
# Calculate the mean and standard errorl_model <- lm(Sepal.Length ~ 1, iris) |
Step 2: Finding confidence interval.
Now, to find the confidence interval we have confint() function in R. This function is specifically used to compute confidence intervals for one or more parameters in a fitted model. The syntax is given below,
Syntax:
confint(object, parm, level = 0.95, …)
Parameters:
object: It represents fitted model object.
parm : It represents parameters to be given confidence intervals (either a vector)
level : It represents the confidence level.
… : It represents additional argument for different methods.
R
# Find the confidence intervalconfint(model, level=0.95) |
Combining all the steps
Example:
R
# R program to find the confidence interval# Calculate the mean and standard errormodel <- lm(Sepal.Length ~ 1, iris)# Find the confidence intervalconfint(model, level=0.95) |
Output:


