The analysis of the MtCars dataset explores key relationships among
variables such as miles per gallon (mpg), number of cylinders,
horsepower, and transmission. The first analysis identifies how fuel
efficiency varies across different vehicle characteristics, suggesting
that cars with fewer cylinders tend to have higher mpg. While heavier
vehicles and those with higher horsepower exhibit lower fuel efficiency.
By categorizing the dataset based on cylinder count, the study confirms
a strong negative correlation between horsepower and fuel efficiency.
The analysis also examines the impact of transmission type (automatic 0
vs. manual 1) on mpg, suggesting that manual cars may have a slight edge
in efficiency. Overall, the results align with expectations that a
vehicle with a 4-cylinder engine and a 5-speed manual transmission is
the most fuel-efficient.
Variable Descriptions
- mpg - Miles per gallon (fuel efficiency)
- cyl - Number of cylinders (4, 6, or 8)
- disp- Displacement (cubic inches)
- hp - Gross horsepower
- drat- Rear axle ratio
- wt - Weight of the car (1000 lbs)
- qsec- 1/4 mile time (in seconds)
- vs - Engine type (0 = V-shaped, 1 = straight)
- am - Transmission type (0 = automatic, 1 = manual)
- gear- Number of forward gears
- carb- Number of carburetors
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
rm(list = ls())
gc()
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 593553 31.7 1365672 73 660385 35.3
## Vcells 1103941 8.5 8388608 64 1770207 13.6
set.seed(123123)
data(mtcars)
# Load necessary libraries
library(dplyr)
library(knitr)
library(ggplot2)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# Summarize by number of cylinders
summary_by_cyl <- mtcars %>%
group_by(cyl) %>%
summarise(
avg_mpg = round(mean(mpg, na.rm = TRUE), 2),
avg_hp = round(mean(hp, na.rm = TRUE), 2),
avg_wt = round(mean(wt, na.rm = TRUE), 2),
avg_qsec = round(mean(qsec, na.rm = TRUE), 2)
)
# Display the summary
kable(summary_by_cyl, caption = "Summary of mtcars by Number of Cylinders")
Summary of mtcars by Number of Cylinders
4 |
26.66 |
82.64 |
2.29 |
19.14 |
6 |
19.74 |
122.29 |
3.12 |
17.98 |
8 |
15.10 |
209.21 |
4.00 |
16.77 |
# Summarize by number of gears
summary_by_gear <- mtcars %>%
group_by(gear) %>%
summarise(
avg_mpg = round(mean(mpg, na.rm = TRUE), 2),
avg_hp = round(mean(hp, na.rm = TRUE), 2),
avg_wt = round(mean(wt, na.rm = TRUE), 2),
avg_qsec = round(mean(qsec, na.rm = TRUE), 2)
)
# Display the summary
kable(summary_by_gear, caption = "Summary of mtcars by Number of Gears")
Summary of mtcars by Number of Gears
3 |
16.11 |
176.13 |
3.89 |
17.69 |
4 |
24.53 |
89.50 |
2.62 |
18.96 |
5 |
21.38 |
195.60 |
2.63 |
15.64 |
# Summarize by transmission type
summary_by_am <- mtcars %>%
group_by(am) %>%
summarise(
avg_mpg = round(mean(mpg, na.rm = TRUE), 2),
avg_hp = round(mean(hp, na.rm = TRUE), 2),
avg_wt = round(mean(wt, na.rm = TRUE), 2),
avg_qsec = round(mean(qsec, na.rm = TRUE), 2)
)
kable(summary_by_am, caption = "Summary of mtcars by Transmission Type")
Summary of mtcars by Transmission Type
0 |
17.15 |
160.26 |
3.77 |
18.18 |
1 |
24.39 |
126.85 |
2.41 |
17.36 |
# Compare avg_mpg by cyl and gear
comparison <- mtcars %>%
group_by(cyl, gear) %>%
summarise(avg_mpg = round(mean(mpg, na.rm = TRUE), 2)) %>%
ungroup()
# Display the comparison
kable(comparison, caption = "Comparison of avg_mpg by Number of Cylinders and Gears")
Comparison of avg_mpg by Number of Cylinders and
Gears
4 |
3 |
21.50 |
4 |
4 |
26.92 |
4 |
5 |
28.20 |
6 |
3 |
19.75 |
6 |
4 |
19.75 |
6 |
5 |
19.70 |
8 |
3 |
15.05 |
8 |
5 |
15.40 |
# Plot the comparison
ggplot(comparison, aes(x = factor(cyl), y = avg_mpg, fill = factor(gear))) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Comparison of avg_mpg by Number of Cylinders and Gears",
x = "Number of Cylinders", y = "Average MPG", fill = "Number of Gears") +
theme_minimal()
