2024-03-20

## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'plotly' was built under R version 4.3.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Simple Linear Regression

Simple Linear Regression is a statistical method that allows us to study the relationship between two continuous variables.

  • Objective: Predict the value of a dependent variable (Y) based on the value of an independent variable (X).
  • Mathematical Model: \(Y = \beta_0 + \beta_1X + \epsilon\)

The Model

The regression line is defined as:

\[y = \beta_0 + \beta_1x + \epsilon\]

where: - \(y\) is the dependent variable, - \(x\) is the independent variable, - \(\beta_0\) is the y-intercept, - \(\beta_1\) is the slope, - \(\epsilon\) is the error term.

Assumptions

Simple Linear Regression relies on several assumptions:

  1. Linear relationship
  2. Independence
  3. Homoscedasticity
  4. Normality of residuals

Linear Regression in R

summary(mtcars$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90
summary(mtcars$hp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    52.0    96.5   123.0   146.7   180.0   335.0

Slide with Plot

## `geom_smooth()` using formula = 'y ~ x'

Linear Regression in R

model <- lm(mpg ~ hp, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07

Plots

Residual Plots

Interpretation

  • The coefficient for hp (\(\beta_1\)): Represents the change in mpg for each unit increase in hp.
  • R-squared: Indicates how well the independent variable predicts the dependent variable.
  • p-value: Tests the hypothesis that the coefficient is significantly different from 0.