# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     name: ir
# ---

# + [markdown] colab_type="text" id="view-in-github"
# <a href="https://colab.research.google.com/github/Ciiku-Kihara/PCA-ANOMALY-DETECTION-AND-ASSOCIATION-ANALYSIS-WITH-R/blob/main/Anomaly_Detection_with_R.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="8CpsgpJbLdQz"
# # Anomaly Detection with R
#
#
#

# + [markdown] id="33NE-8v8CmsW"
# ## a) Defining the analysis question
#
# You are a Data analyst at Carrefour Kenya and are currently undertaking a project that will inform the marketing department on the most relevant marketing strategies that will result in the highest no. of sales (total price including tax).
#
# I am expected to detect any anomalies in sales

# + [markdown] id="wZSZp22JCm_d"
# ## b) Defining the metric for success
#
# Be able to effectively identify anomalies in sales in the data provided

# + [markdown] id="HVy3K5fwCng1"
# ## c) Understanding the context
#
# Carrefour was launched in the region in 1995 by UAE-based Majid Al Futtaim, which is the exclusive franchisee to operate Carrefour in over 30 countries across the Middle East, Africa, and Asia, and fully owns the operations in the region. Today, Majid Al Futtaim operates over 320 Carrefour stores in 16 countries, serving more than 750,000 customers daily and employing over 37,000 colleagues.
#
# Carrefour operates different store formats, as well as multiple online offerings to meet the growing needs of its diversified customer base. In line with the brand’s commitment to provide the widest range of quality products and value for money, Carrefour offers an unrivalled choice of more than 500,000 food and non-food products, and a locally inspired exemplary customer experience to create great moments for everyone every day.

# + [markdown] id="9jEWKlBsCnu1"
# ## d) Recording the experimental design
#
# - Problem Definition
# - Anomaly Detection
# - Provide insights based on my analysis
# - Provide recommendations
#

# + [markdown] id="xVfDopdICn9E"
# ## e) Data Relevance
#
# The dataset adequate to apply anomaly detection techniques

# + [markdown] id="LukZafoZCoLo"
# # 2. Installing and importing relevant libraries

# + colab={"base_uri": "https://localhost:8080/"} id="UlqxFNG7JqqB" outputId="314a42d3-ec81-439a-86c6-239a3e07011a"
# Installing the necessary packages

install.packages(c("anomalize", "tidyverse", "tibbletime", "timetk"))


# + colab={"base_uri": "https://localhost:8080/"} id="UtGcf6GNKqIz" outputId="768642f4-9f07-445a-869e-e503310b455c"
# Loading the libraries

library(anomalize)
library(tidyverse)
library(tibbletime)
library(timetk)

# + [markdown] id="YQw5fHAGHAUx"
# # 3. Loading and checking the data

# + id="XDFYhbe8LCEY"
# Reading the dataset

ano = read.csv("Anomaly.csv")

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="Yafar7kbLB4Q" outputId="caaf19ff-2951-44e7-db4b-2d30b6446147"
# Loading the first 6 rows of the dataset

head(ano)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="qK7R1ruoLBw9" outputId="83c9ad5c-13c8-4f7a-a159-c25d06eecbe7"
# Obtaining the dimensions of the dataset

dim(ano)

# The dataset has 1000 rows and 2 columns

# + colab={"base_uri": "https://localhost:8080/"} id="0xF1Db1yLBgF" outputId="31be841f-6fc5-4c98-f7d4-299db3725c3d"
# Getting a glimpse of the structure of the dataset

str(ano)

# Of the 2 columns, 1 is an integer and the other is type character

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="_ONJeDqwRFNR" outputId="f9ff4c3b-0e4e-4528-c616-178bcdba6c9a"
# Checking for duplicates

sum(duplicated(ano))

# There are no duplicated records in the dataset

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="8MbyHVh9RDX1" outputId="5d094d28-b906-4d9b-9682-5fe25a51b5a3"
# Checking for null values

colSums(is.na(ano))

# There are no null values in the dataset

# + colab={"base_uri": "https://localhost:8080/", "height": 530} id="KysO_YLF7VFl" outputId="64c8f5cc-e696-406e-ba36-b866a67395e4"
# Analysis of the sales column

