---
title: "Comp/Stat 112: Six Main Verbs"
author: "Assignment 5 Solutions"
output:
  bookdown::html_document2:
    split_by: none
    toc: yes
    toc_depth: 3
    toc_float:
      toc_collapsed: true
    number_sections: false
---

```{r 07_setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
library(tidyverse)
library(ggplot2)
library(mosaicData) #install.pacakges('mosaicData')
library(lubridate) 
Birthdays <- Birthdays %>% select(state, date, year, births)
```


```{r}
Birthdays <-
  Birthdays %>%
  mutate(
    month = month(date, label = TRUE),
    weekday = wday(date, label = TRUE)
  )
```

## Assignment 5 (Part 1): due Wednesday, Feb 22 @ 11:59pm {-}

Now we are ready to return to the `Birthdays` data set to investigate some drivers of daily births in the US.

### Seasonality {-}

For this activity, we need to work with data aggregated across the states.

```{exercise, name="Total Across States", label="us-daily-births"}
Create a new data table, `DailyBirths`, that adds up all the births for each day across all the states.  Plot out daily births vs date.

```

```{r}

```

***For all of the remaining exercises, start with your `DailyBirths` data frame.***

```{exercise, name="Examine Seasonality", label="seasonality"}

To examine seasonality in birth rates, look at the number of births by

a. week of the year (1-53)
b. month of the year (January to December)
c. Julian day (1-366)

When are the most babies born? The fewest?

```

```{r}

```


### Day of the Week {-}

```{exercise, name="Examine Patterns within the Week", label="weekday"}
To examine patterns within the week, make a box plot showing the number of births by day of the week. Interpret your results.

```


```{r}

```


### Holidays {-}

```{exercise, name="Two Year Sample", label="two-year"}
Pick a two-year span of the `DailyBirths` that falls in the 1980s, say, 1980/1981.  Extract out the data just in this interval, calling it `MyTwoYears`.  (Hint: `filter()`, `year()`).  Plot out the births in this two-year span day by day. Color each date according to its day of the week. Explain the pattern that you see.

```

```{r}

```



```{exercise, name="Holidays", label="holidays"}
Now let's update the plot from Exercise \@ref(exr:two-year) to include the holidays.   

a. Add a variable to `MyTwoYears` called `is_holiday`. It should be `TRUE` when the day is a holiday, and `FALSE` otherwise. One way to do this is with the transformation verb `%in%`, for instance, `is_holiday = date %in% Holidays$date`.   
b. Add a `geom_point` layer to your plot that sets the color of the points based on the day of the week and the shape of the points based on whether or not the day is a holiday.   
c. Finally, some holidays seem to have more of an effect than others. It would be helpful to label them. Use `geom_text` with the holiday data to add labels to each of the holidays. Hints: 1. Start by making a new data table called `MyHolidays` that just contains the holidays in your selected two year window. 2. Start your geometry line with `geom_text(data=MyHolidays)`. 3. You'll have to make up a y-coordinate for each label.  4. You can set the orientation of each label with the `angle` argument; e.g., `geom_text(data=MyHolidays, angle=40, ...)`.

```

Hints:

1.To add the y-values in the `MyHolidays`, run the following code chunk: 
  

```{r, eval = FALSE}
MyTwoYears_holidays = MyTwoYears %>%
  filter(is_holiday)

MyHolidays = Holidays %>%
  filter(year %in% c(____, _____)) %>%
  arrange(date) %>%
  mutate(total = MyTwoYears_holidays$total)
```

2. To add labels to the graph:

```{r}
 +  geom_text(data = MyHolidays, aes(x = date, y = total, label = holiday), size = 2.5, angle = 40)
```


> ANSWER:



```{r}

```

### Geography {-}

```{exercise, name="Examine the Effect of Geography", label="geography"}
In any way you choose, explore the effect of geography on birth patterns. For example, do parents in Minnesota have fewer winter babies than in other states? Which states have the largest increases or decreases in their portion of US births over time? Is the weekend effect less strong for states with a higher percentage of their populations living in rural areas? Pick any issue (not all of these) that interests you, explore it, and create a graphic to illustrate your findings.

```

> ANSWER:

```{r}

```


### Superstition {-}

This [article](https://fivethirtyeight.com/features/some-people-are-too-superstitious-to-have-a-baby-on-friday-the-13th/) from FiveThirtyEight demonstrates that fewer babies are born on the 13th of each month, and the effect is even stronger when the 13th falls on a Friday. If you have extra time or want some extra practice, you can try to recreate the first graphic in the article.

> ANSWER:

```{r}

```
