2025-09-21

1

Simple Linear Regression

Simple Linear Regression is used to identify the relationship between two variables.

It can also be useful for predicting outcomes given two variables.

Equation:

\[ y= mx + b \]

Breaking it Down

  • The formula for simple linear regression can be given by y = mx + b.

  • Independent Variable (x): This variable is independent from any other variable. It is an input.

  • Dependent Variable (y): This variable is dependent on the independent variable. It is the output.

  • Slope (m): This is the change in y over the change in x.

  • Intercept (b): This is the value of y when the value of x is zero.

Example (plotly plot)

This is an example of a simple linear regression of a random set of data. We can conclude that there is a positive relationship between the independent and dependent variable.

Ozone Vs Temp (ggplot)

Using the airquality data set, we can generate a simple linear regression model to help us understand the relationship between temperature and ozone.

## `geom_smooth()` using formula = 'y ~ x'

Wind Vs Temp (ggplot)

Within the same airquality data set, we can identify the contrasting negative relationship between temperature and wind.

## `geom_smooth()` using formula = 'y ~ x'

R code

Here is the code used to generate the simple linear regression plot seen in the previous slide representing the relationship between wind and temperature.

{r aq_new2, echo = FALSE, fig.width = 6.5, fig.height = 3.5}

data(“airquality”)

aq_new2 <- na.omit(airquality)

ggplot(data = aq_new2, aes(x = Temp, y = Wind)) + geom_point(alpha = 0.5, color = “purple”) +
geom_smooth(method = “lm”, color = “orange”, se = FALSE) +
coord_cartesian(ylim=c(0,30)) + labs( title = “Simple Linear Regression: Wind vs Temperature”, x = “Temperature”, y = “Wind” )

Numerical Example

Given a data set with coordinates \((0, 1)\) and \((1, 4)\), we can solve mathematically for the slope and intercept to create an question for the simple linear regression.

Step 1: Find slope by calculating the change in y over the change in x.

\(m = \frac{(4-1)}{(1-0)} = \frac{3}{1} = 3\)

Step 2: Find the intercept by identifying the value of y when x is zero. Luckily, we have the coordinate \((0, 1)\) available to us.

\(x = 0\)

\(y = 1\)

Intercept is 1.

Step 3: Create equation using known variables.

\(y=3x+1\)