2026-09-15

What is Linear Regression?

Linear regression is a statistical tool which models the relationship between dependent and independent variables by finding a line of best fit for observed data. The regression line created by the process of linear regression can be used to predict a value of a dependent variable based on a given value of an independent variable.

Iris Data Set

The Iris data set is included in R and contains the following information about 150 irises.

  • Sepal Length
  • Sepal Width
  • Petal Length
  • Petal Width
  • Iris Species
##   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

R Code to Plot the Length of Petals over Width

library(plotly)

mod = lm(iris$Petal.Width~iris$Petal.Length)
p <- plot_ly( iris, x = ~Petal.Length, y = ~Petal.Width, 
              type = "scatter", mode = "markers", color = ~Species
) %>% 
  add_lines(x = ~Petal.Length, 
            y = fitted(lm(mod)),
            name = "Linear Regression Line",
            inherit = FALSE)


htmltools::browsable(p)

Result

Based on this plot, there is a strong positive correlation between an iris petal’s length and width.

Regression Line Formula

The intercept and slope of the regression line can be found:

##       (Intercept) iris$Petal.Length 
##        -0.3630755         0.4157554

Thus, the equation of the regression line is … \[ \hat{y} = -0.363 + 0.415x \]

Where \(\hat{y}\) represents the predicted width of an iris petal, given its length, \(x\).

Plot with Confidence Interval

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

Confidence Interval Explained

The Standard Error attribute of the ggplot2 plot is one way to evaluate the fit of the regression line. The shaded area around the regression line represents a 95% confidence interval. Where the area is narrower, the regression line fits the observed data better.

The fit of the regression line can also be evaluated by looking at residuals…

Residual Plot

Residual Plot Explanation

The points on the residual plot are determined by the following formula:

\[e_i = y_i - \hat{y}_i\] Where \(e_i\) is the residual, \(y_i\) is the observed petal width, and \(\hat{y}_i\) is the predicted petal width from the regression formula.

The closer the residual points are to 0, the closer the regression line fit the observed data.

Based on the plot, the regression line fit some species better than other, as the virginica species has an observably larger range of residuals than the other two.