Clustering-European Employment Data

Summary

Data

Percentage of population employed in different industries in Europian countries during 1979. The purpose of examining this data is to get insights into patterns of employment (if any) amongst European countries in 1970s.

Variable Names

  1. Country: Name of country
  2. Agr: Percentage employed in agriculture
  3. Min: Percentage employed in mining
  4. Man: Percentage employed in manufacturing
  5. PS: Percentage employed in power supply industries
  6. Con: Percentage employed in construction
  7. SI: Percentage employed in service industries
  8. Fin: Percentage employed in finance
  9. SPS: Percentage employed in social and personal services
  10. TC: Percentage employed in transport and communications

K-Means Clustering

Loading Libraries

library(data.table)
library(gridExtra)
library(factoextra)
library(tidyverse)
library(fpc)
library(knitr)

Input Data and test-train split

data <- fread("europeanJobs.txt")

set.seed(13433518)

# randomly select 80% of the data
index <- sample(nrow(data), size = 0.8*nrow(data))
subset <- data[index,]

Clustering

Visualizing clusters with different

k2 <- kmeans(subset[,-1], centers = 2, nstart = 25)
k3 <- kmeans(subset[,-1], centers = 3, nstart = 25)
k4 <- kmeans(subset[,-1], centers = 4, nstart = 25)
k5 <- kmeans(subset[,-1], centers = 5, nstart = 25)

# plots to compare
p1 <- fviz_cluster(k2, geom = "point",  data = subset[,-1]) + ggtitle("k = 2")
p2 <- fviz_cluster(k3, geom = "point",  data = subset[,-1]) + ggtitle("k = 3")
p3 <- fviz_cluster(k4, geom = "point",  data = subset[,-1]) + ggtitle("k = 4")
p4 <- fviz_cluster(k5, geom = "point",  data = subset[,-1]) + ggtitle("k = 5")

grid.arrange(p1, p2, p3, p4, nrow = 2)

plotcluster(subset[,-1], k2$cluster)

plotcluster(subset[,-1], k3$cluster)

plotcluster(subset[,-1], k4$cluster)

plotcluster(subset[,-1], k5$cluster)

Determining number of clusters

Plotting Within Cluster Sum of Squares vs Cluster Size. Cluster size = 3 seems to be appropriate.

#within group sum of squares method
wss <- (nrow(subset[,-1])-1)*sum(apply(subset[,-1],2,var))
for (i in 2:12) wss[i] <- sum(kmeans(subset[,-1],
                                     centers=i)$withinss)
plot(1:12, wss, type="b", xlab="Number of Clusters",ylab="Within groups sum of squares")

Plotting R-Square vs Cluster Size. Cluster size = 3 seems to be appropriate.

# r-square
r_square <- c()

for (i in 1:12) 
{
  # calculating total between sum square for each cluster size
  bss <- sum(kmeans(subset[,-1], centers=i, nstart = 25)$betweenss)
  
  # calculating total sum square for each cluster size
  tss <- sum(kmeans(subset[,-1], centers=i, nstart = 25)$totss)
  
  r_square[i] <- bss/tss
}

# plotting wss vs number of clusters
plot(1:12, r_square, type="b", xlab="Number of Clusters",ylab="R-Square")

Silhouette width and dunn index. Cluster size = 3 seems to be appropriate.

d = dist(subset[,-1], method = "euclidean")
result = matrix(nrow = 14, ncol = 3)
for (i in 2:15){
  cluster_result = kmeans(subset[,-1], i)
  clusterstat=cluster.stats(d, cluster_result$cluster)
  result[i-1,1]=i
  result[i-1,2]=clusterstat$avg.silwidth
  result[i-1,3]=clusterstat$dunn   
}
plot(result[,c(1,2)], type="l", ylab = 'silhouette width', xlab = 'number of clusters')

plot(result[,c(1,3)], type="l", ylab = 'dunn index', xlab = 'number of clusters')

Interpretting Clusters

