# Load the mtcars dataset
data("mtcars")
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ 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(haven)
library(survey)
## Loading required package: grid
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## Loading required package: survival
##
## Attaching package: 'survey'
##
## The following object is masked from 'package:graphics':
##
## dotchart
# Summary statistics by transmission type (am: 0 = Automatic, 1 = Manual)
summary_stats <- mtcars %>%
group_by(am) %>%
summarise(
Average_MPG = mean(mpg, na.rm = TRUE),
Average_HP = mean(hp, na.rm = TRUE),
Average_WT = mean(wt, na.rm = TRUE),
.groups = 'drop'
)
print(summary_stats)
## # A tibble: 2 × 4
## am Average_MPG Average_HP Average_WT
## <dbl> <dbl> <dbl> <dbl>
## 1 0 17.1 160. 3.77
## 2 1 24.4 127. 2.41
# ANOVA: Does transmission type affect fuel efficiency (MPG)?
anova_mpg <- aov(mpg ~ factor(am), data = mtcars)
summary(anova_mpg)
## Df Sum Sq Mean Sq F value Pr(>F)
## factor(am) 1 405.2 405.2 16.86 0.000285 ***
## Residuals 30 720.9 24.0
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ANOVA: Does cylinder count affect horsepower (hp)?
anova_hp <- aov(hp ~ factor(cyl), data = mtcars)
summary(anova_hp)
## Df Sum Sq Mean Sq F value Pr(>F)
## factor(cyl) 2 104031 52015 36.18 1.32e-08 ***
## Residuals 29 41696 1438
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Linear regression: Predict MPG based on horsepower and weight
lm_mpg <- lm(mpg ~ hp + wt, data = mtcars)
summary(lm_mpg)
##
## Call:
## lm(formula = mpg ~ hp + wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.941 -1.600 -0.182 1.050 5.854
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
## hp -0.03177 0.00903 -3.519 0.00145 **
## wt -3.87783 0.63273 -6.129 1.12e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
## F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12
# Bar chart comparing average MPG by transmission type (am: 0 = Automatic, 1 = Manual)
ggplot(mtcars, aes(x = factor(am), y = mpg, fill = factor(am))) +
geom_bar(stat = "summary", fun = "mean") +
labs(
title = "Miles Per Gallon by Transmission Type",
x = "Transmission (0 = Automatic, 1 = Manual)",
y = "Average MPG"
) +
scale_fill_manual(values = c("gray70", "steelblue"), labels = c("Automatic", "Manual")) +
theme_minimal()
# Boxplot: Comparing MPG by transmission type
ggplot(mtcars, aes(x = factor(am), y = mpg, fill = factor(am))) +
geom_boxplot() +
labs(
title = "Boxplot of MPG by Transmission Type",
x = "Transmission (0 = Automatic, 1 = Manual)",
y = "MPG"
) +
scale_fill_manual(values = c("gray70", "steelblue"), labels = c("Automatic", "Manual")) +
theme_minimal()
# Scatter plot: Horsepower vs MPG with cylinder count as color
ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl))) +
geom_point(size = 4) +
labs(
title = "Horsepower vs MPG by Cylinder Count",
x = "Horsepower",
y = "Miles Per Gallon",
color = "Cylinders"
) +
theme_minimal()
Since I’m planning to buy a car, I wanted to explore a few important factors that matter to me as a buyer. Fuel efficiency is a top priority for me, and I believe it is a key consideration for most car buyers and manufacturers alike. To better understand what influences fuel efficiency, I embarked on a data-driven adventure using the mtcars dataset in R. This dataset contains detailed information about 32 different cars, including their miles per gallon (MPG), horsepower, weight, and other features. By analyzing this data, I hoped to discover insights that would help me make a more informed decision and perhaps shed light on what makes some cars more fuel-efficient than others.
As someone planning to buy a car, I wanted to understand how transmission type affects fuel efficiency. The analysis revealed that cars with automatic transmission (coded as 0) have an average MPG of 17.15, while cars with manual transmission (coded as 1) achieve a higher average MPG of 24.39. This means manual cars are more fuel-efficient than automatic cars, which was a surprising and useful insight for me as a potential buyer. To visualize this difference, I have created a bar chart comparing average MPG by transmission type. The chart clearly shows that manual cars (am = 1) achieve significantly higher MPG compared to automatic cars (am = 0). This visual confirmation supports the idea that transmission type plays a significant role in fuel efficiency.
However, I wanted to dig deeper into the data and the boxplot reveals not only the median MPG for each transmission type but also the spread and variability of the data. Interestingly, manual cars (am = 1) show a wider range of MPG values, indicating greater variability in fuel efficiency. On the other hand, automatic cars (am = 0) have a more concentrated distribution. This suggests that while manual cars are generally more efficient, their performance can vary more significantly. For me, this means that if I choose a manual car, I will need to pay closer attention to its specific fuel efficiency ratings.
Another interesting finding is that manual cars have lower average horsepower i.e. 126.85 and are lighter (average weight of 2.41) compared to automatic cars, which have higher horsepower i.e. 160.26 and are heavier (average weight of 3.77). These differences further explain why manual cars tend to be more fuel-efficient. As someone who values both performance and efficiency, this trade-off between power and weight is something I will keep in mind while making my decision.
As I continued my exploration, I wanted to understand how the number of cylinders in a car’s engine affects its performance. The analysis revealed that cylinder count has a strong impact on horsepower. Cars with more cylinders, such as 6 or 8, tend to have higher horsepower, making them more powerful. The ANOVA results confirmed this, showing a highly significant relationship (p-value = 1.32e-08). This means that cylinder count is a key factor in determining a car’s power. For me, this was an important insight because it highlighted the trade-off between engine size and performance that is something I will need to consider when choosing a car.
To investigate what influences fuel efficiency, I employed a linear regression model, using horsepower and weight as predictors of MPG. The results were fascinating: both horsepower and weight significantly affect MPG. For every unit increase in horsepower, MPG decreases by 0.03, and for every unit increase in weight, MPG drops by 3.88. This means that heavier and more powerful cars tend to be less fuel-efficient. The model explained 82.68% of the variation in MPG, which is a strong result and gave me confidence in the findings.
The plot shows that as horsepower increases, MPG generally decreases. What is even more interesting is the color coding by cylinder count, which reveals that cars with fewer cylinders (e.g., 4 cylinders) tend to have higher MPG and lower horsepower, while cars with more cylinders (e.g., 8 cylinders) have lower MPG and higher horsepower. This aligns perfectly with the earlier finding that cylinder count strongly influences horsepower and, indirectly, fuel efficiency. For me, this visualization made it clear that if I prioritize fuel efficiency, I might need to consider cars with fewer cylinders and lower horsepower.
The findings of this study highlight several important relationships that influence fuel efficiency in cars. First, manual cars are more fuel-efficient than automatic cars, likely due to their lighter weight and lower horsepower. This suggests that transmission type plays a significant role in determining how efficiently a car uses fuel. Second, cylinder count strongly influences horsepower, with engines having more cylinders producing more power. This indicates that the design of a car’s engine directly impacts its performance. Finally, horsepower and weight are key predictors of fuel efficiency. Cars with higher horsepower and heavier weight tend to have lower MPG, emphasizing the trade-off between power, size, and efficiency. These insights provide valuable guidance for both car buyers and manufacturers, highlighting the importance of balancing performance with fuel economy.
In conclusion, this study has been really insightful for me as I plan to buy a car. It highlights the importance of transmission type, cylinder count, horsepower, and weight in determining fuel efficiency. Manual cars, with their lighter weight and lower horsepower, offer better fuel economy, while cars with fewer cylinders, lower horsepower, and lighter weight are generally more fuel-efficient. These findings have helped me understand the trade-offs between performance and efficiency, and I believe they can also guide other car buyers in making informed decisions.