2025-04-11

What is Simple Linear Regression?

Simple Linear Regression can be modeled by the following:



\(y = \beta_0 + \beta_1 \cdot x + \epsilon\)


Where:
  • \(y\): Amount spent
  • \(x\): Minutes spent on website
  • \(\epsilon\): Error term

The Dataset — completejourney

  • Real-world data from a grocery chain
  • We’ll focus on:
    • Transactions (transactions)
    • Product information (products)
    • Customer demographics (demographics)

Objective: Predict total spending (sales_value) from number of items (quantity)



For the purposes of loading the data I limited the dataset to the first 1,000 transactions with qty < 20 and sales_value < 100.

Sample of Transaction Data - completejourney

## # A tibble: 6 × 11
##   household_id store_id basket_id   product_id quantity sales_value retail_disc
##   <chr>        <chr>    <chr>       <chr>         <dbl>       <dbl>       <dbl>
## 1 900          330      31198570044 1095275           1        0.5         0   
## 2 900          330      31198570047 9878513           1        0.99        0.1 
## 3 1228         406      31198655051 1041453           1        1.43        0.15
## 4 906          319      31198705046 1020156           1        1.5         0.29
## 5 906          319      31198705046 1053875           2        2.78        0.8 
## 6 906          319      31198705046 1060312           1        5.49        0.5 
## # ℹ 4 more variables: coupon_disc <dbl>, coupon_match_disc <dbl>, week <int>,
## #   transaction_timestamp <dttm>

Visualizing the Data

transactions %>%
  ggplot(aes(x = quantity, y = sales_value)) + geom_point(alpha = 0.2) + geom_smooth(method = "lm", se = FALSE, color = "pink") +
  labs(title = "Sales Value vs Quantity", x = "Quantity in Basket", y = "Sales Value ($)")
## `geom_smooth()` using formula = 'y ~ x'

Fitted Model

Fitted Equation of Spend vs. Quantity Purchased:

\(\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot x\)

  • Slope (\(\hat{\beta}_1\)): increase in spending per additional item
  • Intercept (\(\hat{\beta}_0\)): baseline spending
  • Use summary(model) to view:
    • Coefficients
    • \(R^{2}\): “How much of what I see in Y is predicted by X?”
    • p-value: “How statistically significant is my model”

See the results on the next slide

Summary of Fitted Model

## 
## Call:
## lm(formula = sales_value ~ quantity, data = transactions)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3134 -1.3355 -0.5855  0.4145 18.4045 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.8199     0.1490  12.214  < 2e-16 ***
## quantity      0.7656     0.1061   7.214 1.08e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.265 on 998 degrees of freedom
## Multiple R-squared:  0.04956,    Adjusted R-squared:  0.04861 
## F-statistic: 52.04 on 1 and 998 DF,  p-value: 1.075e-12

Discussion of Results

Our model is statistically significant, but it’s not practically strong.

Multiple R-squared: 0.04956
Only ~4.96% of the variation in your outcome variable (Y) is explained by your predictor (X).(weak linear relationship)

p-value: 1.075e-12
This is extremely small (almost 0), meaning the relationship between your predictor and the outcome is statistically significant (very unlikely to be due to random chance).

Let’s add more predictors, like income.

3-D Plot: Quantity, Spending, and Income

From this plot alone, it does not seem that there is any correlation between income and spending or quantity purchased at this grocery store.

Residual Plot

The residual plot indicates Linearity:

Residuals are randomly clustered around the red 0 line.

Business Insight & Wrap-Up

Quantity is not a statistically significant predictor of spending.

Next Steps:
  • Use more predictors (e.g. product category, time of day)
  • Try multiple regression