2024-06-08

Introduction

  • Simple linear regression is a method used to predict the result of a dependent variable (y), based on the value of an independent variable(x).
  • The linear regression model is given by the equation: \[ y = \beta_0 + \beta_1 x + \epsilon \]
  • We will use a sample dataset to illustrate simple linear regression.

Sample data

set.seed(123)
x <- rnorm(50)
y <- 2 * x + rnorm(50)
data <- data.frame(x = x, y = y)
head(data)
##             x           y
## 1 -0.56047565 -0.86763278
## 2 -0.23017749 -0.48890173
## 3  1.55870831  3.07454617
## 4  0.07050839  1.50961907
## 5  0.12928774  0.03280448
## 6  1.71506499  4.94660058

Plotly plot

First Visualization

Second Visualization

R Code for Plotly Plot

library(plotly)
fig <- plot_ly(data, x = ~x, y = ~y, z = ~y, type = 'scatter3d', mode = 'markers')
fig

R Code for First Visualization

ggplot(data, aes(x = x, y = y)) +
  geom_point() + geom_smooth(method = "lm", col = "red") +
  theme_minimal() +
  ggtitle("Scatter Plot with Regression Line")

R Code for Second Visualization

ggplot(data, aes(x = x, y = y)) +
  geom_point(aes(color = y)) +
  geom_smooth(method = "lm", col = "blue") +
  theme_minimal() +
  ggtitle("Scatter Plot with Color Gradient")

Equation

The coefficients of the linear regression model can be found using the equation: \[ \beta = (X^T X)^{-1} X^T y \]

Explanation

  • For my sample data, I chose to do a 3d rendering to show an additional dimension for a potential problem.
  • For my ggplot visualization 1, I used a line to show the correlation between the x and y-axis. In this particular graph, a higher x-axis is correlated with a higher y-axis.
  • For my ggplot visualization 2, the lower y-values are darker than the higher y-values. This is another way to show correlation.
  • My second equation represents the minimum sum of squared parts. This is also known as the least squares regression.