Reputation: 9
How would I use the 95% Confidence Interval=(population mean) +/- 1.96 x(Standard error of the population mean) equation in program r to solve for the confidence interval? My bio-metrics professor wants us to use the equation but we can't figure out how to input it into Program R.
Upvotes: 0
Views: 1858
Reputation: 50668
As you don't provide sample data, here is an example using the iris
sample dataset. Specifically, let's calculate the 95% CI of the mean of the sepal widths.
Let's plot the distribution Sepal.Width
library(ggplot)
ggplot(iris, aes(Sepal.Width)) + geom_density()
The 95% confidence interval is defined by those observations that lie within 1.96 units of the standard deviation around the mean (assuming an underlying normal distribution).
CI <- with(iris, mean(Sepal.Width) + c(-1, 1) * 1.96 * sd(Sepal.Width))
CI
#[1] 2.203035 3.911631
We confirm that indeed 95% of observations lie within CI
with(iris, sum(Sepal.Width >= CI[1] & Sepal.Width <= CI[2]) / length(Sepal.Width))
#[1] 0.9466667
Upvotes: 3