- This presentation will explore Simple Linear Regression.
- We will use the built-in
mtcarsdataset in R. - The goal is to model the relationship between a car’s weight (
wt) and its fuel efficiency (mpg).
mtcars dataset in R.wt) and its fuel efficiency (mpg).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 \]
mpg)wt)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.
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 |
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.
From the model summary, we get our coefficients:
This means that for every 1000 lbs increase in weight, the car’s fuel efficiency is predicted to decrease by 5.34 mpg.