Azure: Sales KPI Data Analysis
Tags: azure, datascience, analytics
Published: 2022-02-08 14:25:01

Goal: Build a model to predict Sales Variable 

[Dataset used](https://docs.google.com/spreadsheets/d/14i1v-q-yM_Vq7NnjaySRfzd-7aOxqApIFtAtp9isFbk/edit?usp=sharing)

## Data Preprocessing: Original Dataset
- 348 rows x 14 columns

143 rows with missing values in total (around 41% of total dataset)

A large proportion of the dataset has missing values, especially the year 2014 where most KPI1 to KPI14 and WebVisits data are missing.

One option was too exclude WebVisits and KPI1 to KPI4 from the analysis completely to keep a larger dataset and the other was to remove the rows with missing values and work with the rest.

Latter was chosen because the WebVisits and KPI data held the most information to model the sales (as seen in section 2)

## Step 1 : Clean missing data
- Remove all rows where there are data points missing, including 8 rows where the Sales data was missing (143 rows removed).
- Remaining dataset: 205 rows and 14 columns. 

## Step 2 : edit metadata
- Make the following variables categorical: - “Year” and “Month”.
 “country” which ranges from 1 to 9
 “language” which ranges from 1 to 6
- Make “new.site” a boolean : 1 if a new website was used and 0 otherwise.

## Step 3 : split data

The dataset is split in a training set and a test set. Since most of the data for the year 2014 has been removed due to missing values, we are left with a training set consisting of the years 2014, 2015 and 2016. The test set set will then be the year 2017.

Training set: 189 rows and 14 columns 
Test set: 16 rows and 14 columns
 
## Regression models
Predictive variables :
- All variables are taken into account
1st approach: Classical approach
- The dataset is split between the training set, including data from year 2014 to 2016. The model is trained using all 13 variables and tested on the year 2017.
2nd approach: Principal component analysis
- We use PCA followed by a linear regression for training the model. The dimensions are reduced to 7 (almost half of the original variables). PCA is concerned with feature extraction and not feature selection which is another technique that will be used later o to dimension reduction.

                   Performance of Models

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez51uciwmx5qt4wyry36.png)

## Decision Trees 
Predictive variables :
- All variables are taken into account
1st approach: Boosted Decision Tree Regression
- This algorithm creates a collection of regression trees using boosting. The dataset is split between the training set, including data from year 2014 to 2016. The model is trained using all 13 variables and tested on the year 2017.
2nd approach: Decision Forest Regression
- The same approach is used with the training and test sets.
 
                   Performance of Models

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co44dl6tlm52ldhzur1d.png)

Among the models, Classic Linear regression performs the best

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbkfzj7e9sephszkqsn5.png)


## Data Preprocessing
## Step 1
- The preprocessing done for the previous part is used in this part as well. The dataset is split in a training set and a test set where the test set is the data for the year 2017.

Training set: 189 rows and 14 columns 
Test set: 16 rows and 14 columns

## Feature Selection
Predictive variables :
- In section 1, all variables were used in the analysis. This probably created some noise in the results, hence the use of Feature Selection is highly justified.

- The number of desired features is set to 6 because the correlation drops from 0.82 to 0.43 when showing that as from sum_local_wiki the variables are not highly correlated with the Sales variable.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p27bkhgegejbpc9hoqqw.png)
 

## Regression Model
The variables used in the models are country, WebVisits, KPI1, KPI2, KPI3 and KPI4. 

## 1st approach: Linear regression
The model performs better than the previous model which used all the variables. 
- In section 1, the coefficient of regression was 0.89 but in this model with feature selection the coefficient of regression is 0.91.
- The relative squared error is also lower here with a value of 0.09.

## Decision Tree models
 2 approaches:
  - Approach 1 : Boosted Decision Tree Regression
  - Approach 2 : Decision Forest Regression

- The variables used in the models are country, WebVisits, KPI1, KPI2, KPI3 and KPI4.

- Decision Forest Regression performs better when feature selection is used. The coefficient of determination rises from 0.83 to 0.88.

- Boosted Decision Tree Regression which was the worse model in the first part performs even worse with feature selection suggesting that it is not the right model for our data.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8bj23kaily67l3xkqip.png)

**Conclusion: Linear regression with Feature Selection performs the best out of all the models tested, with and without feature selection.**