---
title: "Histogram Bins"
author: "Russell Almond"
date: "January 17, 2019"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
```

The data show the duration (in minutes) of eruptions of the Geyser 'Old Faithful' in Yellowstone National Park, Wyoming.  Härdle (1991).

## Bin size and bandwidth.

The number of histograms in a describe how much detail you get.  Try adjusting thing  number to see the effect.  Too few bins might cause the viewer to miss out on important details. Too many bins might cause the viewer to see details that are just an artifact of the sample (which might be different if the data were taken from a different window of time).

The smooth line is a kernel smoother (a technique which available in R, but not SPSS).  The bandwidth of the smoother is like the bin size of the histogram.  Large bandwidths provide less detail and smaller provide more.  

With both bin sizes and bandwidths:
* The analyst sometimes need to try a few values to find the best one for your purposes.
* The defaults in the stat packages are usually a good starting point.  Try more bins (or smaller bandwidth) and fewer bins (less bandwidth) than the automatically chosen starting point.

```{r eruptions, echo=FALSE}
inputPanel(
  selectInput("n_breaks", label = "Number of bins:",
              choices = c(10, 20, 35, 50), selected = 20),
  
  sliderInput("bw_adjust", label = "Bandwidth adjustment:",
              min = 0.2, max = 2, value = 1, step = 0.2)
)

renderPlot({
  hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
       xlab = "Duration (minutes)", main = "Geyser eruption duration")
  
  dens <- density(faithful$eruptions, adjust = input$bw_adjust)
  lines(dens, col = "blue")
})
```
Härdle, W. (1991). *Smoothing Techniques with Implementation in S.* New York: Springer.

## Credits

I borrowed this document from one of the Shiny sample documents.  Original instructions below.  (Russell Almond).

This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately. 

To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).



