Simple linear regression helps us predict one variable based on another.
Example: predicting height based on weight.
It fits a straight line through data points that best describes their relationship.
2025-10-19
Simple linear regression helps us predict one variable based on another.
Example: predicting height based on weight.
It fits a straight line through data points that best describes their relationship.
We try to find a line that fits the data:
\[ y = \beta_0 + \beta_1 x + \varepsilon \]
where
- \(y\) = dependent variable (what we predict)
- \(x\) = independent variable (what we use to predict)
- \(\beta_0\) = intercept
- \(\beta_1\) = slope
- \(\varepsilon\) = random error
set.seed(123) x <- 1:20 y <- 3 + 2*x + rnorm(20, 0, 3) df <- data.frame(x, y) head(df)
Each point represents one observation of x and y. The black line shows the best-fit regression line. You can see the overall trend of y increasing with x.
Shows how far each observed y is from the predicted y. Points above zero are over-predictions; below zero are under-predictions. Random scatter around zero indicates a good fit.
(Not possible to render 3d) Displays the relationship between x and y as interactive points. The upward trend shows that as x increases, y generally increases too — matching the positive slope seen in simple linear regression.
The slope (\(\beta_1\)) and intercept (\(\beta_0\)) are estimated using:
\[ \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} \]
Displays the regression model results, including slope, intercept, residuals, and R² value — to summarize how well the line fits the data.
## ## Call: ## lm(formula = y ~ x, data = df) ## ## Residuals: ## Min 1Q Median 3Q Max ## -5.964 -1.808 -0.113 1.558 5.201 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 3.9303 1.3860 2.836 0.011 * ## x 1.9519 0.1157 16.870 1.78e-12 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 2.984 on 18 degrees of freedom ## Multiple R-squared: 0.9405, Adjusted R-squared: 0.9372 ## F-statistic: 284.6 on 1 and 18 DF, p-value: 1.778e-12