2025-04-13

library(dplyr)
library(plotly)
library(ggplot2)

Simple Linear Regression Overview

Using Simple Linear Regression will help us find an estimation for a relationship between two variables which are an independent and dependent variable. This data is shown in a straight line model to show how the dependent variable changes upon the independent variable.

Use of Equation

Simple Linear Regression will use the following formula or equation to help determine the relationship between an independent variable, x, and a dependent variable, y.

\(y = \beta_0 + \beta_1\cdot x + \varepsilon\)

  • \(\beta_0\) = Y-Intercept
  • \(\beta_1\) = Slope
  • \(\varepsilon\) = Error Term
  • \(x\) = Independent Variable
  • \(y\) = Dependent Variable

mtcars dataset

For this presentation on Simple Linear Regression, we will observe if there is a relationship between miles per gallon(mpg) and horse power(hp), weight(wt), and displacement(disp) from the dataset, mtcars.

data(mtcars)
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

Regression Equation

Below is the equation that will be used for the relationship between mpg and hp as well as mpg and wt.

\(y = \beta_0 + \beta_1\cdot x\)

  • \(\beta_0\) = Y-Intercept
  • \(\beta_1\) = Slope (Effects of hp/wt/disp)
  • \(x\) = Horsepower/Weight/Displacement
  • \(y\) = Miles Per Gallon

Plotly of MPG and HP

R code used to create previous plot

mod = lm(mpg ~ hp, data=mtcars)
x = mtcars$hp; y = mtcars$mpg

xax <- list(title = "Horsepower",
        titlefont = list(family="Modern Computer Roman"))
yax <- list(title = "Miles Per Gallon",
        titlefont = list(family="Modern Computer Roman"),
        range = c(0,50)
        )

fig <- plot_ly(x=x, y=y, type="scatter", mode="markers", name="mtcars", width = 800, height = 430) %>%
  add_lines(x = x, y = fitted(mod), name="fitted") %>%
  layout(xaxis = xax, yaxis = yax)

ggplot of mpg and wt

ggplot of mpg and disp