centers_df <- round(as.data.frame(k3$centers))
centers_df$cluster <- rownames(centers_df)
centers_df <- select(centers_df, cluster, everything())
kable(centers_df)
cluster Agr Min Man PS Con SI Fin SPS TC
1 52 1 14 1 5 8 5 9 5
2 25 1 28 1 9 10 2 17 6
3 9 1 28 1 8 16 5 24 7
subset$cluster <- k3$cluster
kable(arrange(subset, cluster))
Country Agr Min Man PS Con SI Fin SPS TC cluster
Greece 41.4 0.6 17.6 0.6 8.1 11.5 2.4 11.0 6.7 1
Turkey 66.8 0.7 7.9 0.1 2.8 5.2 1.1 11.9 3.2 1
Yugoslavia 48.7 1.5 16.8 1.1 4.9 6.4 11.3 5.3 4.0 1
Spain 22.9 0.8 28.5 0.7 11.5 9.7 8.5 11.8 5.5 2
Bulgaria 23.6 1.9 32.3 0.6 7.9 8.0 0.7 18.2 6.7 2
Czechoslovakia 16.5 2.9 35.5 1.2 8.7 9.2 0.9 17.9 7.0 2
Ireland 23.2 1.0 20.7 1.3 7.5 16.8 2.8 20.8 6.1 2
Portugal 27.8 0.3 24.5 0.6 8.4 13.3 2.7 16.7 5.7 2
USSR 23.7 1.4 25.8 0.6 9.2 6.1 0.5 23.6 9.3 2
Rumania 34.7 2.1 30.1 0.6 8.7 5.9 1.3 11.7 5.0 2
Denmark 9.2 0.1 21.8 0.6 8.3 14.6 6.5 32.2 7.1 3
Belgium 3.3 0.9 27.6 0.9 8.2 19.1 6.2 26.6 7.2 3
WGermany 6.7 1.3 35.8 0.9 7.3 14.4 5.0 22.3 6.1 3
Austria 12.7 1.1 30.2 1.4 9.0 16.8 4.9 16.8 7.0 3
Sweden 6.1 0.4 25.9 0.8 7.2 14.4 6.0 32.4 6.8 3
UK 2.7 1.4 30.2 1.4 6.9 16.9 5.7 28.3 6.4 3
Finland 13.0 0.4 25.9 1.3 7.4 14.7 5.5 24.3 7.6 3
France 10.8 0.8 27.5 0.9 8.9 16.8 6.0 22.6 5.7 3
Luxembourg 7.7 3.1 30.8 0.8 9.2 18.5 4.6 19.2 6.2 3
Italy 15.9 0.6 27.6 0.5 10.0 18.1 1.6 20.1 5.7 3
  1. Cluster 1: Countries with more blue collar jobs - High percentage of the population is employed in agriculture or manufacturing industry.

  2. Cluster 2: Countries with balance of blue collar and white collar jobs - Blue collar and white collar jobs are in similar percentages.

  3. Cluster 3: Countries with more white collar jobs - High percentage of the population is either employed in service industry, social/personal services.

Hierarchical Clustering

Hierarchical Clustering/Ward’s Method

# Calculate the distance matrix
distance <- dist(subset[,-1])

#Obtain clusters using the Wards method
hierarchical_clustering <- hclust(distance, method="ward.D")

plot(hierarchical_clustering)

#Cut dendrogram at the 3 clusters level and obtain cluster membership
hierarchical_clustering_3_clusters = cutree(hierarchical_clustering,k=3)

subset$cluster <- hierarchical_clustering_3_clusters

subset[,-1] %>%
  group_by(cluster) %>% 
  summarise_all(funs(mean)) %>% 
  round() %>% 
  kable()
## Warning: funs() is soft deprecated as of dplyr 0.8.0
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once per session.
cluster Agr Min Man PS Con SI Fin SPS TC
1 9 1 28 1 8 16 5 24 7
2 25 1 28 1 9 10 2 17 6
3 52 1 14 1 5 8 5 9 5
# Same cluster representation as K-means clustering

Association Rule Mining

About Cincinnati Zoo:

  • Cincinnati Zoo was founded in 1873. Officially opened in 1875. Second oldest in the Nation after Pennsylvania Zoo.
  • Zoo houses over 300 animal and over 3,000 plant species.
  • Reptile house is the oldest Zoo building in the country, dating from 1875.
  • Zoo serves over a million visitors each year.

Goal of Project:

To Study association of the items Zoo members buy.


Data:

Food Table-Over 14,000 records of [Demographics + Email, Food items purchased, Dates of purchase(July,2010 through March,2011), Price of food item purchased]


Loading Libraries

library(data.table)
library(tidyverse)
library(knitr)
library(arules)
library(arulesViz)

Loading Data

TransFood <- read.csv('https://xiaoruizhu.github.io/Data-Mining-R/data/food_4_association.csv')
TransFood <- TransFood[, -1]

# Find out elements that are not equal to 0 or 1 and change them to 1.
Others <- which(!(as.matrix(TransFood) ==1 | as.matrix(TransFood) ==0), arr.ind=T )
TransFood[Others] <- 1

# converting to spare format
TransFood <- as(as.matrix(TransFood), "transactions")

Exploring Data

Summary of Data

