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).
2026-04-12
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).
\[ Y = a + bX \]
Y = dependent variable
a = intercept (starting value)
b = slope (how much Y increases when X increases)
X = independent variable
For this example, we are using the diamonds dataset from ggplot2.
Question: How does diamond size (carat) affect price?
This scatter plot shows a positive relationship between carat and price. As carat increases, price also increases.
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.
This boxplot shows how diamond prices change depending on the cut. Some cuts have higher prices and more variation compared to others.
This 3D plot shows the relationship between carat, price, and depth. It helps visualize how multiple variables interact at the same time.
\[ \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.
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.