2025-10-19

What is Simple Linear Regression?

It is the first step on learning Statistics!

Finding out the relationship between two measures. For example: weight and height?, sleep and mood?.

Draw the best straight line through the points. (this line shows the pattern).

Use that line to make predictions. If I know enough of a topic I can make better predictions on it.

The Simple Linear Regression Model

The model is:

\[ y = \beta_0 + \beta_1 x + \varepsilon \]

Where:

  • \(y\) = the value we want to predict
  • \(x\) = the value we use to predict
  • \(\beta_0\) = intercept (baseline when x = 0)
  • \(\beta_1\) = slope (how much y changes when x increases by 1)
  • \(\varepsilon\) = error (the randomness/noise)

Why Do We Use SLR?

We use simple linear regression to:

  • See if two things are related
    (Do they move together?)

  • Understand the direction of the relationship

    • If one goes up, does the other go up or down?
  • Measure how strong the relationship is

  • Draw a line that shows the pattern

  • Make predictions
    If we know one value (X), we can guess the other (Y)

Real-life examples:

  • More hours studied → higher test score?
  • More weight → lower gas mileage?
  • More sleep → better mood?

How Do We Find the Best Line? (Math)

We use the least squares method to find the line.

The slope of the line is:

\[ \beta_1 = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2} \]

The intercept of the line is:

\[ \beta_0 = \bar{y} - \beta_1 \bar{x} \]

These formulas give us the best-fitting line that minimizes the distance between the points and the line.

Example Dataset: pressure

We will use the built-in pressure dataset in R.

It contains measurements of:

  • temperature (in degrees Celsius)
  • pressure (in mm of mercury)

We want to see how temperature affects pressure.

As temperature increases, pressure also increases.

This makes it a good example for simple linear regression.

Preview of the Dataset

Before building the model, let’s look at the first few rows of the pressure dataset.

temperature pressure
0 0.0002
20 0.0012
40 0.0060
60 0.0300
80 0.0900

Scatterplot of Temperature vs Pressure (ggplot)

Code

library(ggplot2)

ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point(size = 3) +
  labs(title = "Scatterplot of Temperature vs Pressure",
       x = "Temperature (°C)",
       y = "Pressure (mm of mercury)")

Interactive Scatterplot (Plotly)

Now let’s take a closer look at the scatterplot so we can really see its point.

Scatterplot with Regression Line

Code:

library(ggplot2)

ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(title = "Temperature vs Pressure with Regression Line",
       x = "Temperature (°C)",
       y = "Pressure (mm of mercury)")

The line shows the general trend, but it doesn’t fit the data very well because the relationship is curved, not perfectly straight.

Conclusion: What Simple Linear Regression (SLR) Tells Us

  • Purpose: To Model and predict a response \(y\) from one predictor \(x\) with a straight line.
  • What it gives: A clear trend direction (slope), an intercept, and simple predictions.
  • When it works well: The relationship between \(x\) and \(y\) is approximately linear, with constant variance and independent errors.
  • Limits: If the true pattern is curved or shows changing spread, a straight line can mislead or fit poorly.