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 airports in 2013. We will generate simple graphical and numerical summaries of data on these flights and explore delay times. Since this is a large data set, along the way you’ll also learn the indispensable skills of data processing and subsetting.
In this lab, we will explore and visualize the data using the tidyverse suite of packages. The data can be found in the companion package for OpenIntro labs, openintro.
Let’s load the packages.
Remember that we will be using R Markdown to create reproducible lab reports. In RStudio, go to New File -> R Markdown… Then, choose From Template and then choose Lab Report for OpenIntro Statistics Labs from the list of templates.
See the following video describing how to get started with creating these reports for this lab, and all future labs:
The Bureau of Transportation Statistics (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 transportation data available, such as the flights data we will be working with in this lab.
First, we’ll view the nycflights data frame. Type the following in your console to load the data:
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
## [1] "year" "month" "day" "dep_time" "dep_delay" "arr_time"
## [7] "arr_delay" "carrier" "tailnum" "flight" "origin" "dest"
## [13] "air_time" "distance" "hour" "minute"
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:
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 AirwaysDL: Delta Air Lines Inc.EV: ExpressJet Airlines Inc.F9: Frontier Airlines Inc.FL: AirTran Airways CorporationHA: Hawaiian Airlines Inc.MQ: Envoy AirOO: SkyWest Airlines Inc.UA: United Air Lines Inc.US: US Airways Inc.VX: Virgin AmericaWN: Southwest Airlines Co.YV: Mesa Airlines Inc.Remember that you can use glimpse to take a quick peek at your data to understand its contents better.
## Rows: 32,735
## Columns: 16
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,…
## $ month <int> 6, 5, 12, 5, 7, 1, 12, 8, 9, 4, 6, 11, 4, 3, 10, 1, 2, 8, 1…
## $ day <int> 30, 7, 8, 14, 21, 1, 9, 13, 26, 30, 17, 22, 26, 25, 21, 23,…
## $ dep_time <int> 940, 1657, 859, 1841, 1102, 1817, 1259, 1920, 725, 1323, 94…
## $ dep_delay <dbl> 15, -3, -1, -4, -3, -3, 14, 85, -10, 62, 5, 5, -2, 115, -4,…
## $ arr_time <int> 1216, 2104, 1238, 2122, 1230, 2008, 1617, 2032, 1027, 1549,…
## $ arr_delay <dbl> -4, 10, 11, -34, -8, 3, 22, 71, -8, 60, -4, -2, 22, 91, -6,…
## $ carrier <chr> "VX", "DL", "DL", "DL", "9E", "AA", "WN", "B6", "AA", "EV",…
## $ tailnum <chr> "N626VA", "N3760C", "N712TW", "N914DL", "N823AY", "N3AXAA",…
## $ flight <int> 407, 329, 422, 2391, 3652, 353, 1428, 1407, 2279, 4162, 20,…
## $ origin <chr> "JFK", "JFK", "JFK", "JFK", "LGA", "LGA", "EWR", "JFK", "LG…
## $ dest <chr> "LAX", "SJU", "LAX", "TPA", "ORF", "ORD", "HOU", "IAD", "MI…
## $ air_time <dbl> 313, 216, 376, 135, 50, 138, 240, 48, 148, 110, 50, 161, 87…
## $ distance <dbl> 2475, 1598, 2475, 1005, 296, 733, 1411, 228, 1096, 820, 264…
## $ hour <dbl> 9, 16, 8, 18, 11, 18, 12, 19, 7, 13, 9, 13, 8, 20, 12, 20, …
## $ minute <dbl> 40, 57, 59, 41, 2, 17, 59, 20, 25, 23, 40, 20, 9, 54, 17, 2…
The nycflights data frame is a massive trove of information. Let’s think about some questions we might want to answer with these data:
To record your analysis in a reproducible format, you can adapt the general Lab Report template from the openintro package. Watch the video above to learn how.
Let’s start by examing the distribution of departure delays of all flights with a histogram.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
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:
The Answer: the three histograms show there are many flights are leaving earlier than schedule. and majority of of flights leaves on time. and then the higher the delays the less flights are leaving.
If you want to visualize only on delays of flights headed to Los Angeles, you 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.
lax_flights <- nycflights %>%
filter(dest == "LAX")
ggplot(data = lax_flights, aes(x = dep_delay)) +
geom_histogram()## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Let’s decipher these two commands (OK, so it might look like four 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).
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.ggplot call from earlier for making a histogram, except that it uses the smaller data frame for flights headed to LAX instead of all flights.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, you can 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”You can also obtain numerical summaries for these flights:
## # A tibble: 1 x 3
## mean_dd median_dd n
## <dbl> <dbl> <int>
## 1 9.78 -1 1583
Note that in the summarise function you created a list of three different numerical summaries that you were interested in. The names of these elements are user defined, like mean_dd, median_dd, n, and you can customize these names as you like (just don’t use spaces in your names). Calculating these summary statistics also requires that you know the function calls. Note that n() reports the sample size.
Summary statistics: Some useful function calls for summary statistics for a single numerical variable are as follows:
meanmediansdvarIQRminmaxNote that each of these functions takes a single vector as an argument and returns a single value.
You can also filter based on multiple criteria. Suppose you are interested in flights headed to San Francisco (SFO) in February:
Note that you can separate the conditions using commas if you want flights that are both headed to SFO and in February. If you are interested in either flights headed to SFO or in February, you can use the | instead of the comma.
sfo_feb_flights. How many flights meet these criteria?The answer: 68 flights.
## year month day dep_time dep_delay
## Min. :2013 Min. :2 Min. : 1.00 Min. : 613 Min. :-10.0
## 1st Qu.:2013 1st Qu.:2 1st Qu.: 7.00 1st Qu.: 943 1st Qu.: -5.0
## Median :2013 Median :2 Median :16.00 Median :1268 Median : -2.0
## Mean :2013 Mean :2 Mean :15.26 Mean :1298 Mean : 10.5
## 3rd Qu.:2013 3rd Qu.:2 3rd Qu.:22.50 3rd Qu.:1742 3rd Qu.: 9.0
## Max. :2013 Max. :2 Max. :28.00 Max. :2159 Max. :209.0
## arr_time arr_delay carrier tailnum
## Min. : 118 Min. :-66.00 Length:68 Length:68
## 1st Qu.:1233 1st Qu.:-21.25 Class :character Class :character
## Median :1497 Median :-11.00 Mode :character Mode :character
## Mean :1607 Mean : -4.50
## 3rd Qu.:2062 3rd Qu.: 2.00
## Max. :2256 Max. :196.00
## flight origin dest air_time
## Min. : 11.0 Length:68 Length:68 Min. :317.0
## 1st Qu.: 85.0 Class :character Class :character 1st Qu.:345.0
## Median : 641.0 Mode :character Mode :character Median :354.0
## Mean : 795.1 Mean :351.9
## 3rd Qu.:1487.2 3rd Qu.:360.0
## Max. :2126.0 Max. :376.0
## distance hour minute
## Min. :2565 Min. : 6.00 Min. : 1.00
## 1st Qu.:2586 1st Qu.: 9.00 1st Qu.:25.00
## Median :2586 Median :12.50 Median :33.50
## Mean :2584 Mean :12.62 Mean :36.35
## 3rd Qu.:2586 3rd Qu.:17.00 3rd Qu.:54.00
## Max. :2586 Max. :21.00 Max. :59.00
## Warning in var(sfo_feb_flights): NAs introduced by coercion
## year month day dep_time dep_delay arr_time
## year 0 0 0.000000 0.0000 0.00000 0.0000
## month 0 0 0.000000 0.0000 0.00000 0.0000
## day 0 0 71.719930 1480.2221 37.95522 1489.5777
## dep_time 0 0 1480.222125 214100.0158 5191.25373 178443.3679
## dep_delay 0 0 37.955224 5191.2537 1107.53731 -2079.9701
## arr_time 0 0 1489.577700 178443.3679 -2079.97015 227437.5838
## arr_delay 0 0 -35.298507 1749.9403 1077.71642 -5254.2388
## carrier NA NA NA NA NA NA
## tailnum NA NA NA NA NA NA
## flight 0 0 -1107.776997 -47789.5443 -6138.62687 -21006.1071
## origin NA NA NA NA NA NA
## dest NA NA NA NA NA NA
## air_time 0 0 -51.266901 -2830.4934 -87.26866 -2341.2485
## distance 0 0 -6.545215 -240.4214 20.05970 -296.7103
## hour 0 0 14.953468 2151.2397 52.74627 1800.0246
## minute 0 0 -15.124671 -1023.9526 -83.37313 -1559.0904
## arr_delay carrier tailnum flight origin dest air_time
## year 0.00000 NA NA 0.0000 NA NA 0.000000
## month 0.00000 NA NA 0.0000 NA NA 0.000000
## day -35.29851 NA NA -1107.7770 NA NA -51.266901
## dep_time 1749.94030 NA NA -47789.5443 NA NA -2830.493415
## dep_delay 1077.71642 NA NA -6138.6269 NA NA -87.268657
## arr_time -5254.23881 NA NA -21006.1071 NA NA -2341.248464
## arr_delay 1316.28358 NA NA -5497.5224 NA NA 53.014925
## carrier NA NA NA NA NA NA NA
## tailnum NA NA NA NA NA NA NA
## flight -5497.52239 NA NA 551286.8622 NA NA 764.424934
## origin NA NA NA NA NA NA NA
## dest NA NA NA NA NA NA NA
## air_time 53.01493 NA NA 764.4249 NA NA 167.926251
## distance 26.64179 NA NA -1321.9122 NA NA 14.122915
## hour 18.40299 NA NA -519.7085 NA NA -28.388938
## minute -90.35821 NA NA 4181.3073 NA NA 8.400351
## distance hour minute
## year 0.000000 0.00000 0.000000
## month 0.000000 0.00000 0.000000
## day -6.545215 14.95347 -15.124671
## dep_time -240.421422 2151.23968 -1023.952590
## dep_delay 20.059701 52.74627 -83.373134
## arr_time -296.710272 1800.02458 -1559.090430
## arr_delay 26.641791 18.40299 -90.358209
## carrier NA NA NA
## tailnum NA NA NA
## flight -1321.912204 -519.70852 4181.307287
## origin NA NA NA
## dest NA NA NA
## air_time 14.122915 -28.38894 8.400351
## distance 46.461809 -2.52590 12.168569
## hour -2.525900 21.64267 -13.027217
## minute 12.168569 -13.02722 278.769096
using histogram
using boxplot
using point
summary of over one hour delays
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -66.00 -21.25 -11.00 -4.50 2.00 196.00
The answer: almost half of February arrived more than 11 minutes early, this vary between different carriers. American Airlines has the most delays and Virgin American has the best arrival time.
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:
sfo_feb_flights %>%
group_by(origin) %>%
summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 2 x 4
## origin median_dd iqr_dd n_flights
## <chr> <dbl> <dbl> <int>
## 1 EWR 0.5 5.75 8
## 2 JFK -2.5 15.2 60
Here, we first grouped the data by origin and then calculated the summary statistics.
nycflights %>%
group_by(carrier) %>%
summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 16 x 4
## carrier median_dd iqr_dd n_flights
## <chr> <dbl> <dbl> <int>
## 1 9E -1 23.2 1696
## 2 AA -2 9 3188
## 3 AS -4.5 8.75 66
## 4 B6 -1 18 5376
## 5 DL -2 9 4751
## 6 EV -1 31 5142
## 7 F9 1 18 69
## 8 FL 1 22 307
## 9 HA -3.5 6.75 34
## 10 MQ -3 16 2507
## 11 OO -6 49 3
## 12 UA 0 14 5770
## 13 US -4 7 2015
## 14 VX -1 12 497
## 15 WN 1 19 1261
## 16 YV -4 26 53
arr_delays of flights in in the sfo_feb_flights data frame, grouped by carrier. Which carrier has the most variable arrival delays?The Answer: UA united airlines and DL delta both have teh highest interquartile range of the delays of flights in this new data frame.
sfo_feb_flights1 <- group_by(sfo_feb_flights, carrier)
summarise(sfo_feb_flights1, Median = median(arr_delay), IQR = IQR(arr_delay))## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 5 x 3
## carrier Median IQR
## <chr> <dbl> <dbl>
## 1 AA 5 17.5
## 2 B6 -10.5 12.2
## 3 DL -15 22
## 4 UA -10 22
## 5 VX -22.5 21.2
Which month would you expect to have the highest average delay departing from an NYC airport?
Let’s think about how you could answer this question:
group_by months, thensummarise mean departure delays.arrange these average delays in descending order## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 12 x 2
## month mean_dd
## <int> <dbl>
## 1 7 20.8
## 2 6 20.4
## 3 12 17.4
## 4 4 14.6
## 5 3 13.5
## 6 5 13.3
## 7 8 12.6
## 8 2 10.7
## 9 1 10.2
## 10 9 6.87
## 11 11 6.10
## 12 10 5.88
The Answer: the mean has a tendency to be pulled toward the extreme values, while the median is more robust measure than the arithmetic mean, but the outliers here could cause an issue and It could happen that one experiences one of these extra delayed flights, I think in this example its better to use the mean.
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 12 x 2
## month mean_dd
## <int> <dbl>
## 1 7 20.8
## 2 6 20.4
## 3 12 17.4
## 4 4 14.6
## 5 3 13.5
## 6 5 13.3
## 7 8 12.6
## 8 2 10.7
## 9 1 10.2
## 10 9 6.87
## 11 11 6.10
## 12 10 5.88
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. Also supposed 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, you can
Let’s start with classifying each flight as “on time” or “delayed” by creating a new variable with the mutate function.
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 of the remaining steps in one code chunk:
nycflights %>%
group_by(origin) %>%
summarise(ot_dep_rate = sum(dep_type == "on time") / n()) %>%
arrange(desc(ot_dep_rate))## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
## origin ot_dep_rate
## <chr> <dbl>
## 1 LGA 0.728
## 2 JFK 0.694
## 3 EWR 0.637
The Answer: I would choose LGA LaGuardia since it has the least delays.
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.avg_speed vs. distance. Describe the relationship between average speed and distance. Hint: Use geom_point().The answer: There is a relation between the speed range and the distance, the speed is more when the distance is longer, like the flights from NYC to Honolulu HNL.
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.The asnswer: the cutoff point of when you can still expect to get to your distincation on time is around 20 minutes.
ggplot(dl_aa_ua, aes(x = dep_delay, y = arr_delay, color = carrier)) +
xlim(-25, 150) +
geom_point()## Warning: Removed 219 rows containing missing values (geom_point).
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.