How are engine size, cylinder count, and city fuel economy related to highway fuel economy among 2024 gasoline vehicles?
This project goes into how engine size and number of cylinders affect city and highway fuel economy across vehicle class. I chose this topic because everyone drives a car in this day and age, and figuring out which one can get you from one place to another using the least amount of fuel. Understanding which vehicle characteristics affect fuel efficiency can help me make more better decisions. The dataset comes from the U.S. Environmental Protection Agency’s vehicle fuel economy database, available at FuelEconomy.gov. These estimates come from vehicle testing conducted by the EPA and manufacturers under EPA oversight. Each observation represents a vehicle configuration for a particular model year. The six variables I selected are make (manufacturer), VClass (EPA vehicle size class), displ (engine displacement in liters), cylinders , city08 and highway08 (estimated city and highway fuel economy in miles per gallon). Higher miles per gallon means greater fuel efficiency. The official data dictionary defines all these variables.
Importing and loading libraries.
I call my libraries and import my dataset, while also erasing any spaces that are at the beginning and ends of cells, as well as hiding the column types.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.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(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(readr)library(ggplot2)
vehicles<-read_csv("vehicles.csv", na=c("","NA"))
Rows: 50647 Columns: 84
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (23): drive, eng_dscr, fuelType, fuelType1, make, model, mpgData, trany,...
dbl (59): barrels08, barrelsA08, charge120, charge240, city08, city08U, city...
lgl (2): phevBlended, tCharger
ℹ 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.
trim_ws =TRUEshow_col_types=FALSE
dim(vehicles)
[1] 50647 84
Filtering and selecting data
I use filter to select and only use any rows that have “Regular” “Premium” and “Midgrade” under the “fuelType” column. I did this because the dataset includes electric and desiel cars, which are not supposed to be included in the dataset. I also used is.na and trimws to exclude any vehicles with alternative technology, such as hybrids, FFV or FCV, by checking to see if the “atvType” is blank. If it isn’t blank it is excluded. I assign all this to “vehicles_subset”
vehicles_subset<-filter(vehicles, year ==2024, fuelType %in%c("Regular", "Premium", "Midgrade"),is.na(atvType) |trimws(atvType) =="")nrow(vehicles_subset)
[1] 752
Then from “vehicles_subset” I select only the columns that have the variables I am going to use (make, VClass, displ, cylinders, city08, highway08) and assign it to “vehicles_selected”
Then before I start making the graphs I start to only keep the rows rhat pass these checks: the make and vehicle class are not missing or blank, and displ, cylinder count, and city/highway MPG are not missing and are greater than 0. I also make sure that the cylinder count is a while number using “floor()”. Then I mutate the categorical values ar categories and the quantitative values as numbers.
then I check and see how many rows were removed. Since none were removed it means my data is not faulty.
nrow(vehicles_selected) -nrow(vehicles_clean)
[1] 0
Visual and Data Exploration
I grouped the vehicles by manufacturer using group_by(make) and calculated each manufacturer’s average city and highway MPG using summarise() and mean(). I used pivot_longer() to organize these averages into a format to let city and highway MPG to be separate bars. Then I ordered the manufacturers by average highway MPG and used geom_col(position = "dodge") to place the bars side by side. I made the bars horizontal with coord_flip(), assigned blue to city MPG and orange to highway MPG, then made it interactive. The graph shows that manufacturers specializing in luxury sports cars and supercars tend to have lower MPG. This pattern may reflect their focus on larger, more powerful engines and performance.
Then I created a scatterplot to examine the relationship between engine size and highway MPG. Each dot represents a vehicle configuration, and its color indicates the number of cylinders. Hovering over a dot shows the manufacturer, engine size, cylinder count, and highway MPG. The graph shows that vehicles with larger engines and more cylinders generally have lower highway MPG, although fuel economy varies among vehicles with similar engine sizes.
I made another interactive scatterplot comparing city and highway MPG, with a color gradient representing engine size. Each dot represents a vehicle configuration, and hovering over it displays its values. The graph shows a strong positive relationship: vehicles with higher city MPG generally also have higher highway MPG. Vehicles with smaller engines tend to have higher MPG in both driving conditions, while larger engines generally have lower fuel economy.
The multiple regression model had an adjusted R² of 0.8764, so approximately 87.69% of the variation in highway MPG was explained by the data, with . City MPG and cylinder count were statistically significant predictors at the 0.05 level, while engine displacement was not significant after accounting for the other predictors. The model was statistically significant overall, with a P-value of 2.2E-16
This project showed that larger engines and more cylinders generally corresponded with lower city and highway MPG among the 2024 gasoline vehicles studied. City and highway MPG had a strong positive relationship, meaning vehicles that were efficient in city driving also tended to be efficient on the highway. The multiple regression model explained about 87.69% of the variation in highway MPG, with an adjusted R² of 0.8764. City MPG and cylinder count were statistically significant predictors, while engine size was not significant after accounting for the other predictors.
The analysis had several limitations. Engine size and cylinder count were strongly correlated, making their individual relationships with MPG harder to separate. The model also left out characteristics such as vehicle weight, transmission, and drivetrain. Finally, EPA estimates may be different from fuel economy experienced in everyday driving, and these relationships do not prove cause and effect.