What is Linear Regression?

Linear regression helps us understand the relationship between two variables:

  • X variable: The predictor (independent variable)
  • Y variable: The outcome (dependent variable)

Goal: Draw the best straight line through data points to make predictions.

Example: How does a car’s weight affect its fuel efficiency?

The Math Behind It

The linear regression equation is:

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

Where:

  • \(Y\) = the outcome we’re predicting
  • \(\beta_0\) = y-intercept (where line crosses y-axis)
  • \(\beta_1\) = slope (how steep the line is)
  • \(X\) = the predictor variable
  • \(\epsilon\) = random error

Finding the Best Line

We use the Least Squares Method to find the best fitting line:

\[\hat{\beta}_1 = \frac{\sum(X_i - \bar{X})(Y_i - \bar{Y})}{\sum(X_i - \bar{X})^2}\]

\[\hat{\beta}_0 = \bar{Y} - \hat{\beta}_1\bar{X}\]

This minimizes the distance between all points and our line.

Our Dataset: Car Data

We’re using the built-in mtcars dataset which contains data about 32 cars.

Weight (1000 lbs) MPG
Mazda RX4 2.620 21.0
Mazda RX4 Wag 2.875 21.0
Datsun 710 2.320 22.8
Hornet 4 Drive 3.215 21.4
Hornet Sportabout 3.440 18.7

Question: Does car weight predict miles per gallon?

The Results

Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.285 1.878 19.858 0
wt -5.344 0.559 -9.559 0

Interpretation:

  • For every 1,000 lb increase in weight, MPG decreases by 5.34
  • The relationship is highly significant (p < 0.001)

R Code: Creating the Model

Here’s how we create the linear regression model in R:

# Load the dataset
data(mtcars)

# Fit linear regression model
# mpg is predicted by wt (weight)
model <- lm(mpg ~ wt, data = mtcars)

# View the results
summary(model)

This simple code creates our entire regression analysis!

Visualization: The Regression Line

Finding: Heavier cars get fewer miles per gallon!

R Code: Creating the Plot

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(size = 4, color = "#82C0CC") +
  geom_smooth(method = "lm", se = TRUE) +
  labs(title = "Car Weight vs. Fuel Efficiency",
       x = "Weight (1000 lbs)",
       y = "Miles Per Gallon") +
  theme_minimal()

This code creates a scatter plot with a regression line. geom_point() plots the data points, geom_smooth(method = "lm") adds the linear regression line with confidence interval, and labs() adds descriptive labels.

Checking Our Model: Residuals

Residuals should be randomly scattered around zero.

3D View: Multiple Variables

Key Takeaways

What we learned:

  • Linear regression finds the best-fit line through data
  • The slope (\(\beta_1\)) tells us how Y changes when X increases
  • R-squared tells us how well our model fits the data
  • Residual plots help us check if our model is appropriate

Real-world uses:

  • Predicting sales based on advertising spend
  • Estimating house prices based on size
  • Forecasting stock prices based on market trends