- A method to model the relationship between two continuous variables.
- Predicts a dependent variable (Y) using one independent variable (X).
06/09/2025
\[ Y = \beta_0 + \beta_1 X + \varepsilon \]
Where: - \(\beta_0\) = intercept
- \(\beta_1\) = slope
- \(\varepsilon\) = error term
set.seed(123) x <- rnorm(100, mean = 50, sd = 10) y <- 5 + 0.8 * x + rnorm(100, sd = 5) data <- data.frame(x, y) head(data)
## x y ## 1 44.39524 36.96416 ## 2 47.69823 44.44300 ## 3 65.58708 56.23621 ## 4 50.70508 43.82635 ## 5 51.29288 41.27621 ## 6 67.15065 58.49538
## `geom_smooth()` using formula = 'y ~ x'
summary(model)
## ## Call: ## lm(formula = y ~ x, data = data) ## ## Residuals: ## Min 1Q Median 3Q Max ## -9.5367 -3.4175 -0.4375 2.9032 16.4520 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 5.79778 2.76324 2.098 0.0385 * ## x 0.77376 0.05344 14.479 <2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 4.854 on 98 degrees of freedom ## Multiple R-squared: 0.6815, Adjusted R-squared: 0.6782 ## F-statistic: 209.7 on 1 and 98 DF, p-value: < 2.2e-16
The least squares method minimizes the sum of squared residuals:
\[ \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} \]