2024-03-20

Simple Linear Regression

Simple Linear Regression is a statistical method that allows us to understand and predict the relationship between two variables: Independent variable (X) and a Dependent variable (Y). It assumes a linear relationship between these variables, represented as: \[Y = \beta_0 + \beta_1X + \epsilon\]

The Goal of Simple Linear Regression

The primary objective is to find the best-fitting line through the data points that minimizes the sum of the squared differences between the observed and predicted values. This line is described by the equation: \(Y = \beta_0 + \beta_1X\)

  • \(Y\) is the dependent variable.
  • \(\beta_0\) is the intercept of the regression line.
  • \(\beta_1\) is the slope of the regression line.
  • \(X\) is the independent variable.
  • \(\epsilon\) represents the error term.

Example - Temperature and Ice Cream Sales

A simple example is the exploration of the relationship between temperature (in degrees Celsius) and ice cream sales (in dollars). Intuitively, we expect higher sales as temperature increases.

Ggplot 1: Sales Vs. Temp

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

## 
## Call:
## lm(formula = sales ~ temperature, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.2678  -2.7618  -0.1731   3.2425  10.4744 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 23.89830    2.01295   11.87   <2e-16 ***
## temperature  1.56215    0.07841   19.92   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.573 on 98 degrees of freedom
## Multiple R-squared:  0.802,  Adjusted R-squared:    0.8 
## F-statistic: 396.9 on 1 and 98 DF,  p-value: < 2.2e-16

Ggplot 1: Sales Vs. Temp CODE

set.seed(123)
temperature <- seq(15, 35, length.out = 100)
sales <- 25 + 1.5 * temperature + rnorm(100, mean = 0, sd = 5)
data <- data.frame(temperature, sales)

library(ggplot2)
ggplot(data, aes(x = temperature, y = sales)) + 
    geom_point() +
    geom_smooth(method = "lm", se = FALSE, color = "red") +
    theme_minimal() +
    ggtitle("Ice Cream Sales vs. Temperature")
## `geom_smooth()` using formula = 'y ~ x'

fit <- lm(sales ~ temperature, data = data)
summary(fit)
## 
## Call:
## lm(formula = sales ~ temperature, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.2678  -2.7618  -0.1731   3.2425  10.4744 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 23.89830    2.01295   11.87   <2e-16 ***
## temperature  1.56215    0.07841   19.92   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.573 on 98 degrees of freedom
## Multiple R-squared:  0.802,  Adjusted R-squared:    0.8 
## F-statistic: 396.9 on 1 and 98 DF,  p-value: < 2.2e-16

Ggplot 2: Distribution of Sales

Ggplot 2: Distribution of Sales CODE

ggplot(data, aes(x = sales)) + 
    geom_histogram(binwidth = 5, fill = "blue", color = "black") +
    theme_minimal() +
    ggtitle("Distribution of Ice Cream Sales")

Interactive Ploty Plot: Temp Vs. Sales

Interactive Ploty Plot: Temp Vs. Sales CODE

plotly_plot <- ggplot(data, aes(x = temperature, y = sales)) + 
  geom_point() + 
  ggtitle("Temperature vs. Ice Cream Sales") + 
  xlab("Temperature (in degrees Celsius)") + 
  ylab("Sales (in dollars)")

ggplotly(plotly_plot)