summary(TransFood)
## transactions as itemMatrix in sparse format with
##  19076 rows (elements/itemsets/transactions) and
##  118 columns (items) and a density of 0.02230729 
## 
## most frequent items:
##   Bottled.WaterFood Slice.of.CheeseFood    Medium.DrinkFood     Small.DrinkFood 
##                3166                3072                2871                2769 
##   Slice.of.PeppFood             (Other) 
##                2354               35981 
## 
## element (itemset/transaction) length distribution:
## sizes
##    0    1    2    3    4    5    6    7    8    9   10   11   12   13   15 
##  197 5675 5178 3253 2129 1293  655  351  178   95   42   14    8    7    1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   1.000   2.000   2.632   4.000  15.000 
## 
## includes extended item information - examples:
##              labels
## 1    Add.CheeseFood
## 2          BeerFood
## 3 Bottled.WaterFood

Frequently bought items

itemFrequencyPlot(TransFood, support = 0.1, cex.names=0.8)


Basket containing more than 10 items

x = TransFood[size(TransFood) > 10]
inspect(x)
##      items                                
## [1]  {Cheese.Fries.BasketFood,            
##       Cheeseburger.BasketFood,            
##       CookieFood,                         
##       French.Fries.BasketFood,            
##       Hot.DogFood,                        
##       PopcornFood,                        
##       Rice.Krispie.TreatFood,             
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood,                    
##       Soft.Pretzel..3_39Food,             
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood}                
## [2]  {Bottled.WaterFood,                  
##       Cheeseburger.BasketFood,            
##       Chicken.Tender.BasketFood,          
##       ChipsFood,                          
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       Rice.Krispie.TreatFood,             
##       Sandwich.BasketFood,                
##       Slice.of.CheeseFood,                
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood}                
## [3]  {Cheeseburger.BasketFood,            
##       CheeseburgerFood,                   
##       ChipsFood,                          
##       CoffeeFood,                         
##       CookieFood,                         
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Hot.ChocolateFood,                  
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood}                  
## [4]  {Bottled.WaterFood,                  
##       Chicken.Tender.BasketFood,          
##       Chicken.TendersFood,                
##       CoffeeFood,                         
##       CookieFood,                         
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Krazy.KritterFood,                  
##       SandwichFood,                       
##       Veggie.Burger.BasketFood,           
##       Veggie.BurgerFood}                  
## [5]  {Cheeseburger.BasketFood,            
##       Chicken.Tender.BasketFood,          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       PopcornFood,                        
##       SandwichFood,                       
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood}                    
## [6]  {Cheese.Fries.BasketFood,            
##       CheeseburgerFood,                   
##       ChipsFood,                          
##       French.Fries.BasketFood,            
##       Grilled.Chicken.SandwichFood,       
##       Hot.ChocolateFood,                  
##       Hot.Dog.BasketFood,                 
##       Hot.DogFood,                        
##       Krazy.KritterFood,                  
##       Sandwich.BasketFood,                
##       Small.DrinkFood}                    
## [7]  {Burger.BasketFood,                  
##       Cheese.Fries.BasketFood,            
##       Chicken.Tender.BasketFood,          
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Ice.Cream.ConeFood,                 
##       IceeFood,                           
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       Rice.Krispie.TreatFood,             
##       Slice.of.PeppFood}                  
## [8]  {Bottled.WaterFood,                  
##       Cheeseburger.BasketFood,            
##       Chicken.Nugget.BasketFood,          
##       Grilled.Chicken.Sandwich.BasketFood,
##       Hot.Dog.BasketFood,                 
##       Medium.DrinkFood,                   
##       Medium.Iced.TeaFood,                
##       NachoFood,                          
##       PopcornFood,                        
##       Small.Diet.PepsiFood,               
##       Small.Sierra.MistFood,              
##       Souvenir.Diet.PepsiFood,            
##       Souvenir.RefillFood}                
## [9]  {Bottled.WaterFood,                  
##       Cheese.ConeyFood,                   
##       Chicken.Tender.BasketFood,          
##       ChipsFood,                          
##       FloatFood,                          
##       French.Fries.BasketFood,            
##       Ice.Cream.ConeFood,                 
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       SaladFood,                          
##       Slice.of.PeppFood}                  
## [10] {BeerFood,                           
##       Cheeseburger.BasketFood,            
##       Hot.Dog.BasketFood,                 
##       Ice.Cream.ConeFood,                 
##       Medium.DrinkFood,                   
##       PopcornFood,                        
##       Sandwich.BasketFood,                
##       SandwichFood,                       
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Soft.Pretzel..3_39Food,             
##       Souvenir.DrinkFood,                 
##       ToppingFood}                        
## [11] {Bottled.WaterFood,                  
##       CheeseburgerFood,                   
##       Chicken.TendersFood,                
##       ChipsFood,                          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Medium.DrinkFood,                   
##       Rice.Krispie.TreatFood,             
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood}                    
## [12] {Add.CheeseFood,                     
##       Bottled.WaterFood,                  
##       GatoradeFood,                       
##       Grilled.Chicken.Sandwich.BasketFood,
##       Hot.Dog.BasketFood,                 
##       Krazy.KritterFood,                  
##       Large.FryFood,                      
##       NachoFood,                          
##       PopcornFood,                        
##       Sandwich.BasketFood,                
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood}                
## [13] {Bottled.WaterFood,                  
##       Cheeseburger.BasketFood,            
##       Chicken.Tender.BasketFood,          
##       French.Fries.BasketFood,            
##       Gourmet.CupFood,                    
##       Hot.ChocolateFood,                  
##       Hot.DogFood,                        
##       Medium.DrinkFood,                   
##       Slice.of.CheeseFood,                
##       Small.DrinkFood,                    
##       Veggie.BurgerFood}                  
## [14] {Bud.LightFood,                      
##       Cheeseburger.BasketFood,            
##       Chicken.Nugget.BasketFood,          
##       Fish.BasketFood,                    
##       GatoradeFood,                       
##       Hot.Dog.BasketFood,                 
##       Land.SharkFood,                     
##       Medium.Dr.PepperFood,               
##       Medium.Pink.LemonadeFood,           
##       October.FestFood,                   
##       Soft.Pretzel..3_39Food,             
##       Souvenir.PepsiFood}                 
## [15] {Cheese.ConeyFood,                   
##       Chicken.Nugget.BasketFood,          
##       ChipsFood,                          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Rice.Krispie.TreatFood,             
##       Slice.of.CheeseFood,                
##       Small.DrinkFood,                    
##       Small.PepsiFood,                    
##       Souvenir.Diet.PepsiFood,            
##       Souvenir.DrinkFood}                 
## [16] {BeerFood,                           
##       Bottled.WaterFood,                  
##       Cheese.ConeyFood,                   
##       Chicken.Nugget.BasketFood,          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Hot.DogFood,                        
##       Ice.Cream.ConeFood,                 
##       IceeFood,                           
##       Medium.DrinkFood,                   
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       ToppingFood}                        
## [17] {Cheese.Fries.BasketFood,            
##       CheeseburgerFood,                   
##       Chicken.TendersFood,                
##       French.Fries.BasketFood,            
##       Hot.Chocolate.SouvenirFood,         
##       Kettle.ChipsFood,                   
##       Krazy.KritterFood,                  
##       Sandwich.BasketFood,                
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Souvenir.DrinkFood}                 
## [18] {Cheese.Fries.BasketFood,            
##       Gourmet.CupFood,                    
##       Ice.Cream.ConeFood,                 
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       Rice.Krispie.TreatFood,             
##       Sandwich.BasketFood,                
##       Siberian.ChillFood,                 
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       ToppingFood}                        
## [19] {Cheeseburger.BasketFood,            
##       Chicken.Nugget.BasketFood,          
##       CookieFood,                         
##       Grilled.Chicken.Sandwich.BasketFood,
##       Hot.Dog.BasketFood,                 
##       Krazy.KritterFood,                  
##       Rice.Krispie.TreatFood,             
##       Souvenir.Dr.PepperFood,             
##       Souvenir.PepsiFood,                 
##       Souvenir.RefillFood,                
##       Souvenir.Sierra.MistFood}           
## [20] {Bottled.WaterFood,                  
##       BurgerFood,                         
##       CheeseburgerFood,                   
##       Chicken.TendersFood,                
##       ChiliFood,                          
##       CoffeeFood,                         
##       French.Fries.BasketFood,            
##       Kettle.ChipsFood,                   
##       Slice.of.PeppFood,                  
##       Small.DrinkFood,                    
##       Souvenir.RefillFood}                
## [21] {Bottled.WaterFood,                  
##       CheeseburgerFood,                   
##       ChipsFood,                          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Ice.Cream.ConeFood,                 
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       SandwichFood,                       
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood,                    
##       Soft.Pretzel..3_39Food,             
##       Souvenir.DrinkFood}                 
## [22] {Bottled.WaterFood,                  
##       CheeseburgerFood,                   
##       French.Fries.BasketFood,            
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       Rice.Krispie.TreatFood,             
##       SaladFood,                          
##       Sandwich.BasketFood,                
##       SandwichFood,                       
##       Slice.of.CheeseFood,                
##       Souvenir.DrinkFood}                 
## [23] {Bottled.WaterFood,                  
##       Cheese.Fries.BasketFood,            
##       Chicken.Tender.BasketFood,          
##       CoffeeFood,                         
##       GatoradeFood,                       
##       Gourmet.CupFood,                    
##       Grilled.Chicken.Sandwich.BasketFood,
##       Hot.Dog.BasketFood,                 
##       Medium.DrinkFood,                   
##       MilkFood,                           
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood}                    
## [24] {Burger.BasketFood,                  
##       BurgerFood,                         
##       ChiliFood,                          
##       French.Fries.BasketFood,            
##       GatoradeFood,                       
##       Hot.DogFood,                        
##       Kettle.ChipsFood,                   
##       SaladFood,                          
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Small.DrinkFood,                    
##       Souvenir.RefillFood}                
## [25] {Bottled.WaterFood,                  
##       CheeseburgerFood,                   
##       French.Fries.BasketFood,            
##       Grilled.Chicken.SandwichFood,       
##       Hot.DogFood,                        
##       Krazy.KritterFood,                  
##       Rice.Krispie.TreatFood,             
##       Slice.of.CheeseFood,                
##       Slice.of.PeppFood,                  
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood}                
## [26] {Cheeseburger.BasketFood,            
##       Chicken.Nugget.BasketFood,          
##       Grilled.Chicken.Sandwich.BasketFood,
##       Hot.Dog.BasketFood,                 
##       Souvenir.Diet.Pepsi.RefillFood,     
##       Souvenir.Dr.Pepper.RefillFood,      
##       Souvenir.PepsiFood,                 
##       Souvenir.Pink.LemonadeFood,         
##       Souvenir.RefillFood,                
##       Souvenir.Sierra.Mist.RefillFood,    
##       Souvenir.Sierra.MistFood}           
## [27] {Bottled.WaterFood,                  
##       CheeseburgerFood,                   
##       Chicken.TendersFood,                
##       French.Fries.BasketFood,            
##       Grilled.Chicken.SandwichFood,       
##       Ice.Cream.ConeFood,                 
##       MilkFood,                           
##       Slice.of.CheeseFood,                
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood,                
##       ToppingFood}                        
## [28] {Cheese.ConeyFood,                   
##       Chicken.Tender.BasketFood,          
##       Chili.Cheese.SandwichFood,          
##       ChipsFood,                          
##       GatoradeFood,                       
##       Hot.DogFood,                        
##       Medium.DrinkFood,                   
##       Siberian.ChillFood,                 
##       Side.of.CheeseFood,                 
##       Slice.of.PeppFood,                  
##       Small.DrinkFood,                    
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood}                
## [29] {Chicken.Tender.BasketFood,          
##       CookieFood,                         
##       Krazy.KritterFood,                  
##       Medium.DrinkFood,                   
##       PopcornFood,                        
##       SaladFood,                          
##       Sandwich.BasketFood,                
##       Slice.of.CheeseFood,                
##       Small.DrinkFood,                    
##       Souvenir.DrinkFood,                 
##       Souvenir.RefillFood,                
##       Veggie.Burger.BasketFood}           
## [30] {Cheese.ConeyFood,                   
##       Cheeseburger.BasketFood,            
##       Chicken.Nugget.BasketFood,          
##       French.Fries.BasketFood,            
##       Hot.DogFood,                        
##       Medium.Diet.PepsiFood,              
##       Side.of.CheeseFood,                 
##       Small.Diet.PepsiFood,               
##       Small.DrinkFood,                    
##       Small.PepsiFood,                    
##       Small.Pink.LemonadeFood}

