- This presentation will dive into the statistics topic: Simple Linear Regression.
- Simple linear regression is a statistical model that fits a linear function to data points in a scatter plot.
- It is called “simple” linear regression because it involves mapping one independent variable to a dependent variable (as opposed to multiple independent variables).
- Linear regression allows you to make predictions about your data.
Scatter Plot Example (Using Plotly)
- Let’s get right into the examples!
- We are going to be practicing simple linear regression using the
airqualitydata set. - Let’s begin by creating a simple scatter plot in Plotly.
- See the R Code below used to generate this plot:
plot_ly(data = airquality, x = ~Day, y = ~Temp, color = ~Month,
type = "scatter", mode = "markers")
- This plot charts temperatures based on the day. The points are colored by month.
- These data points are very spread out across the plot. They do not seem to follow a linear pattern.
The Scatter Plot and Fitted Line (Using GGPlot)
- The previous plot may not have been a good candidate for the linear regression model because the points were so spread out.
- Let’s try another example, and this time fit a line to the data:
ggplot(data = airquality, aes(x = Ozone, y = Solar.R)) + geom_point(color = "deeppink") +
geom_smooth(method = "lm", color = "purple", se = FALSE) +
labs(title = "Graph of Solar by Ozone", x = "Ozone Level",
y = "Solar Level") + xlim(0, 150) + ylim(0, 300)
- These data points appear more linear than the previous example, but it may still be difficult to make accurate predictions about the data based on this linear model.
Better Candidate for Linear Regression (Using GGPlot)
- Let’s try an example with the
carsdata set.
ggplot(data = cars, aes(x = speed, y = dist)) + geom_point(color = "pink") +
geom_smooth(method = "lm", color = "cyan", se = FALSE) +
labs(title = "Graph of Speed vs. Distance", x = "Speed",
y = "Distance") + xlim(0, 100) + ylim(0, 100)
- This is a much better example of simple linear regression!
- The points follow a more linear pattern, and therefore, we can make more accurate predictions.
Interpolation vs. Extrapolation
- Let’s define some key terms for simple linear regression.
- We will use this information to make predictions about our data.
- Interpolation:
- When you make a prediction based on a value inside your data.
- In the previous plot, an example of interpolation would be predicting the distance at a speed of 6.
- Not risky; likely an accurate prediction.
- Extrapolation:
- When you make a prediction based on a value outside of your data.
- In the previous plot, an example of extrapolation would be predicting the distance at a speed of 50.
- Risky due to the potential for outlier data points.
Calculate the Linear Regression Line (Latex)
We can estimate the formula for the linear regression line in the example with the cars data set based off the graph.
- Slope-intercept formula for a line: \(y=mx+b\)
- \(y\) represents the dependent variable (distance)
- \(x\) represents the independent variable (speed)
- \(m\) represents the slope
- \(b\) represents the y-intercept
- Calculate:
\(p_1 = (4,2), p_2 = (8, 16)\)
\(x_1 = 4, x_2=8, y_1=2, y_2=16\)
\(m=\frac{y_2-y_1}{x_2-x_1}=\frac{16-2}{8-4}=\frac{14}{4}=\frac{7}{2}\)
\(16=\frac{7}{2}(8)+b \leftrightarrow\) \(16=28+b\leftrightarrow\) \(b=-12\)
Estimated formula for the linear regression model: \(y=\frac{7}{2}x-12\)
Make Predictions! (Latex)
We can now make predictions about the data using this formula.
Interpolation Example: What is the estimated distance when speed = 6?
\(y=\frac{7}{2}x-12\)
\(y = \frac{7}{2}(6)-12\)
\(y=9\)
The estimated distance is \(9\).
Extrapolation Example: What is the estimated distance when speed = 50?
\(y=\frac{7}{2}x-12\)
\(y = \frac{7}{2}(50)-12\)
\(y=163\)
The estimated distance is \(163\).
Conclusion
- In conclusion, simple linear regression is a helpful tool to be able to make predictions by either interpolating or extrapolating.
- It is best to use a linear regression model for data that already has a linear pattern, as opposed to data that is very spread out.
- Interpolation is typically more accurate and less risky than extrapolation.