2025-06-10

Iris Data Set

I will be using the default data set in r named “iris” in order to model a simple linear regression, the iris data set includes measurements of Sepal Length, Sepal Width, Petal Length, Petal Width, and Species Name of 150 Irises. Below there are examples of what is in this data set.
NOTE: The sepal of a flower are the leaves located below the petals and their main function is to protect the flower before it blooms.

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

Iris Sepal Length VS Sepal Width

NOTE: From here on I will be using plots only with the Setosa Irises in order to better model simple linear regression.

Sepal Length VS Sepal Width of Setosa Irises

This is a scatter plot using sepal length on the x-axis and sepal width on the y-axis. As we can see there is a positive correlation between sepal length and sepal width, as we would expect as the sepal length of the flower grows so does it’s sepal width. Now what if we wanted to find what is the best line in order to represent this correlation, this is where we can use a simple linear regression.

Simple Linear Regression

A simple linear regression is used to find a “line of best fit”, specifically a line where the sum of all the distances squared from the points to the line is as small as possible. The equation for a simple linear regression is \[y = \beta_0 + \beta_1 x + \varepsilon\]

Where:
* x is the independent variable
* y is the dependent variable
* \(\beta_0\) is the y-intercept, the expected y-value when x is 0.
* \(\beta_1\) is the slope, how much y is expected to change by for every unit of x
* \(\varepsilon\) is the error, often assumed to be normally distributed with mean 0

The specific values that can be used in the equation to create the line of best fit are \(\hat{\beta_0}\) and \(\hat{\beta_1}\).
The equation \[\hat{y} = \hat{\beta_0} + \hat{\beta_1} x\] can then be used to determine the estimated y, \(\hat{y}\), for a given x.

Simple Linear Regression Continued

In order to find the line of best fit for linear regression you must find the values of \(\hat\beta_0\) and \(\hat\beta_1\) that will minimize the sum of squared residuals when comparing to the original data. A residual is the value of the vertical line from an observed data point to the line of best fit and can be calculated by subtracting the expected y value from the measured y value \(y - \hat{y}\). In other words you must find the minimum value to the equation \[\sum_{i=1}^n (y - \hat{y})^2\]
Since we earlier defined that \[\hat{y} = \hat{\beta_0} + \hat{\beta_1} x\] we can substitute \(\hat{\beta_0} + \hat{\beta_1} x\) for \(\hat{y}\), this gives us the final equation \[\sum_{i=1}^n (y - (\hat{\beta_0} + \hat{\beta_1} x))^2\]. Solving this will give us our line of best fit.

Sepal Length VS Sepal Width of Setosa Irises With the Line of Best Fit

Sepal Length VS Sepal Width of Setosa Irises With the Line of Best Fit and Residuals