Determining confidence interval in R
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:
- Iris setosa
- versicolor
- virginica
R
# 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
# 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 (Sx̄), we need to find the standard deviation (s) and the length of the sample data (n).
R
# 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
R
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 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 bound upper_bound <- mean_value + margin_error |
Combining all the steps
Example:
R
# 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
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 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