2023-02-11

knitr::opts_chunk$set(echo = FALSE)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)
library(leaflet)
library(ggplot2)
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
library(MASS) # to load crabs data
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
## The following object is masked from 'package:dplyr':
## 
##     select

Introduction to Dataset

Crabs…

##   sp sex index   FL  RW   CL   CW  BD
## 1  B   M     1  8.1 6.7 16.1 19.0 7.0
## 2  B   M     2  8.8 7.7 18.1 20.8 7.4
## 3  B   M     3  9.2 7.8 19.0 22.4 7.7
## 4  B   M     4  9.6 7.9 20.1 23.1 8.2
## 5  B   M     5  9.8 8.0 20.3 23.0 8.2
## 6  B   M     6 10.8 9.0 23.0 26.5 9.8

Linear Regression Equation of Frontal Lobe Size vs. Rear Width Size

Here is our simple linear regression equation we will be using to estimate the Frontal Lobe Size using our Rear Width Size data:

model: \(\text(Frontal Lobe Size) = \beta_0 + \beta_1\cdot \text(Rear Width Size) + \epsilon; \hspace{1cm} \epsilon \sim \mathcal(N) (0;\sigma^2)\)
 fitted: \(\text(Frontal Lobe Size) = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text(Rear Width Size)\)               \(\hat{\beta}_0 = b_0 - \text{estimate of }\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1\)

Graphing the Simple Linear Regression Equation using ggplot2

Let’s try a Linear Regression Equation of Rear Width Size vs. Carapace Length

Using the same equation as before, I can also estimate the Rear Width Size with the Caraspace length. BTW, Caraspace refers to the hard upper shell.

model: \(\text(Rear Width Size) = \beta_0 + \beta_1\cdot \text(Carapace Length) + \epsilon; \hspace{1cm} \epsilon \sim \mathcal(N) (0;\sigma^2)\)
 fitted: \(\text(Rear Width Size) = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text(Carapace Length)\)               \(\hat{\beta}_0 = b_0 - \text{estimate of }\beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1\)

Code to make the Simple Linear Regression Graph with ggplot2

Heres the code

data("crabs")
y = crabs$RW; x = crabs$CL
model = lm(y~x, data = crabs, )
plot2 <- ggplot(data=crabs,aes(x,y)) + geom_point() +
  geom_smooth(formula = 'y~x', method = 'lm', se = TRUE, color = 'pink') + 
  theme_minimal() +
  labs(x = 'Carapace Length(mm)',
       y = 'Rear Width Size (mm)',
       title = 'Simple Linear Regression Plot
       of Rear Width by Carapace Length') +
  theme(plot.title = element_text(hjust = 0.5, size = 4, face = 'bold'))

Graphing the Simple Linear Regression Equation using ggplot2 (again!)

To get this graph right here ↓

Graphing the Simple Linear Regression Equation using plotly this time!

Thank you!