---
title:  "ADA1: Class 07, Introduction to Data"
author: Your Name
date:   now # last-modified
description: |
  [Advanced Data Analysis 1](https://www.math.unm.edu/~luyan/ADA1/ADA125.html),
  Stat 427/527, Fall 2025
format:
  html:
    theme:                  litera
    highlight-style:        atom-one
    page-layout:            full      # article, full   # https://quarto.org/docs/output-formats/page-layout.html
    toc:                    true
    toc-location:           body  # body, left, right
    number-sections:        false
    self-contained:         false     # !!! this can cause a render error
    code-overflow:          scroll    # scroll, wrap
    code-block-bg:          true
    code-block-border-left: "#30B0E0"
    code-copy:              false     # true, false, hover a copy buttom in top-right of code block
---

# Rubric

The context of this assignment comes from
[OpenIntro Labs](http://openintrostat.github.io/oilabs-tidy/) for R and tidyverse:

* [2. Intro to data](http://openintrostat.github.io/oilabs-tidy/02_intro_to_data/intro_to_data.html)

_This is a template for the assignment.  Modify this and turn it in._

```{r load-packages}
#| output:  false
#| warning: false
#| error:   false
library(tidyverse)
library(openintro)
```

## (1 p) 1. Histograms

*Look carefully at these two histograms. How do they compare? Are features
revealed in one that are obscured in another?*

* [SOLUTION] Write the text of your answer here...

```{r}
data(nycflights)
nycflights |> names()
nycflights |> str()

p1 <- ggplot(data = nycflights, aes(x = dep_delay))
p1 <- p1 + geom_histogram(binwidth = 15)

p2 <- ggplot(data = nycflights, aes(x = dep_delay))
p2 <- p2 + geom_histogram(binwidth = 150)

# grid of plots with patchwork
#   https://patchwork.data-imaginist.com/articles/guides/assembly.html
library(patchwork)
p1 / p2

```


## (1 p) 2. SFO in February

*Create a new data frame that includes flights headed to SFO in February, and
save this data frame as sfo_feb_flights. How many flights meet these criteria?*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

sfo_feb_flights <-
  nycflights |>
  filter(
    dest  == "SFO"
  , month == 2
  )

```


## (1 p) 3. Arrival delays

*Describe the distribution of the arrival delays (variable: arr_delay) of the flights headed to SFO in February using a
histogram and appropriate summary statistics. Hint: The summary statistics you
use should depend on the shape of the distribution.*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

```


## (1 p) 4. Carrier median and IQR

*Calculate the median and interquartile range for `arr_delays` of flights in
the `sfo_feb_flights` data frame, grouped by carrier. Which carrier has the most
variable arrival delays?*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

```


## (1 p) 5. Departure delays by month

*Suppose you really dislike departure delays and you want to schedule your
travel in a month that minimizes your potential departure delay leaving NYC.
One option is to choose the month with the lowest mean departure delay. Another
option is to choose the month with the lowest median departure delay. What are
the pros and cons of these two choices?*

* Write the text of your answer here...

```{r}
# Insert code here

```


## (1 p) 6. On time departure percentage

*If you were selecting an airport simply based on on time departure percentage,
which NYC airport (variable "origin") would you choose to fly out of?*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

nycflights <-
  nycflights |>
  mutate(
    dep_type = ifelse(dep_delay < 5, "on time", "delayed")
  )

p <- ggplot(data = nycflights, aes(x = origin, fill = dep_type))
p <- p + geom_bar()
p


sum_ontime <-
  nycflights |>
  group_by(
    origin
  ) |>
  summarize(
    ot_dep_rate = sum(dep_type == "on time") / n()
  ) |>
  arrange(
    desc(ot_dep_rate)
  )

sum_ontime

```


## (1 p) 7. Average speed

*Mutate the data frame so that it includes a new variable that contains the
average speed, `avg_speed` traveled by the plane for each flight (in mph). Hint:
Average speed can be calculated as `distance` divided by number of hours of
travel, and note that `air_time` is given in minutes.*

* Write the text of your answer here...

```{r}
# Insert code here

```


## (1 p) 8. Scatterplot of speed and distance

*Make a scatterplot of `avg_speed` vs. `distance`. Describe the relationship
between average speed and distance. Hint: Use geom_point().*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

```


## (2 p) 9. Scatterplot of arrival and departure delay

*Replicate the following plot. Hint: The data frame plotted only contains
flights from American Airlines, Delta Airlines, and United Airlines, and the
points are colored by carrier. Once you replicate the plot, determine (roughly)
what the cutoff point is for departure delays where you can still expect to get
to your destination on time.*

* [SOLUTION] Write the text of your answer here...

```{r}
# Insert code here

```


