---
title: "Introduction to data"
output:
  html_document:
    theme: cerulean
    highlight: pygments
    css: ../lab.css
    toc: true
    toc_float: true
---

```{r global-options, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
library(dplyr)
library(ggplot2)
library(oilabs)
```

Some define statistics as the field that focuses on turning information into
knowledge. The first step in that process is to summarize and describe the raw
information -- the data. In this lab we explore flights, specifically a random
sample of domestic flights that departed from the three major 
New York City airport in 2013. We will generate simple graphical and numerical 
summaries of data on these flights and explore delay times. As this is a large 
data set, along the way you'll also learn the indispensable skills of data 
processing and subsetting.


## Getting started

### Load packages

In this lab we will explore the data using the `dplyr` package and visualize it 
using the `ggplot2` package for data visualization. The data can be found in the
companion package for OpenIntro labs, `oilabs`.

Let's load the packages.

```{r load-packages, message=FALSE}
library(dplyr)
library(ggplot2)
library(oilabs)
```

### Creating a reproducible lab report

Remember that we will be using R Markdown to create reproducible lab reports. 
See the following video describing how to get started with creating these 
reports for this lab, and all future labs:

[**Basic R Markdown with an OpenIntro Lab**](https://www.youtube.com/watch?v=Pdc368lS2hk)
<iframe width="560" height="315" src="https://www.youtube.com/embed/Pdc368lS2hk" frameborder="0" allowfullscreen></iframe>

### The data

The [Bureau of Transportation Statistics](http://www.rita.dot.gov/bts/about/) 
(BTS) is a statistical agency that is a part of the Research and Innovative 
Technology Administration (RITA). As its name implies, BTS collects and makes 
available transportation data, such as the flights data we will be working with 
in this lab.

We begin by loading the `nycflights` data frame. Type the following in your console
to load the data:

```{r load-data, eval = TRUE}
data(nycflights)
```

The data set `nycflights` that shows up in your workspace is a *data matrix*, 
with each row representing an *observation* and each column representing a 
*variable*. R calls this data format a **data frame**, which is a term that will 
be used throughout the labs. For this data set, each *observation* is a single flight.

To view the names of the variables, type the command

```{r names}
names(nycflights)
```

This returns the names of the variables in this data frame. The **codebook**
(description of the variables) can be accessed by pulling up the help file:

```{r}
?nycflights
```

One of the variables refers to the carrier (i.e. airline) of the flight, which 
is coded according to the following system.

- `carrier`: Two letter carrier abbreviation.
    + `9E`:           Endeavor Air Inc.
    + `AA`:      American Airlines Inc.
    + `AS`:        Alaska Airlines Inc.
    + `B6`:             JetBlue Airways
    + `DL`:        Delta Air Lines Inc.
    + `EV`:    ExpressJet Airlines Inc.
    + `F9`:      Frontier Airlines Inc.
    + `FL`: AirTran Airways Corporation
    + `HA`:      Hawaiian Airlines Inc.
    + `MQ`:                   Envoy Air
    + `OO`:       SkyWest Airlines Inc.
    + `UA`:       United Air Lines Inc.
    + `US`:             US Airways Inc.
    + `VX`:              Virgin America
    + `WN`:      Southwest Airlines Co.
    + `YV`:          Mesa Airlines Inc.


A very useful function for taking a quick peek at your data frame and viewing
its dimensions and data types is `str`, which stands for **str**ucture.

```{r str}
str(nycflights)
```

The `nycflights` data frame is a massive trove of information. Let's think about 
some questions we might want to answer with these data:

- How delayed were flights that were headed to Los Angeles?
- How do departure delays vary over months?
- Which of the three major NYC airports has a better on time percentage for 
departing flights?


## Analysis

### Lab report
To record your analysis in a reproducible format, you can adapt the general Lab 
Report template from the `oilabs` package. Watch the video above to learn how.

### Departure delays

Let's start by examing the distribution of departure delays of all flights with a 
histogram.

```{r hist-dep-delay}
qplot(x = dep_delay, data = nycflights, geom = "histogram")
```

This function says to plot the `dep_delay` variable from the `nycflights` data 
frame on the x-axis. It also defines a `geom` (short for geometric object), 
which describes the type of plot you will produce. 

Histograms are generally a very good way to see the shape of a single 
distribution of numerical data, but that shape can change depending on how the 
data is split between the different bins. You can easily define the binwidth you 
want to use:

```{r hist-dep-delay-bins}
qplot(x = dep_delay, data = nycflights, geom = "histogram", binwidth = 15)
qplot(x = dep_delay, data = nycflights, geom = "histogram", binwidth = 150)
```

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

If we want to focus only on departure delays of flights headed to Los Angeles, 
we need to first `filter` the data for flights with that destination (`dest == "LAX"`)
and then make a histogram of the departure delays of only those flights.

```{r lax-flights-hist}
lax_flights <- nycflights %>%
  filter(dest == "LAX")
qplot(x = dep_delay, data = lax_flights, geom = "histogram")
```

Let's decipher these two commands (OK, so it might look like three lines, but
the first two physical lines of code are actually part of the same command. It's 
common to add a break to a new line after `%>%` to help readability).

- Command 1: Take the `nycflights` data frame, `filter` for flights headed to LAX, and 
save the result as a new data frame called `lax_flights`.
    + `==` means "if it's equal to".
    + `LAX` is in quotation marks since it is a character string.
- Command 2: Basically the same `qplot` call from earlier for making a histogram, 
except that it uses the smaller data frame for flights headed to LAX instead of all
flights.

<div id="boxedtext">
**Logical operators: ** Filtering for certain observations (e.g. flights from a 
particular airport) is often of interest in data frames where we might want to 
examine observations with certain characteristics separately from the rest of 
the data. To do so we use the `filter` function and a series of 
**logical operators**. The most commonly used logical operators for data 
analysis are as follows:

- `==` means "equal to"
- `!=` means "not equal to"
- `>` or `<` means "greater than" or "less than"
- `>=` or `<=` means "greater than or equal to" or "less than or equal to"
</div>

We can also obtain numerical summaries for these flights:

```{r lax-flights-summ}
lax_flights %>%
  summarise(mean_dd = mean(dep_delay), median_dd = median(dep_delay), n = n())
```

Note that in the `summarise` function we created a list of three different
numerical summaries that we were interested in. The names of these elements are 
user defined, like `mean_dd`, `median_dd`, `n`, and you could customize these names 
as you like (just don't use spaces in your names). Calculating these summary 
statistics also require that you know the function calls. Note that `n()` reports
the sample size.

<div id="boxedtext">
**Summary statistics: ** Some useful function calls for summary statistics for a 
single numerical variable are as follows:

- `mean`
- `median`
- `sd`
- `var`
- `IQR`
- `min`
- `max`

Note that each of these functions take a single vector as an argument, and 
returns a single value. 
</div>

We can also filter based on multiple criteria. Suppose we are interested in
flights headed to San Francisco (SFO) in February:

```{r}
sfo_feb_flights <- nycflights %>%
  filter(dest == "SFO", month == 2)
```

Note that we can separate the conditions using commas if we want flights that
are both headed to SFO **and** in February. If we are interested in either
flights headed to SFO **or** in February we can use the `|` instead of the comma.

1.  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? 
    
1.  Describe the distribution of the **arrival** delays of these flights using a 
    histogram and appropriate summary statistics. **Hint:** The summary 
    statistics you use should depend on the shape of the distribution.
    
Another useful technique is quickly calculating summary 
statistics for various groups in your data frame. For example, we can modify the 
above command using the `group_by` function to get the same summary stats for 
each origin airport:

```{r summary-custom-list-origin}
sfo_feb_flights %>%
  group_by(origin) %>%
  summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())
```

Here, we first grouped the data by `origin`, and then calculated the summary 
statistics.

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

### Departure delays over months

Which month would you expect to have the highest average delay departing from an
NYC airport?

Let's think about how we would answer this question:

- First, calculate monthly averages for departure delays. With the new language
we are learning, we need to
    + `group_by` months, then
    + `summarise` mean departure delays.
- Then, we need to `arrange` these average delays in `desc`ending order

```{r mean-dep-delay-months}
nycflights %>%
  group_by(month) %>%
  summarise(mean_dd = mean(dep_delay)) %>%
  arrange(desc(mean_dd))
```

1.  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?

<!--
Which month has the highest average departure delay from an NYC airport? What
    about the highest median departure delay? Which of these measures is more 
    reliable for deciding which month(s) to avoid flying if you really dislike 
    delayed flights.
-->

### On time departure rate for NYC airports

Suppose you will be flying out of NYC and want to know which of the 
three major NYC airports has the best on time departure rate of departing flights. 
Suppose also that for you a flight that is delayed for less than 5 minutes is 
basically "on time". You consider any flight delayed for 5 minutes of more to be 
"delayed".

In order to determine which airport has the best on time departure rate, 
we need to 

- first classify each flight as "on time" or "delayed",
- then group flights by origin airport,
- then calculate on time departure rates for each origin airport,
- and finally arrange the airports in descending order for on time departure
percentage.

Let's start with classifying each flight as "on time" or "delayed" by
creating a new variable with the `mutate` function.

```{r dep-type}
nycflights <- nycflights %>%
  mutate(dep_type = ifelse(dep_delay < 5, "on time", "delayed"))
```

The first argument in the `mutate` function is the name of the new variable
we want to create, in this case `dep_type`. Then if `dep_delay < 5` we classify 
the flight as `"on time"` and `"delayed"` if not, i.e. if the flight is delayed 
for 5 or more minutes.

Note that we are also overwriting the `nycflights` data frame with the new 
version of this data frame that includes the new `dep_type` variable.

We can handle all the remaining steps in one code chunk:

```{r}
nycflights %>%
  group_by(origin) %>%
  summarise(ot_dep_rate = sum(dep_type == "on time") / n()) %>%
  arrange(desc(ot_dep_rate))
```

1.  If you were selecting an airport simply based on on time departure 
    percentage, which NYC airport would you choose to fly out of?

We can also visualize the distribution of on on time departure rate across 
the three airports using a segmented bar plot.

```{r}
qplot(x = origin, fill = dep_type, data = nycflights, geom = "bar")
```

* * *

## More Practice

1.  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.
    
1.  Make a scatterplot of `avg_speed` vs. `distance`. Describe the relationship
    between average speed and distance.
    **Hint:** Use `geom = "point"`.

1.  Replicate the following plot. **Hint:** The data frame plotted only
    contains flights from American Airlines, Delta Airlines, and United
    Airlines, and the points are `color`ed 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.

```{r echo=FALSE, eval=TRUE, fig.width=7, fig.height=4}
dl_aa_ua <- nycflights %>%
  filter(carrier == "AA" | carrier == "DL" | carrier == "UA")
qplot(x = dep_delay, y = arr_delay, data = dl_aa_ua, color = carrier)
```