2025-10-27

Introduction

This is a simple linear regression model for cars using the mtcars library.

We consider the relationship between \(x\) and \(y\)

We can write the relationship as: \[ y_i = \beta_0 + \beta_1 x_i + \epsilon_i \]

where
- \(\beta_0\): intercept
- \(\beta_1\): slope
- \(\epsilon_i\): random error term

Example Dataset

We will use the mtcars dataset to measure car performance. We are interested in seeing the relationship between horsepower (hp) and fuel efficiency (mpg).

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

Graphing

First, we look at a graph with power as the dependent and efficiency as the independent.

This plot may suggest a negative relationship.

Analysis

Now, let’s analyze our linear model to find \(\beta_1\) and \(\epsilon_i\)

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

Analysis

Now, we can populate our \(\beta_1\) as 30.1 and \(\epsilon_i\) as -0.07. This means that every 1 hp gain results in an average loss of 0.07 mpg.

3D Graph

Now, for fun, why don’t we add another variable so that we can visualize the data in 3D. A car’s hp and mpg can be determined by the engine and weight, so lets look at how the data stacks up with weight and cylinder count added.

Conclusion

This graph reveals many interesting trends. It suggests that mpg and cylinder count is inversely relational. We could also say that cylinder count and hp are directly relational. Additionally, weight and mpg are inversely relational.

I hope this analysis helped visualize linear regressions and trends in cars.