What Is Linear Regression?

Linear regression is a statistical method used to examine the relationship between a dependent variable and one or more independent variables.

  • Dependent variable (Y): The outcome we want to predict.
  • Independent variable (X): A factor used to explain or predict the outcome.
  • Regression line: The line that best represents the relationship between the variables.

Our Example:

Can we predict a car’s fuel efficiency (MPG) based on its weight and horsepower?

The Mathematics of Linear Regression

Simple linear regression uses the following equation:

\[ \hat{Y} = \beta_0 + \beta_1 X \]

Where:

  • \(\hat{Y}\) = Predicted value
  • \(\beta_0\) = Intercept (predicted Y when X = 0)
  • \(\beta_1\) = Slope (change in predicted Y for each unit increase in X)
  • \(X\) = Independent variable

For our example:

\[ \widehat{MPG} = \beta_0 + \beta_1(\text{Weight}) \]

Exploring the mtcars Dataset

For this analysis, we’re using R’s built-in mtcars dataset, which contains information about 32 cars from the 1973–74 Motor Trend US magazine.

The three variables we’re focusing on are:

  • MPG: Fuel efficiency measured in miles per gallon.
  • Weight: Vehicle weight measured in pounds.
  • Horsepower: The car’s engine horsepower.

Our Goal:

We want to see how weight and horsepower relate to fuel efficiency and whether we can use them to predict MPG.

Summary Statistics

Before building our regression model, let’s look at some basic statistics for the variables we’re using.

Variable Minimum Average Maximum
MPG 10.4 20.09 33.9
Weight (lbs) 1513.0 3217.25 5424.0
Horsepower 52.0 146.69 335.0

Vehicle Weight vs. Fuel Efficiency

Observation: Heavier vehicles generally have lower MPG. The downward regression line shows a negative relationship between vehicle weight and fuel efficiency.

Horsepower vs. Fuel Efficiency

Observation: Cars with more horsepower generally have lower MPG. Similar to vehicle weight, horsepower shows a negative relationship with fuel efficiency.

Exploring All Three Variables in 3D

Multiple Linear Regression

Multiple linear regression uses two or more variables to predict an outcome.

We’re using vehicle weight and horsepower to predict MPG:

\[ \widehat{MPG} = \beta_0 + \beta_1(\text{Weight}) + \beta_2(\text{Horsepower}) \]

Where:

  • \(\beta_0\) = Intercept
  • \(\beta_1\) = Weight coefficient
  • \(\beta_2\) = Horsepower coefficient

Each coefficient tells us how its variable relates to predicted MPG while holding the other variable constant.

Building Our Regression Model in R

We use lm() to predict MPG based on vehicle weight and horsepower.

# Building our regression model
model <- lm(mpg ~ weight_lbs + hp, data = cars_data)

Model Results:

Variable Coefficient
Intercept 37.2273
Weight (lbs) -0.0039
Horsepower -0.0318

Model Fit: \(R^2 = 0.827\) (82.7% of MPG variation explained)

Interpreting Our Regression Model

Based on our results, the estimated regression equation is:

\[ \widehat{MPG} = 37.2273 - 0.0039(\text{Weight}) - 0.0318(\text{Horsepower}) \]

What do these numbers tell us?

  • Weight: For every additional 1,000 lbs, predicted MPG decreases by approximately 3.9, holding horsepower constant.
  • Horsepower: For every additional 10 hp, predicted MPG decreases by approximately 0.32, holding weight constant.

Takeaway: Both weight and horsepower have negative coefficients, meaning higher values are associated with lower predicted fuel efficiency.

Predicting a Car’s Fuel Efficiency

Let’s predict the MPG of a car weighing 3,000 lbs with 150 horsepower.

Predicted Fuel Efficiency: 20.83 MPG

What does this tell us?

Based on our regression model, a vehicle weighing 3,000 lbs with 150 horsepower is estimated to achieve approximately 20.83 MPG. Actual fuel efficiency may differ depending on other factors not included in our model.

Conclusion

Our analysis of the mtcars dataset showed that vehicle weight and horsepower are both negatively associated with fuel efficiency.

Key Findings:

  • Heavier vehicles generally have lower MPG.
  • Higher horsepower is also associated with lower MPG.
  • Our regression model predicted 20.83 MPG for a 3,000 lb car with 150 horsepower.

Conclusion:

Using multiple linear regression, we can estimate fuel efficiency from vehicle weight and horsepower. However, correlation does not imply causation.