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. You can find the instructions for this lab here.

# 1. load the library "tidyverse"
library(tidyverse)


# 2. use the read_csv file to read the dataset
nycflights <- read_csv("data/nycflights.csv")

Exercise 1

Question: Experiment with different binwidths to adjust your histogram in a way that will show its important features. You may also want to try using the + scale_x_log10() . What features are revealed now that were obscured in the original histogram? Note: When using the log scale, you may need to experiment with bin widths that are smaller than 1, such as binwidth=0.1 or even less!

# Write your code to create a histogram of delays

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 15, fill = "#dc143c", color = "black") +
  labs(x = "Departure Delay (min)", y = "Amount of Flights")

ggplot(data = nycflights, aes(x = dep_delay)) + 
  geom_histogram(binwidth = 150,fill="#dc143c", color='black') + 
  labs(x='Departure Delay(min)', y='Amount of flights')

#Original Histogram with binwidth=150
original_hist<- ggplot(data = nycflights, aes(x = dep_delay)) + 
  geom_histogram(binwidth = 150,fill="#dc143c", color='black') + 
  labs(title= 'Original histogram binwidth=150', x='Departure Delay(min)', y='Amount of flights')

#Histogram with a smaller binwidth and log scale:
new_histogram<-ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 0.1,fill="#dc143c", color='black') +
  scale_x_log10() + 
  labs(title='New histogram log scale and binwidth=0.1',x='Logarythm of Departure Delay (log(min))', y='Amount of flights')

library(gridExtra)
grid.arrange(original_hist, new_histogram, ncol=2)

Answer: Right skewed distribution. Binwidth= 15. The resulting histogram will have more bins, each representing a narrower range of delay values. This can provide a more detailed view of the distribution, especially if there are variations in the data within smaller ranges. on the other hand with a binwidth of 150, the resulting histogram has fewer bins, each representing a broader range of delay values.

Effect of log-scale: compression of values emphasizing differences in the lower part of the scale. The distributiin is right skewed and therefore the logarithmic scale helps visualize the distribution more effectively. The majority of data is clustered in a small range, but the logarithmic scale, allows identification of patterns in small values. There is however loss of precision since interpretation of values on the logarithmic scale is not as straightforward as on a linear scale.

Exercise 2

Question: 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? Try using the function nrow() inside of inline code in your answer, and knit your file to see that your text shows the answer correctly.

# Insert code for Exercise 2 here
sfo_feb_flights <- nycflights %>%
  filter(dest == "SFO", month == 2)

nrow(sfo_feb_flights)
## [1] 68

Answer: There were 68 flights from New York to San-Fransisco in February.

Exercise 3

Question: Describe the distribution of the arrival delays of flights headed to SFO in February, using an appropriate histogram and summary statistics. Hint: The summary statistics you use should depend on the shape of the distribution.

# Insert code for Exercise 3 here
ggplot(data = sfo_feb_flights, aes(x = arr_delay)) +
  geom_histogram(binwidth = 3, fill = "#dc143c", color = "black") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  labs( x = "Arrival Delay (min)", y = "Amount of Flights") +
  ggtitle("SSFO Arrival Delays in February")

sfo_feb_flights %>%
  filter(arr_delay > 0) %>%
  ggplot(aes(x = arr_delay)) +
  geom_histogram(binwidth = 2, fill = "#dc143c", color = "black") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  labs(x = "Arrival Delay (min)", y = "Amount of Flights") +
  ggtitle("SFO Arrival Delays in February")

sfo_feb_flights %>%
  group_by(origin) %>%
  summarise(Median = median(arr_delay), IQR = IQR(arr_delay), n_flights = n())

Answer: The histogram is right skewed in both histograms, which means that most flights arrive on time or even early. From the summary we can see that flights departing from EWR were more likely to arrive before time than flights from JFK.

Exercise 4

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

# Insert code for the Exercise here

sfo_feb_flights %>%
  group_by(carrier) %>%
  summarise(median_dd = median(arr_delay), iqr_dd = IQR(arr_delay), n_flights = n())

Answer: DL and UA have the highest IQR, indicating higher variability in arrival delays. Carriers with negative median values (like B6, DL, UA, and VX) indicate that, on average, flights tend to arrive early. AA has a median of 5 minutes late, indicating that on average AA flights tend to arrive late.

Exercise 5

