2024-10-30

What is Linear Regression?

Linear regression is the statistical technique of finding the linear line of best fit over the data to evaluate if there is a linear relationship and to predict data if so, as seen in the graph below.

The Math Behind Linear Regression

The line of best fit can be used to model the relationship between two variables. If the relationship can be modeled by a straight line with some error, linear regression can be used. It can be represented by this equation:

\(y = \beta _1x+\beta _0+\epsilon\)

where \(\beta _1\) represents the slope parameter, \(\beta _0\) represents the y-intercept parameter, and \(\epsilon\) represents the error. When writing the equation for the line, the \(\epsilon\) is usually dropped as the line seeks to find the average prediction.

Making Prediction with Linear Regression

If we plot the petal length against the petal width from the iris data set, we get a graph that looks like this. This graph has some gaps in the data, like what the width would be at length 2-3.

Making Predictions (cont.)

If we add the linear regression line, we can estimate what the petal width will be given any length, including those in the gap in the data, by finding the \(x\) and \(y\) coordinates of that line.

Find the Linear Regression

Using ggplot2, we can plot a graph similar to the example we just saw. We can use ggplot and geom_point to first make the scatter plot of the data, using code like this:

fig <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width)) +
  geom_point()

Find the Linear Regression (cont.)

We can then add geom_smooth to graph the line. We will define the formula as y~x to plot \(y\), petal width, in relation to \(x\), petal length. Defining method as lm indicates we are performing a linear regression, and the program will calculate the \(\beta_0\) and \(\beta_1\) from the equation. Since geom_smooth has a default value for the parameter se as true, it will show the confidence interval, or the \(\epsilon\), as a gray area surrounding the line.

fig <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width)) +
  geom_point() +
  geom_smooth(formula = y~x, method="lm")

Linear Regression

In the end, our code will output this figure showing the data as scatterpoints, the linear regression as the blue line, and the margin of error as the gray area around the line.