2025-10-19

What is Simple Linear Regression?

  • A statistical method used to model the relationship between:
    • One independent variable (X)
    • One dependent variable (Y)
  • The relationship is described by a straight line
  • Helps predict Y given a known X

Equation:

\[ \hat{Y} = \beta_0 + \beta_1 X \]

Where:
- \(\beta_0\) = intercept
- \(\beta_1\) = slope
- \(\hat{Y}\) = predicted value of Y

Example Dataset

  • We will use the diamonds dataset from the ggplot2 package
  • Variables used:
    • carat → independent variable (X)
    • price → dependent variable (Y)
## # A tibble: 6 × 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48

Fitting a Regression Model

  • The simple linear regression model describes the relationship between two continuous variables

  • We model Price as a function of Carat

  • Equation form:

    \[ \hat{y} = \beta_0 + \beta_1 x \]

  • Where

    • \(\beta_0\): intercept
    • \(\beta_1\): slope (change in price per unit increase in carat)

Scatter Plot of Price vs Carat using ggplot2

  • The scatter plot shows how diamond price increases with carat size
    • This indicates a positive linear relationship.
  • The red line represents the model’s average predicted prices.
  • The spread of points shows price variation not explained by carat alone.

Box Plot of Price vs Clarity using ggplot2

  • The boxplot shows how price varies across clarity grades.
  • The middle line marks the median price for each clarity level.
  • Red dots represent outliers with unusually high or low prices.

R Code: Creating a 3D Scatter Plot with Plotly

  • The code below creates a 3D scatter plot using the diamonds dataset.
  • It maps carat, depth, and price to the x, y, and z axes.
  • Color represents price, and points are interactive for hover and rotation.
set.seed(123)

plot_ly(
  data = diamonds, x = ~carat, y = ~depth, z = ~price,
  color = ~price, colors = "viridis", type = "scatter3d",
  mode = "markers", marker = list(size = 3, opacity = 0.7)
) %>%
layout(
  title = "3D Scatter Plot: Diamond Price by Carat and Depth",
  scene = list(
    xaxis = list(title = "Carat (Size of Diamond)"), 
    yaxis = list(title = "Depth (%)"), 
    zaxis = list(title = "Price (USD)")
  )
)

3D Scatter Plot using Plotly

Conclusion

  • Simple linear regression helps describe and predict how one variable changes with another.
  • In the diamonds dataset, carat is the strongest predictor of price.
  • The regression line effectively captures the positive trend between size and cost.
  • Model accuracy improves when considering other features like clarity and cut.