Brittany
Brittany

Reputation: 9

How do I use the 95% confidence interval in r using the equation for a 95% confidence interval?

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

Answers (1)

Maurits Evers
Maurits Evers

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.

  1. Let's plot the distribution Sepal.Width

    library(ggplot)
    ggplot(iris, aes(Sepal.Width)) + geom_density()
    

enter image description here

  1. 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
    
  2. 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

Related Questions