Demonstration of Confidence Intervals Using R (Animated)

Yihui Xie 2007-10-02

NOTE: There’s a typo in the graph! The \(\mu\) should be replaced with the sample mean \(\bar{X}\)! And the code is available in the R package animation; see library(animation); ?conf.int

Recently I’ve been thinking about attending the conference of IASC2008, and my vague idea is to use animations in statistical education, especially in the demonstration of some algorithms containing iterations or loops. Perhaps the main difficulty just lies in an integrated theory – currently I’m still not clear about the literature in this area (if anyone knows, please do drop me a line).

The other day when I was browsing the R documentation pages, I noticed that the amount of materials in those pages is increasing at a high speed. Among them I also saw some excellent web sites built by students (e.g. from Taiwan), which has triggered my own idea on building an independent website introducing animated pictures in statistics.

So… Let’s come back to the topic. In fact this is a demonstration I made several weeks ago, and today I modified it a little bit in order that the coverage rate can be better shown. The idea behind this simulation is simple: draw samples (random numbers) from the population which follows N(0, 1), and calculate confidence intervals (CI) based on these samples respectively. I believe everybody surely knows the formula in the main title of the figure below (suppose sigma is known, then compute the CI for the unknown mu).

Demonstration of Confidence Intervals Using R (Animated)

R code for the demo:

library(animation)
conf.int