---
title: 'ANALYSIS - Favorita Grocery Sales Forecasting'
author: "Rahul Anand"
date: '`r Sys.Date()`'
output:
  html_document:
    number_sections: false
    fig_caption: true
    toc: true
    fig_width: 7
    fig_height: 7
    theme: lumen
    highlight: pygments
    code_folding: hide
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
#loc <- '/Users/ranand/Desktop/kaggle/favorita-grocery-sales-forecasting/'
loc <- '../'
```

## PREPARATIONS {.tabset .tabset-fade}
Load the required Data/Libraries

### LIBRARIES
Load all the Libraries in the shell
```{r lib, message = FALSE}
library(ggplot2)
library(data.table)
library(lubridate)
library(plotly)
library(shiny)
```

### DATA
Load all the data in the RAM
```{r fread, message=FALSE}
# "loc" is working directory "../"
holidays_events   <- fread(paste0(loc,'input/holidays_events.csv'), showProgress = FALSE)
items             <- fread(paste0(loc,'input/items.csv'), showProgress = FALSE)
oil               <- fread(paste0(loc,'input/oil.csv'), showProgress = FALSE)
stores            <- fread(paste0(loc,'input/stores.csv'), showProgress = FALSE)
transactions      <- fread(paste0(loc,'input/transactions.csv'), showProgress = FALSE)
train             <- fread(paste0(loc,'input/train.csv'), showProgress = FALSE)
test              <- fread(paste0(loc,'input/test.csv'), showProgress = FALSE)

train <- train[sample(.N, .N/100),]
```

### CLEAN
Change all the Dates in date format
```{r clean, message=FALSE}
holidays_events[, `:=`(date = as.Date(fast_strptime(date, "%Y-%m-%d")))]
oil[, `:=`(date = as.Date(fast_strptime(date, "%Y-%m-%d")))]
transactions[, `:=`(date = as.Date(fast_strptime(date, "%Y-%m-%d")))]
train[, `:=`(date = as.Date(fast_strptime(date, "%Y-%m-%d")))]
train[, `:=`(month = month(date), wod = weekdays(date, T), year = year(date))]
train[, wod:=factor(wod, levels = c('Mon','Tue','Wed','Thu','Fri','Sat','Sun'))]
test[, `:=`(date = as.Date(fast_strptime(date, "%Y-%m-%d")))]
test[, `:=`(month = month(date), wod = weekdays(date, T), year = year(date))]
test[, wod:=factor(wod, levels = c('Mon','Tue','Wed','Thu','Fri','Sat','Sun'))]
```

### FULL_DATA
Join all data.tables to get full Data
```{r join, message=FALSE}
setkey(items, item_nbr)
setkey(oil, date)
setkey(stores, store_nbr)
setkeyv(transactions, c('date','store_nbr'))
fullData_train <- merge(x=train, y=items, by='item_nbr', all.x=TRUE)
fullData_train <- merge(x=fullData_train, y=oil, by='date', all.x=TRUE)
fullData_train <- merge(x=fullData_train, y=stores, by='store_nbr', all.x=TRUE)
fullData_train <- merge(x=fullData_train, y=transactions, by=c('date','store_nbr'),all.x=TRUE)
fullData_test  <- merge(x=test, y=items, by='item_nbr', all.x=TRUE)
fullData_test  <- merge(x=fullData_test, y=oil, by='date', all.x=TRUE)
fullData_test  <- merge(x=fullData_test, y=stores, by='store_nbr', all.x=TRUE)
fullData_test  <- merge(x=fullData_test, y=transactions, by=c('date','store_nbr'),all.x=TRUE)
```

## BASE DATA {.tabset .tabset-fade}
Summary of All the Data

### TRAIN
```{r, message = FALSE}
head(train)
str(train)
```

### HOLIDAYS
```{r, message = FALSE}
head(holidays_events)
str(holidays_events)
summary(holidays_events)
```

### ITEMS
```{r, message = FALSE}
head(items)
str(items)
summary(items)
```

### OIL
```{r, message = FALSE}
head(oil)
str(oil)
summary(oil)
```

### STORES
```{r, message = FALSE}
head(stores)
str(stores)
summary(stores)
```

### TRANSACTIONS
```{r, message = FALSE}
head(transactions)
str(transactions)
summary(transactions)
```

### TEST
```{r, message = FALSE}
head(test)
str(test)
```

## TRAIN DATA (Features) {.tabset .tabset-fade}
Basic Analysis 

### MONTH_YEAR
```{r, message = FALSE}
plotData <- train[, .(mean_sales=mean(unit_sales)), by=list(month, year)]
p <- ggplot(plotData, aes(x = as.factor(month), y = mean_sales)) + 
  geom_line(aes(color = as.factor(year), group = as.factor(year))) + 
  xlab('Month') + guides(color=guide_legend("Year"))
ggplotly(p)
```

### WEEK_MONTH
```{r, message = FALSE}
plotData <- train[, .(mean_sales=mean(unit_sales)), by=list(month, wod)]
p <- ggplot(plotData, aes(x = as.factor(month), y = wod)) + 
  geom_tile(aes(fill=mean_sales)) + xlab('Month') + ylab('Year') + 
  scale_colour_brewer(palette = "Dark2")
