AdilK
AdilK

Reputation: 727

R Prog- Market Basket Analysis - How to visualize product recommendations while using Support and Confidence Loop?

grid <- expand.grid(support = seq(0.05, 0.1, 0.01),
confidence = seq(0.05, 0.1, 0.01))

In this example, the expand.grid is used to increment support metrics from 0.05 till 0.10 with an increment of 0.01. Same for Confidence metric. Below code [from SO] runs the apriori algorithm to find lift when the above Support and Confidence metrics are met.

Is there a way to visualize all the possible Lifts to help with product recommendations to show? Thx

#SO solution to loop through support,confidence
library(dplyr)
res <- 
grid %>% 
group_by(support, confidence) %>% 
do(model = apriori(
tr,
parameter = list(support = .$support, confidence = .$confidence,maxlen=1)
)) %>% 
mutate(n_rules = length(model)) %>%
ungroup()

View(res$model)
warnings()

summary(res$model[res$confidence == 0.05 & res$support == 0.05][[1]])

Upvotes: 1

Views: 136

Answers (1)

nniloc
nniloc

Reputation: 4243

Assuming you have already seen the arulesViz standard plots, maybe you want to run arules::inspect on each model and store outputs? Then you can create a custom plot however you want.

library(arules)
id <- c("1","1","1","2","2","2","3","3","3")
obj <- c("a", "b", "j", "a", "g","c", "a","k","c")
df <- data.frame(id,obj)
tr <- as(split(df$obj, df$id), "transactions")

grid <- expand.grid(support = seq(0.05, 0.1, 0.01),
                    confidence = seq(0.05, 0.1, 0.01))

library(dplyr)
res <- 
  grid %>% 
  group_by(support, confidence) %>% 
  do(model = apriori(
    tr,
    parameter = list(support = .$support, confidence = .$confidence,maxlen=1)
  )) %>% 
  mutate(n_rules = length(model)) %>%
  ungroup()

One option is to use purrr::map_dfr to create a single dataframe with all 36 models

purrr::map_dfr(res$model, inspect)

Or if arulesViz::plot is what you are looking for, you can apply it to all 36 models by mapping plot to res$model. The 36 plots can be put into a single larger plot using gridExtra. This is pretty busy by default, but with some customization it might suit your needs.

library(arulesViz)
plot_list <- purrr::map(res$model, plot)
gridExtra::grid.arrange(grobs = plot_list)

Upvotes: 1

Related Questions