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\)
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\)
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).
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.
The best fitted line is the line with the shortest distance with each data point.
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)
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.
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.
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\)