Topic: Simple Linear Regression

  • This presentation will explore Simple Linear Regression.
  • We will use the built-in mtcars dataset in R.
  • The goal is to model the relationship between a car’s weight (wt) and its fuel efficiency (mpg).

The Statistical Model

The core of simple linear regression is a linear equation that models the relationship between a dependent variable (Y) and an independent variable (X).

The formula is: \[ Y = \beta_0 + \beta_1 X + \epsilon \]

  • \(Y\) is the dependent variable (mpg)
  • \(X\) is the independent variable (wt)
  • \(\beta_0\) is the y-intercept
  • \(\beta_1\) is the slope
  • \(\epsilon\) represents the error term

Data Visualization with ggplot

First, let’s visualize the relationship between weight and MPG with a scatter plot. This helps us see if a linear relationship is a reasonable assumption.

The Graph

R Code for Fitting the Model

We use the lm() function in R to create the linear model. The formula mpg ~ wt tells R to model mpg as a function of wt.

library(broom)
library(knitr)

data(mtcars)
model <- lm(mpg ~ wt, data = mtcars)

kable(tidy(model), digits = 3)
term estimate std.error statistic p.value
(Intercept) 37.285 1.878 19.858 0
wt -5.344 0.559 -9.559 0

Visualizing the Regression Line

Now we can add the regression line calculated by our mode l to the scatter plot. This line represents the predicted mpg for any given wt.

Visualizing the Regression Line

Interactive Plot with Plotly

Interpreting the Results

From the model summary, we get our coefficients:

  • Intercept (\(\beta_0\)): 37.285
  • Slope (\(\beta_1\)): -5.344

This means that for every 1000 lbs increase in weight, the car’s fuel efficiency is predicted to decrease by 5.34 mpg.

Conclusion

  • This presentation successfully demonstrated a simple linear regression analysis.
  • We found a clear, negative relationship between a car’s weight and its fuel efficiency.