---
title: "Exploratory Data Analysis"
author: J. Hathaway
params:
  day: 8
  ptitle: true
  pbackground: true
  dtype: "none"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

if (params$day %% 2 == 0) md_intro_format <- "even_intro_format.Rmd"
if (params$day %% 2 == 1) md_intro_format <- "odd_intro_format.Rmd"

```

# Becoming the Critic. {data-background=#e8d725}

```{r nextone}
vday <- params$day - 1
vlink <- paste0("http://www.perceptualedge.com/example", vday, ".php")
titlelink <- paste0('{data-background-iframe="', vlink, '"}')
```

## `r titlelink`

[Visualization of the Day](`r vlink`)

## Great Quotes

> 'There are no routine statistical questions, only questionable statistical routines.' 

— Sir David Cox

> 'Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise.'

— John Tukey

```{r child=md_intro_format}

```



## Socrative Hours Quiz

[socrative.com](https://socrative.com)

## Your research questions

**[Task 3:](https://byuistats.github.io/M335/class_tasks/task03_details.html)**

> - Share your research question with your neighbor and explain why finding an answer to the question with data would be exciting.
> - Then we can discuss a few as a class.

# What is EDA? {data-background=#e8d725}

## Socrative Quiz

> - [Socrative: Exploratory Analysis](https://socrative.com/)

## Exploratory Data Analysis

EDA is fundamentally a creative process. And like most creative processes, the key to asking quality questions is to generate a large quantity of questions.
 
> 1. What type of variation occurs within my variables?

> 2. What type of covariation occurs between my variables?

## Understanding `case_when()`

`case_when()` is particularly useful inside mutate when you want to create a new variable that relies on a complex combination of existing variables. **Write a short sentence that says what this code is doing?**

```{r starwars, eval=FALSE, echo=TRUE}

starwars %>%
  select(name:mass, gender, species) %>%
  mutate(
    type = case_when(
      height > 200 | mass > 200 ~ "large",
      species == "Droid"        ~ "robot",
      TRUE                      ~  "other"
    )
  )

#> # A tibble: 87 x 6
#>                  name height  mass gender species  type
#>                 <chr>  <int> <dbl>  <chr>   <chr> <chr>
#>  1     Luke Skywalker    172    77   male   Human other
#>  2              C-3PO    167    75   <NA>   Droid robot
#>  3              R2-D2     96    32   <NA>   Droid robot
#>  4        Darth Vader    202   136   male   Human large


```


# Old Faithful

## Exploring Old Faithful goals

> 1. **Make the [histogram shown in the book](http://r4ds.had.co.nz/EDA_files/figure-html/unnamed-chunk-9-1.png) with the black and white theme and an improved x-axis label.**

## Exploring Old Faithful (1)

```{r example2, fig.height=3}
faithful %>%
  ggplot(aes(x = eruptions)) + 
    geom_histogram(color = "white") +
    theme_bw() +
    labs(x = "Duration of eruption (minutes)", 
         y = "Number of Observations")
```



## Exploring Old Faithful goals (2)

> 1. Make the [histogram shown in the book](http://r4ds.had.co.nz/EDA_files/figure-html/unnamed-chunk-9-1.png) with the black and white theme and an improved x-axis label.
> 2. **Use the mutate function to modify our plot to fill the histogram for two groups of waiting times.**

```{r dkdkdjfd, eval=FALSE}
mutate(waiting_group = case_when(waiting < 67 ~ " < 67 min",
                                   waiting >= 67 ~ " > 67 min"))
```

## Exploring Old Faithful (3)


```{r thestuf33, fig.height=3}
faithful %>%
  mutate(waiting_group = case_when(waiting < 67 ~ " < 67 min",
                                   waiting >= 67 ~ " > 67 min")) %>%
  ggplot(aes(x = eruptions, fill = waiting_group)) + 
    geom_histogram(color = "white") +
    scale_fill_brewer(type = "qual") +
    theme_bw() + theme(legend.position = "bottom") +
    labs(x = "Duration of eruption (minutes)", 
         fill = "Duration\nof Wait", y = "Number of Observations")
```

## Exploring Old Faithful goals (4)

> 1. Make the [histogram shown in the book](http://r4ds.had.co.nz/EDA_files/figure-html/unnamed-chunk-9-1.png) with the black and white theme and an improved x-axis label.
> 2. Use the mutate function to modify our plot to fill the histogram for two groups of waiting times.
> 3. **Use the waiting variable to make a hexbin plot of the relationship between waiting time and duration.**


## Exploring Old Faithful (5)

```{r realldkdjf, fig.width=5, fig.height=4}
faithful %>%
ggplot(aes(x = eruptions, y = waiting)) + 
  geom_hex() + theme_bw() +
  labs(x = "Duration of eruption (minutes)", 
       y = "Time between eruptions (minutes)", 
       fill = "Number of\nObservations")
```






