EDA:Retail Analysis

Author

Jon Herrick

Retail Analysis Project

This is the first part in my retail data analysis project. In this project, I will analyze the the company’s retail sales across their global footprint. I do not know anything about this data or the company, so I will use this analysis to learn about and process the data for analysis. I will walk through my decision making process on what to do with missing data and any abnormalities.

Exploratory Data Analysis

Before I can do any of the retail analysis, I need to understand the structure of the data. I will check for missing data and view the descriptive statistics.

Before I begin, I want to communicate what I found before digging into the EDA work.

It is my experience, most people do not understand the impact of their decisions around data. Raw data in the real world is inconsistent and unstructured. In research I did not document, I looked at the number of product descriptions associated with the stock_codes and found a huge mess.

Theoretically, there should be a 1 to 1 relationship between descriptions and their codes. There may be differences captured in size and color, but the description would be the same. What I found was a many to many relationship for many items. Some of the descriptions were filled in with notes documenting wrong item codes and other issues along with legitimate descriptions.

This is concerning. If I faced this in my job, it would be a roadblock and prevent further work until extensive data engineering hours were spent to fix the issue or apply standards that would clean the data moving forward.

For the purpose of this project, I decided to do the best I could with the time I had and proceed as if I did not find this issue.

OK, on with the analysis.

Missing Data

Below is a profile table of the retail data with the features, number of missing data points and the percentage missing. There are only two features with missing data; description is missing 0.004% and customer_id are missing 22.8%.

The missing customers are excessive and needs to be explored further.

I wonder how much of the missing description data is also missing customer id? I might be able to remove all of those records. I will look into that after I get a better idea of the missing customer data.

# A tibble: 12 × 3
   feature      num_missing pct_missing
   <fct>              <int>       <dbl>
 1 invoice                0     0      
 2 stock_code             0     0      
 3 description         4382     0.00411
 4 quantity               0     0      
 5 invoice_date           0     0      
 6 price                  0     0      
 7 customer_id       243007     0.228  
 8 country                0     0      
 9 date_invoice           0     0      
10 trans_type             0     0      
11 year                   0     0      
12 month                  0     0      

Feature Engineering

Before I do this analysis, I am going to engineer two new columns that label the missing customer_ids and product descriptions. I will create a new data frame so that I can track these customers throughout the analysis.

df_na <-df %>% 
  mutate(cust_na = case_when( is.na(customer_id) ~ "NO_ID",
                            !is.na(customer_id) ~ "HAS_ID"), 
         product_na = case_when(is.na(description) ~ "NO_DESC",
                             !is.na(description) ~ "HAS_DESC" ),
         customer_id = as.character(customer_id)
         )

Initial Questions

Below are some initial questions I have as I begin digging into this data.

  • Are the missing customers concentrated to one country?

  • What is the distribution across transaction types?

  • Is there a time period that stands out?

  • Is there any correlation between the missing product descriptions and customers?

  • Can I use stock_code to fill the missing product descriptions?

  • Are there any suggestions I can make to fix any data issues?

Customer ID

The bar chart below shows the percent of missing customers by country.

In total, there are 14 countries with incomplete customer data. One data point is labeled “Unspecified”, leaving me to believe this column of data has already been cleaned up.

Hong Kong and Bermuda are missing 100% of their customer data, and Bahrain is missing 53%. 11 other countries are contributing to the missing customers, missing between 27.2% to 0.9% of their customer data.

Transaction Type

The missing customers are spread out between transaction types. This does not seem to offer any insight into the missing customer_ids.

Year

There does not seem to be a particular year that wiped out the customer data. I will continue on to checking the months.

Month

I chose to look at the months independent of the years because I was curious if the holiday seasons caused extra load on the system causing a failure in the process somewhere. While December has a high percentage, I don’t think seasonal server load is the reason. If it were, I would expect to see November with a higher percentage.

Just to be safe, I will recreate this report with year/month to see if maybe there is an issue that happened at a particular time.

Month by Year

Looking at the monthly by year chart, there is a seasonal pattern that confirms what is seen in the monthly chart above. There are consistently a higher percentage of unknown customers during the same months year over year.

Anomaly Detection

To be sure, I plotted a time series chart to test for any statistical anomalies.

There are no anomalies detected in the year/month. The fact that there are no anomalies detected tells me that there were not any circumstances that caused the customer_ids to go missing. There must be something in the retail process that is causing this.

Product Description

In this next section, I will be looking at the records missing the product description.

Recall the first table in this analysis, description was missing .04% of the total column. The first order of business is to see how that number is broken out by country.

We can see in the bar chart below, the entire 0.04% is in the United Kingdom.

Product Description and Customer ID

In the next cut of data, I will look into the product descriptions by counting the records with and without a description, crossed with the customer id groups to quickly see if the missing data is connected some how.

cust_na product_na n
HAS_ID HAS_DESC 824364
NO_ID HAS_DESC 238625
NO_ID NO_DESC 4382


Sure enough, all of the records with missing product descriptions are also missing the customer ids as marked by cust_na = ‘NO_ID’.

Transaction Type

The distribution of missing products across transaction type tells me that the missing descriptions are not because of one particular type and really does not provide any further explanation.

trans_type product_na n
Purchase HAS_DESC 1042728
Purchase NO_DESC 1693
Return HAS_DESC 20261
Return NO_DESC 2689


Year

Below, year and month to see if there is a concentration of periods with null descriptions. The data seems to say that the issue is not as prevalent in 2011 ans it was in the years prior.

Month

I was curious about how the prices of the null items were distributed so I grouped the products with the null descriptions by price to see if anything stood out and sure enough, all 4,382 records had a $0 price.

product_na cust_na price n
NO_DESC NO_ID 0 4382


To recap what I’ve discovered in the missing product data:

  • All of the items are isolated to the United Kingdom
  • All of the missing descriptions are also missing customer_id
  • The percentage of transactions that are missing both product description and customer id are not significant
  • 100% of the items have a price of $0

Considering these findings, I will be excluding these records from my analysis. I will do this next and continue my analysis with the missing customers.

What to do with these missing customers?

There are 22.8% of the customers are missing their ids. That is too many records to throw away. I think the best case for this analysis is to keep their transactions because they are valuable for the product and sales analysis.

Next Steps

In the next steps of my analysis, I will analyze the customer data.

I will look at the typical customer metrics, including sales, transactions and units sold across all countries. I can look at the performance of the countries at a later time.