2025-06-04

Introduction to Simple Linear Regression

Simple linear regression models the relationship between two variables with a straight line.

\[ y = \beta_0 + \beta_1 x + \varepsilon \]

Where \(\beta_0\) is the intercept, \(\beta_1\) is the slope, and \(\varepsilon\) is the error term.

Use Case Example

Let’s predict a person’s weight based on their height using weight-height.csv dataset.

The dataset includes height and weight for 500 individuals. We’ll simulate data and fit a linear model.

Gender Height Weight
Male 73.84702 241.8936
Male 68.78190 162.3105
Male 74.11011 212.7409
Male 71.73098 220.0425
Male 69.88180 206.3498

Scatter Plot with Fitted Line (ggplot2)

model <- lm(Weight ~ Height, data=data)

ggplot(data, aes(x=Height, y=Weight)) +
  geom_point(color="steelblue") +
  geom_smooth(method="lm", se=FALSE, color="darkred") +
  labs(title="Weight vs Height", x="Height (inches)", y="Weight (lbs)")

Residual Plot (ggplot2)

data$residuals <- residuals(model)

ggplot(data, aes(x=Height, y=residuals)) +
  geom_point(color="darkgreen") +
  geom_hline(yintercept=0, linetype="dashed") +
  labs(title="Residuals Plot", x="Height", y="Residuals")

Model Summary in LaTeX

\[ \text{Estimated model: } \hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x \]

\[ \hat{\beta}_0 = \text{Intercept},\ \hat{\beta}_1 = \text{Slope},\ \varepsilon \sim N(0, \sigma^2) \]

Model Fitting Code (R)

model <- lm(Weight ~ Height, data=data)
summary(model)

This R code fits the model and prints the summary including p-values and R-squared.

3D Interactive Plot (plotly)

data$residuals <- residuals(model)
plot_ly(data, x = ~Height, y = ~Weight, z = ~residuals,
        type = 'scatter3d', mode = 'markers', marker = list(size = 4, color = 'purple'))

Conclusion

  • Simple linear regression is easy and powerful
  • We modeled weight ~ height
  • Good model fit if assumptions hold

Thanks for watching!