2025-03-17

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Slide

set.seed(123)

x <- 1:100
y <- 5 + 2 * x + rnorm(100, mean = 0, sd = 10)

data <- data.frame(x, y)

model <- lm(y ~ x, data = data)

data$predicted <- predict(model)

fig <- plot_ly(data, x = data$x, y = data$y, type = 'scatter', mode = 'markers', name = 'Actual Data')

fig <- add_trace(fig, x = data$x, y = data$predicted, mode = 'lines', name = 'Regression Line', line = list(color = 'red'))

fig <- layout(fig, 
              title = "Linear Regression in R using Plotly",
              xaxis = list(title = "X"), yaxis = list(title = "Y"))

Slide

Slide

The linear regression’s equation is written in the format of: y = \(mx+ b\)

Slide

set.seed(123)
x <- 1:100
y <- 5 + 7 * x + rnorm(200, mean = 10, sd = 20)
data <- data.frame(x, y)

plot = ggplot(data, aes(x = x, y = y)) +
  geom_point() +  
  geom_smooth(method = "lm", col = "red") +  
  labs(title = "Linear Regression with ggplot2",
       x = "X", y = "Y")

Slide

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

Slide

set.seed(123)
x <- 1:100
y <- 5 + 10 * x + rnorm(300, mean = 20, sd = 30)

plot2 = ggplot(data, aes(x = x, y = y)) +
  geom_point() +  
  geom_smooth(method = "lm", aes(fill = "Confidence Interval"), alpha = 0.3, col = "blue") +
  labs(title = "Linear Regression with Confidence Interval",
       x = "X", y = "Y") +
  theme_minimal()

Slide

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

Slide

The slope of the equation is determined from the equation: \((y2-y1) / (x2-x1)\)