Draft

Lab 4: Flights II

The Data

We will be answering more questions using the flights data frame located in the stat20data package.

Reminder

You will need to create your own .qmd file, as well as load in any necessary packages that you may need to answer the questions!

Question 1

Mutate a new column onto the original flights data frame which, for each flight, contains the average speed of the plane, measured in miles per hour. Call the new column avg_speed, and save the resulting data frame back into the object flights.

Hint: Look through the column names or the help file to find variables that can be used to calculate this.

Question 2

In this question, we’ll be focusing on the relationship between distance and average speed.

part a

Create a scatter plot to visualize the above relationship. Place distance on the x axis and average speed on the y axis. Label your axes.

part b

Describe the relationship between the two variables that you see. Specifically, comment on the direction, shape, and strength of the association.

part c

Add code to title your plot with a brief summary of what you wrote in part b.

part d

Write dplyr code to calculate the correlation coefficient between average speed and distance.

part e

With your current scatter plot, add a layer geom_smooth(method = "lm"). This will allow you to superimpose the least squares line—of the linear model—that explains average speed with distance.

part f

Based on your answers to parts c-e, do you believe it is appropriate to fit a linear model explaining average speed with distance? Explain in one to two sentences.

Question 3

In this question, we’ll be focusing on the relationship between departure delay and arrival delay.

part a

Create a scatter plot to visualize this relationship. Place departure delay on the x axis and arrival delay on the y axis.

part b

Describe the relationship between the two variables that you see. Specifically, comment on the direction, shape, and strength of the association.

part c

Give the plot a title based on what you wrote in part b.

part d

Write dplyr code to calculate the correlation coefficient between arrival delay and departure delay.

part e

Based on your answers to parts a-d, do you believe it is appropriate to fit a linear model explaining arrival delay with departure delay? Explain in one to two sentences.

Question 4

part a

Fit a linear model using lm() to estimate arrival delay using departure delay.

part b

Write out the mathematical form of the linear model based on the output in part a.

part c

Based on the output for part a, provide a verbal interpretation for both the slope coefficient and the y-intercept coefficient.

part d

Create a new data frame from flights with the following columns:

  • carrier

  • departure delay

  • arrival delay

  • the residuals of the linear model you fit in part a.

Hint: review the “Summarizing Numerical Associations” tutorial for how to calculate the residuals.

part e

Which flight carrier had the smallest residuals, on average?

Write dplyr code and then use it to answer this question in one sentence.

Question 5

part a

From flights, create a new data frame max_delay_by_carrier that shows the maximum departure delay for each carrier; this new data frame should have the following columns:

  • carrier: 2-letter carrier abbreviation

  • max_delay: maximum departure delay

Display your max_delay_by_carrier data frame by sorting max_delay values in descending order.

part b

With max_delay_by_carrier, create a bar chart that allows you to visualize the maximum delay time of each carrier in descending order. Hint: to rank the bars you need to use the reorder() function, e.g., aes(x = reorder(carrier, -max_delay), y = max_delay). Add appropriate axis labels and title.

Question 6

part a

From flights, create a new data frame prop_delay_by_carrier that shows the proportion of flights with departure delays (dep_delay > 0) for each carrier; this new data frame should have the following columns:

  • carrier: 2-letter carrier abbreviation

  • total: total number of flights

  • delayed: number of flights with departure delays

  • prop: proportion of flights with departure delays

Display your prop_delay_by_carrier data frame by sorting prop values in descending order.

part b

With prop_delay_by_carrier, create a bar chart that allows you to visualize the proportion of departure delays of each carrier in descending order. Hint: to rank the bars you need to use the reorder() function, e.g., aes(x = reorder(carrier, -prop), y = prop). Add appropriate axis labels and title.

Last Question

Will you ensure that your submission to Gradescope…

  1. is of a pdf generated from a qmd file,
  2. has all of your code visible to readers,
  3. and assigns each of the questions to all pages that show your work for that question?

(This one is easy! Just answer “yes” or “no”)