ggplotly(p)
```

### STORE
```{r, message = FALSE}
plotData <- train[, .(mean_sales=mean(unit_sales)), by=list(store_nbr, year)]
p <- ggplot(plotData, aes(x = reorder(as.factor(store_nbr),-mean_sales), y = mean_sales)) + 
  geom_col(aes(fill = as.factor(store_nbr))) + 
  facet_wrap(~year) + xlab('Store') + theme(legend.position = 'none') + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### ITEM
```{r, message = FALSE}
plotData <- train[, .(mean_sales=mean(unit_sales)), by=list(item_nbr, year)]
plotData <- head(plotData[order(-mean_sales)], 100)
p <- ggplot(plotData, aes(x = reorder(as.factor(item_nbr),-mean_sales), y = mean_sales)) + 
  geom_col(aes(fill = as.factor(item_nbr))) + 
  facet_wrap(~year) + xlab('Item') + theme(legend.position = 'none') +
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### PROMOTION
```{r, message = FALSE}
plotData <- train[, .(mean_sales=mean(unit_sales)), by=list(onpromotion, year)]
p <- ggplot(plotData, aes(x = reorder(onpromotion,-mean_sales), y = mean_sales)) + 
  geom_col(aes(fill = onpromotion)) + 
  facet_wrap(~year) + xlab('onpromotion') + theme(legend.position = 'none') +
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

## ITEM FEATURES {.tabset .tabset-fade}
### ITEM_FAMILY
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(family, year)]
p <- ggplot(plotData, aes(x = reorder(family,-mean_sales), y = mean_sales)) + 
  geom_col(aes(fill = family)) + 
  facet_wrap(~year) + xlab('Item Family') + theme(legend.position = 'none') +
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### ITEM_CLASS
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(class, year)]
plotData <- head(plotData[order(-mean_sales)], 100)
p <- ggplot(plotData, aes(x = reorder(class,-mean_sales), y = mean_sales)) + 
  geom_col(aes(fill = class)) + facet_wrap(~year) + xlab('Item Class') + 
  theme(legend.position = 'none') + theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### PERISHABLE
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(perishable, year, wod)]
p <- ggplot(plotData, aes(x = wod, y = mean_sales)) + 
  geom_col(aes(fill = perishable, group = perishable), position = 'dodge') + 
  facet_wrap(~year) + theme(legend.position = 'none') + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

## STORE FEATURES {.tabset .tabset-fade}
### STORE_CLUSTER
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(cluster, wod)]
p <- ggplot(plotData, aes(x = wod, y = mean_sales)) + 
  geom_col(aes(fill = as.factor(wod)), position = 'dodge') + 
  facet_wrap(~cluster) + theme(legend.position = 'none') + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### STORE_TYPE
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(type, wod)]
p <- ggplot(plotData, aes(x = wod, y = mean_sales)) + 
  geom_col(aes(fill = as.factor(wod)), position = 'dodge') + 
  facet_wrap(~type) + theme(legend.position = 'none') + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

### STORE_STATE
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales)), by=list(state, wod, onpromotion)]
p <- ggplot(plotData, aes(x = wod, y = mean_sales)) + 
  geom_col(aes(fill = as.factor(onpromotion)), position = 'dodge') + 
  facet_wrap(~state) + theme(legend.position = 'none') + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=4))
ggplotly(p)
```

## COMPARE DISTRUBUTON in SALES {.tabset .tabset-fade}
### WOD
```{r, message = FALSE}
varName = 'wod'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

### TYPE
```{r, message = FALSE}
varName = 'type'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

### ONPROMOTION
```{r, message = FALSE}
varName = 'onpromotion'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

### FAMILY
```{r, message = FALSE}
varName = 'family'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

### PERISHABLE
```{r, message = FALSE}
varName = 'perishable'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

### STATE
```{r, message = FALSE}
varName = 'state'
plotData  <- fullData_train[unit_sales>= -25 & unit_sales <= 25]
reqString <- paste0('as.factor(', varName, ')')
p <- ggplot(plotData, aes(x = unit_sales)) + theme(legend.position = 'none') +
  geom_density(aes_string(fill=reqString, color=reqString), alpha = 0.4)
ggplotly(p)
```

## OIL PRICES {.tabset .tabset-fade}
### Variation
```{r, message = FALSE, warning=FALSE}
plotData <- oil; plotData[, year:=year(date)]
p <- ggplot(plotData, aes(x = date, y=dcoilwtico)) + facet_wrap(~year, scales = 'free') +
  geom_line(color='red') + geom_smooth(method = "loess", color = "grey", span = 1/5) +
  theme(axis.text.x = element_blank())
ggplotly(p)
```

### SALE-vs-OIL
```{r, message = FALSE}
plotData <- fullData_train[, .(mean_sales=mean(unit_sales, na.rm = T)), by = list(oilP = cut(dcoilwtico, 10))]
p <- ggplot(plotData, aes(x = oilP, y=mean_sales)) + 
  geom_col(aes(fill=mean_sales)) + scale_colour_brewer(palette = "Spectral") + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1, size=6))
ggplotly(p)
```

