October 31, 2024

Introduction

What is Simple Linear Regression?

Simple Linear Regression models the relationship between one independent variable (X) and one dependent variable (Y)

Since there is only one independant variable, the regression is termed “simple”

We model the relationship using the equation:

\[Y = \beta_0 + \beta_1X + \epsilon\]

Where:
   \(\beta_0\) is the y-intercept
   \(\beta_1\) is the slope
   \(\epsilon\) is the error term

Mathematical Foundation

Models for Linear Regression are often fitted using the least squares approach.

The least squares method minimizes:

\[\sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - (\hat{\beta}_0 + \hat{\beta}_1x_i))^2\]

Which results in:

\[\hat{\beta}_1 = \frac{\sum(x_i - \bar{x})(y_i - \bar{y})}{\sum(x_i - \bar{x})^2}\]

Data Generation

Here we will generate a sample data set that we can use to model the Linear Regression.

set.seed(123)
n <- 100
x <- rnorm(n, mean = 50, sd = 10)
y <- 2 + 0.5 * x + rnorm(n, mean = 0, sd = 5)
data <- data.frame(x = x, y = y)

# Fit the model
model <- lm(y ~ x, data = data)

Scatter Plot

Here is the scatter plot for the model we just generated. The line of best fit is the blue line in the plot.

Residual Analysis

A residual is the difference between the actual value of a dependent variable and the value predicted by the model. We are graphing the Residual vs Fitted Values plot here.

data$residuals <- residuals(model)
data$fitted <- fitted(model)

ggplot(data, aes(x = fitted, y = residuals)) +
  geom_point(alpha = 0.5) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  theme_minimal() +
  labs(title = "Residual Plot",
       x = "Fitted Values",
       y = "Residuals")

3D Visualization of Linear Regression using Plotly

Plot

Thank You

Thank you for viewing this presentation !