2024-06-04

What is Regression?

  • Regression is a way of using patterns in data to make educated guesses about things.

  • Imagine you are a small business, trying to determine the sweet spot for your advertising budget. You want to determine what impact those ads have on your sales.

  • Regression is like finding a magic way to connect your numbers. It looks at the pattern in your data and draws a line that best represents how your sales change with ad spend.

The Data

We will start by generating some random data using R. With this data, we will create our model. Here’s the code used:

set.seed(123)
n <- 30 #number of samples 
ad_spend <- sample(10000:100000, n, replace = TRUE) #ad spend randomly generated
sales <- 5000 + 0.2 * ad_spend + rnorm(n, mean = 0, sd = 5000) #sales rand gen
year <- rep(c(2020, 2021, 2022), each = 10) #years 
data <- data.frame(ad_spend, sales, year) #creating data frame
head(data) #sample of a sample, anyone? 
##   ad_spend     sales year
## 1    61662 11005.418 2020
## 2    67869 29418.580 2020
## 3    12985 13636.810 2020
## 4    39924  7369.257 2020
## 5    78292 18643.976 2020
## 6    72554 17177.523 2020

Let’s Plot the data….

To get a better idea of what we are looking at!

The Model : Background

In general, a regression model looks something like this:

Data = Model + Error

Better known as \(y = B + m x\)

Where \(B\) is our Model and \(m x\) is our error, or deviation from our model.

(am I using the term model you define model? You bet I am! Stay with me…)

Infrences Using our Model

We’re using Linear Regression in our example, because we are using a continuous variable.

So, our hypothetical model looks something like this:

Predicted Sales = Sales if 0 Ad spend + \(\beta\) Increase in sales per ad dollar spent

Our \(\beta\) is 0.2 in our hypothetical.

Regression Line & Outliers

Our trend or regression line is calculated using the sum of all squared distances of each point to the line:

\[\sum_{i=1}^{n} (y_i - \hat{y}_i)^2\] Because the line is calculated based off the distance of each point, we want to keep an eye on our outliers. Points that are way off from our model can greatly impact our trend line.

Let’s take a look at two plots, watch how the trend line differs if we take away one single data point….

Linear Regression Line & Outliers

This is an extreme example, but as you can see one outlier really does make a difference!

Refrences: