2024-04-05

Simple Linear Regression

A simple linear regression is when you take data and create a “line of best fit.” It takes all the data points and uses them to predict where future data points may go on the scatter plot. There are ways to compute it manually, but R provides convenient tools to plot it for us.
We are using the built in R dataset, ChickWeight. Using this data we will graph a simple linear regression with one dependent variable, weight.

ChickWeight Dataset

Here is a preview of what the dataset looks like:

head(ChickWeight)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1

There are 4 columns, weight, Time, Chick, and Diet. We will use weight, Time, and Diet for the following graphs.

Linear Regression Math

The basic equation in matrix notation can be written out like this: \[ y = \mathbf X \beta+\varepsilon \]

Matrix Multiplication

Further expanding the equation \(y=\mathbf X \beta+\varepsilon\), we define each of the variables as: \[ y= \begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix}, \;X= \begin{bmatrix} x^T_1\\ x^T_2\\ \vdots\\ x^T_n\\ \end{bmatrix}, \;\beta= \begin{bmatrix} \beta_0\\ \beta_1\\ \beta_2\\ \vdots\\ \beta_p\\ \end{bmatrix}, \varepsilon= \begin{bmatrix} \varepsilon_1\\ \varepsilon_2\\ \vdots\\ \varepsilon_n\\ \end{bmatrix} \]

Trend Line by Diet

Graphing this dataset using ggplot2, we can see the regression lines and intervals of confidence for each diet:

Overall Trend Line

This is what the graph looks like with one regression line for the overall trend:

3D Plotly

Here we can see each chick as its own level on the Z-axis using plotly