options(repr.plot.width = 8, repr.plot.height = 8)
p = ano %>% ggplot(aes(x = Sales))

p + geom_histogram(color="darkblue", fill="lightblue" ) +
    labs(title = "Histogram of Sales", x = "Sales", y = "Count") +
    theme(axis.text = element_text(size=18),
          axis.title = element_text(size = 18),
          plot.title = element_text(hjust = 0.5, size = 22))

# + [markdown] id="paE0VtQoJE0b"
# # 4. Implement the solution

# + id="kdEat5jVIogS"
# Changing the date to date column'

ano$Date <- as.Date(ano$Date,format = "%m/%d/%y")

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="yOJbrJ8iJMne" outputId="31a53206-bd8c-4403-d304-ea90366f25c6"
head(ano)

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="w30kGba3F99n" outputId="0a5cbf5e-9bb3-4a55-bf74-376b86a8a228"
# Ordering the date column

ano1  = ano[order(ano$Date), ]     
head(ano1) 

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="mKKA2zycLY9K" outputId="c0da1de6-2a09-4970-ba0a-dd7455e8d355"
# Aggreagate the Sales by date

ano2 = aggregate(ano1["Sales"], by=list(ano1$Date), sum)
head(ano2)

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="sXnPMF8XJEvF" outputId="63df29d2-93a7-48e8-c0f9-bb1dc4ac55c0"
# Converting the aggregated product into a tibble

df <- as_tibble(ano2)
head(df)

# + colab={"base_uri": "https://localhost:8080/"} id="ahrYsJJR8iE0" outputId="fe5de4e6-86f1-4478-dc32-345255d628f5"
# Building the model using anomalize and increasing alpa to 0.25

df_anomalized <- df %>%
    time_decompose(Sales, merge = TRUE) %>%
    anomalize(remainder, alpha = 0.25) %>%
    time_recompose()
    

# + colab={"base_uri": "https://localhost:8080/"} id="OQFg-wtA8h7F" outputId="acd1c2aa-5e6e-417c-d0cf-1bad2346a8ca"
# Looking at the structure of the new dataset

str(df_anomalized)

# + colab={"base_uri": "https://localhost:8080/", "height": 617} id="siNQPN1X8hwM" outputId="1e6289d3-954a-412d-84ee-f373f18cc3b0"
# Plotting the amomalies

options(repr.plot.width = 10, repr.plot.height = 10)
df_anomalized %>% plot_anomalies(time_recomposed = TRUE) +
    labs(title = "Anomalies Detected", x = "Date", y = "Observed") + theme(axis.text = element_text(size=18),
          axis.title = element_text(size = 18),
          plot.title = element_text(hjust = 0.5, size = 22),
          legend.title = element_text(size=18),
          legend.text = element_text(size=18))

# + colab={"base_uri": "https://localhost:8080/", "height": 491} id="sgtcHcdZ8hka" outputId="a8ce7c83-c33c-4602-f25e-9d9fb0ce84b3"
# Extracting the anomalous data point

df %>%
    time_decompose(Sales, merge = TRUE) %>%
    anomalize(remainder, alpha = 0.25) %>%
    time_recompose() %>%
    filter(anomaly == 'Yes')

# + [markdown] id="fRXVjEilJV4S"
# # 5. Insights
#
# The insights that can be made from the analysis are as follows:
#
# - Very low sales were made on the 13th of February and 22nd of January that year
# - Really high sales were made on 9th March, 7th February and 14th of March.
#

# + [markdown] id="4afpprHzJe9v"
# # 6. Recommendations
#
# In light of the above insights, the following recommendations can be made:
#
# - The company should look into finding out why the sales were so high in the days mentioned. For example, high sales on the 7th of February would be because of early Valentine's shopping. 
#
# - Also given that its the year that COVID hit the globe, high sales on those days may have caused by a mass rush to stock up on supplies as people anticipate a lockdown
#
# - Given that the 13th of February is just before Valentine's day, the company can introduce special offers to increase sales on that day. The company should also look into why there were low sales on the 22nd of January as  well.

StarcoderdataJupyterScriptsDedupFiltered