2024-02-17

What is Simple Linear Regression?

Simple linear regression is a relationship between two variables. It is used to try and find some type of correlation between two variables in a linear manner. This is meant to determine if one variable is dependent on another.

What does the equation look like?

This is the linear regression equation:

\[ Y = \beta_0 + \beta_1 X + \varepsilon \]

Example of a positive linear relationship

Consider data of hours of physical activity a week (\(X\)) and general happiness from 1-100 (\(Y\)).

Scatter Plot

R Code for Scatter Plot

# R code for creating a scatter plot

library(ggplot2)


Physical_Activity <- c(1, 2, 3, 4, 5)
Happiness <- c(60, 70, 80, 90, 100)

your_data <- data.frame(Physical_Activity, Happiness)


scatter_plot <- ggplot(your_data, aes(x = Physical_Activity, y = Happiness)) +
  geom_point() +
  labs(title = "Scatter Plot of Physical Activity vs Happiness Level")

print(scatter_plot)

Now, if we added our regression line

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

3D Plot

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: `includeHTML()` was provided a `path` that appears to be a complete HTML document.
## ✖ Path: C:\Users\rocco\AppData\Local\Temp\Rtmpm6utMT\file3e10440625c0.html
## ℹ Use `tags$iframe()` to include an HTML document. You can either ensure `path` is accessible in your app or document (see e.g. `shiny::addResourcePath()`) and pass the relative path to the `src` argument. Or you can read the contents of `path` and pass the contents to `srcdoc`.
plotly

Conclusion

In conclusion, simple linear regression is a way of showing correlation between two variables. By using a regression line, we can make assumptions about whether or not the variables are dependent on one another.