0
0
R Programmingprogramming~5 mins

Confidence intervals in R Programming

Choose your learning style9 modes available
Introduction

A confidence interval shows a range where we expect a true value to be, based on sample data. It helps us understand how sure we are about our estimate.

When you want to estimate the average height of a group but only have a sample.
When checking the average test score of students and want to know the range it might fall in.
When measuring the average time people spend on a website and want to guess the true average.
When comparing two groups and want to see if their averages are different with some certainty.
Syntax
R Programming
t.test(x, conf.level = 0.95)$conf.int

x is your numeric data vector (sample).

conf.level sets the confidence level, usually 0.95 for 95%.

Examples
Calculate 95% confidence interval for the sample data.
R Programming
data <- c(5, 7, 8, 6, 9)
t.test(data)$conf.int
Calculate 99% confidence interval for the sample data.
R Programming
data <- c(10, 12, 11, 13, 14)
t.test(data, conf.level = 0.99)$conf.int
Sample Program

This program calculates and prints the 95% confidence interval for the sample data.

R Programming
data <- c(20, 22, 19, 24, 21, 23, 20)
ci <- t.test(data)$conf.int
print(ci)
OutputSuccess
Important Notes

The confidence interval gives a range, not a single number.

Higher confidence levels give wider intervals.

Make sure your data is roughly normal or sample size is large for best results.

Summary

Confidence intervals estimate a range for a true value based on sample data.

Use t.test() in R to calculate confidence intervals easily.

Common confidence levels are 95% and 99%.