Skip to main content
Chemistry LibreTexts

6.4: Using R to Find Confidence Intervals

  • Page ID
    220902
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The confidence interval for a population’s mean, \(\mu\), given an experimental mean, \(\bar{x}\), for \(n\) samples is defined as

    \[\mu = \bar{x} \pm \frac {z \sigma} {\sqrt{n}} \nonumber\]

    if we know the population's standard deviation, \(\sigma\), and as

    \[\mu = \bar{x} \pm \frac {t s} {\sqrt{n}} \nonumber\]

    if we assume that the sample's standard deviation, \(s\), is a reasonable predictor of the population's standard deviation. To find values for \(z\) we use R'sqnorm()function, which takes the form

    qnorm(p)

    wherepis the probability on one side of the normal distribution curve that a result is not included within the confidence interval. For a 95% confidence interval, \(p = 0.05/2 = 0.025\) because the total probability of 0.05 is equally divided between both sides of the normal distribution. To find \(t\) we use R'sqt()function, which takes the form

    qt(p, df)

    wherepis defined as above and wheredfis the degrees of freedom or \(n - 1\).

    For example, if we have a mean of \(\bar{x} = 12\) for 10 samples with a known standard deviation of \(\sigma = 2\), then for the 95% confidence interval the value of \(z\) and the resulting confidence interval are

    # for a 95% confidence interval, alpha is 0.05 and the probability, p, on either end of the distribution is 0.025;

    # the value of z is positive on one side of the normal distribution and negative on the other side;

    # as we are interested in just the magnitude, not the sign, we use the abs() function to return the absolute value

    z = qnorm(0.025)
    conf_int_pop = abs(z * 2/sqrt(10))
    conf_int_pop

    [1] 1.23959

    Adding and subtracting this value from the mean defines the confidence interval, which, in this case is \(12 \pm 1.2\).

    If we have a mean of \(\bar{x} = 12\) for 10 samples with an experimental standard deviation of \(s = 2\), then for the 95% confidence interval the value of \(t\) and the resulting confidence interval are

    t = qt(p = 0.025, 9)
    conf_int_samp = abs(t * 2/sqrt(10))
    conf_int_samp

    [1] 1.430714

    Adding and subtracting this value from the mean defines the confidence interval, which, in this case is \(12 \pm 1.4\).


    This page titled 6.4: Using R to Find Confidence Intervals is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by David Harvey.

    • Was this article helpful?