2024-02-05

What is Linear Regression?

  • Linear regression is a statistical tool used to predict the value(s) of data that have a relationship/correlation to one another.
  • There is a generality implied with the term “linear regression:” it can refer to any number of independent variables modeled against a dependent variable.

What is Simple Linear Regression?

  • Simple linear regression is particular in that it refers only to linear regression models that involve two variables: one independent and one dependent.
  • This is often considered the most basic, or “simple,” form of linear regression

Assumptions of Simple Linear Regression

  • There is a dependent variable that relies on some relationship between itself and the independent variable
  • It assumes there is a linear relationship between the two variables, say X and Y

Mathematical Model

The mathematical equation of a simple linear regression line is given by:

\[ y = \beta_0 + \beta_1x \]

  • \(\beta_0\) is the y-intercept,
  • \(\beta_1\) is the slope of the line,
  • You can also add an \(\epsilon\) error term.

Estimating Coefficients: Least Squares Method

There is also a formula used for estimating coefficients called the least squares method. This can be used to find (\(\beta_0\) and \(\beta_1\)) in the case of simple linear regression:

\[ \beta_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2} \]

\[ \beta_0 = \bar{y} - \beta_1\bar{x} \]

Example of Data You Could Use Linear Regression On

Keep in mind: you cannot use simple linear regression on this because it has 2 independent variables

GGPlot of weight vs miles/gallon

Taking the data from the previous graph, we can create a simple linear regression model that also shows information on the second independent variable, horsepower

Code of the GGPlot

The following is the code used for the previous graph

p <- ggplot(mtcars, aes(x = wt, y = mpg))
p <- p + geom_point(aes(color = hp))
p <- p + labs(title = "Car Weight vs. MPG",
              x = "Weight (1000 lbs)",
              y = "Miles Per Gallon",
              color = "Horsepower")
p <- p + geom_smooth(method = "lm", se = FALSE, color = "black")

p <- p + theme_minimal() #simply and make it look better

Residuals Explored

Another concept often seen with linear regression is the concept of residuals, which can be thought of as an error in a sense. They are denoted by the dotted red lines showing the distance between each data point and the linear regression line.