What is a linear regression?

  • Linear regression relies on a data set and creates a “trend” in the data that can be helpful predicting the outcome of certain decisions or outcomes.
  • A common name for linear regression is called the “line of best fit” because it finds the “best fit” line to correlate with the data points. \[ y = \beta_0 + \beta_1 x + \epsilon \] \(\beta_0\) is the y-intercept of the regression, \(\beta_1\) is the slope of the line, and \(\epsilon\) is the difference between a predicted value from the line and an actual observed value.

Example Data Set

  • Using R code and plotly, we can create a random distribution of data to create a live example.
  • For this first explanation, I will be displaying what R code is used to create the scatter plot.
# Generate some example data
set.seed(42)
x <- 1:20
y <- 3 + 2*x + rnorm(20, sd=5)
model <- lm(y ~ x)

Code continuation

fig = plot_ly(
  x = ~x, 
  y = ~y, 
  type = 'scatter', 
  mode = 'markers',
  name = 'Data Points',
  marker = list(color = 'darkblue', size = 8, opacity = 0.9),
  ) %>%
  layout(
    title = list(
      text = "Interactive Random Data Set",
      font = list(size = 22)
    ),
    xaxis = list(title = "X Values", showgrid = FALSE, zeroline = FALSE),
    yaxis = list(title = "Y Values", showgrid = FALSE, zeroline = FALSE),
    legend = list(orientation = "h", y = -0.2, xanchor = "center", x = 0.5
    ))
fig

Linear Regression Application

  • After applying our regression formula, we then have a line of best fit.

Usage in different software

  • Here, we will use the same process on a different program called ggplot2 to express the diverse usability of linear regressions.

Applied Linear Regression in ggplot2

How is \(\epsilon\) important to linear regressions?

  • \(\epsilon\), used in the regression formula: \(y = \beta_0 + \beta_1 x + \epsilon\) is essentially the error term, or as aforementioned, is the difference between a predicted value from the line and an actual observed value.
  • This explanation can be reduced to a simple equation: \(\epsilon_i = y_i - \hat{y_i}\)

Visual Demonstration Using Plotly

Graph Explanation

  • Those vertical dotted lines represented the error term, or residual, from the line of best fit to the actual data value.
  • If a point/data value is above the line of best fit, \(\epsilon\) is a positive value and the model was an underprediction.
  • If a point/data value is below the line of best fit, \(\epsilon\) is a negative value and the model was an overprediction.