Linear regression models the relationship between a quantitative response variable and one or more predictors.
We use the mtcars dataset: - Response: mpg - Predictors: wt (weight), hp (horsepower)
Linear regression models the relationship between a quantitative response variable and one or more predictors.
We use the mtcars dataset: - Response: mpg - Predictors: wt (weight), hp (horsepower)
The multiple linear regression model:
\[ Y_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \varepsilon_i \]
For our example:
\[ mpg = \beta_0 + \beta_1(wt) + \beta_2(hp) + \varepsilon \]
Least squares estimator:
\[ \hat{\beta} = (X^T X)^{-1} X^T y \]
Test statistic for significance:
\[ t = \frac{\hat{\beta}_j}{SE(\hat{\beta}_j)} \]
## # A tibble: 3 × 5 ## term estimate std.error statistic p.value ## <chr> <dbl> <dbl> <dbl> <dbl> ## 1 (Intercept) 37.2 1.60 23.3 2.57e-20 ## 2 wt -3.88 0.633 -6.13 1.12e- 6 ## 3 hp -0.0318 0.00903 -3.52 1.45e- 3
# Fit a multiple regression model fit_multiple <- lm(mpg ~ wt + hp, data = mtcars) # View summary statistics summary(fit_multiple) # Create a scatter plot ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(method = "lm")