Running Apriori Algorithm to determine association rules

Using apriori() algorithm to find all rules (the default association type for apriori()) with a minimum support of 0.3% and a confidence of 0.5.

# Run the apriori algorithm
basket_rules <- apriori(TransFood,parameter = list(sup = 0.003, conf = 0.5,target="rules"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.003      1
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 57 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[115 item(s), 19076 transaction(s)] done [0.00s].
## sorting and recoding items ... [74 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [42 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].

Summary of the algorithm

summary(basket_rules)
## set of 42 rules
## 
## rule length distribution (lhs + rhs):sizes
##  2  3  4 
## 12 27  3 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   2.000   3.000   2.786   3.000   4.000 
## 
## summary of quality measures:
##     support           confidence          lift            count       
##  Min.   :0.003093   Min.   :0.5032   Min.   : 3.125   Min.   : 59.00  
##  1st Qu.:0.003683   1st Qu.:0.5944   1st Qu.: 6.307   1st Qu.: 70.25  
##  Median :0.004561   Median :0.7586   Median : 8.235   Median : 87.00  
##  Mean   :0.006836   Mean   :0.7404   Mean   : 8.929   Mean   :130.40  
##  3rd Qu.:0.007496   3rd Qu.:0.8721   3rd Qu.: 9.016   3rd Qu.:143.00  
##  Max.   :0.028570   Max.   :1.0000   Max.   :26.179   Max.   :545.00  
## 
## mining info:
##       data ntransactions support confidence
##  TransFood         19076   0.003        0.5

Checking the generated rules

inspect(basket_rules)
##      lhs                                    rhs                              support confidence      lift count
## [1]  {Small.Pink.LemonadeFood}           => {Chicken.Nugget.BasketFood}  0.003355001  0.5925926 16.034463    64
## [2]  {Grilled.Chicken.SandwichFood}      => {French.Fries.BasketFood}    0.003721954  0.6698113  6.862149    71
## [3]  {FloatFood}                         => {Ice.Cream.ConeFood}         0.007024533  0.7089947  6.355631   134
## [4]  {Side.of.CheeseFood}                => {Cheese.ConeyFood}           0.004665548  0.6846154 25.912149    89
## [5]  {Side.of.CheeseFood}                => {Hot.DogFood}                0.006290627  0.9230769 21.605663   120
## [6]  {BurgerFood}                        => {French.Fries.BasketFood}    0.004613126  0.6616541  6.778579    88
## [7]  {SandwichFood}                      => {French.Fries.BasketFood}    0.007653596  0.6822430  6.989510   146
## [8]  {Hot.Chocolate.Souvenir.RefillFood} => {Hot.Chocolate.SouvenirFood} 0.014992661  0.5596869 13.180972   286
## [9]  {ToppingFood}                       => {Ice.Cream.ConeFood}         0.028569931  0.9981685  8.947868   545
## [10] {Add.CheeseFood}                    => {Soft.Pretzel..3_39Food}     0.019133990  0.6965649  7.601643   365
## [11] {Chicken.TendersFood}               => {French.Fries.BasketFood}    0.017299224  0.7586207  7.771992   330
## [12] {CheeseburgerFood}                  => {French.Fries.BasketFood}    0.016879849  0.7931034  8.125264   322
## [13] {Cheese.ConeyFood,                                                                                        
##       Side.of.CheeseFood}                => {Hot.DogFood}                0.004351017  0.9325843 21.828193    83
## [14] {Hot.DogFood,                                                                                             
##       Side.of.CheeseFood}                => {Cheese.ConeyFood}           0.004351017  0.6916667 26.179034    83
## [15] {Bottled.WaterFood,                                                                                       
##       ToppingFood}                       => {Ice.Cream.ConeFood}         0.004036486  1.0000000  8.964286    77
## [16] {Add.CheeseFood,                                                                                          
##       Bottled.WaterFood}                 => {Soft.Pretzel..3_39Food}     0.003826798  0.8021978  8.754419    73
## [17] {CheeseburgerFood,                                                                                        
##       Chicken.TendersFood}               => {French.Fries.BasketFood}    0.003931642  0.9615385  9.850863    75
## [18] {Chicken.TendersFood,                                                                                     
##       Souvenir.DrinkFood}                => {French.Fries.BasketFood}    0.003197735  0.7922078  8.116088    61
## [19] {Chicken.TendersFood,                                                                                     
##       Krazy.KritterFood}                 => {French.Fries.BasketFood}    0.005661564  0.9557522  9.791584   108
## [20] {Chicken.TendersFood,                                                                                     
##       Slice.of.PeppFood}                 => {French.Fries.BasketFood}    0.003669532  0.9210526  9.436090    70
## [21] {Chicken.TendersFood,                                                                                     
##       Small.DrinkFood}                   => {French.Fries.BasketFood}    0.004822814  0.8214286  8.415452    92
## [22] {Chicken.TendersFood,                                                                                     
##       Medium.DrinkFood}                  => {French.Fries.BasketFood}    0.004141329  0.8144330  8.343783    79
## [23] {Bottled.WaterFood,                                                                                       
##       Chicken.TendersFood}               => {French.Fries.BasketFood}    0.003459845  0.7586207  7.771992    66
## [24] {Chicken.TendersFood,                                                                                     
##       Slice.of.CheeseFood}               => {French.Fries.BasketFood}    0.005399455  0.8728814  8.942580   103
## [25] {CheeseburgerFood,                                                                                        
##       Souvenir.DrinkFood}                => {French.Fries.BasketFood}    0.003250157  0.9117647  9.340936    62
## [26] {CheeseburgerFood,                                                                                        
##       Krazy.KritterFood}                 => {French.Fries.BasketFood}    0.005451877  0.8813559  9.029402   104
## [27] {CheeseburgerFood,                                                                                        
##       Slice.of.PeppFood}                 => {French.Fries.BasketFood}    0.003721954  0.8658537  8.870582    71
## [28] {CheeseburgerFood,                                                                                        
##       Small.DrinkFood}                   => {French.Fries.BasketFood}    0.004141329  0.8315789  8.519441    79
## [29] {CheeseburgerFood,                                                                                        
##       Medium.DrinkFood}                  => {French.Fries.BasketFood}    0.005189767  0.8761062  8.975619    99
## [30] {Bottled.WaterFood,                                                                                       
##       CheeseburgerFood}                  => {French.Fries.BasketFood}    0.003092892  0.7662338  7.849987    59
## [31] {CheeseburgerFood,                                                                                        
##       Slice.of.CheeseFood}               => {French.Fries.BasketFood}    0.005242189  0.8695652  8.908607   100
## [32] {ChipsFood,                                                                                               
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.008282659  0.5808824  3.607068   158
## [33] {CookieFood,                                                                                              
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.004508283  0.5243902  3.256272    86
## [34] {Hot.DogFood,                                                                                             
##       Krazy.KritterFood}                 => {French.Fries.BasketFood}    0.003669532  0.6140351  6.290727    70
## [35] {Ice.Cream.ConeFood,                                                                                      
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.003407423  0.5200000  3.229010    65
## [36] {GatoradeFood,                                                                                            
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.010117425  0.5830816  3.620724   193
## [37] {Slice.of.PeppFood,                                                                                       
##       Souvenir.DrinkFood}                => {Slice.of.CheeseFood}        0.008125393  0.5032468  3.124979   155
## [38] {Medium.DrinkFood,                                                                                        
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.013629692  0.5273834  3.274858   260
## [39] {Bottled.WaterFood,                                                                                       
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.010694066  0.5151515  3.198903   204
## [40] {Krazy.KritterFood,                                                                                       
##       Medium.DrinkFood,                                                                                        
##       Slice.of.PeppFood}                 => {Slice.of.CheeseFood}        0.003250157  0.5535714  3.437477    62
## [41] {Medium.DrinkFood,                                                                                        
##       Slice.of.PeppFood,                                                                                       
##       Small.DrinkFood}                   => {Slice.of.CheeseFood}        0.003145313  0.6000000  3.725781    60
## [42] {Medium.DrinkFood,                                                                                        
##       Slice.of.CheeseFood,                                                                                     
##       Small.DrinkFood}                   => {Slice.of.PeppFood}          0.003145313  0.5172414  4.191545    60

Basket rules of size greater than 2

inspect(subset(basket_rules, size(basket_rules)>2))
##      lhs                      rhs                           support confidence      lift count
## [1]  {Cheese.ConeyFood,                                                                       
##       Side.of.CheeseFood}  => {Hot.DogFood}             0.004351017  0.9325843 21.828193    83
## [2]  {Hot.DogFood,                                                                            
##       Side.of.CheeseFood}  => {Cheese.ConeyFood}        0.004351017  0.6916667 26.179034    83
## [3]  {Bottled.WaterFood,                                                                      
##       ToppingFood}         => {Ice.Cream.ConeFood}      0.004036486  1.0000000  8.964286    77
## [4]  {Add.CheeseFood,                                                                         
##       Bottled.WaterFood}   => {Soft.Pretzel..3_39Food}  0.003826798  0.8021978  8.754419    73
## [5]  {CheeseburgerFood,                                                                       
##       Chicken.TendersFood} => {French.Fries.BasketFood} 0.003931642  0.9615385  9.850863    75
## [6]  {Chicken.TendersFood,                                                                    
##       Souvenir.DrinkFood}  => {French.Fries.BasketFood} 0.003197735  0.7922078  8.116088    61
## [7]  {Chicken.TendersFood,                                                                    
##       Krazy.KritterFood}   => {French.Fries.BasketFood} 0.005661564  0.9557522  9.791584   108
## [8]  {Chicken.TendersFood,                                                                    
##       Slice.of.PeppFood}   => {French.Fries.BasketFood} 0.003669532  0.9210526  9.436090    70
## [9]  {Chicken.TendersFood,                                                                    
##       Small.DrinkFood}     => {French.Fries.BasketFood} 0.004822814  0.8214286  8.415452    92
## [10] {Chicken.TendersFood,                                                                    
##       Medium.DrinkFood}    => {French.Fries.BasketFood} 0.004141329  0.8144330  8.343783    79
## [11] {Bottled.WaterFood,                                                                      
##       Chicken.TendersFood} => {French.Fries.BasketFood} 0.003459845  0.7586207  7.771992    66
## [12] {Chicken.TendersFood,                                                                    
##       Slice.of.CheeseFood} => {French.Fries.BasketFood} 0.005399455  0.8728814  8.942580   103
## [13] {CheeseburgerFood,                                                                       
##       Souvenir.DrinkFood}  => {French.Fries.BasketFood} 0.003250157  0.9117647  9.340936    62
## [14] {CheeseburgerFood,                                                                       
##       Krazy.KritterFood}   => {French.Fries.BasketFood} 0.005451877  0.8813559  9.029402   104
## [15] {CheeseburgerFood,                                                                       
##       Slice.of.PeppFood}   => {French.Fries.BasketFood} 0.003721954  0.8658537  8.870582    71
## [16] {CheeseburgerFood,                                                                       
##       Small.DrinkFood}     => {French.Fries.BasketFood} 0.004141329  0.8315789  8.519441    79
## [17] {CheeseburgerFood,                                                                       
##       Medium.DrinkFood}    => {French.Fries.BasketFood} 0.005189767  0.8761062  8.975619    99
## [18] {Bottled.WaterFood,                                                                      
##       CheeseburgerFood}    => {French.Fries.BasketFood} 0.003092892  0.7662338  7.849987    59
## [19] {CheeseburgerFood,                                                                       
##       Slice.of.CheeseFood} => {French.Fries.BasketFood} 0.005242189  0.8695652  8.908607   100
## [20] {ChipsFood,                                                                              
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.008282659  0.5808824  3.607068   158
## [21] {CookieFood,                                                                             
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.004508283  0.5243902  3.256272    86
## [22] {Hot.DogFood,                                                                            
##       Krazy.KritterFood}   => {French.Fries.BasketFood} 0.003669532  0.6140351  6.290727    70
## [23] {Ice.Cream.ConeFood,                                                                     
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.003407423  0.5200000  3.229010    65
## [24] {GatoradeFood,                                                                           
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.010117425  0.5830816  3.620724   193
## [25] {Slice.of.PeppFood,                                                                      
##       Souvenir.DrinkFood}  => {Slice.of.CheeseFood}     0.008125393  0.5032468  3.124979   155
## [26] {Medium.DrinkFood,                                                                       
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.013629692  0.5273834  3.274858   260
## [27] {Bottled.WaterFood,                                                                      
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.010694066  0.5151515  3.198903   204
## [28] {Krazy.KritterFood,                                                                      
##       Medium.DrinkFood,                                                                       
##       Slice.of.PeppFood}   => {Slice.of.CheeseFood}     0.003250157  0.5535714  3.437477    62
## [29] {Medium.DrinkFood,                                                                       
##       Slice.of.PeppFood,                                                                      
##       Small.DrinkFood}     => {Slice.of.CheeseFood}     0.003145313  0.6000000  3.725781    60
## [30] {Medium.DrinkFood,                                                                       
##       Slice.of.CheeseFood,                                                                    
##       Small.DrinkFood}     => {Slice.of.PeppFood}       0.003145313  0.5172414  4.191545    60

Basket rules of size lift greater than 10

inspect(subset(basket_rules, lift>10))
##     lhs                                    rhs                              support confidence     lift count
## [1] {Small.Pink.LemonadeFood}           => {Chicken.Nugget.BasketFood}  0.003355001  0.5925926 16.03446    64
## [2] {Side.of.CheeseFood}                => {Cheese.ConeyFood}           0.004665548  0.6846154 25.91215    89
## [3] {Side.of.CheeseFood}                => {Hot.DogFood}                0.006290627  0.9230769 21.60566   120
## [4] {Hot.Chocolate.Souvenir.RefillFood} => {Hot.Chocolate.SouvenirFood} 0.014992661  0.5596869 13.18097   286
## [5] {Cheese.ConeyFood,                                                                                       
##      Side.of.CheeseFood}                => {Hot.DogFood}                0.004351017  0.9325843 21.82819    83
## [6] {Hot.DogFood,                                                                                            
##      Side.of.CheeseFood}                => {Cheese.ConeyFood}           0.004351017  0.6916667 26.17903    83

Basket rules containing Cheese.ConeyFood on rhs and lift greater than 5

Cheese.ConeyFood.rhs <- subset(basket_rules, subset = rhs %in% "Cheese.ConeyFood" & lift>5)

inspect(Cheese.ConeyFood.rhs)
##     lhs                                 rhs                support    
## [1] {Side.of.CheeseFood}             => {Cheese.ConeyFood} 0.004665548
## [2] {Hot.DogFood,Side.of.CheeseFood} => {Cheese.ConeyFood} 0.004351017
##     confidence lift     count
## [1] 0.6846154  25.91215 89   
## [2] 0.6916667  26.17903 83

Visualizing Association Rules

Plotting confidence vs support of all rules

plot(basket_rules)


Plot selected rules with their corresponding support and lift

plot(basket_rules, method="grouped")


Graph Plot of 5 rules with highest lift

plot(head(sort(basket_rules, by="lift"), 5), method = "graph")