Learn R Programming

animation (version 2.0-0)

clt.ani: Demonstration of the Central Limit Theorem.

Description

Demonstration of the Central Limit Theorem.

Usage

clt.ani(obs = 300, FUN = rexp, col = c("bisque", "red", 
    "black"), mat = matrix(1:2, 2), widths = rep(1, ncol(mat)), 
    heights = rep(1, nrow(mat)), ...)

Arguments

obs
the number of sample points to be generated from the distribution
FUN
the function to generate n random numbers from a certain distribution
col
a vector of length 2 specifying the colors of the histogram and the density line
mat,widths,heights
arguments passed to layout to set the layout of the two graphs.
...
other arguments passed to plot.default to plot the P-values

Value

  • None.

Details

First of all, a number of obs observations are generated from a certain distribution for each variable $X_j$, $j = 1, 2, \cdots, n$, and $n = 1, 2, \cdots, nmax$, then the sample means are computed, and at last the density of these sample means is plotted as the sample size $n$ increases, besides, the p-values from the normality test shapiro.test are computed for each $n$ and plotted at the same time. As long as the conditions of the Central Limit Theorem (CLT) are satisfied, the distribution of the sample mean will be approximate to the Normal distribution when the sample size n is large enough, no matter what is the original distribution. The largest sample size is defined by nmax in ani.options.

References

E. L. Lehmann, Elements of Large-Sample Theory. Springer-Verlag, New York, 1999. http://animation.yihui.name/prob:central_limit_theorem

See Also

hist, density

Examples

Run this code
oopt = ani.options(interval = 0.1, nmax = ifelse(interactive(), 
    150, 2))
op = par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 
    0), tcl = -0.3)
clt.ani(type = "s")
par(op)

## HTML animation page
saveHTML({
    par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0), tcl = -0.3)
    ani.options(interval = 0.1, nmax = ifelse(interactive(), 
        150, 10))
    clt.ani(type = "h")
}, img.name = "clt.ani", htmlfile = "clt.ani.html", ani.height = 500, 
    ani.width = 600, title = "Demonstration of the Central Limit Theorem", 
    description = c("This animation shows the distribution of the sample", 
        "mean as the sample size grows."))

## other distributions: Chi-square with df = 5
f = function(n) rchisq(n, 5)
clt.ani(FUN = f)

ani.options(oopt)

Run the code above in your browser using DataLab