This project analyzes the relationship between a car’s horsepower and its fuel efficiency. Understanding this relationship is important because it helps explain how engine power affects fuel consumption.
The goal of this analysis is to determine whether cars with higher horsepower tend to have lower miles per gallon (MPG).
Dataset
This project uses the mtcars dataset, which is a real dataset included in R. It contains data on 32 car models and includes variables such as horsepower (hp), miles per gallon (mpg), weight, and number of cylinders.
The dataset is a real-world dataset included in R’s datasets package. It is based on fuel consumption and design characteristics of 32 automobiles and is widely used in statistical education and research.
mpg cyl disp hp
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
drat wt qsec vs
Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
Median :3.695 Median :3.325 Median :17.71 Median :0.0000
Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
am gear carb
Min. :0.0000 Min. :3.000 Min. :1.000
1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
Median :0.0000 Median :4.000 Median :2.000
Mean :0.4062 Mean :3.688 Mean :2.812
3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000 Max. :8.000
Visualization
We use a scatter plot with a regression line to analyze the relationship between horsepower and fuel efficiency.
library(ggplot2)ggplot(mtcars, aes(x = hp, y = mpg)) +geom_point(color ="steelblue", size =3, alpha =0.7) +geom_smooth(method ="lm", color ="red", se =TRUE) +labs(title ="Horsepower vs Fuel Efficiency",x ="Horsepower (hp)",y ="Miles Per Gallon (MPG)" ) +theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
Analysis
The scatter plot shows a clear negative relationship between horsepower and miles per gallon. As horsepower increases, fuel efficiency decreases.
The regression line confirms this downward trend, indicating that cars with more powerful engines tend to consume more fuel.
This suggests a trade-off between performance and fuel efficiency.
Statistical Summary
model <-lm(mpg ~ hp, data = mtcars)summary(model)
Call:
lm(formula = mpg ~ hp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-5.7121 -2.1122 -0.8854 1.5819 8.2360
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
hp -0.06823 0.01012 -6.742 1.79e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.863 on 30 degrees of freedom
Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07
The regression model supports the visualization, showing a statistically significant negative relationship between horsepower and MPG.
Conclusion
In conclusion, this analysis demonstrates that higher horsepower is associated with lower fuel efficiency. This means that more powerful vehicles generally use more fuel.
This relationship is important for both consumers and manufacturers when considering performance versus efficiency trade-offs.
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1+1
[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).