What is Linear Regression?

Linear Regression: The method of finding the relationship between two variables.

Best fit line: the line that minimizes the distance between the line and all the data points

Equation for best fit line: \(y = mx + c\)

How to make a Linear Regression Model

Step 1: Begin by making a scatterplot.

In this example we are using the dataset “mtcars” to see the relationship between mpg (miles per gallon) and hp (horsepower).

Example 1: mtcars

Step 2: Then, we apply the equation of a line to make the best fit line.

We allow the computer to calculate and apply the best fitted line. Based on the graph below, it appears that the miles per gallon decreases as the horsepower of a car increases.

Example 1: mtcars, continued

The best fitted line is the line with the shortest distance with each data point.

Code for previous graph

fitted = lm(mpg ~ hp, mtcars)

new_mtcars$predict = predict(fitted)

ggplot(data = new_mtcars, aes(x = hp, y = mpg, color = hp))+
  geom_point(alpha = 1)+labs(title = "mpg vs. hp")+
  geom_smooth(method = "lm", se = F, color = "red")+
  geom_segment(aes(xend = hp, yend = predict), alpha = .5)

Example 2: airquality

Let’s look at another example. Here, we are using the data set “airquality.” Our scatterplot compares the relationship between the Ozone concentration and the Temperature.

Example 2: airquality, continued

Now, we let the computer calculate and apply the best fitted line to the graph.

The graph seems to indicate that when the ozone concentration gets higher, the temperature seems to rise as well.

It can also be done in 3D!

Instead of a fitted line, we find the fitted plane. Similar to the fitted line, the fitted plane is the plane with the shortest distance between each of the data points.

Equation for fitted plane: \(z = ax + by + c\)