VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-find-confidence-intervals-in-r/

⇱ How to Find Confidence Intervals in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Find Confidence Intervals in R

Last Updated : 13 Apr, 2026

A confidence interval is a range of values used to estimate an unknown population parameter, such as the mean or proportion. It shows how much uncertainty is associated with a sample estimate. The interval is calculated from sample data and includes a confidence level, usually 95%, which means that if we repeat the sampling process many times, about 95% of the calculated intervals would contain the true value.

Parameters:

  • = sample mean
  • = z-score (from standard normal distribution for given confidence level, e.g., 1.96 for 95%)
  • = sample standard deviation
  • = sample size

Method 1: Using t.test() to Find Confidence Interval

We use the t.test() function to find the confidence interval of the sample mean in R programming language.

  • t.test(): Performs a t-test and returns confidence intervals.
  • $conf.int: Extracts the confidence interval from the result.

Output:

👁 dataset
Output

We can say with 95% confidence that the true mean lies between 25.33415 and 29.66585.

Method 2: Manually Calculating Confidence Interval (Using Iris Dataset)

We manually calculate the confidence interval for the mean of Sepal.Length from the iris dataset using statistical formulas.

  • mean(): Computes the sample mean
  • length(): Finds the number of observations
  • sd(): Calculates sample standard deviation
  • sqrt(): Computes square root
  • qt(): Calculates t-score for a given confidence level

Output:

[1] 5.709732 5.976934

We can be 95% confident that the population mean of Sepal.Length lies between 5.709732 and 5.976934.

Method 3: Using confint() with Linear Model

We fit a simple linear model to calculate the mean and use confint() to get the confidence interval.

  • lm(): Creates a linear model
  • confint(): Returns confidence intervals for model parameters
  • level: Defines the confidence level (default is 0.95)

Output:

👁 matrix
Output

We are 95% confident that the true population mean of Sepal.Length lies between 5.709732 and 5.976934. This matches the results we got in the manual method.

Comment

Explore