
---
title: "Cluster Clients"
output:
  html_document:
    highlight: espresso
    theme: flatly
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(dplyr)
library(readr)
library(stringr)
library(ggplot2)
library(reshape2)

```


# Idea:
From the the table `cliente_tabla.csv` we can cluster clients by name. The basic idea is that different kind of businesses have different demand and return values.

Just by browsing through the table we can see that there are fast food stores (MC Donalds, Burger King) and larger supermarket chains (Walmart). 

Lets see if they differ!




```{r }
#Load Data
cliente_tabla <- read_csv("../input/cliente_tabla.csv")
producto_tabla <- read_csv("../input/producto_tabla.csv")
train <- read_csv("../input/train.csv")
train_4 <- filter(train, Semana == 4) %>% select(Cliente_ID,Producto_ID,Venta_uni_hoy,Dev_uni_proxima)
rm(train)
```
First lets make all letter upper case.
```{r}
cliente_tabla$NombreCliente <- toupper(cliente_tabla$NombreCliente)
```
## Big chain supermarkets

Lets see if we find Walmart:
```{r}

filter(cliente_tabla,str_detect(cliente_tabla$NombreCliente, "WAL MART| WALMART "  ) == TRUE)
```
**Comercial Mexicana** should be another big supermarket chain
```{r}

filter(cliente_tabla,str_detect(cliente_tabla$NombreCliente, "COMERCIAL MEXICANA"  ) == TRUE)
```
## Fast Food Stores
We can also cluster fast food stores. We focus on Mc Donalds and Pollo Feliz
```{r}

filter(cliente_tabla,str_detect(cliente_tabla$NombreCliente, "MC DONALDS|POLLO FELIZ"  ) == TRUE)
```

# Data
Now lets explore what kind of products they order

## Products of big chain stores
First we delete duplicates
```{r}

cliente_tabla %>%  group_by(Cliente_ID) %>% summarize(n=n()) %>% filter(n==1) %>% dplyr::select (Cliente_ID) %>% 
  left_join(cliente_tabla, by = "Cliente_ID") -> cliente_clean

#Mark fast food and chain stores
cliente_clean$chain <- str_detect(cliente_clean$NombreCliente, "WAL MART| WALMART")
cliente_clean$fastfood <- str_detect(cliente_clean$NombreCliente, "MC DONALDS|POLLO FELIZ")
```


The top Products for the chain stores are:
```{r}
train_4 <- left_join(train_4,cliente_clean,by="Cliente_ID")

train_4 %>% 
  filter(chain == TRUE) %>%
  group_by(Producto_ID) %>%
  summarize(n=n())%>%
  arrange(desc(n))  %>% top_n(10) -> top10_products_chain


producto_tabla %>% inner_join(top10_products_chain, by = "Producto_ID") %>%
  arrange(desc(n))  
  
```
## Products of fast food stores

The top Products for fast food stores:
```{r}


train_4 %>% 
  filter(fastfood == TRUE) %>%
  group_by(Producto_ID) %>%
  summarize(n=n())%>%
  arrange(desc(n))  %>% top_n(10) -> top10_products_fastfood


producto_tabla %>% inner_join(top10_products_fastfood, by = "Producto_ID") %>%
  arrange(desc(n))  
  
```
> So we already see that the top products are different. Chain stores demand packed bread for direct retail to the customer and fast food stores demand buns (bollo).

## Demand and return
Since we have to predict adjusted demand its interesting to see if this stores differ in demand and return rates.

### Chain stores
```{r}

# We use the top10_products_chain and group over Products

producto_tabla %>% 
  inner_join(top10_products_chain, by = "Producto_ID") %>%
  inner_join(train_4, by = "Producto_ID") %>%
  group_by(NombreProducto ) %>%
  summarize(mean_demand_t0= mean(Venta_uni_hoy),
            sd_demand_t0 = sd(Venta_uni_hoy),
            mean_return_t1= mean(Dev_uni_proxima ),
            sd_return_t1 = sd(Dev_uni_proxima )) ->stats_chain


 stats_chain %>% select(mean_demand_t0,mean_return_t1)
 
```

### Fast food stores
```{r}

# We use the top10_products_fastfood and group over Products

producto_tabla %>% 
  inner_join(top10_products_fastfood, by = "Producto_ID") %>%
  inner_join(train_4, by = "Producto_ID") %>%
  group_by(NombreProducto ) %>%
  summarize(mean_demand_t0= mean(Venta_uni_hoy),
            sd_demand_t0 = sd(Venta_uni_hoy),
            mean_return_t1= mean(Dev_uni_proxima ),
            sd_return_t1 = sd(Dev_uni_proxima )) -> stats_fastfood
  
stats_fastfood %>% select(mean_demand_t0,mean_return_t1)
```


### Plotting demand/ return
```{r}
plot_data <- melt(stats_fastfood, id="NombreProducto" )
plot_data <- filter(plot_data, variable == "mean_demand_t0" | variable == "mean_return_t1"  )
ggplot(plot_data, aes(x = NombreProducto, y =value, fill = variable)) +
  geom_bar(stat = "identity") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+ 
    coord_flip()

```

