What is Linear Regression?

Linear regression is a way to find a line that best fits through data points.

  • We use it to predict one variable from another variable
  • For example: predict car fuel efficiency from car weight
  • It’s like drawing the “best” straight line through scattered points
  • We will use the mtcars dataset that comes with R

What We’ll Learn Today

  • The basic math formula for linear regression
  • How to make scatter plots in R
  • How to check if our model is good
  • Cool 3D plots using plotly
  • Some R code examples

The Math Behind It

The basic linear regression equation is:

\[ y = a + bx + error \]

Where: - \(y\) is what we want to predict (like mpg) - \(x\) is what we use to predict (like weight) - \(a\) is the y-intercept (where line crosses y-axis) - \(b\) is the slope (how steep the line is) - \(error\) is the mistake our prediction makes

More Math Stuff

We find the best line by minimizing the sum of squared errors:

\[ \sum (y_i - \hat{y}_i)^2 \]

Where \(\hat{y}_i\) is our predicted value.

The formulas for the slope and intercept are: \[ b = \frac{\sum(x_i - \bar{x})(y_i - \bar{y})}{\sum(x_i - \bar{x})^2} \] \[ a = \bar{y} - b\bar{x} \]

Loading the Data

# Load libraries we need
library(ggplot2)
library(plotly)

# Load the mtcars data
data(mtcars)
# Look at the first few rows
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 Plot - Weight vs MPG

# Make a scatter plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Car Weight vs Miles per Gallon",
       x = "Weight (1000 lbs)", 
       y = "Miles per Gallon") +
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

Second Plot - Checking Our Model

# First fit the model
model1 <- lm(mpg ~ wt, data = mtcars)

# Make residuals plot with ggplot
library(ggplot2)
residuals_data <- data.frame(
  fitted = fitted(model1),
  residuals = residuals(model1)
)

ggplot(residuals_data, aes(x = fitted, y = residuals)) +
  geom_point() +
  geom_hline(yintercept = 0, color = "red") +
  labs(title = "Residuals vs Fitted Values",
       x = "Fitted Values", 
       y = "Residuals") +
  theme_bw()

Cool 3D Plot with Two Predictors

# Make a 3D plot with weight, horsepower, and mpg
plot_ly(data = mtcars, 
        x = ~wt, 
        y = ~hp, 
        z = ~mpg,
        type = "scatter3d",
        mode = "markers") %>%
  layout(scene = list(
    xaxis = list(title = "Weight"),
    yaxis = list(title = "Horsepower"), 
    zaxis = list(title = "MPG")
  ))

R Code Example

# Fit a simple linear model
my_model <- lm(mpg ~ wt, data = mtcars)

# Look at the results
summary(my_model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
# Make a prediction for a 3000 lb car
predict(my_model, newdata = data.frame(wt = 3.0))
##        1 
## 21.25171

What Did We Learn?

  • Linear regression finds the best line through data points
  • Heavier cars tend to have worse fuel efficiency
  • We can check our model with residual plots
  • R makes it easy to fit models and make predictions
  • 3D plots help us see relationships with multiple variables

Next Steps

  • Try using different variables as predictors
  • Learn about multiple regression (using more than one predictor)
  • Check model assumptions more carefully
  • Maybe try some non-linear models