2024-04-07

Introduction to Linear Regression

Linear regression is a model used in statistics that details the relationship between a dependent variable and one or more independent variables.

What are some of its applications?

Machine Learning, forecast modeling in various industries such as business, agriculture, weather, supply chain, among other ones.

The Linear Equation

The equation of a straight line is usually written as \(y = mx + c\).

  • \(y\): Dependent variable
  • \(x\): Independent variable
  • \(m\): Slope of the line
  • \(c\): Y-intercept

Linear Regression Equation

The equation of a simple linear regression line is:

\[ y = \beta_0 + \beta_1x \]

Simple Linear Regression

This plot shows the linear relationship between various data points

## `geom_smooth()` using formula = 'y ~ x'

Linear Regression in 2 groups

This example is like the previous one, but this one involves 2 groups of data points

## `geom_smooth()` using formula = 'y ~ x'

Plotly Code Example

R code that generates a plotly as an example for linear regression

library(plotly)
set.seed(123)
data = data.frame(
  x = 1:100,
  y = 2 * (1:100) + rnorm(100, mean = 0, sd = 20)
)

mod = lm(y ~ x, data = data)
plot_ly(data, x = ~x, y = ~y, type = "scatter", 
mode = "markers", name= "values") %>%
add_trace(x = ~x, y = fitted(mod), mode = "lines", 
name = "Regression Line")

Plotly Example

Another example of linear regression with plotly