2024-06-04

Introduction

Simple Linear Regression is a statistical method to model the relationship between two continuous variables.

Simple Linear Regression is widely used in various fields such as economics, finance, biology, and engineering to understand the relationship between two variables and make predictions based on that relationship.

It provides valuable insights into the nature of the relationship and helps in decision-making processes.

Mathematical Background

The simple linear regression model is represented as: \[ y = \beta_0 + \beta_1 x + \epsilon \] Where:

  • \(y\) is the dependent variable
  • \(x\) is the independent variable
  • \(\beta_0\) is the y-intercept
  • \(\beta_1\) is the slope of the line
  • \(\epsilon\) is the error term

Sample Data

Let’s consider a dataset mtcars which contains various attributes of cars.

# Load necessary libraries
library(ggplot2)
library(plotly)

# Load the dataset
data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

3D Plot - MPG vs Weight vs Horsepower

MPG vs Cylinders Plot

Code behind MPG vs Cylinders Plot

ggplot(mtcars, aes(x = as.factor(cyl), y = mpg)) +
  geom_boxplot(fill = "#85C1E9", color = "#3498DB", alpha = 0.7) +  
  geom_point(position = position_jitter(width = 0.3), 
             color = "#1F618D", alpha = 0.7) +  
  labs(title = "Miles per Gallon Across Different Number of Cylinders",
       x = "Number of Cylinders", y = "Miles per Gallon") +  
  theme_minimal() +  
  theme(  
    plot.title = element_text(size = 18, face = "bold", 
                              color = "#2874A6"),  
    axis.title.x = element_text(size = 14),  
    axis.title.y = element_text(size = 14), 
    axis.text = element_text(size = 12, color = "#566573"), 
    legend.position = "none", 
    plot.caption = element_text(size = 10, color = "#566573", 
                                hjust = 1)
  )

Creating the Linear Model

We are creating a Linear Regression relationship between the variables Miles Per Gallon (mpg) and Weight of the car (wt)

# Create the linear model
model <- lm(mpg ~ wt, data = mtcars)

Model Summary

## 
## 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

Linear Regression Model Plot

Interpretation of Results

The linear regression results can be interpreted as follows:

  • The intercept (\(\beta_0\)) represents the expected value of the dependent variable (mpg) when the independent variable (wt) is zero.
  • \(\beta_0\) = 37.2851
  • The slope (\(\beta_1\)) indicates the change in the dependent variable for a one-unit change in the independent variable.
  • \(\beta_1\) = -5.3445
  • The R-squared value indicates how well the independent variable explains the variability in the dependent variable.
  • R-squared = 0.7528

The negative slope indicates that as the weight of the car increases, the miles per gallon decreases.

Conclusion

Simple linear regression is a powerful tool for understanding the relationship between two continuous variables.

The analysis of the mtcars dataset demonstrated the relationship between the weight of a car and its fuel efficiency.