4/25/2020

The Normal Distribution Shiny App

This is app shows how varying the parameters (mean, standard deviation, and sample size) effect the shape of the histogram drawn from a randomly simulated Normal distribution. You can also change the number of bins in the histogram (how many data points are aggregated into a bar).

It is intended as a pedagogical tool to help conceptualize the effects of the parameters on the Normal distribution.

The app is hosted here: https://derekdixon.shinyapps.io/ShinyNormalDist/

What is a Normal Distribution?

A bell-shaped distribution symmetric about it’s mean. Here I will take 1000 random samples from a normal distribution with a mean of zero and a standard deviation of 1 and display it’s plot.

# Create a vector of 1000 random draws and store in x
x <- rnorm(n=1000, mean=0, sd=1)

# Create the plot
NormPlot <- hist(x)

# Display the plot
NormPlot
## $breaks
##  [1] -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## [16]  3.0
## 
## $counts
##  [1]   1   0   1   4  16  39  96 139 161 214 167 105  35  16   6
## 
## $density
##  [1] 0.002 0.000 0.002 0.008 0.032 0.078 0.192 0.278 0.322 0.428 0.334 0.210
## [13] 0.070 0.032 0.012
## 
## $mids
##  [1] -4.25 -3.75 -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25
## [13]  1.75  2.25  2.75
## 
## $xname
## [1] "x"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"

Chaning the Mean

What happens to our plot if we change the mean? Let’s make it mean of 50.

# Create a vector of 1000 random draws and store in x2
x2 <- rnorm(n=1000, mean=50, sd=1)

# Create the plot
NormPlot2 <- hist(x2)

# Display the plot
NormPlot2
## $breaks
##  [1] 46.0 46.5 47.0 47.5 48.0 48.5 49.0 49.5 50.0 50.5 51.0 51.5 52.0 52.5 53.0
## 
## $counts
##  [1]   1   3   5  14  32  85 169 177 216 135 103  39  16   5
## 
## $density
##  [1] 0.002 0.006 0.010 0.028 0.064 0.170 0.338 0.354 0.432 0.270 0.206 0.078
## [13] 0.032 0.010
## 
## $mids
##  [1] 46.25 46.75 47.25 47.75 48.25 48.75 49.25 49.75 50.25 50.75 51.25 51.75
## [13] 52.25 52.75
## 
## $xname
## [1] "x2"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"

We see that it has the effect of shifting the entire curve to the right on the x-axis.

Changing the Standard Deviation

What happens to our plot if we change the standard deviation? Let’s make it 10.

# Create a vector of 1000 random draws and store in x3
x3 <- rnorm(n=1000, mean=0, sd=10)

# Create the plot
NormPlot3 <- hist(x3)

# Display the plot
NormPlot3
## $breaks
##  [1] -35 -30 -25 -20 -15 -10  -5   0   5  10  15  20  25  30  35
## 
## $counts
##  [1]   1   4  27  48  88 144 184 205 140  79  51  19   6   4
## 
## $density
##  [1] 0.0002 0.0008 0.0054 0.0096 0.0176 0.0288 0.0368 0.0410 0.0280 0.0158
## [11] 0.0102 0.0038 0.0012 0.0008
## 
## $mids
##  [1] -32.5 -27.5 -22.5 -17.5 -12.5  -7.5  -2.5   2.5   7.5  12.5  17.5  22.5
## [13]  27.5  32.5
## 
## $xname
## [1] "x3"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"

We see that it has the affect of “widening” the curve.

Conclusion

You can play with all of these parameters and observe the effects in the Shiny App. Enjoy.