- Simple Linear Regression models relationship between two variables
- Used for prediction and explanation
- Example: Predict salary from years of experience
2026-03-09
library(ggplot2) library(plotly) set.seed(123)
\[ Y_i = \beta_0 + \beta_1 X_i + \epsilon_i \]
\[ \hat{\beta}_1 = \frac{\sum (X_i - \bar{X})(Y_i - \bar{Y})} {\sum (X_i - \bar{X})^2} \]
\[ \hat{\beta}_0 = \bar{Y} - \hat{\beta}_1 \bar{X} \]
x <- rnorm(100, 10, 2) y <- 5 + 2*x + rnorm(100, 0, 3) df <- data.frame(x, y)
ggplot(df, aes(x=x, y=y)) + geom_point() + theme_minimal()
ggplot(df, aes(x=x, y=y)) + geom_point() + geom_smooth(method="lm", se=FALSE, color="red") + theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
model <- lm(y ~ x, data=df)
df$pred <- predict(model)
plot_ly(df,
x = ~x,
y = ~y,
z = ~pred,
type = "scatter3d",
mode = "markers")
model <- lm(y ~ x, data=df) summary(model)