2026-04-12

Slide 2

What is Linear Regression?

Linear regression is a statistical method used to show the relationship between two variables. It uses a straight line (best fit line) to show how changes in one variable (X) affect another variable (Y).

Regression Line Formula

\[ Y = a + bX \]

Y = dependent variable

a = intercept (starting value)

b = slope (how much Y increases when X increases)

X = independent variable

Dataset Overview

For this example, we are using the diamonds dataset from ggplot2.

Question: How does diamond size (carat) affect price?

Carat vs Price

This scatter plot shows a positive relationship between carat and price. As carat increases, price also increases.

Linear Model (R code Example)

model <- lm(price ~ carat, data = diamonds)
summary(model)

This code builds a linear regression model using carat to predict price. Basically, it helps show how the size of a diamond affects how expensive it is.

Price by Cut

This boxplot shows how diamond prices change depending on the cut. Some cuts have higher prices and more variation compared to others.

3-D Plot

This 3D plot shows the relationship between carat, price, and depth. It helps visualize how multiple variables interact at the same time.

Predicted Value Formula

\[ \hat{Y} = a + bX \]

Y = predicted value

a = intercept (starting value)

b = slope (how much Y changes when X changes)

X = independent variable

This formula is used to calculate the predicted value of Y based on X.

Conclusion

Linear regression is a simple way to understand the relationship between variables.

In this example, we saw that as carat increases, price also increases.

Overall, the diamonds dataset shows a clear positive relationship between diamond size and price.