2025-09-21

What is Linear Regression?

Linear regression is a statistical method to model the relationship between two variables using a straight line.

For example, we can ask: “Does practicing more hours per week lead to a faster race time for a swimmer?”

  • Independent Variable (X): Weekly Practice Hours
  • Dependent Variable (Y): 100m Race Time (in seconds)

The Math Behind the Line

The relationship is described by a simple equation.

\[Y = \beta_0 + \beta_1 X + \epsilon\]

  • \(Y\) is what we want to predict (the Race_Time).
  • \(X\) is what we use to predict (the Practice_Hours).
  • \(\beta_0\) is the intercept, or the starting point of the line.
  • \(\beta_1\) is the slope, telling us how much \(Y\) changes when \(X\) increases by one.
  • \(\epsilon\) is the random error, because no model is perfect.

Finding the “Best-Fit” Line

How do we find the best line? We use the Method of Least Squares.

This method finds the one line that minimizes the sum of the squared vertical distances from each data point to the line.

\[\text{Minimize:} \sum_{i=1}^{n} (y_i - (\hat{\beta}_0 + \hat{\beta}_1 x_i))^2\]

The computer runs this calculation to find the best slope (\(\hat{\beta}_1\)) and intercept (\(\hat{\beta}_0\)) for our data.

A First Look at the Swimmer Data

This scatter plot shows our sample data which we generated previously.

The R Code Used

This slide shows the R code used to create the model and the plot with the regression line.

# Build the linear model to predict Race_Time 
# using Practice_Hours
model <- lm(Race_Time ~ Practice_Hours, data = swim_data)

# Create the plot and add the regression line
ggplot(swim_data, aes(x = Practice_Hours, y = Race_Time)) +
  geom_point(color = "steelblue", size = 3) +
  geom_smooth(method = "lm", se = FALSE, color = "firebrick") +
  labs(title = "Race Time vs. Practice Hours",
       x = "Weekly Practice Hours",
       y = "100m Race Time (s)") +
  theme_minimal()

Adding the Regression Line

Here is the same plot with the best-fit regression line added.

An Interactive Version of the Plot

This interactive plot was made with plotly. Hover over the points to see the exact values for each swimmer.

Results & Conclusion

The R model gave us this final equation:

Predicted Time = 57.96 + -0.5 * Practice_Hours

  • -0.5 (the slope): Each extra hour of practice changes the predicted time by about 0.5 seconds. (Negative = faster time.)

Linear regression helped us quantify how more practice time is associated with faster race times for swimmers. The end