Introduction
- Simple linear regression models the relationship between two
variables.
- It helps us understand the correlation and make predictions.
Equation of Linear Regression
\[
Y = \beta_0 + \beta_1 X + \epsilon
\]
- \(Y\) is the dependent
variable
- \(X\) is the independent
variable
- \(\beta_0\) is the intercept
- \(\beta_1\) is the slope
- \(\epsilon\) is the error term
Example Data
library(ggplot2)
library(plotly)
set.seed(42)
data <- data.frame(X = 1:100, Y = 3 + 2 * (1:100) + rnorm(100, mean = 0, sd = 10))
head(data)
Scatter Plot
ggplot(data, aes(x = X, y = Y)) +
geom_point() +
geom_smooth(method = "lm", col = "red") +
labs(title = "Scatter Plot with Regression Line", x = "X", y = "Y")
3D Plot with Plotly
library(plotly)
plot_ly(data, x = ~X, y = ~Y, type = "scatter", mode = "markers")
Model Fitting
model <- lm(Y ~ X, data = data)
summary(model)
Conclusion
- Simple linear regression is a fundamental statistical tool.
- It helps in understanding relationships and making predictions.
- Further extensions include multiple regression, polynomial
regression, etc.