Open In App

How to Find Confidence Intervals in R?

The confidence interval in R signifies how much uncertainty is present in statistical data. a fundamental statistical technique, confidence intervals offer a range of likely values for an unknown population parameter based on sample data. They are essential to decision-making, hypothesis testing, and statistical inference.
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 S



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)

S= s / √n :  It represents the standard error of the mean  

Determining confidence interval in R: 




# Sample data
data <- c(23, 28, 32, 27, 25, 30, 31, 29, 26, 24)
 
# Calculate the confidence interval
result <- t.test(data)
 
# Extract the confidence interval
confidence_interval <- result$conf.int
 
# Print the confidence interval
confidence_interval

Output:

[1] 25.33415 29.66585
attr(,"conf.level")
[1] 0.95

Based on the supplied data, the result [1] 25.33415 29.66585 shows the mean’s computed confidence interval. The confidence interval in this instance is [25.33415, 29.66585]. The output of attr(,”conf.level”) shows that 95% confidence was utilized to compute the interval.

Accordingly, based on the sample data provided, we can say with a 95% degree of certainty that the true population means is within the range of 25.33415 to 29.66585.

Calculate for Iris Data Set
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:




# Printing the contents of iris inbuilt dataset
print(iris)

Output:

    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
12           4.8         3.4          1.6         0.2     setosa
13           4.8         3.0          1.4         0.1     setosa
14           4.3         3.0          1.1         0.1     setosa
15           5.8         4.0          1.2         0.2     setosa

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 program to determine the mean
 
# Calculate the mean of the Sepal.Length
mean_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 (S), we need to find the standard deviation (s) and the length of the sample data (n).




# Compute the size
n <- length(iris$Sepal.Length)
 
# Find the standard deviation
standard_deviation <- sd(iris$Sepal.Length)
 
# Find the standard error
standard_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 the 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




alpha = 0.05
degrees_of_freedom = sample.n - 1
t_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 S

It can be easily calculated as,




margin_error <- t_score * standard_error

 
 The confidence interval is equal to the mean +/- margin of error. It can be calculated as,




# Calculate the lower bound 
lower_bound <- mean_value - margin_error
 
# Calculate the upper bound
upper_bound <- mean_value + margin_error

Combining all the steps

Example:




# R program to find the confidence interval
 
# Calculate the mean of the sample data
mean_value <- mean(iris$Sepal.Length)
 
# Compute the size
n <- length(iris$Sepal.Length)
 
# Find the standard deviation
standard_deviation <- sd(iris$Sepal.Length)
 
# Find the standard error
standard_error <- standard_deviation / sqrt(n)
alpha = 0.05
degrees_of_freedom = n - 1
t_score = qt(p=alpha/2, df=degrees_of_freedom,lower.tail=F)
margin_error <- t_score * standard_error
 
# Calculating lower bound and upper bound
lower_bound <- mean_value - margin_error
upper_bound <- mean_value + margin_error
 
# Print the confidence interval
print(c(lower_bound,upper_bound))

Output:

[1] 5.709732 5.976934

Method 2: Calculating Confidence Intervals using confint() function

We can compute confidence intervals using the inbuilt functions in R. The steps are given below,

Step 1: Calculate 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 intervals) 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.




# Calculate the mean and standard error
l_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.




# Find the confidence interval
 
confint(model, level=0.95)

 
Combining all the steps

Example:




# R program to find the confidence interval
 
# Calculate the mean and standard error
model <- lm(Sepal.Length ~ 1, iris)
 
# Find the confidence interval
confint(model, level=0.95)

 Output:

               2.5 %   97.5 %
(Intercept) 5.709732 5.976934

Article Tags :