-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightDelaysAnalysis_Team3.Rmd
More file actions
614 lines (470 loc) · 25.9 KB
/
FlightDelaysAnalysis_Team3.Rmd
File metadata and controls
614 lines (470 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
---
title: "FlightDelays"
author: "[redacted]"
date: "[redacted]"
output: html_document
---
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
**This exercise is under construction. Please report any errors at https://forms.gle/2W4tffs4YJA1jeBv9**
Goal: Experience the Fraud Analytics Process Model
Background:
The Fraud Analytics Process Model:
1. ID Problem
2. ID Data Sources
3. Select Data
4. Clean Data
5. Transform Data
6. Analyze Data
7. Interpret Model
8. Refine Model
**The first ten questions are for the individual assignment and the remaining questions are for the team assignment. Specifically:**
**For the individual assignment submissions: Delete everything below Q10. Then, knit and submit your html and Rmd files.**
**For the team assignment submissions: Pick any individual team member's answers for the first ten questions. Then complete the remaining questions to submit for the team assignment.**
1. Delete everything below Q10 and submit for individual assignment.
2. Pick any individual team member's answers for the first ten questions and then knit the remaining questions to submit for the team assignment.
Individual assignment: 25 total points
Team assignment: 135 total points
## [1 point] Q1.
*Start by entering your name and today's date in Lines 3 and 4, respectively.*
*Then, run the chunk of code below by clicking on the green arrow (that points to the right) on the top right of the chunk.*
*Tip: This line of code (knitr...) is generated by RStudio. It allows you to format (knit) your output so you can share in a readable format. We will use an html format for output as specified in Line 5. You will be instructed to "knit" after completing the assignment without any errors.*
*Tip: I numbered code chunks corresponding to their numbers. Chunk 1 specified the knitting parameters.*
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# 1. ID Problem:
*This assignment is inspired by https://www.nbcnews.com/business/business-news/united-airlines-fined-1-9-million-long-tarmac-delays-largest-n1280051 and similar events.*
*Flight cancellations and delays due to weather are not the airlines' responsibility. Our goal is to detect and report any delays that are not due to weather for further investigation.*
# 2. ID Data Sources
*We will analyze data in nycflights13 because it contains information about all flights originating from NYC airports in 2013. Please take a moment to skim https://cran.r-project.org/web/packages/nycflights13/nycflights13.pdf with a focus on finding information about cancellations/delays as well as weather.*
## [2 points] Q2.
*Install and load packages tidyverse and nycflights13.*
*Tip: tidyverse package gives you a tidy way of manipulating data.*
*Tip: install.packages("packageName") command installs package packageName. library(packageName) will load package packageName.*
*Tip: You only need to install packages once and you are free to load them afterwards. In fact, you will need to remove the install command to knit and submit your solutions.*
```{r}
#install.packages("tidyverse")
library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)
```
## [1 point] Q3.
*Confirm that the package you loaded in the previous chunk by using the search() function. There is nothing enclosed in parentheses because search doesn't need any information (parameters).*
```{r}
# Confirmed what packages have been loaded
search()
```
## [1 point] Q4.
*Now, take a peek inside nycflights13 by using the command ls("package:nycflights13")*
*Tip: ls("package:packageName") gives the names of the objects in packageName.*
```{r}
ls("package:nycflights13")
```
## [1 point] Q5.
*Now, see what is in your environment by using the command ls() without anything inside the parentheses.*
*Tip: ls() provides information about data sets and functions defined by the user. This will give character(0) if there is nothing in the environment. It is a good idea to start every R script with a clean environment so previous data doesn't corrupt your script.*
*Tip: You may clean your environment at any time by using the broom in the environment tab (likely near top right of RStudio) or using the command rm(list = ls()).*
```{r}
#rm(list = ls())
ls()
```
## [2 points] Q6.
*Find the tibbles that contain information about flight delays and weather using the information from nycflights13 documentation, and assign them to variables names tFlights and tWeather, respectively.*
*Tip: Tibbles are a more robust and refined version of data frames. You may Google tibble to learn more about tibbles (but it is not necessary at this time).*
*Tip: You may start variable names with 1+ letters abbreviating the data type, followed by a descriptive word (or words) for the variable. For example, "tFlights" can be used for the tibble containing flights data. By just reading the variable name, you can deduce that it is a tibble because it starts with "t", and that it contains flights data because of the word "flights". You need not worry about long variable names because you can copy-paste or use autocomplete. You may learn more about naming conventions at https://en.wikipedia.org/wiki/Naming_convention_(programming).*
```{r}
tFlights = flights
view(tFlights)
tWeather = weather
view(tWeather)
```
## [1 point] Q7.
*Now, use ls() to see how your environment have changed.*
*Tip: Also, checkout the new variables in the environment tab.*
```{r}
ls()
```
## [2 points] Q8.
*Now, use head function to print and check out the first 10 lines of the tibbles containing the flights and weather data.*
*Tip: Use https://cran.r-project.org/web/packages/nycflights13/nycflights13.pdf to understand the details of each data set.*
*Tip: Always check what you read into your program.*
*Tip: Get accustomed vertical and horizontal scrolling.*
*T1ip: Get accustomed to clicking on different tabs when they are printed by one code chunk.*
```{r}
head(tFlights, 10)
head(tWeather, 10)
```
## [4 points] Q9.
*Now, print the structure and summary statistics of the tibbles containing the flights and weather data.*
*Tip: Get accustomed to working with datasets that can't fit on a spreadsheet (and your head).*
```{r}
str(tFlights)
summary(tFlights)
str(tWeather)
summary(tWeather)
```
# 3. Select Data
## [10 points] Q10.
*Using the information above, identify columns of each tibble that you will need for your analysis.*
*Tip: Copy-paste from https://cran.r-project.org/web/packages/nycflights13/nycflights13.pdf and then delete what you don't need after completing your analysis. This will serve as your data dictionary.*
*Tip: You may update this list as you progress through the rest of the assignment. Don't worry about getting it perfect right now.*
```{r}
### This section doesn't require code. Just list all the columns of each dataset you'll need for your analysis.###
#year, month, day, hour Time of recording.
#origin Weather station. Named origin to facilitate merging with flights data.
# flight dataset
# year, month, day flight date information.
# dep_delay flight delay time in minutes, positive number means there is a delay.
# carrier airline abbreviation
# flight flight number
# origin flight departure airport
# dest flight arrival airport
# hour flight departure hour
# minute flighty departure minute
# weather dataset
# origin weather station in the flight departure airport
# year, month, day, hour time when the weather information is recorded
# temp, dewp, humid temperature information
# wind_dir, wind_speed,
# wind_gust wind information
# time_hour time when weather was recorded
# precip precipitation in inches
# pressure sea level pressure in millibars
# visib weather visibility
```
**For the individual assignment submissions: Delete the everything below this line. Then, knit and submit your html and Rmd files.**
**For the team assignment submissions: Pick any individual team member's answers for the first ten questions. Then complete the remaining questions to submit for the team assignment.**
# 4. Clean Data
# 5. Transform Data
*Cleaning and transforming usually goes hand in hand. (This usually includes merging and sometimes includes sampling and other techniques.)*
## [6 points] Q11.
*Merge your the flights and weather tibbles to form one dataset called tFlightWeather with all the information you need from both tibbles in one dataframe.*
*Then, print the structure of the merged dataset.*
*Tip: The function merge(x1, x2, by = c("col1", "col2")) will merge x1 and x2 wherever columns col1 and col2 are identical. Google "merge function in R" for more details.*
*Tip: To identify the column(s) for merging the weather and flight data, consider what you need to connect the flight data to the weather data. Hint: there are 2 columns containing information about the originating airport and time.*
*Tip: Read the nycflights.pdf documentation at the link shared earlier.*
*Rubric: Merging = 5; structure = 1*
```{r}
tFlightWeather= merge(tFlights, tWeather, by=c("origin","time_hour"))
str(tFlightWeather)
```
## [3 points] Q12.
*Does the merged dataset have fewer rows than the flights dataset? If so, which rows are omitted, and why? Does any data type change?*
*Tip: Merge is an inner join. Look for rows with missing information on join.*
```{r}
### This section requires code and descriptive answers
# Yes, the merged data set have fewer rows than the flights data set.The flights data set have 336776 rows while the merged data set have 335220 rows.
# There are many hours in the weather data set that don't have any flights.
# There is no weather information from 19:00PM on November 2 to 1AM on November 3
print("The number of row in flights dataset is:")
nrow(tFlights)
print("The number of row in flights and weather merged dataset is:")
nrow(tFlightWeather)
# Check how many rows are omitted
anti_join(tFlights, tWeather,
by = c("time_hour", "origin")) %>%
nrow()
```
## [4 points] Q13.
*Answer the following questions based on the merged dataset:*
*1. What are the columns with .x and .y appended to their names?*
# year, month, day and hour columns have .x and .y appended to their column names
*2. Are they redundant?*
# Yes
*3. Why isn't there a .x and .y appended to origin and time_hour.*
# .x and .y did not append to origin and time_hour because they are shared columns being used to join both data sets.
*Rubric: 1 points for each part; 1 point for overall logic.*
```{r}
### This section doesn't require code. Just list all the columns of each dataset you'll need for your analysis.###
```
## [7 points] Q14.
*Now, remove any duplicate columns from merged data and store the result in dfMyFlightsWeather.*
*Then, rename the columns so that there is no ".x" and ".y" in the column names.*
*Finally, print the structure of dfMyFlightsWeather to validate your code.*
*Tip: x = y %>% select(!c("col1", "col2", ...)) will remove columns named col1, col2, ...*
*Tip: x = rename(x, c("oldcol1" = "newcol1", "oldcol2" = "newcol2", ...)) will rename oldcol1, oldcol2, ... *
*Tip: Googling for help is a learning objective for this chunk.*
*Rubric: Remove columns = 3; rename = 3, structure = 1*
```{r}
dfMyFlightsWeather = tFlightWeather %>% select(!c("year.y", "month.y", "day.y", "hour.y"))
#install.packages("plyr")
library("plyr")
dfMyFlightsWeather = rename(dfMyFlightsWeather, c("year.x" = "year", "month.x"="month", "day.x"="day", "hour.x"="hour"))
str(dfMyFlightsWeather)
```
# 6. Analyze Data
# 7. Interpret Model
# 8. Refine Model
## [5 points] Q15.
*Now, plot a simple histogram of all values in the dep_delay column. Just use the default values for any optional parameters. Assign your the output from ggplot to a descriptive variable name so you can reuse it later.*
*Then, print pMyFlightsWeather.*
*Tip: I used pMyFlightsWeather = ggplot(dfMyFlightsWeather, aes(x = dep_delay)) + geom_histogram().*
*Tip: Always remember to visualize the data before analyzing it.*
*Tip: ggplot is your friend.*
*Rubric: Plotting = 4, naming plot = 1*
```{r}
# You may copy, paste, and modify this chunk to answer later questions.
pMyFlightsWeather = ggplot(dfMyFlightsWeather, aes(x = dep_delay)) + geom_histogram()
pMyFlightsWeather
```
## [2 points] Q16.
*Now, print the summary statistics of dep_delay column in the merged dataframe.*
```{r}
summary(dfMyFlightsWeather$dep_delay)
```
## [3 points] Q17.
*Describe what you have learned about the distribution of dep_delay?*
*Rubric: Minimum of 3 items for 1 point each (max of 3 points).*
```{r}
### This section doesn't require code. Just answer the question.###
# Majority flights are on time
# 8227 flights have NA information, probably they were cancelled flights
```
## [6 points] Q18.
*The previous histogram plot included canceled flights. Now, create a new dataframe called dfCanceled that contains just the flights that were canceled (dep_delay is NA).*
*Then, plot a simple histogram of all values in dfCanceled's dep_delay column.*
*Tip: You may copy, paste, and modify a previous chunk to answer part(s) of this question.*
*Tip: filter(!is.na(dep_delay)) will filter out all rows with "na" for dep_delay.*
*Tip: Don't forget to assign the output from ggplot to a descriptive variable name so you can reuse it later.*
*Rubric: Filtering = 3; plotting = 2, naming plot = 1*
```{r}
dfCanceled = dfMyFlightsWeather %>% filter(is.na(dep_delay))
cancelledFlight = ggplot(dfCanceled, aes(x = dep_delay)) + geom_histogram()
cancelledFlight
#str(cancelledFlight)
```
## [3 points] Q19.
*Review and explain the previous histogram. Is that what you expected?*
```{r}
### This section doesn't require code. Just answer the question.###
# The plot histogram is empty because it is for all flights which were cancelled, not delayed. Thus nothing is displayed.
```
## [2 points] Q20.
*Now, set a variable called delayCutoff in the chunk below with a cutoff of the maximum delay that you do not want to investigate?*
*Tip: We declare it as a variable because we are likely to change it if it is too low or too high.*
*Tip: There's no right answer for this question. 0 is obviously not a good choice because it will distract us with flights that are just a few minutes late. 24 hours (1440 minutes) is not a good choice because they are going to ignore too many flights that are several hours late. Feel free to return to this line, refine your choice, and restart from here.*
```{r}
# Find out flights that are delayed for 2 hours
delayCutoff = 120
```
## [6 points] Q21.
*Now, create a new dataframe dfDelayedCutoff that contains all flights that are defined as delayed based on your delayCutoff.*
*Then, plot a simple histogram of all values in dfDelayedCutoff's dep_delay column.*
*Tip: This dataframe should not include canceled flights.*
*EXA Tip: filter(delayCutoff < dep_delay) will filter out all rows with "na" dep_delay.*
*Rubric: Filtering = 3; plotting = 2, naming plot = 1*
```{r}
# Find out all those flights that are delayed for at least two hours
dfDelayedCutoff = dfMyFlightsWeather %>% filter(delayCutoff < dep_delay)
delayedCutoffFlights = ggplot(dfDelayedCutoff, aes(x = dep_delay)) + geom_histogram()
delayedCutoffFlights
```
## [2 points] Q22.
*Now, print the summary statistics of dfDelayedCutoff's dep_delay column.*
```{r}
print(summary(dfDelayedCutoff$dep_delay))
```
## [6 points] Q23.
*Let's focus on dfDelayedCutoff now and visualize the dependency on weather components, starting with temp, by plotting it.*
*Also, also a trendline to visualize any patterns.*
*Tip: Use ggplot(DATA, aes(x = VAR_X, y = VAR_Y, color = COLOR_CODING)) + geom_point() + geom_smooth(method = "lm") + labs(x = "VAR_X's name (units)", y = "VAR_Y's name(units)", title = "VAR_Y's name vs. VAR_X's name")*
*Tip: color doesn't need to be set for this chunk.*
*Rubric: ggplot call = 5, storing the result = 1*
```{r}
# You can copy, paste, and modify this chunk to answer later questions.
pTemperature = ggplot(dfDelayedCutoff, aes(x = temp, y = dep_delay, color = "#FFCC00")) +
geom_point() + geom_smooth(method = "lm") +
labs(x = "Temperature (F)", y = "Departure Delay (minutes)", title = "Departure Delay vs. Temperature")
pTemperature
```
## [2 points] Q24.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
# We don't see any trend, it seems there were a tiny bit more departure delays when the temperature was more than 75°F.
```
## [4 points] Q25.
*Repeat the directions of the previous plot with color coding based on the origin.*
*Rubric: ggplot call = 2, color coding = 1, storing the result = 1*
```{r}
# You can copy, paste, and modify this chunk to answer later questions.
pTemperatureColor = ggplot(dfDelayedCutoff, aes(x = temp, y = dep_delay, color = origin)) +
geom_point() + geom_smooth(method = "lm") +
labs(x = "Temperature (F)", y = "Departure Delay (minutes)", title = "Departure Delay vs. Temperature")
pTemperatureColor
```
## [2 points] Q26.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
# There was no correlation between departure delay and temperature
# Most flights delayed less than 500 minutes
# Flights from airport LGA delayed less minutes
# All three airports (LGA, JFK and EWR) had similar delay pattern. Most departure flights delayed for less than 500 minutes, quite a few departure flights delayed between 500 to 1000 minutes, very few departure flights delayed for more than 1000 minutes.
```
## [3 points] Q27.
*Repeat the previous plot with dew point as the independent variable (x-axis).*
*Tip: Don't forget to check the units.*
*Rubric: ggplot call = 2, storing the result = 1*
```{r}
pDewpoint = ggplot(dfDelayedCutoff, aes(x = dewp, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Dewpoint (F)", y="Departure Delay (minutes)", title = "Departure Delay vs. Dewpoint")
pDewpoint
```
## [1 point] Q28.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was no obviously correlation between dewpoint and departure delay, but there were a little more departure flight delayed when the dewpoint was around 70°F.
#
```
## [3 points] Q29.
*Repeat the previous plot with relative humidity as the independent variable (x-axis).*
*Tip: Don't forget to check the units.*
```{r}
pHumidity = ggplot(dfDelayedCutoff, aes(x = humid, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Humidity (relative humidity based on the scale of 100)", y="Departure Delay (minutes)", title = "Departure Delay vs. Humidity")
pHumidity
```
## [1 point] Q30.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was no clear correlation between humidity and departure delay.
#
```
## [3 points] Q31.
*Repeat the previous plot with wind direction as the independent variable (x-axis).*
```{r}
pWindDirection = ggplot(dfDelayedCutoff, aes(x = wind_dir, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Wind Direction (in degrees)", y="Departure Delay (minutes)", title = "Departure Delay vs Wind Direction")
pWindDirection
```
## [1 point] Q32.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was no correlation between wind direction and departure delay.
```
## [3 points] Q33.
*Repeat the previous plot with wind speed as the independent variable (x-axis).*
```{r}
pWindSpeed = ggplot(dfDelayedCutoff, aes(x = wind_speed, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Wind Speed (in degrees)", y="Departure Delay (minutes)", title = "Departure Delay vs Wind Direction")
pWindSpeed
```
## [1 point] Q34.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was no clear correlation between wind speed and departure delay.
#
```
## [3 points] Q35.
*Repeat the previous plot with wind gust as the independent variable (x-axis).*
```{r}
pWindGust = ggplot(dfDelayedCutoff, aes(x = wind_gust, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Wind Gust (in degrees)", y="Departure Delay (minutes)", title = "Departure Delay vs Wind Gust")
pWindGust
```
## [1 point] Q36.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was some correlation between wind gust and departure delay. It seems there were more departure delays when then Wind Gust was around 20 degrees.
#
```
## [3 points] Q37.
*Repeat the previous plot with precipitation as the independent variable (x-axis).*
```{r}
pPrecip = ggplot(dfDelayedCutoff, aes(x = precip, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Precipitation (in inches)", y="Departure Delay (minutes)", title = "Departure Delay vs Preciptation")
pPrecip
```
## [1 point] Q38.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was some correlation between Precipitation and departure delay. There were more departure delays when the precipitation was less than 0.25 inches. Maybe when the weather was bad travelers would be late for checking in and that could cause the issue.
#
# Overall, airport LGA had more departure delays, followed by JFK, and then followed by EWR.
#
```
## [3 points] Q39.
*Repeat the previous plot with sea level pressure as the independent variable (x-axis).*
```{r}
pSeaLevel = ggplot(dfDelayedCutoff, aes(x = pressure, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Pressure (in millibars)", y="Departure Delay (minutes)", title = "Departure Delay vs Pressure")
pSeaLevel
```
## [1 point] Q40.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# It didn't seem there was a clear correlation between sea level pressure and departure delay.
#
```
## [3 points] Q41.
*Repeat the previous plot with visibility as the independent variable (x-axis).*
```{r}
pVisibility = ggplot(dfDelayedCutoff, aes(x = visib, y = dep_delay, color = origin)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Visibility (in miles)", y="Departure Delay (minutes)", title = "Departure Delay vs Visibility")
pVisibility
```
## [1 point] Q42.
*What trends do you see, if any?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# There was a correlation between visibility and departure delay. When the visibility number was higher, there were more flights departure delay.
#
```
## [19 points] Q43.
*Now plot the histograms of all weather related aspects for the flights that were canceled.*
*Tip: This chunk plots 9 histograms.*
*Tip: You may google how to display multiple plots in a grid. I used cowplot library is one of those items. After installing cowplot, plot_grid(X1, x2, ...) can display x1, x2, ... in one grid.*
*Rubric: 2 points for each histogram. 10 points for displaying them in one grid.*
```{r}
# Do not delete these line of code. This will help you get started with temp.
# You may copy, paste, and modify this chunk to complete this question.
#
# Note:
# Check the PDF file from URL https://cran.r-project.org/web/packages/nycflights13/nycflights13.pdf to get the column format from the weather data frame
pCTemp = ggplot(dfCanceled, aes(x = temp)) + geom_histogram() + labs(x = "Temperature (F)")
pCTemp
pCDewp = ggplot(dfCanceled, aes(x = dewp)) + geom_histogram() + labs(x = "Dewpoint (F)")
pCDewp
pCHumidity = ggplot(dfCanceled, aes(x = humid)) + geom_histogram() + labs(x = "Humidity (relative humidity based on the scale of 100)")
pCHumidity
pCWindDirection = ggplot(dfCanceled, aes(x = wind_dir)) + geom_histogram() + labs(x = "Wind Direction (in degrees)")
pCWindDirection
pCWindSpeed = ggplot(dfCanceled, aes(x = wind_speed)) + geom_histogram() + labs(x = "Wind Speed (in degrees)")
pCWindSpeed
pCWindGust = ggplot(dfCanceled, aes(x = wind_gust)) + geom_histogram() + labs(x = "Wind Gust (in degrees)")
pCWindGust
pCPrecipitation = ggplot(dfCanceled, aes(x = precip)) + geom_histogram() + labs(x = "Precipitation (in inches.)")
pCPrecipitation
pCPressure = ggplot(dfCanceled, aes(x = pressure)) + geom_histogram() + labs(x = "Pressure (in millibars)")
pCPressure
pCVisib = ggplot(dfCanceled, aes(x = visib)) + geom_histogram() + labs(x = "Visibility (in miles)")
pCVisib
# Install the cowplot library and plot all 9 histograms
#install.packages("cowplot")
library("cowplot")
plot_grid(pCTemp, pCDewp, pCHumidity, pCWindDirection, pCWindSpeed, pCWindGust, pCPrecipitation, pCPressure, pCVisib)
```
## [20 points] Q44.
*Reflect on your analysis and summarize what you have learned about the dependence of delayed and canceled flights on the weather. What does this tell you about the likely causes of delayed and canceled flights?*
```{r}
### This section doesn't require code. Just answer the question.###
#
# In general, there was not so much correlation between departure delay and weather.
# Maybe there was a correlation between cancelled flights and weather. There were more cancelled flights when the weather conditions got worst. And when those flights were cancelled due to weather, they were not counted as departure delay. Thus we didn't see much correlation between departure delay and weather.
```
## [5 points] Q45.
*Knit to html after eliminating all the errors. Submit both the Rmd and html files.*
*Tip: Do not worry about minor formatting issues.*
*Tip: This will take some time as you are processing medium size data sets.*
```{r}
### This section doesn't require code. Just knit and submit the Rmd and html files.###
```