clt.ani {animation}R Documentation

Demonstration of the Central Limit Theorem

Description

First of all, a number of obs observations are generated from a certain distribution for each variable X_j, j = 1, 2, ..., n, and n = 1, 2, ..., 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.

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 hist

Details

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.

Value

None.

Author(s)

Yihui Xie

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

Alias

clt.ani

Keyword

dynamic, distribution, dplot

Examples


oopt = ani.options(interval = 0.1, nmax = 150)
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)

## Not run: 
 
# HTML animation page
ani.options(ani.height = 500, ani.width = 600, outdir = getwd(), nmax = 100,
    interval = 0.1, title = "Demonstration of the Central Limit Theorem",
    description = "This animation shows the distribution of the sample
    mean as the sample size grows.")
ani.start()
par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0), tcl = -0.3)
clt.ani(type = "h")
ani.stop()
## End(Not run)
ani.options(oopt)

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

Results


R version 2.9.0 (2009-04-17)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(animation)
> png(filename="images/clt.ani_%03d.png" ,width=480, height=480)
> ### Name: clt.ani
> ### Title: Demonstration of the Central Limit Theorem
> ### Aliases: clt.ani
> ### Keywords: dynamic distribution dplot
> 
> ### ** Examples
> 
> 
> oopt = ani.options(interval = 0.1, nmax = 150)
> 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)
> 
> ## Not run: 
> ##D  
> ##D # HTML animation page
> ##D ani.options(ani.height = 500, ani.width = 600, outdir = getwd(), nmax = 100,
> ##D     interval = 0.1, title = "Demonstration of the Central Limit Theorem",
> ##D     description = "This animation shows the distribution of the sample
> ##D     mean as the sample size grows.")
> ##D ani.start()
> ##D par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0), tcl = -0.3)
> ##D clt.ani(type = "h")
> ##D ani.stop()
> ## End(Not run)
> ani.options(oopt)
> 
> # other distributions: Chi-square with df = 5 
> f = function(n) rchisq(n, 5) 
> clt.ani(FUN = f) 
> 
> 
> 
> dev.off()
null device 
          1 
> 









































































































































































































[Package animation Index]