Binomial Simulator

A shiny app

Julia Watzek
Developing Data Products course

What is this app?

This application is a visualization for random samples drawn from a binomial distribution with the following user-specified parameters.

  • Number of observations (n)
  • Number of trials (size)
  • Probability of success (prob)

Users don't need to repeatedly execute code and, in fact, don't need knowledge of R at all.

Sample plot with default parameters

library(ggplot2)
n = 100; size = 15; prob = .5
df = data.frame(x = rbinom(n, size, prob))

( g = ggplot(df, aes(x = x)) + theme_bw() + scale_x_continuous(limits = c(0, size)) +
    geom_histogram(aes(y = ..density..), binwidth = 1, colour = 'black', fill = 'grey95') )

plot of chunk unnamed-chunk-2

Normal approximation

The density of the normal distribution with mean and standard deviation of the generated sample can be superimposed on the plot.

g + stat_function(fun = dnorm, args = list(mean = mean(df$x), sd = sd(df$x)), 
                  colour = 'steelblue', size = 1)

plot of chunk unnamed-chunk-4

Application

This makes the app a useful tool for statistics teachers to illustrate the normal approximation of the binomial distribution when n is large enough.

It also nicely demonstrates the concept of repeated sampling from a known probability distribution, which facilitates understanding of null-hypothesis significance testing.