2024-06-09

Simple Linear Regression

  • Simple linear regression is a method for showing whether or not a relationship exists between two variables.
  • Simple linear regression can also be used to predict the value of one variable based on the value of another variable if a strong relationship exists between the two.

Simple Linear Regression (Cont.)

  • Independent Variable
    • The variable that is being used to predict the dependent variable.
    • Represented by \(x\)
  • Dependent Variable
    • The variable that we want to predict.
    • Represented by \(y\)

Simple Linear Regression Equation

  • A simple linear regression model can be represented by the following formula: \[y = \beta_0 + \beta_1 x + \epsilon\]
    • \(y\): dependent variable
    • \(x\): independent variable
    • \(\beta_0\): constant
    • \(\beta_1\): slope of the regression line
    • \(\epsilon\): error term

Simple Linear Regression Example

Here we can see that as \(x\), the independent variable, increases, \(y\), a variable that is dependent on \(x\), increases as well.

Simple Linear Regression Example 2

In this example, we can see that there is a negative correlation between \(x\) and \(y\), as \(x\) increases, \(y\) decreases.

Simple Linear Regression Example 3

In this example, the model shows that there is not a strong relationship between \(x\) and \(y\)

R Code

x <- rnorm(100, mean = 0, sd = 2)
y <- x + rnorm(100, mean = 0, sd = .5)
data <- data.frame(x, y)

plot_ly(data, x = x, y = y, type = 'scatter', mode = 'markers') %>%
  add_lines(x = x, y = fitted(lm(y ~ x, data = data)), name = 'Fitted')

x <- rnorm(100, mean = 0, sd = 2)
y <- -x + rnorm(100, mean = 0, sd = .5)
data <- data.frame(x, y)
ggplot(data, aes(x = x, y = y)) +
  geom_point() + 
  geom_smooth(method='lm', formula= y~x)

x <- rnorm(100, mean = 0, sd = 2)
y <- rnorm(100, mean = 0, sd = 2)
data <- data.frame(x, y)
ggplot(data, aes(x = x, y = y)) +
  geom_point() + 
  geom_smooth(method='lm', formula= y~x)