2026-03-09

1. What is Linear Regression?

Linear regression is a method that models the relationship between a dependent variable and one or more independent variables.

Simple Linear Regression Model:

\[Y = \beta_0 + \beta_1X + \epsilon\]

Where:

  • Y is the dependent variable
  • X is the independent variable
  • \(\beta_0\) is the intercept
  • \(\beta_1\) is the slope

2. The mtcars Dataset

This linear regression demonstration will use R’s built-in mtcars dataset. Here are first few columns and rows of the data:

# Showing the first few rows of the dataset
head(mtcars[, 1:4])
##                    mpg cyl disp  hp
## Mazda RX4         21.0   6  160 110
## Mazda RX4 Wag     21.0   6  160 110
## Datsun 710        22.8   4  108  93
## Hornet 4 Drive    21.4   6  258 110
## Hornet Sportabout 18.7   8  360 175
## Valiant           18.1   6  225 105

3. Visualizing the Data

Before fitting a model, it is helpful to visualize the relationship between variables. Let’s look at car weight (wt) and miles per gallon (mpg).

4. Fitting a Simple Model

Calculate the line of best fit using the lm() function in R.

# Fitting the linear model
model <- lm(mpg ~ wt, data = mtcars)
summary(model)$coefficients
##              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.285126   1.877627 19.857575 8.241799e-19
## wt          -5.344472   0.559101 -9.559044 1.293959e-10

5. Visualizing the Regression Line

Here is the initial ggplot updated to include the linear regression line that was just calculated.

6. Multiple Linear Regression

If we want to use more than one predictor to get a more accurate model we can use multiple linear regression!

Multiple Regression Model:

\[Y = \beta_0 + \beta_1X_1 + \beta_2X_2 + ... + \beta_nX_n + \epsilon\]

This allows us to account for multiple variables at once.

7. Adding a Third Variable (Cylinders)

See how the number of engine cylinders (cyl) impacts the relationship between weight and MPG.

8. Interactive Data Exploration

Interactive plot that shows specific car details on hover.