03.17.2024

Simple Linear Regression

Simple linear regression is a method to find a relationship between two variables, specifically an independent variable and a dependent variable. In finding a relationship, one plots the independent variable vs. the dependent variable in a scatterplot. From the scatterplot one can determine if there is a relationship based of the the best fit line.

The rest of this presentation will discuss how to do this using plots and examples.

The “Best Fit Line” - The Equation

The “best fit line” is a line that can be drawn to show the overall trend for the data on the scatterplot. It uses the following equation:

\(y = \beta x + \alpha\)

  • \(y\) and \(x\) are the variables for the independent and dependent variables
  • \(\alpha\) is the y-intercept, or where the best fit line intersects with the y-axis (\(x = 0\))
  • \(\beta\) is the slope of the line, or the rate at with the data is changing

An Example

For this presentation, we will look at the flowers data set. Specifically, we will look at the relationship between the height and weight of flowers in the data set.

##   treat nitrogen block height weight leafarea shootarea flowers
## 1   tip   medium     1    7.5   7.62     11.7      31.9       1
## 2   tip   medium     1   10.7  12.14     14.1      46.0      10
## 3   tip   medium     1   11.2  12.76      7.1      66.7      10
## 4   tip   medium     1   10.4   8.78     11.9      20.3       1
## 5   tip   medium     1   10.4  13.58     14.5      26.9       4
## 6   tip   medium     1    9.8  10.08     12.2      72.7       9

Plot Using ggplot

Plot Using ggplot (with best fit line)

R Code for ggplot Plot

x = flowers$height
y = flowers$weight

fit = lm(y ~ x)
predicted_y = predict(fit)

data <- data.frame(x = x, y = y, predicted_y = predicted_y)

ggplot(data, aes(x = x, y = y)) +
  geom_point() +
  geom_line(aes(y = predicted_y), color = "pink") +
  labs(title = "The Relationship Between Flower Height and Weight",
       x = "Flower Height",
       y = "Flower Weight")

Plot Using Plotly

R Code for Plotly Plot

x = flowers$height
y = flowers$weight

fit = lm(y ~ x)

predicted_y = predict(fit)

plot_ly(x = x, y = y, type = 'scatter',
        mode = 'markers') %>%
  layout(title = "The Relationship Between Flower Height and Weight",
         xaxis = list(title = "Flower Height"),
         yaxis = list(title = "Flower Weight")) %>%
  add_trace(x = x, y = predicted_y, mode = 'lines', name = 'Best Fit Line')

Interpret Data

As we can see from the first graph created with plotly, we have an equation for our simple linear regression, or for our best fit line:

\(y = 0.35 x + 9.74\)

Here, \(\alpha = 9.74\), so based on the best fit line, when \(flower \space height = 0\), \(flower \space weight = 9.74\). Flower weight has a consistently higher magnitude than flower height.

Then we can look at \(\beta\), or the slope of the line. Here, \(\beta = 0.35\). The rate of change is small but positive, meaning there is a slight positive correlation between flower height and flower weight.