2024-04-07

Simple Linear Regression on the Iris Dataset

  • To complete the assignment, I will create a presentation on Simple Linear Regression by utilizing the Iris dataset that already comes pre-installed in R. I will make sure to accomplish all the requirements for the assignment such as:

    At least 8 slides; At least one plotly plot; At least two ggplot plots; At least two slides with math text written in Latex; At least one slide with R code

  • What is Simple Linear Regression?

  • Simple Linear Regression (SLR) is a statistical concept used to model the relationship between two variables.

  • SLR would come in handy when trying to predict the value of a dependent varible by using the value of an independent variable, or when trying to determine how two variables are related to each other.

  • For this presentation, we will use SLR to model the relationship between the length of an Iris flower petal when compared to the width of the Iris flower petal.

Using the Iris Dataset in R

  • Before we can start modeling the relationship, we first need to import the call the dataset.
data("iris")
head("iris")
## [1] "iris"
  • After pulling up the data and using the “head()” function, we can see that there is a column value field called “Species”. I will use the “table()” function on that column to illustrate all values within that field.

  • After calling the “table()” function on that specific column, we can see that we obtain three unique values for Species: Setosa, Versicolor, and Virginica. For this presentation, we will model the relationshiop between petal lenght and petal width using the data from the species Setosa. I will create a new dataset using only those values.

irisRefined <- iris[iris$Species == "setosa", ]

Creating a GGPlot in R Using ggplot

  • First, I will create a plot that depicts the relationship between the petal length versus petal width.
library(ggplot2)
ggplot(data = irisRefined, aes(x = Petal.Length, y = Petal.Width)) + geom_point(color='red') + ggtitle("Scatterplot of 'Petal Length' versus 'Petal Width'")

- Here we can see the data plotted out in a Scatterplot using ggplot. From looking at the data we can kind of see a positive correlation between petal length and petal width.

The Math Behind Building a SLR

  • Building a SLR model requires some mathematical calculations.

  • Below is the basic format of a Simple Linear Regression:

  • \(y = mx + c\)

  • This format is known as the slope-intercept form of a function, but it also serves as the recipe for a SLR.

  • A more detailed undertanding of SLR in this dataset would look like this:

  • \(y(petal width) = m(correlation factor between variables)x(petal length) + c(constant)\)

Putting it together

  • Now that we have a better grasp of what an SLR looks like, let us add it to the plot we had previously built.
library(ggplot2)
ggplot(data = irisRefined, aes(x = Petal.Length, y = Petal.Width)) + geom_point(color='red') + ggtitle("Scatterplot of 'Petal Length' versus 'Petal Width'") + geom_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

- The blue line that is on display in the plot shows the ‘line of best fit’, also known as the SLR.

Creating a Plotly Plot

  • Now that I have illustrated the concept of an SLR using the Iris dataset, it is time I meet the Plotly plot requirement for this assignment. Since to make a plot in Plotly I require three variables (it is a 3D plot), I will use the Sepal.Length field as my third Variable.
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(irisRefined, x=~Petal.Length, y=~Petal.Width, z=~Sepal.Length) %>% add_markers(size=1.5)
  • This plot depicts the multivariate relationship between the variables of Petal Length, Petal Width, and Sepal Length. I do not think it provides much value, but I am just trying to meet the Plotly requirement.

One Last Math Equation to Meet the Requirement of Math Text Written in LaTex

  • I think I explained the concept well, and anyway the main purpose of this assignment is to get comfortable making ioslides, which I think I have demonstrated proficiency over. Nonetheless, I still have that one outstanding requirement of math equation.

  • I will display the general equation for a logistic regression model, a more advanced concept than the SLR.

  • \[f(x)=\frac{1}{1+e^{-x}}\]

  • While we have not touched on this in class, there are much more complex models of regression that can be used to analyze data. The logistic regression model is one of those.