setwd("C:/Users/juanc/OneDrive/Desktop/School/INFOH_510/Data_Dives")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
adc <- read_csv("adc_week_3.csv") #loading data from Week 3 Data Dive
## Rows: 20345 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): carrier, airport, weath_ratio_bin
## dbl (19): month, arrived_flights, total_delayed, air_carrier_delay, weather_...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
adc_names <- read_csv("adc_names.csv") #Names for Airlines and Airports
## Rows: 17 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): carrier, carrier_name
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
In this weeks data dive we will focus on random sampling of our data. Data frames can be created from the 5x random samples (with replacement). Each sample will continue 50% of the original data and contain both categorical and continuous data.
We will then proceed to scrutinize the sample data frames, figuring out the similarities and differences of the samples, the anomalies and what classifies as an outlier. In the next phase we will incorporate the Monte Carlo simulation with the random samples.
Finally we will draw conclusions from these random samples. How does the random samples and their analysis mold our future questions.
set.seed(25)
rows_sampled <- round(nrow(adc) * 0.5)
rand_samp1 <- adc |>
sample_n(size = rows_sampled, reeplace =TRUE)
rand_samp2 <- adc |>
sample_n(size = rows_sampled, reeplace =TRUE)
rand_samp3 <- adc |>
sample_n(size = rows_sampled, reeplace =TRUE)
rand_samp4 <- adc |>
sample_n(size = rows_sampled, reeplace =TRUE)
rand_samp5 <- adc |>
sample_n(size = rows_sampled, reeplace =TRUE)
We can now create different functions to apply to the samples and view the data.
# Creating a function to apply to Random Samples
summarize_data1 <- function(data) {
data |>
group_by(month) |>
summarize(
total_flights = sum(arrived_flights, na.rm = TRUE),
total_delayed_flights = sum(total_delayed, na.rm = TRUE),
average_delay_minutes = mean(total_delayed_minutes, na.rm = TRUE),
) |>
select(month, total_flights, total_delayed_flights, average_delay_minutes) |>
ggplot(aes(x = month, y = total_delayed_flights)) +
geom_line() +
geom_point() +
labs(
title = "Total Delayed Flights Over Months",
x = "Month",
y = "Total Delayed Flights"
) +
scale_x_continuous(breaks = 1:12, labels = month.abb)
}
rand_samps <- list(rand_samp1, rand_samp2, rand_samp3, rand_samp4, rand_samp5)
# Create and print plots for each data frame
for (i in seq_along(rand_samps)) {
print(summarize_data1(rand_samps[[i]])) }
In almost all random sample groups, the month of December typically has the highest number of overall delays.
The same function can be applied with total minutes delayed.
summarize_data2 <- function(data) {
data |>
group_by(month) |>
summarize(
total_flights = sum(arrived_flights, na.rm = TRUE),
total_delayed_flights = sum(total_delayed, na.rm = TRUE),
sum_delay_minutes = sum(total_delayed_minutes, na.rm = TRUE),
average_delay_minutes = mean(total_delayed_minutes, na.rm = TRUE),
) |>
select(month, total_flights, total_delayed_flights, average_delay_minutes, sum_delay_minutes) |>
ggplot(aes(x = month, y = sum_delay_minutes)) +
geom_line() +
geom_point() +
labs(
title = "Total Minutes Delayed Over Months",
x = "Month",
y = "Total Minutes Delayed"
) +
scale_x_continuous(breaks = 1:12, labels = month.abb)
}
for (i in seq_along(rand_samps)) {
print(summarize_data2(rand_samps[[i]])) }
We see that the total delayed flights matches closely to the total minutes delayed per month.
Using this information, further analysis will be conducted on the month of December.
Within December, which airline has the most total delays and the what are the break out of the delays within the data.
december_function <- function(data) {
data |>
group_by(carrier) |>
filter(month == '12') |>
summarise(
total_delayed = sum(total_delayed),
air_carrier_delay = sum(air_carrier_delay),
national_aviation_system_delay = sum(national_aviation_system_delay),
security_delay = sum(security_delay),
late_aircraft_delay = sum(late_aircraft_delay),
weather_delay = sum(weather_delay),
late_aircraft_delay = sum(late_aircraft_delay)
) |>
ggplot( mapping = aes(x = reorder(carrier, -total_delayed))) +
geom_bar(aes(y = total_delayed, fill = "Total Delayed"), stat = "identity") +
geom_bar(aes(y = air_carrier_delay, fill = "Air Carrier Delay"), stat = "identity") +
geom_bar(aes(y = national_aviation_system_delay, fill = "NAS Delay"), stat = "identity") +
geom_bar(aes(y = security_delay, fill = "Security Delay"), stat = "identity") +
geom_bar(aes(y = weather_delay, fill = "Weather Delay"), stat = "identity") +
labs(title = "December Delay Types by Carrier",
x = "Carrier",
y = "Delays",
fill = "Delay Type") +
theme_minimal()
}
for (i in seq_along(rand_samps)) {
print(december_function(rand_samps[[i]])) }
adc_names
## # A tibble: 17 × 2
## carrier carrier_name
## <chr> <chr>
## 1 9E Endeavor Air Inc.
## 2 AA American Airlines Inc.
## 3 AS Alaska Airlines Inc.
## 4 B6 JetBlue Airways
## 5 DL Delta Air Lines Inc.
## 6 F9 Frontier Airlines Inc.
## 7 G4 Allegiant Air
## 8 HA Hawaiian Airlines Inc.
## 9 MQ Envoy Air
## 10 NK Spirit Air Lines
## 11 OH PSA Airlines Inc.
## 12 OO SkyWest Airlines Inc.
## 13 QX Horizon Air
## 14 UA United Air Lines Inc.
## 15 WN Southwest Airlines Co.
## 16 YV Mesa Airlines Inc.
## 17 YX Republic Airline
The graph is arranged in descending order of total delays, but we can see that Southwest Airlines and Skywest Airlines have the greatest air carrier delays.
Note: Late Aircraft delay was not used in the analysis as this delay may be caused by other delays in the previous airport. Late aircraft delays is the majority of total delays due to this. The ‘double dipping’ of delays is valid as delay in one leg of a travel may cause a delay in the next leg.
Removing this to delay will benfit the ability to view the data clearly.
Throughout all random samples, December has the highest number of delays. As December is a high travel period, it it normal for higher delays to be present. However using this analysis, we see some carrier are more prone to delays, especially carrier derived delays than others.
In future analysis we can evaluate the carrier delays in each month and see whether they fluctuate or remain consistent throughout the year. The principle questions this data can be used to answer is which airline and airport consistently under performs in delayed flights.
This can be followed on to by seeing why those airports and carriers are under performing.