03/16/2025

library(knitr)

Simple Linear Regression Introduction

Simple Linear regression (SLR) is a linear regression model with a single explanatory variable. Why this is used :

  • SLR is useful for understanding and predicting relationships between two variables.
  • It is a powerful tool for data analysis and research.

Formula

The formula of Simple Linear Regression is: \[ Y = \alpha + \beta x \] Where: Y is the dependent variable (output), alpha is the intercept, beta is the slope, x is the independent variable (input).

Example: Predicting Exam Scores

Given the regression equation:

\[ {Y} = 50 + 5x \]

where: - \(Y\) = predicted exam score,
- \(x\) = hours studied,
- 50 = base score,
- 5 = score increase per hour.

For 8 hours of study:

\[ Y = 50 + 5(8) = 90 \]

A student studying 8 hoursis expected to score 90.

R Code for Plotly Plot

R Code for ggplot2

##   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

R Code for ggplot2

R Code for ggplot2

R Code for 2D Plotly Plot (continued)

set.seed(123)
data(mtcars)

myX = mtcars$hp    
myY = mtcars$mpg   
myZ = mtcars$disp  

plot_ly(x = myX, y = myY, z = myZ,
        type = "scatter3d", mode = "markers",
        color = myZ) %>%
  hide_colorbar()