2024-03-20

Simple Linear Regression

What is simple linear regression?

  • Simple Linear Regression helps mathematicians predict values of one variable based on previously predicted values as well as the value of a variable based on some other variable.

  • Dependent variable - The variable we want to predict.

  • Independent variable - The variable we use to calculate the value of another variable.

Simple Linear Regression Equation

\[ Y_i = \beta_0 + \beta_1X_i \]

\(Y_i\)
  • The dependent variable that is observed for the ith observation.
\(\beta_0\)
  • The constant/intercept of the regression line.
\(\beta_1\)
  • The slope/coefficient of the regression line.
\(X_i\)
  • The independent variable for the ith observation.

Few Real Life Examples Of Simple Linear Regression

  • Determining the ideal height of a person depending on the age of the person.

  • Determining the relationship between years of education and salary per year.

  • Determining the relationship between the weight of the car and its miles per gallon (Efficiency of how well fuel is used). (Example shown next)

Example of a Simple Linear Regression

  • One real life example of where linear regression could be used is determining the the miles per gallon (MPG) of a car depending on the weight of the car. If we were to apply that example in a linear regression equation, the equation would be: \[MPG = \beta_0 + \beta_1(Weight)\]
  • In this equation we observe that the MPG of a car is the dependent variable and the weight is the independent variable. Meaning, the MPG of a car will depend on how much the car weighs.

Example dataset

  • To observe the relationship between the miles per gallon of a car and the weight of the car, we first plot the first 7 rows of the mtcars data set.

Sample data set:

##                    mpg    wt
## Mazda RX4         21.0 2.620
## Mazda RX4 Wag     21.0 2.875
## Datsun 710        22.8 2.320
## Hornet 4 Drive    21.4 3.215
## Hornet Sportabout 18.7 3.440
## Valiant           18.1 3.460
## Duster 360        14.3 3.570

Plot of MPG vs Weight

Simple Linear Regression code (MPG vs Weight)

model <- lm(mpg ~ wt, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Regression code (MPG vs Weight)

ggplot(
  mtcars, aes(x = mpg, y = wt)) + 
  geom_point() + 
  geom_smooth(method = "lm", col = "red") + 
  ggtitle("Vehicle MPG and weight relation") + 
  xlab("Miles per gallon (Miles)") + 
  ylab("Weight (in 1000 lbs)"
       )
## `geom_smooth()` using formula = 'y ~ x'

Regression graph (MPG vs Weight)

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

Plotly plot

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

Citations