Introduction

Simple linear regression is a statistical method used to describe the relationship between one explanatory variable and one response variable.

In this presentation, we will examine the relationship between car weight and miles per gallon using the built-in mtcars dataset.

What is Simple Linear Regression?

A simple linear regression model is written as:

\[ Y = \beta_0 + \beta_1 X + \epsilon \]

where:

  • \(Y\) is the response variable
  • \(X\) is the explanatory variable
  • \(\beta_0\) is the intercept
  • \(\beta_1\) is the slope
  • \(\epsilon\) is the random error term

Interpretation of the Slope

The slope tells us how much the response variable changes when the explanatory variable increases by one unit.

For a fitted regression line:

\[ \hat{Y} = b_0 + b_1 X \]

  • \(b_0\) is the estimated intercept
  • \(b_1\) is the estimated slope

If \(b_1 < 0\), then as \(X\) increases, \(Y\) tends to decrease.

Data Used

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

First ggplot

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(
    title = "Scatterplot of Weight vs MPG",
    x = "Weight",
    y = "Miles per Gallon"
  )

This plot shows that heavier cars tend to have lower miles per gallon.

Second ggplot

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Regression Line for Weight and MPG",
    x = "Weight",
    y = "Miles per Gallon"
  )
## `geom_smooth()` using formula = 'y ~ x'

This graph includes the fitted regression line, showing a negative linear relationship.

Linear Regression Model

model <- lm(mpg ~ wt, data = mtcars)
coef(model)
## (Intercept)          wt 
##   37.285126   -5.344472

The fitted regression equation is:

\[ \hat{Y} = 37.2851 - 5.3445(wt) \]

Example Prediction

Suppose a car has weight equal to 3. Using the estimated regression equation:

\[ mpg = 37.2851 - 5.3445(wt) \]

When \(wt = 3\):

\[ mpg = 37.2851 - 5.3445(3) \]

\[ mpg = 21.2516 \]

So the predicted miles per gallon is about 21.25.

R Code Example

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Regression Line for Weight and MPG",
    x = "Weight",
    y = "Miles per Gallon"
  )
## `geom_smooth()` using formula = 'y ~ x'

Conclusion

Simple linear regression helps us understand and quantify the relationship between two variables.

In this example, the relationship between car weight and miles per gallon is negative, meaning heavier cars generally get worse gas mileage.

This method is useful in many fields such as business, health, engineering, and science.