2025-11-09

Linear Regression

Linear regression is a statistical method that is used to model the relationship between two variables. The line created by this method can be called the “line of best fit,” and it represents the equation that best describes how the two variables relate to each other, whether that be a positive correlation, negative correlation, or no correlation at all. Here is an example of a linear regression plot.

model: \(\text{Y} = \beta_0 + \beta_1\cdot \text{X} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N} (0; \sigma^2)\)
fitted: \(\text{Y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{X}\)               \(\hat{\beta}_0 = b_0 - \text{estimate of }\beta_0\)               \(\hat{\beta}_1 =b_1 - \text{estimate of }\beta_1\)

Dataset iris

data("iris")
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

In this dataset, there are 150 observations of iris flowers with 5 variables: sepal length, sepal width, petal length, petal width, and species. We will use this dataset to model simple linear regression between different variables.

Note: the sepal is the green, leaf-like part of the iris flower that encloses the bud.

Plotting the iris Dataset Using ggplot2

We can make a scatter plot of sepal width vs. sepal length using the following code:

p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + 
  labs(x="Sepal Length", y="Sepal Width")
p

It is hard to tell if there is a correlation between the two variables at a first glance. Let’s add the line of best fit
in the next slide.

Plotting the iris Dataset Using ggplot2

We can add the line of best fit by using the geom_smooth() function:

p + geom_smooth(formula=y~x, method="lm", se=FALSE)

Based on this plot, we can see that the line of best fit has a negative slope, meaning that sepal length and sepal
width have a negative correlation. In general, as sepal length increases, sepal width decreases.

Plotting the iris Dataset Using ggplot2

We can follow the same steps to plot sepal length vs. petal length. This time, we write se=TRUE to include the default 95% confidence interval, which is shown by the gray band around the blue line of best fit.

p + geom_smooth(formula=y~x, method="lm", se=TRUE)

Based on this plot, we can see that petal length and sepal length have a positive correlation. This means that in
general, as petal length increases, so does sepal length.

Plotting the iris Dataset Using plotly

We can now make a scatter plot of petal width vs. petal length using the following code:

model = lm(Petal.Width ~ Petal.Length, data=iris)
xax <- list(title="Petal Length", titlefont = list(family="Modern Computer Roman"))
yax <- list(title="Petal Width", titlefont = list(family="Modern Computer Roman"))
plot_ly(x=iris$Petal.Length, y=iris$Petal.Width, type="scatter", 
  mode="markers", height=340, name="data") %>% layout(xaxis=xax, yaxis=yax)

Plotting the iris Dataset Using plotly

We can add the line of best fit by using the add_lines() function:

plot_ly(x=iris$Petal.Length, y=iris$Petal.Width, type="scatter", mode="markers", 
  height=340, name="data") %>% layout(xaxis=xax, yaxis=yax) %>% 
  add_lines(x=iris$Petal.Length, y=fitted(model), name="fitted")

Based on this plot, we can say that petal length and petal width have a positive correlation. This means that in
general, as petal length increases, so does petal width.

Plotting the iris Dataset Using plotly

We can follow the same steps to plot sepal width vs. petal width:

model: \(\text{Sepal Width} = \beta_0 + \beta_1\cdot \text{Petal Width} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N} (0; \sigma^2)\)
fitted: \(\text{Sepal Width} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Petal Width}\)
\(\hat{\beta}_0 = b_0 - \text{estimate of }\beta_0; \hspace{1cm} \hat{\beta}_1 =b_1 - \text{estimate of }\beta_1\)

Based on this plot, we can say that petal width and sepal width have a negative correlation. This means that
in general, as petal width increases, sepal width decreases.