2024-06-24

<– Carter Yin DAT301 –>

What is linear regression?

Given a set of data and a linear regression model:

  • Linear regression estimates the linear relationship between a dependent variable (aka. predictor) and independent (aka. explanatory, response) variable(s)
  • When we have only one explanatory variable, we use simple linear regression.

Given a list of pairs of x and y values \[(x_0y_0, x_1y_1, x_2y_2, ..., x_iy_i)\] as data points

we could find a (the slope) and b (the y-intercept) such that
\(y = ax+b\) is a line which estimates the data based on the data points.

Dataset “iris”

The built-in dataset “iris” in R has 5 columns:

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

We can discover, for example, whether the sepal length has a correlation to the petal length of flowers of a species.

Example 1 - Correlation between Petal Length and Petal Width of Setosa (plotly)

# Plotting Width Against Length
setosa = iris[iris$Species=='setosa', ]
x = setosa[, "Petal.Length"]
y = setosa[, "Petal.Width"]
# linear regression model
lm_mod <- lm(y~x)

x_axis <- list(title = "Petal Length")
y_axis <- list(title = "Petal Width")
#plotly
scplot <- plot_ly(x=x, y=y, type="scatter", mode="markers") %>%
  add_lines(x = x, y = fitted(lm_mod)) %>%
  layout(xaxis = x_axis, yaxis = y_axis)

Example 1

Correlation?

We could see a very weak correlation from the plot from Example 1.

But how do we mathematically express how strong a correlation is?

Correlation Coefficient (Pearson’s r)

\[ r= { \sum_{} (x_i-\bar{x})(y_i-\bar{y}) \over \sqrt{\sum_{} (x_i-\bar{x})^2 \sum_{}(y_i-\bar{y})^2 } } \]

\(r = 1\) means that x and y have a perfectly. positive correlation

\(r = -1\) means that x and have a perfectly. negative correlation

If \(|r| = 1\), all the data points should be on their regression line.

\(r = 0\) means that there is no correlation at all.

Example 2 - Correlation between Sepal Width and Petal Width of Versicolor (ggplot2)

Example 3 - Correlation betweenz Sepal Width and Petal Width of All 3 Species (ggplot2)

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