Question: Create a list of origin airports and their rate of on-time-departure. Then visualize the distribution of on-time-departure rate across the three airports using a segmented bar plot (see below). If you could select an airport based on on time departure percentage, which NYC airport would you choose to fly out of? Hint: For the segmented bar plot, will need to map the aesthetic arguments as follows: x = origin, fill = dep_type and a geom_bar() layer. Create three plots, one with geom_bar() layer, one with geom_bar(position = "fill") and the third with geom_bar(position = "dodge"). Explain the difference between the three results.

# Insert code for the Exercise here
nycflights <- nycflights %>%
  mutate(dep_type = ifelse(dep_delay < 5, "on time", "delayed"))

nycflights %>%
  group_by(origin) %>%
  summarise(on_time_dep_rate = sum(dep_type == "on time") / n()) %>%
  arrange(desc(on_time_dep_rate))
ggplot( data = nycflights, aes(x = origin, fill = dep_type)) +
  geom_bar(position = "dodge") +
  labs( x = "Place of Origin", y = "Amount of Flights", fill = "Type of Departure")

ggplot( data = nycflights, aes(x = origin, fill = dep_type)) +
geom_bar(position = "fill") +
labs( x = "Place of Origin", y = "Amount of Flights", fill = "Type of Departure")

Answer: Based on the ‘filled’ bar plot, we can see that among the three airports, LGA has the lowest proportion of delayed flights.

Exercise 6

Question: 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, or if you are brave, in km/h). Now make a scatterplot of avg_speed vs. distance. Describe the relationship between average speed and distance. Hint: Average speed can be calculated as distance divided by number of hours of travel, and note that air_time is given in minutes. You will need to use geom_point(). Think carefully which of the two variables is the predictor (on the x-axis) and which is the outcome variable (on the y-axis) and explain why you made this choice. Describe the relationship between average speed and distance.

# Insert code for the Exercise here
nycflights <- nycflights %>%
  mutate(
    distance_km = distance * 1.6,
    air_time_h = air_time / 60, 
    avg_speed = distance_km / air_time_h)

nycflights %>%
  ggplot(aes(x=distance_km, y = avg_speed))+
  geom_point(color = "#dc143c") +
  geom_smooth(color = "black") +
  labs(x = "Distance (km)", y = "Average Speed (km/h)")

nycflights %>%
  ggplot(aes(x=distance_km, y = avg_speed))+
  geom_point(color = "#dc143c") +
  geom_smooth(color = "black") +
  scale_x_log10() +
  scale_y_log10() +
  labs(x = "Logarithm of Distance (log(km))", y = "Logarithm of Average Speed log((km/h))")

Answer: The scatterplot shows a general positive relationship between distance and average speed, which is expected. As the distance increases, the average speed tends to increase. the logarithmic transformation shows a clearer linear trend in the data.Both plots indicate a positive relationship between distance and average speed.

Long-haul flights seem to travel at higher speeds relative to shorter flights. We believe this has many reasons: the planes tend to be larger and have more powerful engines, the routes can be slightly more flexible to allow for better wind, and the flights typically travel at higher altitudes to encounter less wind resistance.

We generated two plots in order to ensure a thorough exploration of potential patterns that may emerge following the log transformation of the data.

Exercise 7

Question: Replicate the following plot and determine what is the cutoff point for the latest departure delay where you can still have a chance to arrive at your destination on time. Hint: The data frame plotted only contains flights from American Airlines, Delta Airlines, and United Airlines, and the points are colored by carrier. To determine the cut off point, try scaling the x-axis and the y-axis on the logarithmic scale. You can also filter the data, so that you plot only data where arr_delay <= 0.

# Insert code for the Exercise here
nycflights %>%
  dplyr::filter(carrier %in% c("AA", "DL", "UA")) %>%
  ggplot(aes(x = dep_delay, y = arr_delay, color = carrier))+
  geom_point() +
  labs( title = "Plot One", x = "Departure Delay (min)", y = "Arrival Delay (min)", color = "Airline")

nycflights %>%
  dplyr::filter(carrier %in% c("AA", "DL", "UA")) %>%
  dplyr::filter(arr_delay < 0) %>%
  ggplot(aes(x = dep_delay, y = arr_delay, color = carrier))+
  geom_point() +
  geom_smooth(method = "lm") +
  labs( title = "Plot Two", x = "Departure Delay (min)", y = "Arrival Delay (min)", color = "Airline")

Answer: In Plot 2, it’s evident that there is a noteworthy instance where a flight experienced a delay of 62 minutes and yet managed to arrive precisely on time. Upon conducting a linear regression analysis for each airline, we identified distinct cutoff points, emphasizing the variability in the impact of departure delay on arrival time across different carriers.