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.
2025-06-04
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.
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 |
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)")
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")
\[ \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 <- lm(Weight ~ Height, data=data) summary(model)
This R code fits the model and prints the summary including p-values and R-squared.
data$residuals <- residuals(model)
plot_ly(data, x = ~Height, y = ~Weight, z = ~residuals,
type = 'scatter3d', mode = 'markers', marker = list(size = 4, color = 'purple'))
Thanks for watching!