CLT demonstration using Shiny

Jørgen D. Tyvand
March 23, 2018

Overview of the Shiny App

I have created a Shiny app to demonstrate the Central Limit Theorem (CLT). The app is located at https://jdtyvand.shinyapps.io/cltdemo/, and the GitHub repo containing the source code for the app and this presentation can be found at https://www.github.com/JDTyvand/CLTdemo

default

The Central Limit Theorem

From the Central Limit Theorem Wikipedia page:

…For example, suppose that a sample is obtained containing a large number of observations, each observation being randomly generated in a way that does not depend on the values of the other observations, and that the arithmetic average of the observed values is computed. If this procedure is performed many times, the central limit theorem says that the computed values of the average will be distributed according to a normal distribution. A simple example of this is that if one flips a coin many times the probability of getting a given number of heads in a series of flips will approach a normal curve, with mean equal to half the total number of flips in each series.

Note that for this app we have also normalized the mean of the samples, so the distribution at large sample sizes will be approximately standard normal (\( N0, 1) \))

Using the Shiny App

The following choices can be made by the user in the Shiny App

  • The underlying distribution of the samples to be drawn. These include (fair) die rolls, (fair) coin flips, the exponential distribution and the poisson distribution (both with \( \lambda \) = 1).
  • The number of simulations to be made, i.e. the number of times we draw n samples and take the mean.
  • The number of samples in each simulation.

Code example for means for the Die roll case

set.seed(2604)
nosim = 1000; n = 20
sim <- sample(1:6, nosim * n, replace = TRUE)
sims <- matrix(sim, nosim)
mu <- 3.5; s <-  1.71; SE <-  s/sqrt(n)
func <- function(x, n) (mean(x) - mu) / SE
dat <- data.frame(x = apply(sims, 1, func, n))
head(dat, 5)
           x
1  0.3922926
2 -0.3922926
3  0.1307642
4  1.1768779
5 -2.0922273