Lab 2

The Data

data(nycflights)

names(nycflights)
 [1] "year"      "month"     "day"       "dep_time"  "dep_delay" "arr_time" 
 [7] "arr_delay" "carrier"   "tailnum"   "flight"    "origin"    "dest"     
[13] "air_time"  "distance"  "hour"      "minute"   
glimpse(nycflights)
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, 10…
$ 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, 940…
$ 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", "LGA…
$ dest      <chr> "LAX", "SJU", "LAX", "TPA", "ORF", "ORD", "HOU", "IAD", "MIA…
$ 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, 6…
$ minute    <dbl> 40, 57, 59, 41, 2, 17, 59, 20, 25, 23, 40, 20, 9, 54, 17, 24…

Analysis

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 15)

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 150)

Exercise 1

The original histogram does not represent the 0-400 part of the graph very well and shows a lot of blank space that is not able to properly represent the less frequent delays because the proportion of each bin is so skewed.

lax_flights <- nycflights |> 
  filter(dest == "LAX")
ggplot(data = lax_flights, aes(x = dep_delay)) +
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

lax_flights |> 
  summarise(mean_dd   = mean(dep_delay), 
            median_dd = median(dep_delay), 
            n         = n())
# A tibble: 1 × 3
  mean_dd median_dd     n
    <dbl>     <dbl> <int>
1    9.78        -1  1583
sfo_feb_flights <- nycflights |>
  filter(dest == "SFO", month == 2)

length(sfo_feb_flights$year)
[1] 68

Exercise 2

There are 68 flights that are SFO flights that were delayed in February.

ggplot(data = sfo_feb_flights, aes(x = arr_delay)) + 
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

sfo_feb_flights |> 
  summarise( mean_SFO_feb = mean(arr_delay), 
             median_SFO_feb = median(arr_delay), 
             count = n() )
# A tibble: 1 × 3
  mean_SFO_feb median_SFO_feb count
         <dbl>          <dbl> <int>
1         -4.5            -11    68

Exercise 3

When looking at the histogram and summary statistics of the arrival delays for the SFO flight in February it is apparent that the flights commonly arrive earlier than expected rather than actually being delayed.

sfo_feb_flights |>
  group_by(origin) |>
  summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())
# A tibble: 2 × 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
sfo_feb_flights |> 
  group_by(carrier) |> 
  summarize( std_ad = sd(arr_delay), median_ad = median(arr_delay), iqr_ad = IQR(arr_delay), n_flights = n())
# A tibble: 5 × 5
  carrier std_ad median_ad iqr_ad n_flights
  <chr>    <dbl>     <dbl>  <dbl>     <int>
1 AA        29.5       5     17.5        10
2 B6        11.0     -10.5   12.2         6
3 DL        22.0     -15     22          19
4 UA        48.3     -10     22          21
5 VX        40.8     -22.5   21.2        12

Exercise 4

Looking at the standard deviation we see that UA has the highest variability for departure delays.

nycflights |> 
  group_by(month) |> 
  summarise(mean_dd = mean(dep_delay)) |> 
  arrange(desc(mean_dd))
# A tibble: 12 × 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

Exercise 5

When looking at the mean you take into account all outlier delays meaning one substantial delay on an otherwise perfect record could heavily skew the data while the median does not give as much weight to outliers and could potentially give a slightly more accurate representation of what one might expect for an average expected departure delay. One should take both into account when determining expected outcomes.

nycflights <- nycflights |> 
  mutate( dep_type = ifelse( dep_delay < 5, "on time", "delayed"))

nycflights |> 
  group_by(origin) |> 
  summarise( ot_dep_rate = sum(dep_type == "on time") / n()) |>
  arrange(desc(ot_dep_rate))
# A tibble: 3 × 2
  origin ot_dep_rate
  <chr>        <dbl>
1 LGA          0.728
2 JFK          0.694
3 EWR          0.637

Exercise 6

If I were to pick an airport based on the on time departure rate I would pick LGA

ggplot(data = nycflights, aes(x = origin, fill = dep_type)) +
  geom_bar()

nycflights <- nycflights |> 
  mutate( avg_speed = distance / (air_time / 60) )

Exercise 7

The average speed of each flight has been added to the avg_speed column of the dataset nycflights.

Exercise 8

ggplot( data = nycflights, aes( x = distance, y = avg_speed)) + 
  geom_point()

The average speed remains relatively consistent and appears to take on a logarithmic shape. The speed generally increases as the distance becomes larger, but will even out between 400 and 500 mph with some outliers.

Exercise 9

top_airline_df <- nycflights |> 
  filter( carrier == "AA" | carrier == "DL" | carrier == "UA" )

ggplot( top_airline_df,  aes(x = dep_delay, y = arr_delay, color=carrier) ) +
  geom_point()

Based on the graph one can expect to be up to 50 minutes late from departure to still arrive to the destination on time. In order to find a slightlymore accurate estimate I could take the dep delays between 0 and 100 and graph that to view the clustering more closely.