2025-06-08

Simple Linear Regression

Simple linear regression is a basic and most known statistical model that create relationship between one dependent variable (y) and one independent variable (x).

The basic formula is:

\[ y = \beta_0 + \beta_1 x + \varepsilon \]

\[ y = \text{outcome} \\ x = \text{predictor} \\ \beta_1 = \text{slope} \\ \beta_0 = \text{y-intercept} \\ \varepsilon = \text{error term} \]

Good Example of Linear Regression

Bad Example of Linear Regression

Example Linear Regression Model

Using the built-in dataset “cars” to predict distance using speed as independent variable.

Code for the Plotly

mod <- lm(dist ~ speed, data = cars)

x <- cars$speed
y <- cars$dist

predicted <- fitted(mod)

xax <- list(title = "Speed", 
titlefont = list(family = "Modern Computer Roman"))
yax <- list(title = "Distance",
titlefont = list(family = "Modern Computer Roman"))

fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers",
               name = "data", width = 800, height = 430) %>%
  add_lines(x = x, y = predicted, name = "fitted",
  line = list(color = 'red')) %>%
   layout(xaxis = xax, yaxis = yax) %>%
  config(displaylogo = FALSE)

Linear Regression Formula

Based on the dataset and plot, the y intercept is approximately -17.579 and slope is 3.932.
The regression formula for relationship between Distance and Speed is:

\[ y = -17.579 + 3.932x \]

-We are predicting distance is approximately -17.579 when speed is 0.
-Negative distance doesn’t make sense in real world, but the model is mathematical formula, no physical meaning.
-Positive slope means it has direct relationship, which y increases as x increases.
-We are predicting distance increases by 3.932 for every increase of speed by 1.

Prediction using the formula

\[ y = -17.579 + 3.932x \]

We can use the formula to predict the distance for desirable speed.
Let’s say if we want to find out the distance when the speed is 30:

\[ y = -17.579 + 3.932 \cdot 30 \]

\[ y = -17.579 + 117.96 = 100.381 \]

So we predict the distance will be approximately 100 when the speed is 30.