Introduction

This document outlines the process of developing a linear model to predict sales for Walmart, interpreting the significance of three predictors in the model, and evaluating the model’s performance using the \(R^2\) metric on validation data.

Data Preparation

First, we load the necessary libraries and import the data.

library(tidyverse)
library(olsrr)
Walmart <- read_csv("Walmart.csv")
set.seed(123)

Data Prep for Modeling

Summary: The variables store and holiday flag are continous, but should be categorical

summary(Walmart)
##      Store        Date            Weekly_Sales      Holiday_Flag    
##  Min.   : 1   Length:6435        Min.   : 209986   Min.   :0.00000  
##  1st Qu.:12   Class :character   1st Qu.: 553350   1st Qu.:0.00000  
##  Median :23   Mode  :character   Median : 960746   Median :0.00000  
##  Mean   :23                      Mean   :1046965   Mean   :0.06993  
##  3rd Qu.:34                      3rd Qu.:1420159   3rd Qu.:0.00000  
##  Max.   :45                      Max.   :3818686   Max.   :1.00000  
##   Temperature       Fuel_Price         CPI         Unemployment   
##  Min.   : -2.06   Min.   :2.472   Min.   :126.1   Min.   : 3.879  
##  1st Qu.: 47.46   1st Qu.:2.933   1st Qu.:131.7   1st Qu.: 6.891  
##  Median : 62.67   Median :3.445   Median :182.6   Median : 7.874  
##  Mean   : 60.66   Mean   :3.359   Mean   :171.6   Mean   : 7.999  
##  3rd Qu.: 74.94   3rd Qu.:3.735   3rd Qu.:212.7   3rd Qu.: 8.622  
##  Max.   :100.14   Max.   :4.468   Max.   :227.2   Max.   :14.313

summary: now with categorical variable for factors and a rando variable for splitting the data

# Splitting data into training and validation sets
temp0 <- Walmart %>%
  mutate(random = runif(nrow(Walmart)),
         Store = as.factor(Store),
         Holiday_Flag = as.factor(Holiday_Flag))

summary(temp0)
##      Store          Date            Weekly_Sales     Holiday_Flag
##  1      : 143   Length:6435        Min.   : 209986   0:5985      
##  2      : 143   Class :character   1st Qu.: 553350   1: 450      
##  3      : 143   Mode  :character   Median : 960746               
##  4      : 143                      Mean   :1046965               
##  5      : 143                      3rd Qu.:1420159               
##  6      : 143                      Max.   :3818686               
##  (Other):5577                                                    
##   Temperature       Fuel_Price         CPI         Unemployment   
##  Min.   : -2.06   Min.   :2.472   Min.   :126.1   Min.   : 3.879  
##  1st Qu.: 47.46   1st Qu.:2.933   1st Qu.:131.7   1st Qu.: 6.891  
##  Median : 62.67   Median :3.445   Median :182.6   Median : 7.874  
##  Mean   : 60.66   Mean   :3.359   Mean   :171.6   Mean   : 7.999  
##  3rd Qu.: 74.94   3rd Qu.:3.735   3rd Qu.:212.7   3rd Qu.: 8.622  
##  Max.   :100.14   Max.   :4.468   Max.   :227.2   Max.   :14.313  
##                                                                   
##      random         
##  Min.   :0.0000653  
##  1st Qu.:0.2527042  
##  Median :0.4945870  
##  Mean   :0.4980825  
##  3rd Qu.:0.7453083  
##  Max.   :0.9999414  
## 
trainlm <- temp0 %>% 
  filter(random < 0.7) %>% 
  select(-random)

validationlm <- temp0 %>% 
  filter(random >= 0.7) %>% 
  select(-random)

data split 70-30 into training and validation sets

  • training data has r nrow(trainlm) observations
  • the validation data has r nrow(validationlm) observations

linear model to predict sales

  • linear model created using all possible predictors except for “date”.
  • this model allowed for the understanding of then individual affects of each predictor
  • then a model with all possible interactions is created again also excluding “date”
  • the backwards selection was created using alpha=.01
# Create a linear model
model1 <- lm(Weekly_Sales ~ .-Date , data = trainlm)
# Backward selection process on model with interactions
bckwd <- ols_step_backward_p(model1  , details = T , prem = 0.01)
## Backward Elimination Method 
## ---------------------------
## 
## Candidate Terms: 
## 
## 1. Store 
## 2. Holiday_Flag 
## 3. Temperature 
## 4. Fuel_Price 
## 5. CPI 
## 6. Unemployment 
## 
## 
## Step   => 0 
## Model  => Weekly_Sales ~ Store + Holiday_Flag + Temperature + Fuel_Price + CPI + Unemployment 
## R2     => 0.918 
## 
## Initiating stepwise selection... 
## 
## 
## No more variables to be removed.
model2 <- bckwd$model

Calculate R^2

validationlm$sales_predicted <- predict(model2, validationlm)
mean_sales_val <- mean(validationlm$Weekly_Sales)

vallmR2 <- validationlm %>% 
  mutate(res2 = (Weekly_Sales - sales_predicted)^2, 
         tot2 = (Weekly_Sales - mean_sales_val)^2)

r2 <- 1 - sum(vallmR2$res2) / sum(vallmR2$tot2)
r2
## [1] 0.9234099

On the training data of r^2 of .9122 was obtained on the validation data set

Interpret 3 Predictors

-Holiday_Flag1 75352.5 when there is a holiday sales will increase -Fuel_price -42007.4 fuel prices go up Sales will decrease -CPI 2692.5 CPI goes up Sales will increase