---
title: "Homework 3"
author: "Insert Name"
date: "Math 141, Week 3"
output:
  pdf_document
graphics: yes
urlcolor: blue
---

```{r setup, include=FALSE}
# Do not modify this chunk.
knitr::opts_chunk$set(echo = F, message=FALSE, warning = FALSE, fig.align = "center")
library(knitr)
library(tidyverse)
```


**Due: 11:59pm, Friday February 11**

# Instructions
Work through the problems below  and submit this document as a knitted .pdf to the Math 141 S22 Wells Lecture [gradescope page](https://www.gradescope.com/courses/354598). 

**For each problem, put your solution between the bars of red stars.**



### Acknowledgements
If you work with a classmate, please write a note acknowledging this.


## Exercise 1

Daily air quality is measured by the air quality index (AQI) reported by the Environmental Protection Agency. This index reports the pollution level and what associated health effects might be a concern. The index is calculated for five major air pollutants regulated by the Clean Air Act and takes values from 0 to 300, where a higher value indicates lower air quality. AQI was reported for a sample of 91 days in 2011 in Durham, NC. The histogram below shows the distribution of the AQI values on these days.


```{r}
library(openintro)
pm25_2011_durham %>% ggplot(aes(x = daily_aqi_value))+
  geom_histogram(bins = 12, color = "white", fill = "steelblue") + 
  labs(x = "Daily AQI") +
  theme_bw()
```

a. Estimate the median AQI value of this sample based on the graph.

*************************************************


 
*************************************************

b. Would you expect the mean AQI value of this sample to be higher or lower than the median? Explain your reasoning.

*************************************************


 
*************************************************

c. Estimate Q1, Q3, and IQR for the distribution based on the graph.

*************************************************


 
*************************************************

d. Would any of the days in this sample be considered to have an unusually low or high AQI? Explain your reasoning.

*************************************************


 
*************************************************




## Exercise 2

It is time for each of you to create your own version of [the Anne Hathaway graph](https://fivethirtyeight.com/features/the-three-types-of-anne-hathaway-movies/) from the first week of class.  For an actor of your choice (other than Anne Hathaway), create a scatterplot of Rotten Tomatoes rating and Box office gross, with points colored according to another categorical variable of your choice

a. Which actor will you be graphing? Beyond Rotten Tomatoes rating and Box office gross, what is your third variable?  Note: It should be a variable you can collect online or can be defined by the user.

*************************************************


 
*************************************************


b. Create the data frame for your actor and include at least 10 movies.  To show how to create your own data frame in R, an example has been included below containing 4 Ryan Gosling movies. For reference, Rotten Tomatoes ratings and Box Office can be found [here](https://www.rottentomatoes.com/celebrity/ryan_gosling).

```{r}
gosling <- data.frame(
  movie = c("Lars and the Real Girl", 
            "Drive",
            "La La Land", 
            "The Notebook"),
  rotten_tomatoes = c(81, 92, 91, 53),
  box_office = c(5.9, 34.4, 151.1, 81.1),
  genre = c("Drama", "Action", "Musical", "Drama")
)
```


*************************************************


 
*************************************************


c. Construct your graph. Choose a color palette that is not the default `ggplot2` palette.

*************************************************


 
*************************************************

d. Let's add a bit more context to our graph.  If you haven't already, add nice labels, a title, and a caption with the data source.  Additionally, label the movies in the graph.  Here's some code to get you started.  Make sure to replace all lines that say *insert ___* with the appropriate layers, and then change `eval = FALSE` to `eval = TRUE`.

*************************************************

```{r, eval=FALSE}

library(ggrepel) #This contains the `geom_text_repel` function.

insert ggplot layer + 
  insert geom layer + 
  insert scale layer +
  insertlabs layer +
  geom_text_repel(aes(label = movie), size = 4)
```

*************************************************


e. Draw some conclusions about the relationships between box office gross, ratings, and your third variable.  What does this tell us about your actor?


*************************************************


 
*************************************************


## Exercise 3

A local news survey asked 500 randomly sampled Los Angeles residents which shipping carrier they prefer to use for shipping holiday gifts. Data on the age group of each respondent, along with shipping preference, is contained in the `shipping` data frame, which can be loaded with the following code (don't worry about interpreting what each line of code means)

```{r echo = F}
shipping <- tribble(
      ~shipping_method,   ~`18_34`,  ~`35_54`,  ~`55plus`,
      "USPS",                   72,     97,        76,
      "UPS",                    52,        76,      34, 
      "FedEx",                  31,        24,         9,        
      "Something else",          7,         6,         3,   
      "Not sure",                3,         6,          4
    ) %>% 
      pivot_longer(cols = 2:4, names_to = "age", values_to = "count") %>%
      mutate(shipping_method = fct_relevel(shipping_method, "Not sure", "Something else", "FedEx", "UPS", "USPS")) %>%
      mutate(age = fct_relevel(age, "55plus", "35_54", "18_34")) %>%  
      uncount( weights = count)
```


a. Consider the two graphics created from the data by the code below. Which graph would you use to understand the shipping choices of people of different ages? Which graph would you use to understand the age distribution across different types of shipping choices?

```{r out.width = "80%"}
ggplot(shipping, aes(y = shipping_method, fill = age)) + 
  geom_bar(position = "fill") + 
  labs(x = "Proportion", y = "Group", fill = "Age", title = "Graph 1") +
  scale_fill_brewer(type = "seq", palette = 3) +
  theme_bw()
  

ggplot(shipping, aes(y = age, fill = shipping_method)) +
  geom_bar(position = "fill") +
  labs(x = "Proportion", y = "Group", fill = "Age", title = "Graph 2") + 
  scale_fill_brewer(type = "qual", palette = 1) +
  theme_bw()
```

*************************************************


 
*************************************************



b. Use the `table` and `prop.table` functions to create a contingency table representing the same information in Graph 1.

*************************************************


 
*************************************************


c. Use the `table` and `prop.table` functions to create a contingency table representing the same information in Graph 2.


*************************************************


 
*************************************************


d. A new shipping company would like to market to people over the age of 55.  Who will be their biggest competitor?

*************************************************


 
*************************************************
    
d. FedEx would like to reach out to grow their market share to balance the age demographics of FedEx users.  To what age group should FedEx market?

*************************************************


 
*************************************************