Data Web App for Supermarket Customer Analysis
The skills I demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
Data Science Background
Whenever thereβs business, thereβs a need for customer analysis. To do a project on customer segmentation, I found collected customer data at a supermarket on Kaggle.com. I also built an R Shiny Web App that visualizes the relationships between customer segmentation and behaviors. In the end, I explored the possibilities of using the supermarket data to build a logistic regression model to predict customer behaviors at other businesses.
Data Information and Analysis Methods
The customer segmentation data has four categories: People, Products, Promotion, and Place. I focused on three out of the four categories, excluding Promotion. The category People includes all necessary information about customers such as birth year (where we could generate age feature), marital status, incomes, date since becoming customer, and education level. The Product category contains information about the amount customers spent on each product (six types). The Place category shows the means of purchase for each purchase.
I first ran a correlation code using R on the data after routine data cleaning, such as dropping columns not useful for the project and handling NA values. I then did the pivot_longer feature engineering to prepare the dataset for group_by and summarise functions to use ggplot2 to carry out segmentation visualization before deploying the visualizations to R Shiny.
Additional Data Sets
I drew on two additional datasets from Kaggle.com: Starbucks and Airplane customer data, which I intended to use as a test data set for the prediction model to be created from the customer segmentation data. The concurrent features among the three datasets are age level and income level. They are categorical instead of numerical because one of the datasets shows age at the categorical level, and one of the other datasets shows income at the categorical level. The target I could use for my prediction model is binary customer satisfaction rating, i.e., βsatisfiedβ and βnot satisfied.β
Data Visualization
We can tell from the correlation plot above that customer income strongly correlates with purchase frequencies and spending total in a positive way. In contrast, customer income has a strong negative correlation with the store website visit frequency. Other factors that negatively correlate with store website visits are purchase frequency at the physical store and the amount spent on each product. The negative relation is expected because the data sets differentiate purchases made through the storeβs website and visits to the storeβs website.Β Two factors that have a relatively weak correlation with the customer behaviors are the dates since becoming a customer and the year of birth.
The bar plot above shows that for all products, a customer with a primary education background β the lowest level of education in our dataset β spends the least. The behavior is most apparent in their spending on meat products and wines. While logically speaking, thereβs a positive relationship between the level of education and income, we will also look at income to further verify our findings here.
The scatter plot proves our assumption above that, as income increases, the amount a customer spent in the last two years also increases, especially on wines and meat products.
As seen from the plot above, customers with primary education spend the least frequently among all educational levels at the supermarket, regardless of purchasing means. However, the same type of customers visits the supermarketβs website the most often.
Though people with lower education levels tend to earn less and thus spend less at the supermarket, they do still frequently visit the supermarketβs website. Therefore, it might be a good strategy to post discounted products on the supermarketβs website before regular-priced products to attract more orders from people with primary education.
The bar plot above shows a stable purchasing total at the supermarket regardless of marital status. However, widows tend to spend a little more than people with other matrimonial groups in the last two years.
Similarly, the bar chart above further proves our finding by showing that customersβ marital status doesnβt seem to impact the purchasing frequency at the supermarket. However, we can tell that customers purchase more frequently at the physical store or websites than catalog and deals purchases.
The scatter plot above shows an un-correlated relationship between spending total at the supermarket and customer year of birth. This finding proves our discovery from the correlation matrix above.
The bar plot above shows that customersβ spending interest at the supermarket tends to decrease over time as they become customers. As thereβs no indication of when the data was collected β only that it was first uploaded to Kaggle in 2021, we donβt have a clear time frame for the dataset. Even if 2012 was the first year the supermarket started its business, and there was no record before that year, the trend still holds.
Otherwise, we could expect to see the amount spent (in the past two years as is on the Kaggle.com variable description) increase from 2012 to 2014, because there shouldnβt be any spending in 2012, the spending in 2013 should just be the spending in that one year, and the spending in 2014 should be the sum of the spendings in 2012 and 2013.
To count in possible macroeconomic factors, we look at the plot below. The red line is the consumer price index percentage that reflects the price inflation rate of consumer goods. The blue line is the average wage index percentage that demonstrates the wage increase rate of the employed. The plot tells us thatβ with the exception of 2013, in which the average wage increase rate is slightly below the consumer price inflation rateβ in the range of 2012 to 2014, the average wage increase rates are much higher than the inflation rates. Therefore, our finding that customer spending interest decreases over time makes more sense.
To visualize the above plots on a web page, please visit the Shiny Web App I created.
Explore the Relationship among the Supermarket, Starbucks, and Airplane Passenger datasets
To see if I could use the information from the supermarket dataset to predict customer satisfaction at Starbucks and an airplane company based on the customerβs age level and income level, I converted the three data setsβ income, age, and satisfaction to the same categorical classes to make the data sets comparable. The age factor has four levels: β<20,β β20-29,β β30-39,β and β>=40.β The income factor has three levels: βLow,β βMiddle,β and βHigh,β and satisfaction is binary.
After running the xtabs function, we see that the majority of the customers in the supermarket dataset are satisfied with the services at the supermarket, as shown in the tables above. This fact gives a problem for logistic regression because, even if the model predicts all customers are satisfied, the accuracy score of the model will still be high. That means we would be getting false positives for the customers that are not satisfied.
The summary of the logistic regression model above tells us that only the customer group aged 30-39 and the group aged 40 or older have a p-value lower than the standard threshold of 0.05. That indicates that age level and income level donβt obviously impact customer satisfaction.
Our model's deviance residual plot (above left) shows high deviance, while the influence plot (above right) tells us most values are of high variance, and some are of immense influence. The big circle in the influence plot is an area we would want to avoid because it shows the point, i.e., a case (should be a combination of factor levels in our case) that has a significant residual and high leverage on the model. It can have an adverse effect on the model if the case is changed or excluded.
I also found Pchisq, after dropping Age as an independent variable from the model, is 0.435; McFaddenβs pseudo-R^2 based on the deviance is 0.03, meaning thereβs only 3% of the variability in customer satisfaction appears to be explained by the predictors in the model.
Conclusion & Future Works
- People with primary education tend to spend much less in the supermarket than people with higher education background, but they tend to visit the store website more frequently than people with higher education background.
- The higher the education level, the more people will spend on wines.
- Customerβs purchasing interest at the same store tends to decrease over time.
- Year of birth doesnβt affect peopleβs purchasing habits or spending total at supermarkets.
- The higher the income, the more people spend in the supermarket or the web, especially higher-priced products.
- People purchase in the store or on its site more frequently than via catalog and deals purchase.
- For future work, I plan to look for datasets with a good amount of concurrent numerical and categorical variables for building a model to see if customersβ behaviors can be reasonably predicted from one industry to another. Also, I plan to add more features to the Shiny R web App, such as including a model that allows users to select features and see predictions of customer behaviors.
Author Links